def execute(cmd,
            process_input=None,
            check_exit_code=True,
            cwd=None,
            shell=False,
            env_overrides=None,
            stdout_fh=subprocess.PIPE,
            stderr_fh=subprocess.PIPE):
    """Helper method to execute a command through subprocess.

    :param cmd:             Command passed to subprocess.Popen.
    :param process_input:   Input send to opened process.
    :param check_exit_code: Specifies whether to check process return code.
                            If return code is other then `0` - exception will
                            be raised.
    :param cwd:             The child's current directory will be changed to
                            `cwd` before it is executed.
    :param shell:           Specifies whether to use the shell as the program
                            to execute.
    :param env_overrides:   Process environment parameters to override.
    :param stdout_fh:       Stdout file handler.
    :param stderr_fh:       Stderr file handler.
    :returns:               A tuple, (stdout, stderr) from the spawned process.
    :raises:                :class:`exceptions.ProcessExecutionError` when
                            process ends with other then `0` return code.
    """
    # Ensure all string args (i.e. for those that send ints, etc.).
    cmd = map(str, cmd)

    # NOTE(skudriashev): If shell is True, it is recommended to pass args as a
    # string rather than as a sequence.
    str_cmd = subprocess.list2cmdline(cmd)
    if shell:
        cmd = str_cmd
        LOG.debug('Running shell cmd: %r' % cmd)
    else:
        LOG.debug('Running cmd: %r' % cmd)

    if process_input is not None:
        process_input = str(process_input)
        LOG.debug('Process input: %s' % process_input)

    if cwd:
        LOG.debug('Process working directory: %r' % cwd)

    # Override process environment in needed.
    process_env = None
    if env_overrides and len(env_overrides):
        process_env = env.get()
        for k, v in env_overrides.items():
            LOG.debug("Using environment override '%s' => '%s'", k, v)
            process_env[k] = str(v)

    # Run command process.
    exec_kwargs = {
        'stdin': subprocess.PIPE,
        'stdout': stdout_fh,
        'stderr': stderr_fh,
        'close_fds': True,
        'shell': shell,
        'cwd': cwd,
        'env': process_env,
    }
    result = ("", "")
    try:
        obj = subprocess.Popen(cmd, **exec_kwargs)
        result = obj.communicate(process_input)
    except __HOLE__ as e:
        raise excp.ProcessExecutionError(
            str_cmd,
            exec_kwargs=exec_kwargs,
            description="%s: [%s, %s]" % (e, e.errno, e.strerror)
        )
    else:
        rc = obj.returncode

    # Handle process exit code.
    stdout = result[0] or ""
    stderr = result[1] or ""
    if rc != 0 and check_exit_code:
        # Raise exception if return code is not `0`.
        e = excp.ProcessExecutionError(str_cmd,
                                       exec_kwargs=exec_kwargs,
                                       stdout=stdout,
                                       stderr=stderr,
                                       exit_code=rc,
                                       where_output="debug log")
        LOG.debug("Stdout: %s", e.stdout)
        LOG.debug("Stderr: %s", e.stderr)
        raise e
    return stdout, stderr
dataset/ETHPy150Open openstack/anvil/anvil/shell.py/execute