in src/tools/diagnostics/updatecenter_troubleshooter.py [0:0]
def run_command_output(self, cmd, no_output, chk_err=True):
"""
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(no_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.')
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
class CalledProcessError(Exception):
"""Exception classes used by this module."""
def __init__(self, returncode, cmd, output=None):
self.returncode = returncode
self.cmd = cmd
self.output = output
def __str__(self):
return "Command '%s' returned non-zero exit status %d" \
% (self.cmd, self.returncode)
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. Error Code is: " + str(e.returncode), file=sys.stdout)
print("Error: CalledProcessError. Command string was: " + e.cmd, file=sys.stdout)
print("Error: CalledProcessError. Command result was: " +
self.get_subprocess_output_as_asciistring((e.output[:-1])), file=sys.stdout)
if no_output:
return e.returncode, None
else:
return e.returncode, self.get_subprocess_output_as_asciistring(e.output)
if no_output:
return 0, None
else:
return 0, self.get_subprocess_output_as_asciistring(output)