import os import subprocess from typing import Optional from pathlib import Path def run_cmd(args: list[str], cd: Optional[Path] = None) -> int: cmdline = " ".join(args) if cd is not None: print("Settings CWD to", cd) print("Executing:", cmdline) cwd = os.getcwd() if cd is not None: os.chdir(cd) ret = subprocess.call(args, cwd=cd) os.chdir(cwd) return ret def run_sudo_cmd(args: list[str], cd: Optional[Path] = None) -> int: return run_cmd( [ "pkexec", "--user", "root", "--keep-cwd", *args, ], cd=cd, ) def run_cmd_shell(cmd: str) -> None: print("Executing:", cmd) subprocess.run( cmd, shell=True, ) def run_cmd_nonblocking(args: list[str]) -> subprocess.Popen: cmdline = " ".join(args) print("Executing:", cmdline) return subprocess.Popen(args)