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 logging | 8 import logging |
9 import os | 9 import os |
10 import pipes | 10 import pipes |
(...skipping 473 matching lines...) Loading... |
484 if not retry: | 484 if not retry: |
485 break | 485 break |
486 print ("WARNING: subprocess '%s' in %s failed; will retry after a short " | 486 print ("WARNING: subprocess '%s' in %s failed; will retry after a short " |
487 'nap...' % (' '.join('"%s"' % x for x in args), run_cwd)) | 487 'nap...' % (' '.join('"%s"' % x for x in args), run_cwd)) |
488 time.sleep(sleep_interval) | 488 time.sleep(sleep_interval) |
489 sleep_interval *= 2 | 489 sleep_interval *= 2 |
490 raise subprocess2.CalledProcessError( | 490 raise subprocess2.CalledProcessError( |
491 rv, args, kwargs.get('cwd', None), None, None) | 491 rv, args, kwargs.get('cwd', None), None, None) |
492 | 492 |
493 | 493 |
| 494 class GitFilter(object): |
| 495 """A filter_fn implementation for quieting down git output messages. |
| 496 |
| 497 Allows a custom function to skip certain lines (predicate), and will throttle |
| 498 the output of percentage completed lines to only output every X seconds. |
| 499 """ |
| 500 PERCENT_RE = re.compile('.* ([0-9]{1,2})% .*') |
| 501 |
| 502 def __init__(self, time_throttle=0, predicate=None): |
| 503 """ |
| 504 Args: |
| 505 time_throttle (int): GitFilter will throttle 'noisy' output (such as the |
| 506 XX% complete messages) to only be printed at least |time_throttle| |
| 507 seconds apart. |
| 508 predicate (f(line)): An optional function which is invoked for every line. |
| 509 The line will be skipped if predicate(line) returns False. |
| 510 """ |
| 511 self.last_time = 0 |
| 512 self.time_throttle = time_throttle |
| 513 self.predicate = predicate |
| 514 |
| 515 def __call__(self, line): |
| 516 # git uses an escape sequence to clear the line; elide it. |
| 517 esc = line.find(unichr(033)) |
| 518 if esc > -1: |
| 519 line = line[:esc] |
| 520 if self.predicate and not self.predicate(line): |
| 521 return |
| 522 now = time.time() |
| 523 match = self.PERCENT_RE.match(line) |
| 524 if not match: |
| 525 self.last_time = 0 |
| 526 if (now - self.last_time) >= self.time_throttle: |
| 527 self.last_time = now |
| 528 print line |
| 529 |
| 530 |
494 def FindGclientRoot(from_dir, filename='.gclient'): | 531 def FindGclientRoot(from_dir, filename='.gclient'): |
495 """Tries to find the gclient root.""" | 532 """Tries to find the gclient root.""" |
496 real_from_dir = os.path.realpath(from_dir) | 533 real_from_dir = os.path.realpath(from_dir) |
497 path = real_from_dir | 534 path = real_from_dir |
498 while not os.path.exists(os.path.join(path, filename)): | 535 while not os.path.exists(os.path.join(path, filename)): |
499 split_path = os.path.split(path) | 536 split_path = os.path.split(path) |
500 if not split_path[1]: | 537 if not split_path[1]: |
501 return None | 538 return None |
502 path = split_path[0] | 539 path = split_path[0] |
503 | 540 |
(...skipping 409 matching lines...) Loading... |
913 | 950 |
914 Python on OSX 10.6 raises a NotImplementedError exception. | 951 Python on OSX 10.6 raises a NotImplementedError exception. |
915 """ | 952 """ |
916 try: | 953 try: |
917 import multiprocessing | 954 import multiprocessing |
918 return multiprocessing.cpu_count() | 955 return multiprocessing.cpu_count() |
919 except: # pylint: disable=W0702 | 956 except: # pylint: disable=W0702 |
920 # Mac OS 10.6 only | 957 # Mac OS 10.6 only |
921 # pylint: disable=E1101 | 958 # pylint: disable=E1101 |
922 return int(os.sysconf('SC_NPROCESSORS_ONLN')) | 959 return int(os.sysconf('SC_NPROCESSORS_ONLN')) |
OLD | NEW |