CommonAutoRearsh/engine/runner.py

31 lines
731 B
Python

from __future__ import annotations
import subprocess
import time
from pathlib import Path
from engine.models import RunResult
def run_command(command: str, cwd: Path, timeout_seconds: int) -> RunResult:
start = time.perf_counter()
completed = subprocess.run(
command,
cwd=str(cwd),
shell=True,
capture_output=True,
text=True,
encoding="utf-8",
timeout=timeout_seconds,
check=False,
)
runtime_seconds = time.perf_counter() - start
return RunResult(
command=command,
cwd=cwd,
exit_code=completed.returncode,
runtime_seconds=runtime_seconds,
stdout=completed.stdout,
stderr=completed.stderr,
)