RASCSI/python/common/src/util/run.py
nucleogenic 315ef9f248
Auto-format Python sources with black, fix all issues reported by flake8 (#1010)
* Update config for black and flake8
* Auto-format Python sources with black
* Fix issues reported by flake8
* Exclude protobuf files from black
* Address formatting feedback
2022-11-30 05:19:17 +00:00

42 lines
919 B
Python

"""
Utility module for running system commands with basic logging
"""
import asyncio
import logging
def run(program, args=None):
"""Run a command and return its output"""
return asyncio.run(run_async(program, args))
async def run_async(program, args=None):
"""Run a command in the background"""
proc = await asyncio.create_subprocess_exec(
program, *args, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await proc.communicate()
logging.info(
'Executed command "%s %s" with status code %d',
program,
" ".join(args),
proc.returncode,
)
if stdout:
stdout = stdout.decode()
logging.debug(stdout)
if stderr:
stderr = stderr.decode()
logging.warning(stderr)
return {
"returncode": proc.returncode,
"stdout": stdout,
"stderr": stderr,
}