| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 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 codecs | 7 import codecs |
| 8 import cStringIO | 8 import cStringIO |
| 9 import datetime | 9 import datetime |
| 10 import logging | 10 import logging |
| (...skipping 766 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 777 # On cygwin, creating a lock throwing randomly when nearing ~100 locks. | 777 # On cygwin, creating a lock throwing randomly when nearing ~100 locks. |
| 778 # As a workaround, use a single lock. Yep you read it right. Single lock for | 778 # As a workaround, use a single lock. Yep you read it right. Single lock for |
| 779 # all the 100 objects. | 779 # all the 100 objects. |
| 780 lock = threading.Lock() | 780 lock = threading.Lock() |
| 781 | 781 |
| 782 def __init__(self, name): | 782 def __init__(self, name): |
| 783 # A unique string representing this work item. | 783 # A unique string representing this work item. |
| 784 self._name = name | 784 self._name = name |
| 785 self.outbuf = cStringIO.StringIO() | 785 self.outbuf = cStringIO.StringIO() |
| 786 self.start = self.finish = None | 786 self.start = self.finish = None |
| 787 self.resources = [] # List of resources this work item requires. |
| 787 | 788 |
| 788 def run(self, work_queue): | 789 def run(self, work_queue): |
| 789 """work_queue is passed as keyword argument so it should be | 790 """work_queue is passed as keyword argument so it should be |
| 790 the last parameters of the function when you override it.""" | 791 the last parameters of the function when you override it.""" |
| 791 pass | 792 pass |
| 792 | 793 |
| 793 @property | 794 @property |
| 794 def name(self): | 795 def name(self): |
| 795 return self._name | 796 return self._name |
| 796 | 797 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 862 str(task.finish - task.start).partition('.')[0]) | 863 str(task.finish - task.start).partition('.')[0]) |
| 863 else: | 864 else: |
| 864 elapsed = '' | 865 elapsed = '' |
| 865 return """ | 866 return """ |
| 866 %s%s%s | 867 %s%s%s |
| 867 ---------------------------------------- | 868 ---------------------------------------- |
| 868 %s | 869 %s |
| 869 ----------------------------------------""" % ( | 870 ----------------------------------------""" % ( |
| 870 task.name, comment, elapsed, task.outbuf.getvalue().strip()) | 871 task.name, comment, elapsed, task.outbuf.getvalue().strip()) |
| 871 | 872 |
| 873 def _is_conflict(self, job): |
| 874 """Checks to see if a job will conflict with another running job.""" |
| 875 for running_job in self.running: |
| 876 for used_resource in running_job.item.resources: |
| 877 logging.debug('Checking resource %s' % used_resource) |
| 878 if used_resource in job.resources: |
| 879 return True |
| 880 return False |
| 881 |
| 872 def flush(self, *args, **kwargs): | 882 def flush(self, *args, **kwargs): |
| 873 """Runs all enqueued items until all are executed.""" | 883 """Runs all enqueued items until all are executed.""" |
| 874 kwargs['work_queue'] = self | 884 kwargs['work_queue'] = self |
| 875 self.last_subproc_output = self.last_join = datetime.datetime.now() | 885 self.last_subproc_output = self.last_join = datetime.datetime.now() |
| 876 self.ready_cond.acquire() | 886 self.ready_cond.acquire() |
| 877 try: | 887 try: |
| 878 while True: | 888 while True: |
| 879 # Check for task to run first, then wait. | 889 # Check for task to run first, then wait. |
| 880 while True: | 890 while True: |
| 881 if not self.exceptions.empty(): | 891 if not self.exceptions.empty(): |
| 882 # Systematically flush the queue when an exception logged. | 892 # Systematically flush the queue when an exception logged. |
| 883 self.queued = [] | 893 self.queued = [] |
| 884 self._flush_terminated_threads() | 894 self._flush_terminated_threads() |
| 885 if (not self.queued and not self.running or | 895 if (not self.queued and not self.running or |
| 886 self.jobs == len(self.running)): | 896 self.jobs == len(self.running)): |
| 887 logging.debug('No more worker threads or can\'t queue anything.') | 897 logging.debug('No more worker threads or can\'t queue anything.') |
| 888 break | 898 break |
| 889 | 899 |
| 890 # Check for new tasks to start. | 900 # Check for new tasks to start. |
| 891 for i in xrange(len(self.queued)): | 901 for i in xrange(len(self.queued)): |
| 892 # Verify its requirements. | 902 # Verify its requirements. |
| 893 if (self.ignore_requirements or | 903 if (self.ignore_requirements or |
| 894 not (set(self.queued[i].requirements) - set(self.ran))): | 904 not (set(self.queued[i].requirements) - set(self.ran))): |
| 895 # Start one work item: all its requirements are satisfied. | 905 if not self._is_conflict(self.queued[i]): |
| 896 self._run_one_task(self.queued.pop(i), args, kwargs) | 906 # Start one work item: all its requirements are satisfied. |
| 897 break | 907 self._run_one_task(self.queued.pop(i), args, kwargs) |
| 908 break |
| 898 else: | 909 else: |
| 899 # Couldn't find an item that could run. Break out the outher loop. | 910 # Couldn't find an item that could run. Break out the outher loop. |
| 900 break | 911 break |
| 901 | 912 |
| 902 if not self.queued and not self.running: | 913 if not self.queued and not self.running: |
| 903 # We're done. | 914 # We're done. |
| 904 break | 915 break |
| 905 # We need to poll here otherwise Ctrl-C isn't processed. | 916 # We need to poll here otherwise Ctrl-C isn't processed. |
| 906 try: | 917 try: |
| 907 self.ready_cond.wait(10) | 918 self.ready_cond.wait(10) |
| (...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1212 # Just incase we have some ~/blah paths. | 1223 # Just incase we have some ~/blah paths. |
| 1213 target = os.path.abspath(os.path.expanduser(target)) | 1224 target = os.path.abspath(os.path.expanduser(target)) |
| 1214 if os.path.isfile(target) and os.access(target, os.X_OK): | 1225 if os.path.isfile(target) and os.access(target, os.X_OK): |
| 1215 return target | 1226 return target |
| 1216 if sys.platform.startswith('win'): | 1227 if sys.platform.startswith('win'): |
| 1217 for suffix in ('.bat', '.cmd', '.exe'): | 1228 for suffix in ('.bat', '.cmd', '.exe'): |
| 1218 alt_target = target + suffix | 1229 alt_target = target + suffix |
| 1219 if os.path.isfile(alt_target) and os.access(alt_target, os.X_OK): | 1230 if os.path.isfile(alt_target) and os.access(alt_target, os.X_OK): |
| 1220 return alt_target | 1231 return alt_target |
| 1221 return None | 1232 return None |
| OLD | NEW |