OLD | NEW |
1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Generic utils.""" | 5 """Generic utils.""" |
6 | 6 |
7 import errno | 7 import errno |
8 import logging | 8 import logging |
9 import os | 9 import os |
10 import Queue | 10 import Queue |
(...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
447 return None | 447 return None |
448 | 448 |
449 env = {} | 449 env = {} |
450 execfile(config_path, env) | 450 execfile(config_path, env) |
451 config_dir = os.path.dirname(config_path) | 451 config_dir = os.path.dirname(config_path) |
452 return config_dir, env['entries'] | 452 return config_dir, env['entries'] |
453 | 453 |
454 | 454 |
455 class WorkItem(object): | 455 class WorkItem(object): |
456 """One work item.""" | 456 """One work item.""" |
457 # A list of string, each being a WorkItem name. | 457 def __init__(self): |
458 requirements = [] | 458 # A list of string, each being a WorkItem name. |
459 # A unique string representing this work item. | 459 self.requirements = [] |
460 name = None | 460 # A unique string representing this work item. |
| 461 self.name = None |
461 | 462 |
462 def run(self, work_queue): | 463 def run(self, work_queue): |
463 """work_queue is passed as keyword argument so it should be | 464 """work_queue is passed as keyword argument so it should be |
464 the last parameters of the function when you override it.""" | 465 the last parameters of the function when you override it.""" |
465 pass | 466 pass |
466 | 467 |
467 | 468 |
468 class ExecutionQueue(object): | 469 class ExecutionQueue(object): |
469 """Runs a set of WorkItem that have interdependencies and were WorkItem are | 470 """Runs a set of WorkItem that have interdependencies and were WorkItem are |
470 added as they are processed. | 471 added as they are processed. |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
541 self._run_one_task(self.queued.pop(i), args, kwargs) | 542 self._run_one_task(self.queued.pop(i), args, kwargs) |
542 break | 543 break |
543 else: | 544 else: |
544 # Couldn't find an item that could run. Break out the outher loop. | 545 # Couldn't find an item that could run. Break out the outher loop. |
545 break | 546 break |
546 | 547 |
547 if not self.queued and not self.running: | 548 if not self.queued and not self.running: |
548 # We're done. | 549 # We're done. |
549 break | 550 break |
550 # We need to poll here otherwise Ctrl-C isn't processed. | 551 # We need to poll here otherwise Ctrl-C isn't processed. |
551 self.ready_cond.wait(10) | 552 try: |
| 553 self.ready_cond.wait(10) |
| 554 except KeyboardInterrupt: |
| 555 # Help debugging by printing some information: |
| 556 print >> sys.stderr, ( |
| 557 ('\nAllowed parallel jobs: %d\n# queued: %d\nRan: %s\n' |
| 558 'Running: %d') % ( |
| 559 self.jobs, |
| 560 len(self.queued), |
| 561 ', '.join(self.ran), |
| 562 len(self.running))) |
| 563 for i in self.queued: |
| 564 print >> sys.stderr, '%s: %s' % (i.name, ', '.join(i.requirements)) |
| 565 raise |
552 # Something happened: self.enqueue() or a thread terminated. Loop again. | 566 # Something happened: self.enqueue() or a thread terminated. Loop again. |
553 finally: | 567 finally: |
554 self.ready_cond.release() | 568 self.ready_cond.release() |
555 | 569 |
556 assert not self.running, 'Now guaranteed to be single-threaded' | 570 assert not self.running, 'Now guaranteed to be single-threaded' |
557 if not self.exceptions.empty(): | 571 if not self.exceptions.empty(): |
558 # To get back the stack location correctly, the raise a, b, c form must be | 572 # To get back the stack location correctly, the raise a, b, c form must be |
559 # used, passing a tuple as the first argument doesn't work. | 573 # used, passing a tuple as the first argument doesn't work. |
560 e = self.exceptions.get() | 574 e = self.exceptions.get() |
561 raise e[0], e[1], e[2] | 575 raise e[0], e[1], e[2] |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
614 logging.info('Caught exception in thread %s' % self.item.name) | 628 logging.info('Caught exception in thread %s' % self.item.name) |
615 logging.info(str(sys.exc_info())) | 629 logging.info(str(sys.exc_info())) |
616 work_queue.exceptions.put(sys.exc_info()) | 630 work_queue.exceptions.put(sys.exc_info()) |
617 logging.info('Task %s done' % self.item.name) | 631 logging.info('Task %s done' % self.item.name) |
618 | 632 |
619 work_queue.ready_cond.acquire() | 633 work_queue.ready_cond.acquire() |
620 try: | 634 try: |
621 work_queue.ready_cond.notifyAll() | 635 work_queue.ready_cond.notifyAll() |
622 finally: | 636 finally: |
623 work_queue.ready_cond.release() | 637 work_queue.ready_cond.release() |
OLD | NEW |