def run_cmd(*args, **kwargs):
    preexec_fn = None

    if 'chdir' in kwargs:
        def preexec_fn():
            return os.chdir(kwargs['chdir'])

    try:
        proc = subprocess.Popen(args, stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.STDOUT, env=os.environ,
                                preexec_fn=preexec_fn)

        if 'confirm' in kwargs and kwargs['confirm']:
            proc.stdin.write('yes'.encode())
            proc.stdin.flush()

        out, err = proc.communicate()
        out = out.decode('utf-8')
    except Exception:
        raise Exception(
            "Exception while processing the command:\n%s.\n%s" %
            (' '.join(args), traceback.format_exc())
        )

    if proc.returncode != 0:
        raise Exception(
            "Error occurred while processing the command:\n%s.\n"
            "Stdout: %s\nStderr: %s" %
            (' '.join(args), out.strip(), err)
        )

    return out.strip()
Run command and check the return code.
dataset/ETHPy150Open openstack-infra/git-review/git_review/tests/utils.py/run_cmd