1
0
mirror of https://github.com/ARM-software/devlib.git synced 2025-01-31 10:10:46 +00:00

utils/misc: add combined output option to check_output

Add an option to combine stderr and stdout into a single stream.
This commit is contained in:
Sergei Trofimov 2018-06-13 13:47:41 +01:00 committed by setrofim
parent 69cd3be96c
commit d4b0dedc2a

View File

@ -143,7 +143,8 @@ check_output_logger = logging.getLogger('check_output')
check_output_lock = threading.Lock() check_output_lock = threading.Lock()
def check_output(command, timeout=None, ignore=None, inputtext=None, **kwargs): def check_output(command, timeout=None, ignore=None, inputtext=None,
combined_output=False, **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 # pylint: disable=too-many-branches
@ -165,9 +166,10 @@ def check_output(command, timeout=None, ignore=None, inputtext=None, **kwargs):
pass # process may have already terminated. pass # process may have already terminated.
with check_output_lock: with check_output_lock:
stderr = subprocess.STDOUT if combined_output else subprocess.PIPE
process = subprocess.Popen(command, process = subprocess.Popen(command,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=stderr,
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
preexec_fn=preexec_function, preexec_fn=preexec_function,
**kwargs) **kwargs)
@ -181,7 +183,8 @@ def check_output(command, timeout=None, ignore=None, inputtext=None, **kwargs):
if sys.version_info[0] == 3: if sys.version_info[0] == 3:
# Currently errors=replace is needed as 0x8c throws an error # Currently errors=replace is needed as 0x8c throws an error
output = output.decode(sys.stdout.encoding, "replace") output = output.decode(sys.stdout.encoding, "replace")
error = error.decode(sys.stderr.encoding, "replace") if error:
error = error.decode(sys.stderr.encoding, "replace")
finally: finally:
if timeout: if timeout:
timer.cancel() timer.cancel()