Skip to content

Commit 86ba369

Browse files
committed
Refactor admin privilege check and add verbose option
1 parent 0691cb1 commit 86ba369

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

captametropolis/utils.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,33 @@
3030

3131

3232
def is_admin():
33-
try:
34-
return ctypes.windll.shell32.IsUserAnAdmin()
35-
except:
33+
if os.name == "nt":
34+
try:
35+
return ctypes.windll.shell32.IsUserAnAdmin()
36+
except:
37+
return False
38+
elif os.name == "posix":
39+
return os.getuid() == 0
40+
else:
3641
return False
3742

3843

3944
def run_as_admin(verbose: bool = False):
4045
if verbose:
4146
print("Checking admin privileges...")
4247
if not is_admin():
43-
print("WARNING: You need admin privileges to run this script.")
48+
if verbose:
49+
print("WARNING: You need admin privileges to run this script.")
4450
if os.name == "posix":
4551
os.execvp("sudo", ["sudo", "python3"] + sys.argv)
4652
else:
4753
ctypes.windll.shell32.ShellExecuteW(
48-
None, "runas", sys.executable, __file__, None, 1
54+
None, "runas", sys.executable, " ".join(sys.argv), None, 1
4955
)
56+
sys.exit(0)
57+
else:
58+
if verbose:
59+
print("Admin privileges granted.")
5060

5161

5262
def detect_local_whisper(print_info):

0 commit comments

Comments
 (0)