new changes
This commit is contained in:
@@ -9,6 +9,24 @@ from typing import Tuple, Optional
|
||||
|
||||
regex_safe_path = re.compile(r"^[\w\s./\-]*$")
|
||||
|
||||
# systemd often sets PATH to venv-only; subprocess shells then miss /usr/bin (dnf, apt-get, …).
|
||||
_SYSTEM_PATH = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
|
||||
|
||||
|
||||
def ensure_system_path(env: dict[str, str]) -> None:
|
||||
"""Append standard locations to PATH if /usr/bin is missing."""
|
||||
cur = (env.get("PATH") or "").strip()
|
||||
if "/usr/bin" in cur:
|
||||
return
|
||||
env["PATH"] = f"{cur}:{_SYSTEM_PATH}" if cur else _SYSTEM_PATH
|
||||
|
||||
|
||||
def environment_with_system_path(base: Optional[dict[str, str]] = None) -> dict[str, str]:
|
||||
"""Copy of process env (or base) with PATH guaranteed to include system bin dirs."""
|
||||
env = dict(base) if base is not None else os.environ.copy()
|
||||
ensure_system_path(env)
|
||||
return env
|
||||
|
||||
|
||||
def md5(strings: str | bytes) -> str:
|
||||
"""Generate MD5 hash"""
|
||||
@@ -78,6 +96,7 @@ async def exec_shell(
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
cwd=cwd,
|
||||
env=environment_with_system_path(),
|
||||
)
|
||||
try:
|
||||
stdout, stderr = await asyncio.wait_for(
|
||||
@@ -102,6 +121,7 @@ def exec_shell_sync(cmd: str, timeout: Optional[float] = None, cwd: Optional[str
|
||||
capture_output=True,
|
||||
timeout=timeout or 300,
|
||||
cwd=cwd,
|
||||
env=environment_with_system_path(),
|
||||
)
|
||||
out = result.stdout.decode("utf-8", errors="replace") if result.stdout else ""
|
||||
err = result.stderr.decode("utf-8", errors="replace") if result.stderr else ""
|
||||
|
||||
Reference in New Issue
Block a user