From db0ee6f1e452bb5c4434b9335d2bccb1ca35e986 Mon Sep 17 00:00:00 2001 From: "maruel@chromium.org" Date: Wed, 19 Oct 2011 19:57:56 +0000 Subject: [PATCH] Get rid of RunShell*() functions in gcl.py to finish the conversion to subprocess2 R=dpranke@chromium.org BUG= TEST= Review URL: http://codereview.chromium.org/8355007 git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@106358 0039d316-1c4b-4281-b951-d872f2087c98 --- gcl.py | 66 +++++++++++++++---------------------------- tests/gcl_unittest.py | 11 ++++---- 2 files changed, 29 insertions(+), 48 deletions(-) diff --git a/gcl.py b/gcl.py index 8df5de1bc..171017e12 100755 --- a/gcl.py +++ b/gcl.py @@ -38,7 +38,7 @@ from scm import SVN import subprocess2 from third_party import upload -__version__ = '1.2.1' +__version__ = '1.2.2' CODEREVIEW_SETTINGS = { @@ -69,6 +69,7 @@ DEFAULT_LINT_IGNORE_REGEX = r"$^" REVIEWERS_REGEX = r'\s*R=(.+)' + def CheckHomeForFile(filename): """Checks the users home dir for the existence of the given file. Returns the path to the file if it's there, or None if it is not. @@ -227,33 +228,6 @@ def ErrorExit(msg): sys.exit(1) -def RunShellWithReturnCode(command, print_output=False): - """Executes a command and returns the output and the return code.""" - p = subprocess2.Popen( - command, stdout=subprocess2.PIPE, - stderr=subprocess2.STDOUT, universal_newlines=True) - if print_output: - output_array = [] - while True: - line = p.stdout.readline() - if not line: - break - if print_output: - print line.strip('\n') - output_array.append(line) - output = "".join(output_array) - else: - output = p.stdout.read() - p.wait() - p.stdout.close() - return output, p.returncode - - -def RunShell(command, print_output=False): - """Executes a command and returns the output.""" - return RunShellWithReturnCode(command, print_output)[0] - - def FilterFlag(args, flag): """Returns True if the flag is present in args list. @@ -1014,19 +988,25 @@ def CMDcommit(change_info, args): handle, commit_filename = tempfile.mkstemp(text=True) os.write(handle, commit_message) os.close(handle) - - handle, targets_filename = tempfile.mkstemp(text=True) - os.write(handle, "\n".join(change_info.GetFileNames())) - os.close(handle) - - commit_cmd += ['--file=' + commit_filename] - commit_cmd += ['--targets=' + targets_filename] - # Change the current working directory before calling commit. - previous_cwd = os.getcwd() - os.chdir(change_info.GetLocalRoot()) - output = RunShell(commit_cmd, True) - os.remove(commit_filename) - os.remove(targets_filename) + try: + handle, targets_filename = tempfile.mkstemp(text=True) + os.write(handle, "\n".join(change_info.GetFileNames())) + os.close(handle) + try: + commit_cmd += ['--file=' + commit_filename] + commit_cmd += ['--targets=' + targets_filename] + # Change the current working directory before calling commit. + previous_cwd = os.getcwd() + os.chdir(change_info.GetLocalRoot()) + output = '' + try: + output = subprocess2.check_output(commit_cmd) + except subprocess2.CalledProcessError, e: + ErrorExit('Commit failed.\n%s' % e) + finally: + os.remove(commit_filename) + finally: + os.remove(targets_filename) if output.find("Committed revision") != -1: change_info.Delete() @@ -1304,7 +1284,7 @@ def CMDdiff(args): cmd = ['svn', 'diff'] cmd.extend([os.path.join(root, x) for x in files]) cmd.extend(args) - return RunShellWithReturnCode(cmd, print_output=True)[1] + return subprocess2.call(cmd) @no_args @@ -1387,7 +1367,7 @@ def CMDpassthru(args): root = GetRepositoryRoot() change_info = ChangeInfo.Load(args[1], root, True, True) args.extend([os.path.join(root, x) for x in change_info.GetFileNames()]) - return RunShellWithReturnCode(args, print_output=True)[1] + return subprocess2.call(args) def Command(name): diff --git a/tests/gcl_unittest.py b/tests/gcl_unittest.py index e013df2e2..5b3893478 100755 --- a/tests/gcl_unittest.py +++ b/tests/gcl_unittest.py @@ -20,7 +20,8 @@ class GclTestsBase(SuperMoxTestBase): def setUp(self): SuperMoxTestBase.setUp(self) self.fake_root_dir = self.RootDir() - self.mox.StubOutWithMock(gcl, 'RunShell') + self.mox.StubOutWithMock(gcl.subprocess2, 'call') + self.mox.StubOutWithMock(gcl.subprocess2, 'check_output') self.mox.StubOutWithMock(gcl.SVN, 'CaptureInfo') self.mox.StubOutWithMock(gcl.SVN, 'GetCheckoutRoot') self.mox.StubOutWithMock(gcl, 'tempfile') @@ -88,8 +89,7 @@ class GclUnittest(GclTestsBase): 'GetModifiedFiles', 'GetRepositoryRoot', 'ListFiles', 'LoadChangelistInfoForMultiple', 'MISSING_TEST_MSG', 'OptionallyDoPresubmitChecks', 'REPOSITORY_ROOT', 'REVIEWERS_REGEX', - 'RunShell', 'RunShellWithReturnCode', 'SVN', - 'TryChange', 'UnknownFiles', 'Warn', + 'SVN', 'TryChange', 'UnknownFiles', 'Warn', 'attrs', 'breakpad', 'defer_attributes', 'fix_encoding', 'gclient_utils', 'json', 'main', 'need_change', 'need_change_and_args', 'no_args', 'optparse', 'os', 'presubmit_support', 'random', 're', @@ -527,8 +527,9 @@ class CMDCommitUnittest(GclTestsBase): gcl.os.getcwd().AndReturn('prev') gcl.os.chdir(change_info.GetLocalRoot()) - gcl.RunShell(['svn', 'commit', '--file=commit', '--targets=files'], - True).AndReturn(shell_output) + gcl.subprocess2.check_output( + ['svn', 'commit', '--file=commit', '--targets=files'] + ).AndReturn(shell_output) if 'Committed' in shell_output: self.mox.StubOutWithMock(gcl, 'GetCodeReviewSetting') gcl.GetCodeReviewSetting('VIEW_VC').AndReturn('http://view/')