2022-03-19 19:48:19 -04:00
|
|
|
import os
|
|
|
|
import sys
|
2022-11-16 03:34:25 -05:00
|
|
|
import ctypes
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2022-08-19 07:12:58 -04:00
|
|
|
try:
|
|
|
|
from PyQt6.QtCore import QStandardPaths
|
|
|
|
except ImportError:
|
|
|
|
print("PyQt5 fallback (ostools.py)")
|
|
|
|
from PyQt5.QtCore import QStandardPaths
|
2011-08-11 04:17:53 -04:00
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2011-08-11 04:17:53 -04:00
|
|
|
def isOSX():
|
2011-08-23 05:28:58 -04:00
|
|
|
return sys.platform == "darwin"
|
2011-08-11 04:17:53 -04:00
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2011-08-11 04:17:53 -04:00
|
|
|
def isWin32():
|
2011-08-23 05:28:58 -04:00
|
|
|
return sys.platform == "win32"
|
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2011-08-23 05:28:58 -04:00
|
|
|
def isLinux():
|
|
|
|
return sys.platform.startswith("linux")
|
2011-08-11 04:17:53 -04:00
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2011-08-11 04:17:53 -04:00
|
|
|
def isOSXBundle():
|
2022-10-07 16:51:40 -04:00
|
|
|
return isOSX() and (os.path.abspath(".").find(".app") != -1)
|
|
|
|
|
2011-08-23 05:28:58 -04:00
|
|
|
|
2022-11-16 03:34:25 -05:00
|
|
|
def isRoot():
|
2023-01-29 11:29:17 -05:00
|
|
|
"""Return True if running as root on Linux/Mac/Misc"""
|
|
|
|
if hasattr(os, "getuid"):
|
|
|
|
return not os.getuid() # 0 if root
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def isAdmin():
|
|
|
|
"""Return True if running as Admin on Windows."""
|
2022-11-16 03:34:25 -05:00
|
|
|
try:
|
|
|
|
if isWin32():
|
|
|
|
return ctypes.windll.shell32.IsUserAnAdmin() == 1
|
|
|
|
except OSError as win_issue:
|
|
|
|
print(win_issue)
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2022-09-06 07:26:14 -04:00
|
|
|
def validateDataDir():
|
|
|
|
"""Checks if data directory is present"""
|
|
|
|
# Define paths
|
|
|
|
datadir = getDataDir()
|
|
|
|
profile = os.path.join(datadir, "profiles")
|
|
|
|
quirks = os.path.join(datadir, "quirks")
|
|
|
|
logs = os.path.join(datadir, "logs")
|
|
|
|
errorlogs = os.path.join(datadir, "errorlogs")
|
|
|
|
backup = os.path.join(datadir, "backup")
|
2023-07-14 09:09:56 -04:00
|
|
|
themes = os.path.join(datadir, "themes")
|
|
|
|
# ~lisanne Datadir/themes for repository themes
|
|
|
|
# Apparently everything checks this folder for themes already
|
|
|
|
# So hopefully im not plugging into an existng system on accident
|
|
|
|
|
2022-09-06 07:26:14 -04:00
|
|
|
js_pchum = os.path.join(datadir, "pesterchum.js")
|
2023-07-14 09:09:56 -04:00
|
|
|
js_manifest = os.path.join(datadir, "manifest.js")
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2022-09-06 07:26:14 -04:00
|
|
|
dirs = [datadir, profile, quirks, logs, errorlogs, backup]
|
|
|
|
for d in dirs:
|
2023-02-13 14:26:05 -05:00
|
|
|
if not os.path.isdir(d) or not os.path.exists(d):
|
2022-09-06 07:26:14 -04:00
|
|
|
os.makedirs(d, exist_ok=True)
|
|
|
|
|
|
|
|
# pesterchum.js
|
2023-07-14 09:09:56 -04:00
|
|
|
for filepath in [js_pchum, js_manifest]:
|
|
|
|
if not os.path.exists(filepath):
|
|
|
|
with open(filepath, "w") as f:
|
|
|
|
f.write("{}")
|
2022-09-06 07:26:14 -04:00
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2011-08-11 04:17:53 -04:00
|
|
|
def getDataDir():
|
2012-02-16 01:29:57 -05:00
|
|
|
# Temporary fix for non-ascii usernames
|
|
|
|
# If username has non-ascii characters, just store userdata
|
|
|
|
# in the Pesterchum install directory (like before)
|
|
|
|
try:
|
|
|
|
if isOSX():
|
2022-10-07 16:51:40 -04:00
|
|
|
return os.path.join(
|
|
|
|
QStandardPaths.writableLocation(
|
|
|
|
QStandardPaths.StandardLocation.AppLocalDataLocation
|
|
|
|
),
|
|
|
|
"Pesterchum/",
|
|
|
|
)
|
2012-02-16 01:29:57 -05:00
|
|
|
elif isLinux():
|
2022-10-07 16:51:40 -04:00
|
|
|
return os.path.join(
|
|
|
|
QStandardPaths.writableLocation(
|
|
|
|
QStandardPaths.StandardLocation.HomeLocation
|
|
|
|
),
|
|
|
|
".pesterchum/",
|
|
|
|
)
|
2012-02-16 01:29:57 -05:00
|
|
|
else:
|
2022-10-07 16:51:40 -04:00
|
|
|
return os.path.join(
|
|
|
|
QStandardPaths.writableLocation(
|
|
|
|
QStandardPaths.StandardLocation.AppLocalDataLocation
|
|
|
|
),
|
|
|
|
"pesterchum/",
|
|
|
|
)
|
2022-09-06 07:26:14 -04:00
|
|
|
except UnicodeDecodeError as e:
|
|
|
|
print(e)
|
2022-10-07 16:51:40 -04:00
|
|
|
return ""
|