Created
June 12, 2012 18:50
-
-
Save mccutchen/2919350 to your computer and use it in GitHub Desktop.
run_proc.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def run_proc(cmd, stdin=None, env=None): | |
"""Runs the given cmd as a subprocess, where cmd is a list suitable | |
for passing to subprocess.call. Returns a 3-tuple of | |
(exit code, stdout, stderr) | |
If stdin is not None, it will be passed into the subprocess on STDIN. If | |
env is not None, it will be used to augment the environment of the | |
subprocess. | |
""" | |
assert isinstance(cmd, (list, tuple)) | |
popen_args = dict(stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
if stdin is not None: | |
popen_args['stdin'] = subprocess.PIPE | |
if env is not None: | |
new_env = os.environ.copy() | |
new_env.update(env) | |
popen_args['env'] = new_env | |
proc = subprocess.Popen(cmd, **popen_args) | |
out, err = proc.communicate(input=stdin) | |
return proc.returncode, out.strip(), err.strip() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment