2020-03-01 14:36:12 +01:00
|
|
|
import os
|
2020-02-08 13:39:35 +01:00
|
|
|
import shlex
|
|
|
|
import subprocess
|
|
|
|
import logging
|
|
|
|
|
2020-04-30 20:34:34 +02:00
|
|
|
def execute(cmd, wait=True, ignore_errors=False, include_stderr=False, env=None):
|
2020-02-08 13:39:35 +01:00
|
|
|
args = shlex.split(cmd)
|
|
|
|
logging.debug(cmd)
|
|
|
|
try:
|
2020-04-30 20:34:34 +02:00
|
|
|
proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT if include_stderr else subprocess.PIPE, env=env)
|
2020-02-08 13:39:35 +01:00
|
|
|
except FileNotFoundError as e:
|
2020-03-30 21:09:09 +02:00
|
|
|
raise RuntimeError('{} not found'.format(cmd))
|
2020-02-08 13:39:35 +01:00
|
|
|
|
|
|
|
if wait:
|
|
|
|
out, _ = proc.communicate()
|
2020-03-30 21:09:09 +02:00
|
|
|
if proc.returncode != 0:
|
|
|
|
err = '{} exited with code {}'.format(cmd, proc.returncode)
|
|
|
|
if ignore_errors:
|
|
|
|
return err
|
|
|
|
raise RuntimeError(err)
|
2020-02-08 13:39:35 +01:00
|
|
|
return out.decode('utf-8')
|
2020-03-01 14:36:12 +01:00
|
|
|
return ''
|
2020-02-08 13:39:35 +01:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|