This commit is contained in:
Your Name
2026-04-17 03:13:54 +08:00
parent f4b4ed0afe
commit 3cc36a054c

View File

@@ -36,8 +36,16 @@ def _run_shell(command: list[str], timeout: int = 20) -> subprocess.CompletedPro
return subprocess.run(command, capture_output=True, text=True, timeout=timeout) return subprocess.run(command, capture_output=True, text=True, timeout=timeout)
def _run_systemctl(command: list[str], timeout: int = 20, require_sudo: bool = True) -> subprocess.CompletedProcess[str]: def _run_systemctl(
systemctl_command = ["systemctl", *command] command: list[str],
timeout: int = 20,
require_sudo: bool = True,
no_block: bool = False,
) -> subprocess.CompletedProcess[str]:
systemctl_command = ["systemctl"]
if no_block and "--no-block" not in command:
systemctl_command.append("--no-block")
systemctl_command.extend(command)
if os.geteuid() == 0 or not require_sudo: if os.geteuid() == 0 or not require_sudo:
return _run_shell(systemctl_command, timeout=timeout) return _run_shell(systemctl_command, timeout=timeout)
return _run_shell(["sudo", "-n", *systemctl_command], timeout=timeout) return _run_shell(["sudo", "-n", *systemctl_command], timeout=timeout)