OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # | 2 # |
3 # Copyright 2015 The Chromium Authors. All rights reserved. | 3 # Copyright 2015 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """Waits for any one job out of a list to complete or a default timeout.""" | 7 """Waits for any one job out of a list to complete or a default timeout.""" |
8 | 8 |
9 import json | 9 import json |
10 import subprocess | 10 import subprocess |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 return False | 44 return False |
45 | 45 |
46 | 46 |
47 def _is_gs_url(url): | 47 def _is_gs_url(url): |
48 return url.lower().startswith('gs://') | 48 return url.lower().startswith('gs://') |
49 | 49 |
50 | 50 |
51 def _run_gsutil(cmd): | 51 def _run_gsutil(cmd): |
52 # Sleep for a short time between gsutil calls | 52 # Sleep for a short time between gsutil calls |
53 time.sleep(SHORT_INTERVAL) | 53 time.sleep(SHORT_INTERVAL) |
54 global gsutil_path | |
55 cmd = [gsutil_path] + cmd | 54 cmd = [gsutil_path] + cmd |
56 try: | 55 try: |
57 out = subprocess.check_output(cmd) | 56 out = subprocess.check_output(cmd) |
58 return 0, out | 57 return 0, out |
59 except subprocess.CalledProcessError as cpe: | 58 except subprocess.CalledProcessError as cpe: |
60 return cpe.returncode, cpe.output | 59 return cpe.returncode, cpe.output |
61 | 60 |
62 | 61 |
63 def _check_buildbot_job(url): | 62 def _check_buildbot_job(url): |
64 print "Checking buildbot url:", url | 63 print "Checking buildbot url:", url |
65 time.sleep(SHORT_INTERVAL) | 64 time.sleep(SHORT_INTERVAL) |
66 try: | 65 try: |
67 doc = urllib2.urlopen(url).read() | 66 doc = urllib2.urlopen(url).read() |
68 build_status_dict = json.loads(doc) | 67 build_status_dict = json.loads(doc) |
69 if build_status_dict['currentStep'] is None: | 68 if build_status_dict['currentStep'] is None: |
70 print url, " finished." | 69 print url, " finished." |
71 return True | 70 return True |
72 except: | 71 except Exception: |
73 print "Could not retrieve or parse the buildbot url: ", url | 72 print "Could not retrieve or parse the buildbot url: ", url |
74 return False | 73 return False |
75 | 74 |
76 | 75 |
77 def _gs_file_exists(url): | 76 def _gs_file_exists(url): |
78 """Checks that running 'gsutil ls' returns 0 to see if file at url exists.""" | 77 """Checks that running 'gsutil ls' returns 0 to see if file at url exists.""" |
79 return _run_gsutil(['ls', url])[0] == 0 | 78 return _run_gsutil(['ls', url])[0] == 0 |
80 | 79 |
81 | 80 |
82 def main(argv): | 81 def main(argv): |
83 if len(argv) < 3: | 82 if len(argv) < 3: |
84 usage = "Usage: %s <gsutil path> url1 [url2 [url3...]]" | 83 usage = "Usage: %s <gsutil path> url1 [url2 [url3...]]" |
85 print usage % argv[0] | 84 print usage % argv[0] |
86 return 1 | 85 return 1 |
87 | 86 |
| 87 list_of_urls = ', '.join(['<%s>' % url for url in argv[2:]]) |
| 88 print 'Waiting for the following urls: ' + list_of_urls |
88 global gsutil_path | 89 global gsutil_path |
89 start_time = time.time() | 90 start_time = time.time() |
90 gsutil_path = argv[1] | 91 gsutil_path = argv[1] |
91 urls = argv[2:] | 92 urls = argv[2:] |
92 while urls: | 93 while urls: |
93 for url in urls: | 94 for url in urls: |
94 if _is_gs_url(url): | 95 if _is_gs_url(url): |
95 if _gs_file_exists(url): | 96 if _gs_file_exists(url): |
96 if _is_job_url(url): | 97 if _is_job_url(url): |
97 new_url = _run_gsutil(['cat', url])[1] | 98 new_url = _run_gsutil(['cat', url])[1] |
98 print "Buildbot URL found.", new_url | 99 print "Buildbot URL found.", new_url |
99 urls.append(new_url) | 100 urls.append(new_url) |
100 urls.remove(url) | 101 urls.remove(url) |
101 continue | 102 continue |
102 else: | 103 else: |
103 print 'Build finished: ', url | 104 print 'Build finished: ', url |
104 return 0 | 105 return 0 |
105 else: | 106 else: |
106 # Assuming url is buildbot JSON API | 107 # Assuming url is buildbot JSON API |
107 finished = _check_buildbot_job(url) | 108 finished = _check_buildbot_job(url) |
108 if finished: | 109 if finished: |
109 return 0 | 110 return 0 |
110 | 111 |
111 if time.time() - start_time > TIMEOUT_INTERVAL: | 112 if time.time() - start_time > TIMEOUT_INTERVAL: |
112 print "Timed out waiting for: ", urls | 113 print "Timed out waiting for: ", urls |
113 return 1 | 114 return 1 |
114 print "No jobs ready, sleeping." | |
115 time.sleep(LONG_INTERVAL) | 115 time.sleep(LONG_INTERVAL) |
116 print "No jobs to check." | 116 print "No jobs to check." |
117 return 0 | 117 return 0 |
118 | 118 |
119 | 119 |
120 if __name__ == '__main__': | 120 if __name__ == '__main__': |
121 sys.exit(main(sys.argv)) | 121 sys.exit(main(sys.argv)) |
OLD | NEW |