linux-mod-manager/lmm/cmd.py

46 lines
938 B
Python
Raw Permalink Normal View History

2023-12-02 16:45:16 +00:00
import os
import subprocess
from typing import Optional
from pathlib import Path
2023-12-02 16:46:48 +00:00
2023-12-02 16:45:16 +00:00
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
2023-12-02 16:46:48 +00:00
2023-12-02 16:45:16 +00:00
def run_sudo_cmd(args: list[str], cd: Optional[Path] = None) -> int:
return run_cmd(
[
"pkexec",
2023-12-02 16:46:48 +00:00
"--user",
"root",
2023-12-02 16:45:16 +00:00
"--keep-cwd",
*args,
],
cd=cd,
)
2023-12-02 16:46:48 +00:00
2023-12-02 16:45:16 +00:00
def run_cmd_shell(cmd: str) -> None:
print("Executing:", cmd)
subprocess.run(
cmd,
shell=True,
)
2023-12-02 16:46:48 +00:00
2023-12-02 16:45:16 +00:00
def run_cmd_nonblocking(args: list[str]) -> subprocess.Popen:
cmdline = " ".join(args)
print("Executing:", cmdline)
return subprocess.Popen(args)