Skip to content

Instantly share code, notes, and snippets.

Last active February 6, 2018 05:53
What would you like to do?
# coding: utf-8
import os
import sys
import shlex
import logging
import subprocess
PY3 = sys.version_info >= (3,)
lg = logging.getLogger('cmd')
result_lg = logging.getLogger('cmd')
def run_cmd(cmd, shlex_reformat=False, shell=False, **kwargs):
if shlex_reformat and shell:
raise ValueError('shlex_reformat and shell are mutually exclusive')
if shell:
if not isinstance(cmd, str):
raise ValueError('cmd must be str when shell=True')
kwargs['shell'] = shell
# reformat cmd
if shlex_reformat:
if isinstance(cmd, list):
cmd_str = ' '.join(cmd)
cmd_str = cmd
cmd = shlex.split(cmd_str)
lg.debug('cmd: %s, %s', cmd, kwargs)
extra_env = kwargs.pop('env', {})
if extra_env:
env = os.environ.copy()
kwargs['env'] = env
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)
out, err = p.communicate()
if PY3:
out, err = out.decode(), err.decode()
result_lg.debug('cmd=%s returncode=%s out=%s err=%s', cmd, p.returncode, out, err)
return p.returncode, out, err
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment