2021-01-28 13:27:07 +03:00
|
|
|
#!/usr/bin/env python3
|
2021-03-26 17:54:28 +03:00
|
|
|
|
2022-01-19 01:07:49 +03:00
|
|
|
# Copyright (C) 2018-2022 Intel Corporation
|
2021-01-28 13:27:07 +03:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
|
|
""" Common utilities for working with processes.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
import subprocess
|
2021-02-11 09:59:37 +03:00
|
|
|
|
|
|
|
|
|
2021-10-13 14:25:44 +03:00
|
|
|
def cmd_exec(args, timeout=None, env=None, log=None, verbose=True, shell=False):
|
2021-01-28 13:27:07 +03:00
|
|
|
""" Run cmd using subprocess with logging and other improvements
|
|
|
|
|
"""
|
|
|
|
|
if log is None:
|
|
|
|
|
log = logging.getLogger()
|
|
|
|
|
log_out = log.info if verbose else log.debug
|
|
|
|
|
|
|
|
|
|
log_out( # pylint: disable=logging-fstring-interpolation
|
|
|
|
|
f'========== cmd: {" ".join(args)}'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
proc = subprocess.Popen(
|
|
|
|
|
args,
|
2021-02-11 09:59:37 +03:00
|
|
|
env=env,
|
2021-01-28 13:27:07 +03:00
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
|
encoding="utf-8",
|
|
|
|
|
universal_newlines=True,
|
2021-05-26 11:43:59 +03:00
|
|
|
shell=shell,
|
2021-01-28 13:27:07 +03:00
|
|
|
)
|
|
|
|
|
output = []
|
|
|
|
|
for line in iter(proc.stdout.readline, ""):
|
|
|
|
|
log_out(line.strip("\n"))
|
|
|
|
|
output.append(line)
|
|
|
|
|
if line or proc.poll() is None:
|
|
|
|
|
continue
|
|
|
|
|
break
|
2021-10-13 14:25:44 +03:00
|
|
|
outs = proc.communicate(timeout=timeout)[0]
|
2021-01-28 13:27:07 +03:00
|
|
|
|
|
|
|
|
if outs:
|
|
|
|
|
log_out(outs.strip("\n"))
|
|
|
|
|
output.append(outs)
|
|
|
|
|
log_out("========== Completed. Exit code: %d", proc.returncode)
|
|
|
|
|
return proc.returncode, "".join(output)
|