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):
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'):
......
......@@ -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.
......
......@@ -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)
......
......@@ -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):
......
......@@ -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(
......
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