run_subprocess.py 638 Bytes
Newer Older
jtchilders's avatar
jtchilders committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import subprocess,logging
logger = logging.getLogger(__name__)

class SubprocessFailed(Exception): pass
class SubprocessNonzeroReturnCode(Exception): pass
def run_subprocess(cmd,ignore_nonzero_return=False):
   try:
      logger.debug('run_subprocess: ' + cmd)
      p = subprocess.Popen(cmd.split(),stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
      p.wait()
      stdout,stderr = p.communicate()
      if p.returncode != 0 and not ignore_nonzero_return:
         raise SubprocessNonzeroReturnCode(stdout)
      return stdout
15
   except Exception as e:
jtchilders's avatar
jtchilders committed
16 17
      logger.exception('exception received')
      raise SubprocessFailed(str(e))