mirror of
https://github.com/akuker/RASCSI.git
synced 2024-06-07 13:53:33 +00:00
315ef9f248
* 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
42 lines
919 B
Python
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,
|
|
}
|