migrate cros_build_lib.run APIs

The RunCommand APIs have been renamed, so migrate to the new ones.

BUG=chromium:1006587
TEST=unittests passes

Change-Id: Iaaada46aad600b7e58891388865f6ea96db34b18
Reviewed-on: https://chromium-review.googlesource.com/1897284
Tested-by: Mike Frysinger <vapier@chromium.org>
Commit-Ready: Mike Frysinger <vapier@chromium.org>
Legacy-Commit-Queue: Commit Bot <commit-bot@chromium.org>
Reviewed-by: Michael Mortensen <mmortensen@google.com>
diff --git a/clang-format.py b/clang-format.py
index 1b1d365..4106d4e 100755
--- a/clang-format.py
+++ b/clang-format.py
@@ -62,7 +62,7 @@
     cmd = [os.path.join(constants.DEPOT_TOOLS_DIR,
                         'download_from_google_storage.py'), '-b',
            'chromium-clang-format', '-s', hash_file_path]
-    cros_build_lib.RunCommand(cmd=cmd, print_cmd=False)
+    cros_build_lib.run(cmd, print_cmd=False)
   return clang_format_path
 
 
@@ -112,17 +112,14 @@
 
   # Fail gracefully if clang-format itself aborts/fails.
   try:
-    result = cros_build_lib.RunCommand(cmd=cmd,
-                                       print_cmd=False,
-                                       redirect_stdout=True,
-                                       encoding='utf-8',
-                                       errors='replace')
+    result = cros_build_lib.run(cmd, print_cmd=False, stdout=True,
+                                encoding='utf-8', errors='replace')
   except cros_build_lib.RunCommandError as e:
     print('clang-format failed:\n' + str(e), file=sys.stderr)
     print('\nPlease report this to the clang team.', file=sys.stderr)
     return 1
 
-  stdout = result.output
+  stdout = result.stdout
   if stdout.rstrip('\n') == 'no modified files to format':
     # This is always printed when only files that clang-format does not
     # understand were modified.
@@ -135,9 +132,7 @@
 
   if diff_filenames:
     if opts.fix:
-      cros_build_lib.RunCommand(cmd=['git', 'apply'],
-                                print_cmd=False,
-                                input=stdout)
+      cros_build_lib.run(['git', 'apply'], print_cmd=False, input=stdout)
     else:
       print('The following files have formatting errors:')
       for filename in diff_filenames:
diff --git a/pre-upload.py b/pre-upload.py
index 34dfcaf..82825db 100755
--- a/pre-upload.py
+++ b/pre-upload.py
@@ -138,19 +138,21 @@
 def _run_command(cmd, **kwargs):
   """Executes the passed in command and returns raw stdout output.
 
-  This is a convenience func to set some RunCommand defaults differently.
+  This is a convenience func to set some run defaults differently.
 
   Args:
     cmd: The command to run; should be a list of strings.
-    **kwargs: Same as cros_build_lib.RunCommand.
+    **kwargs: Same as cros_build_lib.run.
 
   Returns:
     The stdout from the process (discards stderr and returncode).
   """
   kwargs.setdefault('print_cmd', False)
-  kwargs.setdefault('redirect_stdout', True)
-  kwargs.setdefault('error_code_ok', True)
+  kwargs.setdefault('stdout', True)
+  kwargs.setdefault('check', False)
   result = cros_build_lib.RunCommand(cmd, **kwargs)
+  # NB: We decode this directly rather than through kwargs as our tests rely
+  # on this post-processing behavior currently.
   return result.output.decode('utf-8', 'replace')
 
 
@@ -1422,16 +1424,16 @@
   else:
     options.extend(['--commit', commit])
   cmd = [os.path.join(hooks_dir, 'clang-format.py')] + options
-  cmd_result = cros_build_lib.RunCommand(cmd=cmd,
-                                         print_cmd=False,
-                                         redirect_stdout=True,
-                                         encoding='utf-8',
-                                         errors='replace',
-                                         combine_stdout_stderr=True,
-                                         error_code_ok=True)
+  cmd_result = cros_build_lib.run(cmd,
+                                  print_cmd=False,
+                                  stdout=True,
+                                  encoding='utf-8',
+                                  errors='replace',
+                                  combine_stdout_stderr=True,
+                                  check=False)
   if cmd_result.returncode:
     return HookFailure('clang-format.py errors/warnings\n\n' +
-                       cmd_result.output)
+                       cmd_result.stdout)
   return None
 
 
@@ -1452,12 +1454,12 @@
   # we always do, so disable the check globally.
   options.append('--ignore=GERRIT_CHANGE_ID')
   cmd = [os.path.join(hooks_dir, 'checkpatch.pl')] + options + ['-']
-  cmd_result = cros_build_lib.RunCommand(
-      cmd=cmd, print_cmd=False, input=_get_patch(commit).encode('utf-8'),
-      redirect_stdout=True, combine_stdout_stderr=True, error_code_ok=True,
-      encoding='utf-8', errors='replace')
+  cmd_result = cros_build_lib.run(
+      cmd, print_cmd=False, input=_get_patch(commit).encode('utf-8'),
+      stdout=True, combine_stdout_stderr=True, check=False, encoding='utf-8',
+      errors='replace')
   if cmd_result.returncode:
-    return HookFailure('checkpatch.pl errors/warnings\n\n' + cmd_result.output)
+    return HookFailure('checkpatch.pl errors/warnings\n\n' + cmd_result.stdout)
   return None
 
 
@@ -1469,7 +1471,7 @@
   if files:
     hooks_dir = _get_hooks_dir()
     cmd = [os.path.join(hooks_dir, 'kernel-doc'), '-none'] + files
-    output = _run_command(cmd=cmd, combine_stdout_stderr=True)
+    output = _run_command(cmd, combine_stdout_stderr=True)
     if output:
       return HookFailure('kernel-doc errors/warnings:',
                          items=output.splitlines())
@@ -1618,18 +1620,18 @@
   files = _get_affected_files(commit)
   env['PRESUBMIT_FILES'] = '\n'.join(files)
 
-  cmd_result = cros_build_lib.RunCommand(cmd=script,
-                                         env=env,
-                                         shell=True,
-                                         print_cmd=False,
-                                         input=os.devnull,
-                                         redirect_stdout=True,
-                                         combine_stdout_stderr=True,
-                                         encoding='utf-8',
-                                         errors='replace',
-                                         error_code_ok=True)
+  cmd_result = cros_build_lib.run(cmd=script,
+                                  env=env,
+                                  shell=True,
+                                  print_cmd=False,
+                                  input=os.devnull,
+                                  stdout=True,
+                                  encoding='utf-8',
+                                  errors='replace',
+                                  combine_stdout_stderr=True,
+                                  check=False)
   if cmd_result.returncode:
-    stdout = cmd_result.output
+    stdout = cmd_result.stdout
     if stdout:
       stdout = re.sub('(?m)^', '  ', stdout)
     return HookFailure('Hook script "%s" failed with code %d%s' %
@@ -2075,7 +2077,7 @@
     a blank string upon failure.
   """
   return _run_command(['repo', 'forall', '.', '-c', 'echo ${REPO_PROJECT}'],
-                      redirect_stderr=True, cwd=path).strip()
+                      stderr=True, cwd=path).strip()
 
 
 def direct_main(argv):
@@ -2151,7 +2153,7 @@
   # project from CWD
   if opts.dir is None:
     git_dir = _run_command(['git', 'rev-parse', '--git-dir'],
-                           redirect_stderr=True).strip()
+                           stderr=True).strip()
     if not git_dir:
       raise BadInvocation('The current directory is not part of a git project.')
     opts.dir = os.path.dirname(os.path.abspath(git_dir))
diff --git a/pre-upload_unittest.py b/pre-upload_unittest.py
index 26459f2..e6a3151 100755
--- a/pre-upload_unittest.py
+++ b/pre-upload_unittest.py
@@ -51,7 +51,7 @@
   """Verify we sanely handle unicode content."""
 
   def setUp(self):
-    self.rc_mock = self.PatchObject(cros_build_lib, 'RunCommand')
+    self.rc_mock = self.PatchObject(cros_build_lib, 'run')
 
   def _run(self, content):
     """Helper for round tripping through _run_command."""