2025-12-06 17:47:46 +01:00
|
|
|
# pkgmgr/run_command.py
|
2025-03-06 11:10:11 +01:00
|
|
|
import subprocess
|
2025-12-06 17:47:46 +01:00
|
|
|
import sys
|
|
|
|
|
from typing import List, Optional, Union
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CommandType = Union[str, List[str]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run_command(
|
|
|
|
|
cmd: CommandType,
|
|
|
|
|
cwd: Optional[str] = None,
|
|
|
|
|
preview: bool = False,
|
|
|
|
|
allow_failure: bool = False,
|
|
|
|
|
) -> subprocess.CompletedProcess:
|
|
|
|
|
"""
|
|
|
|
|
Run a command and optionally exit on error.
|
2025-03-06 11:10:11 +01:00
|
|
|
|
2025-12-06 17:47:46 +01:00
|
|
|
- If `cmd` is a string, it is executed with `shell=True`.
|
|
|
|
|
- If `cmd` is a list of strings, it is executed without a shell.
|
2025-03-06 11:10:11 +01:00
|
|
|
"""
|
2025-12-06 17:47:46 +01:00
|
|
|
if isinstance(cmd, str):
|
|
|
|
|
display = cmd
|
|
|
|
|
else:
|
|
|
|
|
display = " ".join(cmd)
|
|
|
|
|
|
|
|
|
|
where = cwd or "."
|
|
|
|
|
|
2025-03-06 11:10:11 +01:00
|
|
|
if preview:
|
2025-12-06 17:47:46 +01:00
|
|
|
print(f"[Preview] In '{where}': {display}")
|
|
|
|
|
# Fake a successful result; most callers ignore the return value anyway
|
|
|
|
|
return subprocess.CompletedProcess(cmd, 0) # type: ignore[arg-type]
|
|
|
|
|
|
|
|
|
|
print(f"Running in '{where}': {display}")
|
|
|
|
|
|
|
|
|
|
if isinstance(cmd, str):
|
|
|
|
|
result = subprocess.run(cmd, cwd=cwd, shell=True)
|
2025-03-06 11:10:11 +01:00
|
|
|
else:
|
2025-12-06 17:47:46 +01:00
|
|
|
result = subprocess.run(cmd, cwd=cwd)
|
|
|
|
|
|
|
|
|
|
if result.returncode != 0 and not allow_failure:
|
|
|
|
|
print(f"Command failed with exit code {result.returncode}. Exiting.")
|
|
|
|
|
sys.exit(result.returncode)
|
|
|
|
|
|
|
|
|
|
return result
|