Commit 559c3f89 authored by maruel@chromium.org's avatar maruel@chromium.org

Add stdout param to SubprocessCallAndFilter().

It will eventually replace 'print_messages' and be used to annotate printed
lines when using --jobs.

Review URL: http://codereview.chromium.org/3174020

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@57092 0039d316-1c4b-4281-b951-d872f2087c98
parent 3a292688
...@@ -1192,6 +1192,8 @@ def Main(argv): ...@@ -1192,6 +1192,8 @@ def Main(argv):
options.entries_filename = options.config_filename + '_entries' options.entries_filename = options.config_filename + '_entries'
if options.jobs < 1: if options.jobs < 1:
parser.error('--jobs must be 1 or higher') parser.error('--jobs must be 1 or higher')
# Useful for --jobs.
options.stdout = sys.stdout
# These hacks need to die. # These hacks need to die.
if not hasattr(options, 'revisions'): if not hasattr(options, 'revisions'):
......
...@@ -154,7 +154,8 @@ class GitWrapper(SCMWrapper): ...@@ -154,7 +154,8 @@ class GitWrapper(SCMWrapper):
merge_base = self._Run(['merge-base', 'HEAD', 'origin']) merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
command = ['diff', merge_base] command = ['diff', merge_base]
filterer = DiffFilterer(self.relpath) 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): def update(self, options, args, file_list):
"""Runs git to update or transparently checkout the working copy. """Runs git to update or transparently checkout the working copy.
...@@ -706,7 +707,8 @@ class SVNWrapper(SCMWrapper): ...@@ -706,7 +707,8 @@ class SVNWrapper(SCMWrapper):
command.extend(args) command.extend(args)
filterer = DiffFilterer(self.relpath) 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): def update(self, options, args, file_list):
"""Runs svn to update or transparently checkout the working copy. """Runs svn to update or transparently checkout the working copy.
......
...@@ -266,7 +266,9 @@ def SubprocessCallAndFilter(command, ...@@ -266,7 +266,9 @@ def SubprocessCallAndFilter(command,
in_directory, in_directory,
print_messages, print_messages,
print_stdout, print_stdout,
fail_status=None, filter_fn=None): fail_status=None,
filter_fn=None,
stdout=None):
"""Runs command, a list, in directory in_directory. """Runs command, a list, in directory in_directory.
If print_messages is true, a message indicating what is being done If print_messages is true, a message indicating what is being done
...@@ -287,9 +289,10 @@ def SubprocessCallAndFilter(command, ...@@ -287,9 +289,10 @@ def SubprocessCallAndFilter(command,
exit with an exit status of fail_status. If fail_status is None (the exit with an exit status of fail_status. If fail_status is None (the
default), gclient will raise an Error exception. default), gclient will raise an Error exception.
""" """
stdout = stdout or sys.stdout
logging.debug(command) logging.debug(command)
if print_messages: if print_messages:
print('\n________ running \'%s\' in \'%s\'' stdout.write('\n________ running \'%s\' in \'%s\'\n'
% (' '.join(command), in_directory)) % (' '.join(command), in_directory))
kid = Popen(command, bufsize=0, cwd=in_directory, kid = Popen(command, bufsize=0, cwd=in_directory,
...@@ -298,7 +301,7 @@ def SubprocessCallAndFilter(command, ...@@ -298,7 +301,7 @@ def SubprocessCallAndFilter(command,
# Do a flush of sys.stdout before we begin reading from the subprocess's # Do a flush of sys.stdout before we begin reading from the subprocess's
# stdout. # stdout.
last_flushed_at = time.time() last_flushed_at = time.time()
sys.stdout.flush() stdout.flush()
# Also, we need to forward stdout to prevent weird re-ordering of output. # 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: # This has to be done on a per byte basis to make sure it is not buffered:
...@@ -310,10 +313,10 @@ def SubprocessCallAndFilter(command, ...@@ -310,10 +313,10 @@ def SubprocessCallAndFilter(command,
if in_byte != '\r': if in_byte != '\r':
if print_stdout: if print_stdout:
if not print_messages: if not print_messages:
print('\n________ running \'%s\' in \'%s\'' stdout.write('\n________ running \'%s\' in \'%s\'\n'
% (' '.join(command), in_directory)) % (' '.join(command), in_directory))
print_messages = True print_messages = True
sys.stdout.write(in_byte) stdout.write(in_byte)
if in_byte != '\n': if in_byte != '\n':
in_line += in_byte in_line += in_byte
if in_byte == '\n': if in_byte == '\n':
...@@ -325,7 +328,7 @@ def SubprocessCallAndFilter(command, ...@@ -325,7 +328,7 @@ def SubprocessCallAndFilter(command,
# which can slow busy readers down. # which can slow busy readers down.
if (time.time() - last_flushed_at) > 10: if (time.time() - last_flushed_at) > 10:
last_flushed_at = time.time() last_flushed_at = time.time()
sys.stdout.flush() stdout.flush()
in_byte = kid.stdout.read(1) in_byte = kid.stdout.read(1)
# Flush the rest of buffered output. This is only an issue with files not # Flush the rest of buffered output. This is only an issue with files not
# ending with a \n. # ending with a \n.
...@@ -335,11 +338,9 @@ def SubprocessCallAndFilter(command, ...@@ -335,11 +338,9 @@ def SubprocessCallAndFilter(command,
if rv: if rv:
msg = 'failed to run command: %s' % ' '.join(command) msg = 'failed to run command: %s' % ' '.join(command)
if fail_status != None: if fail_status != None:
print >> sys.stderr, msg sys.stderr.write(msg + '\n')
sys.exit(fail_status) sys.exit(fail_status)
raise Error(msg) raise Error(msg)
......
...@@ -121,7 +121,8 @@ class GIT(object): ...@@ -121,7 +121,8 @@ class GIT(object):
in_directory, in_directory,
print_messages, print_messages,
print_stdout, print_stdout,
filter_fn): filter_fn,
stdout=None):
"""Runs a command, optionally outputting to stdout. """Runs a command, optionally outputting to stdout.
stdout is passed line-by-line to the given filter_fn function. If stdout is passed line-by-line to the given filter_fn function. If
...@@ -146,7 +147,8 @@ class GIT(object): ...@@ -146,7 +147,8 @@ class GIT(object):
in_directory, in_directory,
print_messages, print_messages,
print_stdout, print_stdout,
filter_fn=filter_fn) filter_fn=filter_fn,
stdout=stdout)
@staticmethod @staticmethod
def GetEmail(repo_root): def GetEmail(repo_root):
...@@ -378,7 +380,7 @@ class SVN(object): ...@@ -378,7 +380,7 @@ class SVN(object):
stderr=stderr).communicate()[0] stderr=stderr).communicate()[0]
@staticmethod @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. """Runs svn checkout, update, or status, output to stdout.
The first item in args must be either "checkout", "update", or "status". The first item in args must be either "checkout", "update", or "status".
...@@ -436,7 +438,8 @@ class SVN(object): ...@@ -436,7 +438,8 @@ class SVN(object):
in_directory, in_directory,
verbose, verbose,
True, True,
CaptureMatchingLines) CaptureMatchingLines,
stdout=stdout)
except gclient_utils.Error: except gclient_utils.Error:
def IsKnownFailure(): def IsKnownFailure():
for x in failure: for x in failure:
...@@ -482,7 +485,8 @@ class SVN(object): ...@@ -482,7 +485,8 @@ class SVN(object):
in_directory, in_directory,
print_messages, print_messages,
print_stdout, print_stdout,
filter_fn): filter_fn,
stdout=None):
"""Runs a command, optionally outputting to stdout. """Runs a command, optionally outputting to stdout.
stdout is passed line-by-line to the given filter_fn function. If stdout is passed line-by-line to the given filter_fn function. If
...@@ -507,7 +511,8 @@ class SVN(object): ...@@ -507,7 +511,8 @@ class SVN(object):
in_directory, in_directory,
print_messages, print_messages,
print_stdout, print_stdout,
filter_fn=filter_fn) filter_fn=filter_fn,
stdout=stdout)
@staticmethod @staticmethod
def CaptureInfo(relpath, in_directory=None, print_error=True): def CaptureInfo(relpath, in_directory=None, print_error=True):
......
...@@ -88,7 +88,8 @@ class SubprocessCallAndFilterTestCase(GclientUtilBase): ...@@ -88,7 +88,8 @@ class SubprocessCallAndFilterTestCase(GclientUtilBase):
in_directory = 'bleh' in_directory = 'bleh'
env = gclient_utils.os.environ.copy() env = gclient_utils.os.environ.copy()
env['LANGUAGE'] = 'en' 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: for i in test_string:
gclient_utils.sys.stdout.write(i) gclient_utils.sys.stdout.write(i)
gclient_utils.subprocess.Popen( gclient_utils.subprocess.Popen(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment