diff --git a/gclient.py b/gclient.py index 4699d1f95..cef153eb7 100644 --- a/gclient.py +++ b/gclient.py @@ -1192,6 +1192,8 @@ def Main(argv): options.entries_filename = options.config_filename + '_entries' if options.jobs < 1: parser.error('--jobs must be 1 or higher') + # Useful for --jobs. + options.stdout = sys.stdout # These hacks need to die. if not hasattr(options, 'revisions'): diff --git a/gclient_scm.py b/gclient_scm.py index be176fb45..50d9be7cf 100644 --- a/gclient_scm.py +++ b/gclient_scm.py @@ -154,7 +154,8 @@ class GitWrapper(SCMWrapper): merge_base = self._Run(['merge-base', 'HEAD', 'origin']) command = ['diff', merge_base] filterer = DiffFilterer(self.relpath) - scm.GIT.RunAndFilterOutput(command, path, False, False, filterer.Filter) + scm.GIT.RunAndFilterOutput(command, path, False, False, filterer.Filter, + stdout=options.stdout) def update(self, options, args, file_list): """Runs git to update or transparently checkout the working copy. @@ -706,7 +707,8 @@ class SVNWrapper(SCMWrapper): command.extend(args) filterer = DiffFilterer(self.relpath) - scm.SVN.RunAndFilterOutput(command, path, False, False, filterer.Filter) + scm.SVN.RunAndFilterOutput(command, path, False, False, filterer.Filter, + stdout=options.stdout) def update(self, options, args, file_list): """Runs svn to update or transparently checkout the working copy. diff --git a/gclient_utils.py b/gclient_utils.py index 0ab322a2d..8c75ecefa 100644 --- a/gclient_utils.py +++ b/gclient_utils.py @@ -266,7 +266,9 @@ def SubprocessCallAndFilter(command, in_directory, print_messages, print_stdout, - fail_status=None, filter_fn=None): + fail_status=None, + filter_fn=None, + stdout=None): """Runs command, a list, in directory in_directory. If print_messages is true, a message indicating what is being done @@ -287,9 +289,10 @@ def SubprocessCallAndFilter(command, exit with an exit status of fail_status. If fail_status is None (the default), gclient will raise an Error exception. """ + stdout = stdout or sys.stdout logging.debug(command) if print_messages: - print('\n________ running \'%s\' in \'%s\'' + stdout.write('\n________ running \'%s\' in \'%s\'\n' % (' '.join(command), in_directory)) kid = Popen(command, bufsize=0, cwd=in_directory, @@ -298,7 +301,7 @@ def SubprocessCallAndFilter(command, # Do a flush of sys.stdout before we begin reading from the subprocess's # stdout. last_flushed_at = time.time() - sys.stdout.flush() + stdout.flush() # Also, we need to forward stdout to prevent weird re-ordering of output. # This has to be done on a per byte basis to make sure it is not buffered: @@ -310,10 +313,10 @@ def SubprocessCallAndFilter(command, if in_byte != '\r': if print_stdout: if not print_messages: - print('\n________ running \'%s\' in \'%s\'' + stdout.write('\n________ running \'%s\' in \'%s\'\n' % (' '.join(command), in_directory)) print_messages = True - sys.stdout.write(in_byte) + stdout.write(in_byte) if in_byte != '\n': in_line += in_byte if in_byte == '\n': @@ -325,7 +328,7 @@ def SubprocessCallAndFilter(command, # which can slow busy readers down. if (time.time() - last_flushed_at) > 10: last_flushed_at = time.time() - sys.stdout.flush() + stdout.flush() in_byte = kid.stdout.read(1) # Flush the rest of buffered output. This is only an issue with files not # ending with a \n. @@ -335,11 +338,9 @@ def SubprocessCallAndFilter(command, if rv: msg = 'failed to run command: %s' % ' '.join(command) - if fail_status != None: - print >> sys.stderr, msg + sys.stderr.write(msg + '\n') sys.exit(fail_status) - raise Error(msg) diff --git a/scm.py b/scm.py index 924f12641..09871778f 100644 --- a/scm.py +++ b/scm.py @@ -121,7 +121,8 @@ class GIT(object): in_directory, print_messages, print_stdout, - filter_fn): + filter_fn, + stdout=None): """Runs a command, optionally outputting to stdout. stdout is passed line-by-line to the given filter_fn function. If @@ -146,7 +147,8 @@ class GIT(object): in_directory, print_messages, print_stdout, - filter_fn=filter_fn) + filter_fn=filter_fn, + stdout=stdout) @staticmethod def GetEmail(repo_root): @@ -378,7 +380,7 @@ class SVN(object): stderr=stderr).communicate()[0] @staticmethod - def RunAndGetFileList(verbose, args, in_directory, file_list): + def RunAndGetFileList(verbose, args, in_directory, file_list, stdout=None): """Runs svn checkout, update, or status, output to stdout. The first item in args must be either "checkout", "update", or "status". @@ -436,7 +438,8 @@ class SVN(object): in_directory, verbose, True, - CaptureMatchingLines) + CaptureMatchingLines, + stdout=stdout) except gclient_utils.Error: def IsKnownFailure(): for x in failure: @@ -482,7 +485,8 @@ class SVN(object): in_directory, print_messages, print_stdout, - filter_fn): + filter_fn, + stdout=None): """Runs a command, optionally outputting to stdout. stdout is passed line-by-line to the given filter_fn function. If @@ -507,7 +511,8 @@ class SVN(object): in_directory, print_messages, print_stdout, - filter_fn=filter_fn) + filter_fn=filter_fn, + stdout=stdout) @staticmethod def CaptureInfo(relpath, in_directory=None, print_error=True): diff --git a/tests/gclient_utils_test.py b/tests/gclient_utils_test.py index 4c3b32faf..dcc0a3af9 100755 --- a/tests/gclient_utils_test.py +++ b/tests/gclient_utils_test.py @@ -88,7 +88,8 @@ class SubprocessCallAndFilterTestCase(GclientUtilBase): in_directory = 'bleh' env = gclient_utils.os.environ.copy() env['LANGUAGE'] = 'en' - print("\n________ running 'boo foo bar' in 'bleh'") + gclient_utils.sys.stdout.write( + '\n________ running \'boo foo bar\' in \'bleh\'\n') for i in test_string: gclient_utils.sys.stdout.write(i) gclient_utils.subprocess.Popen(