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 |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 36 | import random |
Craig Tiller | 336ad50 | 2015-02-24 14:46:02 -0800 | [diff] [blame] | 37 | import signal |
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 Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 42 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 43 | |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 44 | _DEFAULT_MAX_JOBS = 16 * multiprocessing.cpu_count() |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 45 | |
| 46 | |
Craig Tiller | 336ad50 | 2015-02-24 14:46:02 -0800 | [diff] [blame] | 47 | # setup a signal handler so that signal.pause registers 'something' |
| 48 | # when a child finishes |
| 49 | # not using futures and threading to avoid a dependency on subprocess32 |
Craig Tiller | 5058c69 | 2015-04-08 09:42:04 -0700 | [diff] [blame] | 50 | if platform.system() == "Windows": |
| 51 | pass |
| 52 | else: |
| 53 | have_alarm = False |
| 54 | def alarm_handler(unused_signum, unused_frame): |
| 55 | global have_alarm |
| 56 | have_alarm = False |
| 57 | |
| 58 | signal.signal(signal.SIGCHLD, lambda unused_signum, unused_frame: None) |
| 59 | signal.signal(signal.SIGALRM, alarm_handler) |
Craig Tiller | 336ad50 | 2015-02-24 14:46:02 -0800 | [diff] [blame] | 60 | |
| 61 | |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 62 | def shuffle_iteratable(it): |
| 63 | """Return an iterable that randomly walks it""" |
| 64 | # take a random sampling from the passed in iterable |
Nicolas "Pixel" Noble | 99768ac | 2015-05-13 02:34:06 +0200 | [diff] [blame] | 65 | # we take an element with probability 1/p and rapidly increase |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 66 | # p as we take elements - this gives us a somewhat random set of values before |
| 67 | # we've seen all the values, but starts producing values without having to |
| 68 | # compute ALL of them at once, allowing tests to start a little earlier |
| 69 | nextit = [] |
| 70 | p = 1 |
| 71 | for val in it: |
| 72 | if random.randint(0, p) == 0: |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 73 | p = min(p*2, 100) |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 74 | yield val |
| 75 | else: |
| 76 | nextit.append(val) |
| 77 | # after taking a random sampling, we shuffle the rest of the elements and |
| 78 | # yield them |
| 79 | random.shuffle(nextit) |
| 80 | for val in nextit: |
| 81 | yield val |
| 82 | |
| 83 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 84 | _SUCCESS = object() |
| 85 | _FAILURE = object() |
| 86 | _RUNNING = object() |
| 87 | _KILLED = object() |
| 88 | |
| 89 | |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 90 | _COLORS = { |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 91 | 'red': [ 31, 0 ], |
| 92 | 'green': [ 32, 0 ], |
| 93 | 'yellow': [ 33, 0 ], |
| 94 | 'lightgray': [ 37, 0], |
| 95 | 'gray': [ 30, 1 ], |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | |
| 99 | _BEGINNING_OF_LINE = '\x1b[0G' |
| 100 | _CLEAR_LINE = '\x1b[2K' |
| 101 | |
| 102 | |
| 103 | _TAG_COLOR = { |
| 104 | 'FAILED': 'red', |
Craig Tiller | e1d0d1c | 2015-02-27 08:54:23 -0800 | [diff] [blame] | 105 | 'TIMEOUT': 'red', |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 106 | 'PASSED': 'green', |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 107 | 'START': 'gray', |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 108 | 'WAITING': 'yellow', |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 109 | 'SUCCESS': 'green', |
| 110 | 'IDLE': 'gray', |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | |
Nicolas "Pixel" Noble | 99768ac | 2015-05-13 02:34:06 +0200 | [diff] [blame] | 114 | def message(tag, msg, explanatory_text=None, do_newline=False): |
| 115 | if message.old_tag == tag and message.old_msg == msg and not explanatory_text: |
| 116 | return |
| 117 | message.old_tag = tag |
| 118 | message.old_msg = msg |
Craig Tiller | 5058c69 | 2015-04-08 09:42:04 -0700 | [diff] [blame] | 119 | if platform.system() == 'Windows': |
| 120 | if explanatory_text: |
| 121 | print explanatory_text |
Nicolas "Pixel" Noble | 99768ac | 2015-05-13 02:34:06 +0200 | [diff] [blame] | 122 | print '%s: %s' % (tag, msg) |
Craig Tiller | 5058c69 | 2015-04-08 09:42:04 -0700 | [diff] [blame] | 123 | return |
Craig Tiller | 23d2f3f | 2015-02-24 15:23:32 -0800 | [diff] [blame] | 124 | try: |
| 125 | sys.stdout.write('%s%s%s\x1b[%d;%dm%s\x1b[0m: %s%s' % ( |
| 126 | _BEGINNING_OF_LINE, |
| 127 | _CLEAR_LINE, |
| 128 | '\n%s' % explanatory_text if explanatory_text is not None else '', |
| 129 | _COLORS[_TAG_COLOR[tag]][1], |
| 130 | _COLORS[_TAG_COLOR[tag]][0], |
| 131 | tag, |
Nicolas "Pixel" Noble | 99768ac | 2015-05-13 02:34:06 +0200 | [diff] [blame] | 132 | msg, |
Craig Tiller | 23d2f3f | 2015-02-24 15:23:32 -0800 | [diff] [blame] | 133 | '\n' if do_newline or explanatory_text is not None else '')) |
| 134 | sys.stdout.flush() |
| 135 | except: |
| 136 | pass |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 137 | |
Nicolas "Pixel" Noble | 99768ac | 2015-05-13 02:34:06 +0200 | [diff] [blame] | 138 | message.old_tag = "" |
| 139 | message.old_msg = "" |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 140 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 141 | def which(filename): |
| 142 | if '/' in filename: |
| 143 | return filename |
| 144 | for path in os.environ['PATH'].split(os.pathsep): |
| 145 | if os.path.exists(os.path.join(path, filename)): |
| 146 | return os.path.join(path, filename) |
| 147 | raise Exception('%s not found' % filename) |
| 148 | |
| 149 | |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 150 | class JobSpec(object): |
| 151 | """Specifies what to run for a job.""" |
| 152 | |
Jan Tattermusch | e824359 | 2015-04-17 14:14:01 -0700 | [diff] [blame] | 153 | def __init__(self, cmdline, shortname=None, environ=None, hash_targets=None, cwd=None, shell=False): |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 154 | """ |
| 155 | Arguments: |
| 156 | cmdline: a list of arguments to pass as the command line |
| 157 | environ: a dictionary of environment variables to set in the child process |
| 158 | hash_targets: which files to include in the hash representing the jobs version |
| 159 | (or empty, indicating the job should not be hashed) |
| 160 | """ |
murgatroid99 | 132ce6a | 2015-03-04 17:29:14 -0800 | [diff] [blame] | 161 | if environ is None: |
| 162 | environ = {} |
| 163 | if hash_targets is None: |
| 164 | hash_targets = [] |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 165 | self.cmdline = cmdline |
| 166 | self.environ = environ |
| 167 | self.shortname = cmdline[0] if shortname is None else shortname |
| 168 | self.hash_targets = hash_targets or [] |
Craig Tiller | 5058c69 | 2015-04-08 09:42:04 -0700 | [diff] [blame] | 169 | self.cwd = cwd |
Jan Tattermusch | e824359 | 2015-04-17 14:14:01 -0700 | [diff] [blame] | 170 | self.shell = shell |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 171 | |
| 172 | def identity(self): |
| 173 | return '%r %r %r' % (self.cmdline, self.environ, self.hash_targets) |
| 174 | |
| 175 | def __hash__(self): |
| 176 | return hash(self.identity()) |
| 177 | |
| 178 | def __cmp__(self, other): |
| 179 | return self.identity() == other.identity() |
| 180 | |
| 181 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 182 | class Job(object): |
| 183 | """Manages one job.""" |
| 184 | |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 185 | def __init__(self, spec, bin_hash, newline_on_success, travis): |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 186 | self._spec = spec |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 187 | self._bin_hash = bin_hash |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 188 | self._tempfile = tempfile.TemporaryFile() |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 189 | env = os.environ.copy() |
| 190 | for k, v in spec.environ.iteritems(): |
| 191 | env[k] = v |
Craig Tiller | 9d6139a | 2015-02-26 15:24:43 -0800 | [diff] [blame] | 192 | self._start = time.time() |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 193 | self._process = subprocess.Popen(args=spec.cmdline, |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 194 | stderr=subprocess.STDOUT, |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 195 | stdout=self._tempfile, |
Craig Tiller | 5058c69 | 2015-04-08 09:42:04 -0700 | [diff] [blame] | 196 | cwd=spec.cwd, |
Jan Tattermusch | e824359 | 2015-04-17 14:14:01 -0700 | [diff] [blame] | 197 | shell=spec.shell, |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 198 | env=env) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 199 | self._state = _RUNNING |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 200 | self._newline_on_success = newline_on_success |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 201 | self._travis = travis |
Craig Tiller | b84728d | 2015-02-26 15:40:39 -0800 | [diff] [blame] | 202 | message('START', spec.shortname, do_newline=self._travis) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 203 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 204 | def state(self, update_cache): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 205 | """Poll current state of the job. Prints messages at completion.""" |
| 206 | if self._state == _RUNNING and self._process.poll() is not None: |
Craig Tiller | 9d6139a | 2015-02-26 15:24:43 -0800 | [diff] [blame] | 207 | elapsed = time.time() - self._start |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 208 | if self._process.returncode != 0: |
| 209 | self._state = _FAILURE |
| 210 | self._tempfile.seek(0) |
| 211 | stdout = self._tempfile.read() |
Craig Tiller | d0ffe14 | 2015-05-19 21:51:13 -0700 | [diff] [blame] | 212 | message('FAILED', '%s [ret=%d, pid=%d]' % ( |
| 213 | self._spec.shortname, self._process.returncode, self._process.pid), |
| 214 | stdout, do_newline=True) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 215 | else: |
| 216 | self._state = _SUCCESS |
Craig Tiller | 9d6139a | 2015-02-26 15:24:43 -0800 | [diff] [blame] | 217 | message('PASSED', '%s [time=%.1fsec]' % (self._spec.shortname, elapsed), |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 218 | do_newline=self._newline_on_success or self._travis) |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 219 | if self._bin_hash: |
| 220 | update_cache.finished(self._spec.identity(), self._bin_hash) |
Craig Tiller | 9b3cc74 | 2015-02-26 22:25:03 -0800 | [diff] [blame] | 221 | elif self._state == _RUNNING and time.time() - self._start > 300: |
Craig Tiller | 8421678 | 2015-05-12 09:43:54 -0700 | [diff] [blame] | 222 | self._tempfile.seek(0) |
| 223 | stdout = self._tempfile.read() |
| 224 | message('TIMEOUT', self._spec.shortname, stdout, do_newline=True) |
Craig Tiller | 9b3cc74 | 2015-02-26 22:25:03 -0800 | [diff] [blame] | 225 | self.kill() |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 226 | return self._state |
| 227 | |
| 228 | def kill(self): |
| 229 | if self._state == _RUNNING: |
| 230 | self._state = _KILLED |
| 231 | self._process.terminate() |
| 232 | |
| 233 | |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 234 | class Jobset(object): |
| 235 | """Manages one run of jobs.""" |
| 236 | |
Craig Tiller | 533b1a2 | 2015-05-29 08:41:29 -0700 | [diff] [blame] | 237 | def __init__(self, check_cancelled, maxjobs, newline_on_success, travis, |
| 238 | stop_on_failure, cache): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 239 | self._running = set() |
| 240 | self._check_cancelled = check_cancelled |
| 241 | self._cancelled = False |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 242 | self._failures = 0 |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 243 | self._completed = 0 |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 244 | self._maxjobs = maxjobs |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 245 | self._newline_on_success = newline_on_success |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 246 | self._travis = travis |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 247 | self._cache = cache |
Craig Tiller | 533b1a2 | 2015-05-29 08:41:29 -0700 | [diff] [blame] | 248 | self._stop_on_failure = stop_on_failure |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 249 | |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 250 | def start(self, spec): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 251 | """Start a job. Return True on success, False on failure.""" |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 252 | while len(self._running) >= self._maxjobs: |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 253 | if self.cancelled(): return False |
| 254 | self.reap() |
| 255 | if self.cancelled(): return False |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 256 | if spec.hash_targets: |
| 257 | bin_hash = hashlib.sha1() |
| 258 | for fn in spec.hash_targets: |
| 259 | with open(which(fn)) as f: |
| 260 | bin_hash.update(f.read()) |
| 261 | bin_hash = bin_hash.hexdigest() |
| 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, |
| 271 | self._travis)) |
| 272 | except: |
| 273 | message('FAILED', spec.shortname) |
| 274 | self._cancelled = True |
| 275 | return False |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 276 | return True |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 277 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 278 | def reap(self): |
| 279 | """Collect the dead jobs.""" |
| 280 | while self._running: |
| 281 | dead = set() |
| 282 | for job in self._running: |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 283 | st = job.state(self._cache) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 284 | if st == _RUNNING: continue |
Craig Tiller | 533b1a2 | 2015-05-29 08:41:29 -0700 | [diff] [blame] | 285 | if st == _FAILURE or st == _KILLED: |
| 286 | self._failures += 1 |
| 287 | if self._stop_on_failure: |
| 288 | self._cancelled = True |
| 289 | for job in self._running: |
| 290 | job.kill() |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 291 | dead.add(job) |
| 292 | for job in dead: |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 293 | self._completed += 1 |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 294 | self._running.remove(job) |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 295 | if dead: return |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 296 | if (not self._travis): |
| 297 | message('WAITING', '%d jobs running, %d complete, %d failed' % ( |
| 298 | len(self._running), self._completed, self._failures)) |
Craig Tiller | 5058c69 | 2015-04-08 09:42:04 -0700 | [diff] [blame] | 299 | if platform.system() == 'Windows': |
| 300 | time.sleep(0.1) |
| 301 | else: |
| 302 | global have_alarm |
| 303 | if not have_alarm: |
| 304 | have_alarm = True |
| 305 | signal.alarm(10) |
| 306 | signal.pause() |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 307 | |
| 308 | def cancelled(self): |
| 309 | """Poll for cancellation.""" |
| 310 | if self._cancelled: return True |
| 311 | if not self._check_cancelled(): return False |
| 312 | for job in self._running: |
| 313 | job.kill() |
| 314 | self._cancelled = True |
| 315 | return True |
| 316 | |
| 317 | def finish(self): |
| 318 | while self._running: |
| 319 | if self.cancelled(): pass # poll cancellation |
| 320 | self.reap() |
| 321 | return not self.cancelled() and self._failures == 0 |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 322 | |
| 323 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 324 | def _never_cancelled(): |
| 325 | return False |
| 326 | |
| 327 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 328 | # cache class that caches nothing |
| 329 | class NoCache(object): |
| 330 | def should_run(self, cmdline, bin_hash): |
| 331 | return True |
| 332 | |
| 333 | def finished(self, cmdline, bin_hash): |
| 334 | pass |
| 335 | |
| 336 | |
Nicolas Noble | b09078f | 2015-01-14 18:06:05 -0800 | [diff] [blame] | 337 | def run(cmdlines, |
| 338 | check_cancelled=_never_cancelled, |
| 339 | maxjobs=None, |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 340 | newline_on_success=False, |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 341 | travis=False, |
David Garcia Quintas | e90cd37 | 2015-05-31 18:15:26 -0700 | [diff] [blame^] | 342 | infinite_runs=False, |
Craig Tiller | 533b1a2 | 2015-05-29 08:41:29 -0700 | [diff] [blame] | 343 | stop_on_failure=False, |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 344 | cache=None): |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 345 | js = Jobset(check_cancelled, |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 346 | maxjobs if maxjobs is not None else _DEFAULT_MAX_JOBS, |
Craig Tiller | 533b1a2 | 2015-05-29 08:41:29 -0700 | [diff] [blame] | 347 | newline_on_success, travis, stop_on_failure, |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 348 | cache if cache is not None else NoCache()) |
David Garcia Quintas | e90cd37 | 2015-05-31 18:15:26 -0700 | [diff] [blame^] | 349 | # We can't sort an infinite sequence of runs. |
| 350 | if not travis or infinite_runs: |
Craig Tiller | b84728d | 2015-02-26 15:40:39 -0800 | [diff] [blame] | 351 | cmdlines = shuffle_iteratable(cmdlines) |
| 352 | else: |
Craig Tiller | 904da8c | 2015-02-26 15:59:15 -0800 | [diff] [blame] | 353 | cmdlines = sorted(cmdlines, key=lambda x: x.shortname) |
Craig Tiller | b84728d | 2015-02-26 15:40:39 -0800 | [diff] [blame] | 354 | for cmdline in cmdlines: |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 355 | if not js.start(cmdline): |
| 356 | break |
| 357 | return js.finish() |