-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSystemInfo.py
69 lines (56 loc) · 2.21 KB
/
SystemInfo.py
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
import os
import platform
import tempfile
import time
import sys
import subprocess
class SystemInfo:
_insts = []
def __new__(cls):
if SystemInfo._insts:
return SystemInfo._insts[0]
sysInfo = super().__new__(cls)
SystemInfo._insts.append(sysInfo)
sysInfo.usedOS = platform.system().lower()
sysInfo.tmpPath: str = tempfile.gettempdir()
sysInfo.dbFileName = "images.db"
sysInfo.cwd = os.getcwd()
sysInfo.fileLocation = os.path.dirname(os.path.abspath(__file__))
sysInfo.info = "-i" in sys.argv
sysInfo.dataDir = sysInfo.getLocation(f"{sysInfo.fileLocation}/data")
sysInfo.dbPath = sysInfo.getLocation(
f"{sysInfo.dataDir}/{sysInfo.dbFileName}")
sysInfo.infoFile = sysInfo.getLocation(
f"{sysInfo.dataDir}/.last.librerecall")
sysInfo.isWayland: bool = False if sysInfo.usedOS != "linux" else sysInfo.isWaylandSession()
sysInfo.usingSystemd = False if sysInfo.usedOS != "linux" else sysInfo.checkSystemd()
return sysInfo
def checkSystemd(self):
systemdText = subprocess.run(
["systemctl", "-h"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True).stdout
# what a detection..
return systemdText.find("See the systemctl(1) man page for details.") > -1
def getLocation(self, unixPath: str) -> str:
home: str = os.getenv(
"HOME") if self.usedOS != "windows" else os.getenv("UserProfile")
unixPath = unixPath.replace("~", home)
if self.usedOS == "windows":
unixPath = unixPath.replace("/", "\\")
return unixPath
def isWaylandSession(self) -> bool:
if self.usedOS != "linux":
return False
return os.getenv("XDG_SESSION_TYPE") == "wayland"
@staticmethod
def getTimeMS(self=None) -> int:
return time.time_ns() // 1_000_000
def makeInfoFileIfNotExists(self) -> None:
file = None
try:
file = open(self.infoFile, "r")
except FileNotFoundError:
file = open(self.infoFile, "w")
file.write(str(self.getTimeMS()))
finally:
if file:
file.close()