40 lines
1.0 KiB
Python
40 lines
1.0 KiB
Python
__all__ = ("Command",)
|
|
|
|
|
|
import logging
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
logger = logging.getLogger(">")
|
|
|
|
|
|
class Command:
|
|
_logger = logger
|
|
|
|
def __init__(self, executor: str, *args: str) -> None:
|
|
self._executor = executor
|
|
self._args = args
|
|
|
|
def run(self, basepath: Path):
|
|
args = [self._executor, *self._args]
|
|
self._logger.debug(" ".join(args))
|
|
result = subprocess.run(
|
|
args,
|
|
cwd=basepath,
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE,
|
|
)
|
|
self._logstd(result.stdout, logging.TRACE)
|
|
if result.returncode != 0:
|
|
self._logstd(result.stderr, logging.ERROR)
|
|
raise RuntimeError(result.stderr)
|
|
else:
|
|
self._logstd(result.stderr, logging.WARNING)
|
|
|
|
@classmethod
|
|
def _logstd(cls, data: bytes, level: int):
|
|
if data:
|
|
for line in data.decode("utf-8").strip().splitlines():
|
|
if line:
|
|
cls._logger.log(level, line)
|