| 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 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 new_fileobj.write = annotated_write | 313 new_fileobj.write = annotated_write |
| 314 new_fileobj.full_flush = full_flush | 314 new_fileobj.full_flush = full_flush |
| 315 return new_fileobj | 315 return new_fileobj |
| 316 | 316 |
| 317 | 317 |
| 318 def CheckCallAndFilter(args, stdout=None, filter_fn=None, | 318 def CheckCallAndFilter(args, stdout=None, filter_fn=None, |
| 319 print_stdout=None, call_filter_on_first_line=False, | 319 print_stdout=None, call_filter_on_first_line=False, |
| 320 **kwargs): | 320 **kwargs): |
| 321 """Runs a command and calls back a filter function if needed. | 321 """Runs a command and calls back a filter function if needed. |
| 322 | 322 |
| 323 Accepts all subprocess.Popen() parameters plus: | 323 Accepts all subprocess2.Popen() parameters plus: |
| 324 print_stdout: If True, the command's stdout is forwarded to stdout. | 324 print_stdout: If True, the command's stdout is forwarded to stdout. |
| 325 filter_fn: A function taking a single string argument called with each line | 325 filter_fn: A function taking a single string argument called with each line |
| 326 of the subprocess's output. Each line has the trailing newline | 326 of the subprocess2's output. Each line has the trailing newline |
| 327 character trimmed. | 327 character trimmed. |
| 328 stdout: Can be any bufferable output. | 328 stdout: Can be any bufferable output. |
| 329 | 329 |
| 330 stderr is always redirected to stdout. | 330 stderr is always redirected to stdout. |
| 331 """ | 331 """ |
| 332 assert print_stdout or filter_fn | 332 assert print_stdout or filter_fn |
| 333 stdout = stdout or sys.stdout | 333 stdout = stdout or sys.stdout |
| 334 filter_fn = filter_fn or (lambda x: None) | 334 filter_fn = filter_fn or (lambda x: None) |
| 335 assert not 'stderr' in kwargs | 335 assert not 'stderr' in kwargs |
| 336 kid = subprocess2.Popen( | 336 kid = subprocess2.Popen( |
| 337 args, bufsize=0, stdout=subprocess2.PIPE, stderr=subprocess2.STDOUT, | 337 args, bufsize=0, stdout=subprocess2.PIPE, stderr=subprocess2.STDOUT, |
| 338 **kwargs) | 338 **kwargs) |
| 339 | 339 |
| 340 # Do a flush of stdout before we begin reading from the subprocess's stdout | 340 # Do a flush of stdout before we begin reading from the subprocess2's stdout |
| 341 stdout.flush() | 341 stdout.flush() |
| 342 | 342 |
| 343 # Also, we need to forward stdout to prevent weird re-ordering of output. | 343 # Also, we need to forward stdout to prevent weird re-ordering of output. |
| 344 # This has to be done on a per byte basis to make sure it is not buffered: | 344 # This has to be done on a per byte basis to make sure it is not buffered: |
| 345 # normally buffering is done for each line, but if svn requests input, no | 345 # normally buffering is done for each line, but if svn requests input, no |
| 346 # end-of-line character is output after the prompt and it would not show up. | 346 # end-of-line character is output after the prompt and it would not show up. |
| 347 in_byte = kid.stdout.read(1) | 347 in_byte = kid.stdout.read(1) |
| 348 if in_byte: | 348 if in_byte: |
| 349 if call_filter_on_first_line: | 349 if call_filter_on_first_line: |
| 350 filter_fn(None) | 350 filter_fn(None) |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 614 logging.info('Caught exception in thread %s' % self.item.name) | 614 logging.info('Caught exception in thread %s' % self.item.name) |
| 615 logging.info(str(sys.exc_info())) | 615 logging.info(str(sys.exc_info())) |
| 616 work_queue.exceptions.put(sys.exc_info()) | 616 work_queue.exceptions.put(sys.exc_info()) |
| 617 logging.info('Task %s done' % self.item.name) | 617 logging.info('Task %s done' % self.item.name) |
| 618 | 618 |
| 619 work_queue.ready_cond.acquire() | 619 work_queue.ready_cond.acquire() |
| 620 try: | 620 try: |
| 621 work_queue.ready_cond.notifyAll() | 621 work_queue.ready_cond.notifyAll() |
| 622 finally: | 622 finally: |
| 623 work_queue.ready_cond.release() | 623 work_queue.ready_cond.release() |
| OLD | NEW |