in src/core/src/bootstrap/EnvLayer.py [0:0]
def run_command_output(self, cmd, no_output, chk_err=True):
# type: (str, bool, bool) -> (int, any)
""" Wrapper for subprocess.check_output. Execute 'cmd'. Returns return code and STDOUT, trapping expected exceptions. Reports exceptions to Error if chk_err parameter is True """
def check_output(*popenargs, **kwargs):
""" Backport from subprocess module from python 2.7 """
if 'stdout' in kwargs:
raise ValueError('stdout argument not allowed, it will be overridden.')
no_output = False
if type(popenargs[0]) is bool:
no_output = popenargs[0]
popenargs = popenargs[1:]
if no_output is True:
out_file = None
else:
out_file = subprocess.PIPE
process = subprocess.Popen(stdout=out_file, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
raise subprocess.CalledProcessError(retcode, cmd, output=output)
return output
# noinspection PyShadowingNames,PyShadowingNames
class CalledProcessError(Exception):
"""Exception classes used by this module."""
def __init__(self, return_code, cmd, output=None):
self.return_code = return_code
self.cmd = cmd
self.output = output
def __str__(self):
return "Command '%s' returned non-zero exit status %d" \
% (self.cmd, self.return_code)
subprocess.check_output = check_output
subprocess.CalledProcessError = CalledProcessError
try:
output = subprocess.check_output(no_output, cmd, stderr=subprocess.STDOUT, shell=True)
except subprocess.CalledProcessError as e:
if chk_err:
print("Error: CalledProcessError. [Code={0}][Command={1}][Result={2}]".format(str(e.returncode), e.cmd, self.__convert_process_output_to_ascii(e.output[:-1])), file=sys.stdout)
if no_output:
return e.return_code, None
else:
return e.return_code, self.__convert_process_output_to_ascii(e.output)
except Exception as error:
raise "Exception during cmd execution. [Exception={0}][Cmd={1}]".format(repr(error), str(cmd))
if no_output:
return 0, None
else:
return 0, self.__convert_process_output_to_ascii(output)