2024-07-28 02:59:40 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
"""common."""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
from subprocess import PIPE, Popen
|
|
|
|
|
|
|
|
|
|
|
|
def configure_logger(level: str = "INFO") -> None:
|
|
|
|
"""Configure the logger.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
level (str, optional): The logging level. Defaults to "INFO".
|
|
|
|
"""
|
|
|
|
logging.basicConfig(
|
|
|
|
level=level,
|
|
|
|
datefmt="%Y-%m-%dT%H:%M:%S%z",
|
|
|
|
format="%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s",
|
|
|
|
handlers=[logging.StreamHandler(sys.stdout)],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-07-28 14:15:06 -04:00
|
|
|
def bash_wrapper(command: str, path: str = ".") -> tuple[str, int]:
|
2024-07-28 02:59:40 -04:00
|
|
|
"""Execute a bash command and capture the output.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
command (str): The bash command to be executed.
|
|
|
|
path (str): The current working directory, '.' by default
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Tuple[str, int]: A tuple containing the output of the command (stdout) as a string,
|
|
|
|
the error output (stderr) as a string (optional), and the return code as an integer.
|
|
|
|
"""
|
|
|
|
# This is a acceptable risk
|
2024-07-28 14:15:06 -04:00
|
|
|
process = Popen(command.split(), stdout=PIPE, stderr=PIPE, cwd=path) # noqa: S603
|
2024-07-28 02:59:40 -04:00
|
|
|
output, _ = process.communicate()
|
|
|
|
|
|
|
|
return output.decode(), process.returncode
|