This repository was archived by the owner on May 19, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsystem_utils.py
More file actions
72 lines (56 loc) · 2.03 KB
/
Copy pathsystem_utils.py
File metadata and controls
72 lines (56 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import atexit
import ctypes
import os
import subprocess
import sys
import tempfile
import psutil
from config import ADMIN_STATUS_FILE, IS_WINDOWS
# ── Module-level admin state ───────────────────────────────────────────────
is_admin: bool = False
def load_admin_status() -> None:
global is_admin
if os.path.exists(ADMIN_STATUS_FILE):
with open(ADMIN_STATUS_FILE, "r") as f:
is_admin = f.read().strip().lower() == "true"
def check_if_admin() -> bool:
try:
if IS_WINDOWS:
return bool(ctypes.windll.shell32.IsUserAnAdmin())
return os.getuid() == 0
except Exception:
return False
def elevate() -> bool:
if check_if_admin():
raise Exception("The process already has admin rights.")
if IS_WINDOWS:
script = os.path.abspath(sys.argv[0])
command = (
f'"{script}"'
if os.path.splitext(script)[1].lower() == ".exe"
else f'"{sys.executable}" "{script}"'
)
result = ctypes.windll.shell32.ShellExecuteW(
None, "runas", "cmd.exe", f"/k {command} & timeout /t 7 & exit", None, 1
)
if result > 32:
return True
raise Exception("Error restarting the script with admin rights.")
else:
subprocess.Popen(["sudo", sys.executable] + sys.argv)
return True
def check_single_instance() -> None:
pid_file = os.path.join(tempfile.gettempdir(), "script_instance.pid")
if os.path.exists(pid_file):
with open(pid_file, "r") as f:
pid = int(f.read())
if psutil.pid_exists(pid):
print("An instance of the script is already running.")
sys.exit(0)
print("PID file found, but process is no longer running. Overwriting.")
with open(pid_file, "w") as f:
f.write(str(os.getpid()))
def _remove():
if os.path.exists(pid_file):
os.remove(pid_file)
atexit.register(_remove)