def run(command, logger, cwd=None, env=None, shell=False, return_stdout=False,
        inherit_env=True):
  """
  Run a subprocess with the specified command. The command and output of is
  logged to logger. The command will automatically be converted to a string
  or list of command arguments based on the *shell* parameter.

  # Parameters
  command (str, list): A command-string or list of command arguments.
  logger (logging.Logger): A logger that will receive the command output.
  cwd (str, None): The current working directory.
  env (dict, None): The environment for the subprocess.
  shell (bool): If set to #True, execute the command via the system shell.
  return_stdout (bool): Return the output of the command (including stderr)
      to the caller. The result will be a tuple of (returncode, output).
  inherit_env (bool): Inherit the current process' environment.

  # Return
  int, tuple of (int, str): The return code, or the returncode and the
      output of the command.
  """

  if shell:
    if not isinstance(command, str):
      command = ' '.join(quote(x) for x in command)
    if logger:
      logger.info('$ ' + command)
  else:
    if isinstance(command, str):
      command = shlex.split(command)
    if logger:
      logger.info('$ ' + ' '.join(map(quote, command)))

  if env is None:
    env = {}
  if inherit_env:
    env = {**os.environ, **env}

  popen = subprocess.Popen(
    command, cwd=cwd, env=env, shell=shell, stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT, stdin=None)
  stdout = popen.communicate()[0].decode()
  if stdout:
    if popen.returncode != 0 and logger:
      logger.error('\n' + stdout)
    else:
      if logger:
        logger.info('\n' + stdout)
  if return_stdout:
    return popen.returncode, stdout
  return popen.returncode