mirror of
https://github.com/ARM-software/workload-automation.git
synced 2025-02-22 04:49:00 +00:00
Adding ignore parameter to check_output
Adding a parater to wlauto.utils.misc.check_output to specify that it should ignore certain error codes when they are returned by the subprocess and not raise them as errors.
This commit is contained in:
parent
399c9f82c3
commit
5035fe6f44
@ -73,9 +73,19 @@ class TimeoutError(Exception):
|
|||||||
return '\n'.join([self.message, 'OUTPUT:', self.output or ''])
|
return '\n'.join([self.message, 'OUTPUT:', self.output or ''])
|
||||||
|
|
||||||
|
|
||||||
def check_output(command, timeout=None, **kwargs):
|
def check_output(command, timeout=None, ignore=None, **kwargs):
|
||||||
"""This is a version of subprocess.check_output that adds a timeout parameter to kill
|
"""This is a version of subprocess.check_output that adds a timeout parameter to kill
|
||||||
the subprocess if it does not return within the specified time."""
|
the subprocess if it does not return within the specified time."""
|
||||||
|
# pylint: disable=too-many-branches
|
||||||
|
if ignore is None:
|
||||||
|
ignore = []
|
||||||
|
elif isinstance(ignore, int):
|
||||||
|
ignore = [ignore]
|
||||||
|
elif not isinstance(ignore, list):
|
||||||
|
message = 'Invalid value for ignore parameter: "{}"; must be an int or a list'
|
||||||
|
raise ValueError(message.format(ignore))
|
||||||
|
if 'stdout' in kwargs:
|
||||||
|
raise ValueError('stdout argument not allowed, it will be overridden.')
|
||||||
|
|
||||||
def callback(pid):
|
def callback(pid):
|
||||||
try:
|
try:
|
||||||
@ -84,8 +94,6 @@ def check_output(command, timeout=None, **kwargs):
|
|||||||
except OSError:
|
except OSError:
|
||||||
pass # process may have already terminated.
|
pass # process may have already terminated.
|
||||||
|
|
||||||
if 'stdout' in kwargs:
|
|
||||||
raise ValueError('stdout argument not allowed, it will be overridden.')
|
|
||||||
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
||||||
preexec_fn=preexec_function, **kwargs)
|
preexec_fn=preexec_function, **kwargs)
|
||||||
|
|
||||||
@ -103,7 +111,7 @@ def check_output(command, timeout=None, **kwargs):
|
|||||||
if retcode:
|
if retcode:
|
||||||
if retcode == -9: # killed, assume due to timeout callback
|
if retcode == -9: # killed, assume due to timeout callback
|
||||||
raise TimeoutError(command, output='\n'.join([output, error]))
|
raise TimeoutError(command, output='\n'.join([output, error]))
|
||||||
else:
|
elif retcode not in ignore:
|
||||||
raise subprocess.CalledProcessError(retcode, command, output='\n'.join([output, error]))
|
raise subprocess.CalledProcessError(retcode, command, output='\n'.join([output, error]))
|
||||||
return output, error
|
return output, error
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user