Craig Tiller | c2c7921 | 2015-02-16 12:00:01 -0800 | [diff] [blame] | 1 | # Copyright 2015, Google Inc. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # Redistribution and use in source and binary forms, with or without |
| 5 | # modification, are permitted provided that the following conditions are |
| 6 | # met: |
| 7 | # |
| 8 | # * Redistributions of source code must retain the above copyright |
| 9 | # notice, this list of conditions and the following disclaimer. |
| 10 | # * Redistributions in binary form must reproduce the above |
| 11 | # copyright notice, this list of conditions and the following disclaimer |
| 12 | # in the documentation and/or other materials provided with the |
| 13 | # distribution. |
| 14 | # * Neither the name of Google Inc. nor the names of its |
| 15 | # contributors may be used to endorse or promote products derived from |
| 16 | # this software without specific prior written permission. |
| 17 | # |
| 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 30 | """Run a group of subprocesses and then finish.""" |
| 31 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 32 | import hashlib |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 33 | import multiprocessing |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 34 | import os |
Craig Tiller | 5058c69 | 2015-04-08 09:42:04 -0700 | [diff] [blame] | 35 | import platform |
Craig Tiller | 336ad50 | 2015-02-24 14:46:02 -0800 | [diff] [blame] | 36 | import signal |
Nicolas "Pixel" Noble | 5937b5b | 2015-06-26 02:04:12 +0200 | [diff] [blame] | 37 | import string |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 38 | import subprocess |
| 39 | import sys |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 40 | import tempfile |
| 41 | import time |
Nicolas "Pixel" Noble | 5937b5b | 2015-06-26 02:04:12 +0200 | [diff] [blame] | 42 | import xml.etree.cElementTree as ET |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 43 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 44 | |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 45 | _DEFAULT_MAX_JOBS = 16 * multiprocessing.cpu_count() |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 46 | |
| 47 | |
Craig Tiller | 336ad50 | 2015-02-24 14:46:02 -0800 | [diff] [blame] | 48 | # setup a signal handler so that signal.pause registers 'something' |
| 49 | # when a child finishes |
| 50 | # not using futures and threading to avoid a dependency on subprocess32 |
Craig Tiller | 5058c69 | 2015-04-08 09:42:04 -0700 | [diff] [blame] | 51 | if platform.system() == "Windows": |
| 52 | pass |
| 53 | else: |
| 54 | have_alarm = False |
| 55 | def alarm_handler(unused_signum, unused_frame): |
| 56 | global have_alarm |
| 57 | have_alarm = False |
| 58 | |
| 59 | signal.signal(signal.SIGCHLD, lambda unused_signum, unused_frame: None) |
| 60 | signal.signal(signal.SIGALRM, alarm_handler) |
Craig Tiller | 336ad50 | 2015-02-24 14:46:02 -0800 | [diff] [blame] | 61 | |
| 62 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 63 | _SUCCESS = object() |
| 64 | _FAILURE = object() |
| 65 | _RUNNING = object() |
| 66 | _KILLED = object() |
| 67 | |
| 68 | |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 69 | _COLORS = { |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 70 | 'red': [ 31, 0 ], |
| 71 | 'green': [ 32, 0 ], |
| 72 | 'yellow': [ 33, 0 ], |
| 73 | 'lightgray': [ 37, 0], |
| 74 | 'gray': [ 30, 1 ], |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | |
| 78 | _BEGINNING_OF_LINE = '\x1b[0G' |
| 79 | _CLEAR_LINE = '\x1b[2K' |
| 80 | |
| 81 | |
| 82 | _TAG_COLOR = { |
| 83 | 'FAILED': 'red', |
Masood Malekghassemi | e5f7002 | 2015-06-29 09:20:26 -0700 | [diff] [blame] | 84 | 'WARNING': 'yellow', |
Craig Tiller | e1d0d1c | 2015-02-27 08:54:23 -0800 | [diff] [blame] | 85 | 'TIMEOUT': 'red', |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 86 | 'PASSED': 'green', |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 87 | 'START': 'gray', |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 88 | 'WAITING': 'yellow', |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 89 | 'SUCCESS': 'green', |
| 90 | 'IDLE': 'gray', |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | |
Nicolas "Pixel" Noble | 99768ac | 2015-05-13 02:34:06 +0200 | [diff] [blame] | 94 | def message(tag, msg, explanatory_text=None, do_newline=False): |
| 95 | if message.old_tag == tag and message.old_msg == msg and not explanatory_text: |
| 96 | return |
| 97 | message.old_tag = tag |
| 98 | message.old_msg = msg |
vjpai | a29d2d7 | 2015-07-08 10:31:15 -0700 | [diff] [blame] | 99 | if platform.system() == 'Windows' or not sys.stdout.isatty(): |
Craig Tiller | 5058c69 | 2015-04-08 09:42:04 -0700 | [diff] [blame] | 100 | if explanatory_text: |
| 101 | print explanatory_text |
Nicolas "Pixel" Noble | 99768ac | 2015-05-13 02:34:06 +0200 | [diff] [blame] | 102 | print '%s: %s' % (tag, msg) |
Craig Tiller | 5058c69 | 2015-04-08 09:42:04 -0700 | [diff] [blame] | 103 | return |
Craig Tiller | 23d2f3f | 2015-02-24 15:23:32 -0800 | [diff] [blame] | 104 | try: |
vjpai | a29d2d7 | 2015-07-08 10:31:15 -0700 | [diff] [blame] | 105 | sys.stdout.write('%s%s%s\x1b[%d;%dm%s\x1b[0m: %s%s' % ( |
| 106 | _BEGINNING_OF_LINE, |
| 107 | _CLEAR_LINE, |
| 108 | '\n%s' % explanatory_text if explanatory_text is not None else '', |
| 109 | _COLORS[_TAG_COLOR[tag]][1], |
| 110 | _COLORS[_TAG_COLOR[tag]][0], |
| 111 | tag, |
| 112 | msg, |
| 113 | '\n' if do_newline or explanatory_text is not None else '')) |
Craig Tiller | 23d2f3f | 2015-02-24 15:23:32 -0800 | [diff] [blame] | 114 | sys.stdout.flush() |
| 115 | except: |
| 116 | pass |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 117 | |
Nicolas "Pixel" Noble | 99768ac | 2015-05-13 02:34:06 +0200 | [diff] [blame] | 118 | message.old_tag = "" |
| 119 | message.old_msg = "" |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 120 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 121 | def which(filename): |
| 122 | if '/' in filename: |
| 123 | return filename |
| 124 | for path in os.environ['PATH'].split(os.pathsep): |
| 125 | if os.path.exists(os.path.join(path, filename)): |
| 126 | return os.path.join(path, filename) |
| 127 | raise Exception('%s not found' % filename) |
| 128 | |
| 129 | |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 130 | class JobSpec(object): |
| 131 | """Specifies what to run for a job.""" |
| 132 | |
Jan Tattermusch | 725835a | 2015-08-01 21:02:35 -0700 | [diff] [blame^] | 133 | def __init__(self, cmdline, shortname=None, environ=None, hash_targets=None, |
| 134 | cwd=None, shell=False, timeout_seconds=900): |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 135 | """ |
| 136 | Arguments: |
| 137 | cmdline: a list of arguments to pass as the command line |
| 138 | environ: a dictionary of environment variables to set in the child process |
| 139 | hash_targets: which files to include in the hash representing the jobs version |
| 140 | (or empty, indicating the job should not be hashed) |
| 141 | """ |
murgatroid99 | 132ce6a | 2015-03-04 17:29:14 -0800 | [diff] [blame] | 142 | if environ is None: |
| 143 | environ = {} |
| 144 | if hash_targets is None: |
| 145 | hash_targets = [] |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 146 | self.cmdline = cmdline |
| 147 | self.environ = environ |
| 148 | self.shortname = cmdline[0] if shortname is None else shortname |
| 149 | self.hash_targets = hash_targets or [] |
Craig Tiller | 5058c69 | 2015-04-08 09:42:04 -0700 | [diff] [blame] | 150 | self.cwd = cwd |
Jan Tattermusch | e824359 | 2015-04-17 14:14:01 -0700 | [diff] [blame] | 151 | self.shell = shell |
Jan Tattermusch | 725835a | 2015-08-01 21:02:35 -0700 | [diff] [blame^] | 152 | self.timeout_seconds = timeout_seconds |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 153 | |
| 154 | def identity(self): |
| 155 | return '%r %r %r' % (self.cmdline, self.environ, self.hash_targets) |
| 156 | |
| 157 | def __hash__(self): |
| 158 | return hash(self.identity()) |
| 159 | |
| 160 | def __cmp__(self, other): |
| 161 | return self.identity() == other.identity() |
| 162 | |
| 163 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 164 | class Job(object): |
| 165 | """Manages one job.""" |
| 166 | |
Nicolas "Pixel" Noble | 5937b5b | 2015-06-26 02:04:12 +0200 | [diff] [blame] | 167 | def __init__(self, spec, bin_hash, newline_on_success, travis, xml_report): |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 168 | self._spec = spec |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 169 | self._bin_hash = bin_hash |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 170 | self._tempfile = tempfile.TemporaryFile() |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 171 | env = os.environ.copy() |
| 172 | for k, v in spec.environ.iteritems(): |
| 173 | env[k] = v |
Craig Tiller | 9d6139a | 2015-02-26 15:24:43 -0800 | [diff] [blame] | 174 | self._start = time.time() |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 175 | self._process = subprocess.Popen(args=spec.cmdline, |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 176 | stderr=subprocess.STDOUT, |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 177 | stdout=self._tempfile, |
Craig Tiller | 5058c69 | 2015-04-08 09:42:04 -0700 | [diff] [blame] | 178 | cwd=spec.cwd, |
Jan Tattermusch | e824359 | 2015-04-17 14:14:01 -0700 | [diff] [blame] | 179 | shell=spec.shell, |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 180 | env=env) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 181 | self._state = _RUNNING |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 182 | self._newline_on_success = newline_on_success |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 183 | self._travis = travis |
Nicolas "Pixel" Noble | 5937b5b | 2015-06-26 02:04:12 +0200 | [diff] [blame] | 184 | self._xml_test = ET.SubElement(xml_report, 'testcase', |
| 185 | name=self._spec.shortname) if xml_report is not None else None |
Craig Tiller | b84728d | 2015-02-26 15:40:39 -0800 | [diff] [blame] | 186 | message('START', spec.shortname, do_newline=self._travis) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 187 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 188 | def state(self, update_cache): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 189 | """Poll current state of the job. Prints messages at completion.""" |
| 190 | if self._state == _RUNNING and self._process.poll() is not None: |
Craig Tiller | 9d6139a | 2015-02-26 15:24:43 -0800 | [diff] [blame] | 191 | elapsed = time.time() - self._start |
Nicolas "Pixel" Noble | 5937b5b | 2015-06-26 02:04:12 +0200 | [diff] [blame] | 192 | self._tempfile.seek(0) |
| 193 | stdout = self._tempfile.read() |
| 194 | filtered_stdout = filter(lambda x: x in string.printable, stdout.decode(errors='ignore')) |
| 195 | if self._xml_test is not None: |
| 196 | self._xml_test.set('time', str(elapsed)) |
| 197 | ET.SubElement(self._xml_test, 'system-out').text = filtered_stdout |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 198 | if self._process.returncode != 0: |
| 199 | self._state = _FAILURE |
Craig Tiller | d0ffe14 | 2015-05-19 21:51:13 -0700 | [diff] [blame] | 200 | message('FAILED', '%s [ret=%d, pid=%d]' % ( |
| 201 | self._spec.shortname, self._process.returncode, self._process.pid), |
| 202 | stdout, do_newline=True) |
Nicolas "Pixel" Noble | 5937b5b | 2015-06-26 02:04:12 +0200 | [diff] [blame] | 203 | if self._xml_test is not None: |
| 204 | ET.SubElement(self._xml_test, 'failure', message='Failure').text |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 205 | else: |
| 206 | self._state = _SUCCESS |
Craig Tiller | 9d6139a | 2015-02-26 15:24:43 -0800 | [diff] [blame] | 207 | message('PASSED', '%s [time=%.1fsec]' % (self._spec.shortname, elapsed), |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 208 | do_newline=self._newline_on_success or self._travis) |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 209 | if self._bin_hash: |
| 210 | update_cache.finished(self._spec.identity(), self._bin_hash) |
Jan Tattermusch | 725835a | 2015-08-01 21:02:35 -0700 | [diff] [blame^] | 211 | elif self._state == _RUNNING and time.time() - self._start > self._spec.timeout_seconds: |
Craig Tiller | 8421678 | 2015-05-12 09:43:54 -0700 | [diff] [blame] | 212 | self._tempfile.seek(0) |
| 213 | stdout = self._tempfile.read() |
Nicolas "Pixel" Noble | f716c0c | 2015-07-12 01:26:17 +0200 | [diff] [blame] | 214 | filtered_stdout = filter(lambda x: x in string.printable, stdout.decode(errors='ignore')) |
Craig Tiller | 8421678 | 2015-05-12 09:43:54 -0700 | [diff] [blame] | 215 | message('TIMEOUT', self._spec.shortname, stdout, do_newline=True) |
Craig Tiller | 9b3cc74 | 2015-02-26 22:25:03 -0800 | [diff] [blame] | 216 | self.kill() |
Nicolas "Pixel" Noble | 5937b5b | 2015-06-26 02:04:12 +0200 | [diff] [blame] | 217 | if self._xml_test is not None: |
Nicolas "Pixel" Noble | f716c0c | 2015-07-12 01:26:17 +0200 | [diff] [blame] | 218 | ET.SubElement(self._xml_test, 'system-out').text = filtered_stdout |
Nicolas "Pixel" Noble | 5937b5b | 2015-06-26 02:04:12 +0200 | [diff] [blame] | 219 | ET.SubElement(self._xml_test, 'error', message='Timeout') |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 220 | return self._state |
| 221 | |
| 222 | def kill(self): |
| 223 | if self._state == _RUNNING: |
| 224 | self._state = _KILLED |
| 225 | self._process.terminate() |
| 226 | |
| 227 | |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 228 | class Jobset(object): |
| 229 | """Manages one run of jobs.""" |
| 230 | |
Craig Tiller | 533b1a2 | 2015-05-29 08:41:29 -0700 | [diff] [blame] | 231 | def __init__(self, check_cancelled, maxjobs, newline_on_success, travis, |
Nicolas "Pixel" Noble | 5937b5b | 2015-06-26 02:04:12 +0200 | [diff] [blame] | 232 | stop_on_failure, cache, xml_report): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 233 | self._running = set() |
| 234 | self._check_cancelled = check_cancelled |
| 235 | self._cancelled = False |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 236 | self._failures = 0 |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 237 | self._completed = 0 |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 238 | self._maxjobs = maxjobs |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 239 | self._newline_on_success = newline_on_success |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 240 | self._travis = travis |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 241 | self._cache = cache |
Craig Tiller | 533b1a2 | 2015-05-29 08:41:29 -0700 | [diff] [blame] | 242 | self._stop_on_failure = stop_on_failure |
Craig Tiller | 74e770d | 2015-06-11 09:38:09 -0700 | [diff] [blame] | 243 | self._hashes = {} |
Nicolas "Pixel" Noble | 5937b5b | 2015-06-26 02:04:12 +0200 | [diff] [blame] | 244 | self._xml_report = xml_report |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 245 | |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 246 | def start(self, spec): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 247 | """Start a job. Return True on success, False on failure.""" |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 248 | while len(self._running) >= self._maxjobs: |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 249 | if self.cancelled(): return False |
| 250 | self.reap() |
| 251 | if self.cancelled(): return False |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 252 | if spec.hash_targets: |
Craig Tiller | 74e770d | 2015-06-11 09:38:09 -0700 | [diff] [blame] | 253 | if spec.identity() in self._hashes: |
| 254 | bin_hash = self._hashes[spec.identity()] |
| 255 | else: |
| 256 | bin_hash = hashlib.sha1() |
| 257 | for fn in spec.hash_targets: |
| 258 | with open(which(fn)) as f: |
| 259 | bin_hash.update(f.read()) |
| 260 | bin_hash = bin_hash.hexdigest() |
| 261 | self._hashes[spec.identity()] = bin_hash |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 262 | should_run = self._cache.should_run(spec.identity(), bin_hash) |
| 263 | else: |
| 264 | bin_hash = None |
| 265 | should_run = True |
| 266 | if should_run: |
Craig Tiller | 5058c69 | 2015-04-08 09:42:04 -0700 | [diff] [blame] | 267 | try: |
| 268 | self._running.add(Job(spec, |
| 269 | bin_hash, |
| 270 | self._newline_on_success, |
Nicolas "Pixel" Noble | 5937b5b | 2015-06-26 02:04:12 +0200 | [diff] [blame] | 271 | self._travis, |
| 272 | self._xml_report)) |
Craig Tiller | 5058c69 | 2015-04-08 09:42:04 -0700 | [diff] [blame] | 273 | except: |
| 274 | message('FAILED', spec.shortname) |
| 275 | self._cancelled = True |
| 276 | return False |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 277 | return True |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 278 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 279 | def reap(self): |
| 280 | """Collect the dead jobs.""" |
| 281 | while self._running: |
| 282 | dead = set() |
| 283 | for job in self._running: |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 284 | st = job.state(self._cache) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 285 | if st == _RUNNING: continue |
Craig Tiller | 533b1a2 | 2015-05-29 08:41:29 -0700 | [diff] [blame] | 286 | if st == _FAILURE or st == _KILLED: |
| 287 | self._failures += 1 |
| 288 | if self._stop_on_failure: |
| 289 | self._cancelled = True |
| 290 | for job in self._running: |
| 291 | job.kill() |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 292 | dead.add(job) |
Craig Tiller | 74e770d | 2015-06-11 09:38:09 -0700 | [diff] [blame] | 293 | break |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 294 | for job in dead: |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 295 | self._completed += 1 |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 296 | self._running.remove(job) |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 297 | if dead: return |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 298 | if (not self._travis): |
| 299 | message('WAITING', '%d jobs running, %d complete, %d failed' % ( |
| 300 | len(self._running), self._completed, self._failures)) |
Craig Tiller | 5058c69 | 2015-04-08 09:42:04 -0700 | [diff] [blame] | 301 | if platform.system() == 'Windows': |
| 302 | time.sleep(0.1) |
| 303 | else: |
| 304 | global have_alarm |
| 305 | if not have_alarm: |
| 306 | have_alarm = True |
| 307 | signal.alarm(10) |
| 308 | signal.pause() |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 309 | |
| 310 | def cancelled(self): |
| 311 | """Poll for cancellation.""" |
| 312 | if self._cancelled: return True |
| 313 | if not self._check_cancelled(): return False |
| 314 | for job in self._running: |
| 315 | job.kill() |
| 316 | self._cancelled = True |
| 317 | return True |
| 318 | |
| 319 | def finish(self): |
| 320 | while self._running: |
| 321 | if self.cancelled(): pass # poll cancellation |
| 322 | self.reap() |
| 323 | return not self.cancelled() and self._failures == 0 |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 324 | |
| 325 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 326 | def _never_cancelled(): |
| 327 | return False |
| 328 | |
| 329 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 330 | # cache class that caches nothing |
| 331 | class NoCache(object): |
| 332 | def should_run(self, cmdline, bin_hash): |
| 333 | return True |
| 334 | |
| 335 | def finished(self, cmdline, bin_hash): |
| 336 | pass |
| 337 | |
| 338 | |
Nicolas Noble | b09078f | 2015-01-14 18:06:05 -0800 | [diff] [blame] | 339 | def run(cmdlines, |
| 340 | check_cancelled=_never_cancelled, |
| 341 | maxjobs=None, |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 342 | newline_on_success=False, |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 343 | travis=False, |
David Garcia Quintas | e90cd37 | 2015-05-31 18:15:26 -0700 | [diff] [blame] | 344 | infinite_runs=False, |
Craig Tiller | 533b1a2 | 2015-05-29 08:41:29 -0700 | [diff] [blame] | 345 | stop_on_failure=False, |
Nicolas "Pixel" Noble | 5937b5b | 2015-06-26 02:04:12 +0200 | [diff] [blame] | 346 | cache=None, |
| 347 | xml_report=None): |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 348 | js = Jobset(check_cancelled, |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 349 | maxjobs if maxjobs is not None else _DEFAULT_MAX_JOBS, |
Craig Tiller | 533b1a2 | 2015-05-29 08:41:29 -0700 | [diff] [blame] | 350 | newline_on_success, travis, stop_on_failure, |
Nicolas "Pixel" Noble | 5937b5b | 2015-06-26 02:04:12 +0200 | [diff] [blame] | 351 | cache if cache is not None else NoCache(), |
| 352 | xml_report) |
Craig Tiller | b84728d | 2015-02-26 15:40:39 -0800 | [diff] [blame] | 353 | for cmdline in cmdlines: |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 354 | if not js.start(cmdline): |
| 355 | break |
| 356 | return js.finish() |