def RunCommand(self, command_name, args=None, headers=None, debug=0,
                 return_stdout=False, return_stderr=False,
                 return_log_handler=False, cwd=None):
    """Method for calling gslib.command_runner.CommandRunner.

    Passes parallel_operations=False for all tests, optionally saving/returning
    stdout output. We run all tests multi-threaded, to exercise those more
    complicated code paths.
    TODO: Change to run with parallel_operations=True for all tests. At
    present when you do this it causes many test failures.

    Args:
      command_name: The name of the command being run.
      args: Command-line args (arg0 = actual arg, not command name ala bash).
      headers: Dictionary containing optional HTTP headers to pass to boto.
      debug: Debug level to pass in to boto connection (range 0..3).
      return_stdout: If True, will save and return stdout produced by command.
      return_stderr: If True, will save and return stderr produced by command.
      return_log_handler: If True, will return a MockLoggingHandler instance
           that was attached to the command's logger while running.
      cwd: The working directory that should be switched to before running the
           command. The working directory will be reset back to its original
           value after running the command. If not specified, the working
           directory is left unchanged.

    Returns:
      One or a tuple of requested return values, depending on whether
      return_stdout, return_stderr, and/or return_log_handler were specified.
    """
    args = args or []

    command_line = ' '.join([command_name] + args)
    if self.is_debugging:
      self.stderr_save.write('\nRunCommand of %s\n' % command_line)

    # Save and truncate stdout and stderr for the lifetime of RunCommand. This
    # way, we can return just the stdout and stderr that was output during the
    # RunNamedCommand call below.
    sys.stdout.seek(0)
    sys.stderr.seek(0)
    stdout = sys.stdout.read()
    stderr = sys.stderr.read()
    if stdout:
      self.accumulated_stdout.append(stdout)
    if stderr:
      self.accumulated_stderr.append(stderr)
    sys.stdout.seek(0)
    sys.stderr.seek(0)
    sys.stdout.truncate()
    sys.stderr.truncate()

    mock_log_handler = MockLoggingHandler()
    logging.getLogger(command_name).addHandler(mock_log_handler)

    try:
      with WorkingDirectory(cwd):
        self.command_runner.RunNamedCommand(
            command_name, args=args, headers=headers, debug=debug,
            parallel_operations=False, do_shutdown=False)
    finally:
      sys.stdout.seek(0)
      stdout = sys.stdout.read()
      sys.stderr.seek(0)
      stderr = sys.stderr.read()
      logging.getLogger(command_name).removeHandler(mock_log_handler)
      mock_log_handler.close()

      log_output = '\n'.join(
          '%s:\n  ' % level + '\n  '.join(records)
          for level, records in mock_log_handler.messages.iteritems()
          if records)
      if self.is_debugging and log_output:
        self.stderr_save.write(
            '==== logging RunCommand %s %s ====\n' % (self.id(), command_line))
        self.stderr_save.write(log_output)
        self.stderr_save.write('\n==== end logging ====\n')
      if self.is_debugging and stdout:
        self.stderr_save.write(
            '==== stdout RunCommand %s %s ====\n' % (self.id(), command_line))
        self.stderr_save.write(stdout)
        self.stderr_save.write('==== end stdout ====\n')
      if self.is_debugging and stderr:
        self.stderr_save.write(
            '==== stderr RunCommand %s %s ====\n' % (self.id(), command_line))
        self.stderr_save.write(stderr)
        self.stderr_save.write('==== end stderr ====\n')

      # Reset stdout and stderr files, so that we won't print them out again
      # in tearDown if debugging is enabled.
      sys.stdout.seek(0)
      sys.stderr.seek(0)
      sys.stdout.truncate()
      sys.stderr.truncate()

    to_return = []
    if return_stdout:
      to_return.append(stdout)
    if return_stderr:
      to_return.append(stderr)
    if return_log_handler:
      to_return.append(mock_log_handler)
    if len(to_return) == 1:
      return to_return[0]
    return tuple(to_return)
dataset/ETHPy150Open GoogleCloudPlatform/gsutil/gslib/tests/testcase/unit_testcase.py/GsUtilUnitTestCase.RunCommand/original