-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathplatform_specific.py
More file actions
90 lines (76 loc) · 3.26 KB
/
platform_specific.py
File metadata and controls
90 lines (76 loc) · 3.26 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import os
from pathlib import Path
import sys
class PlatformSpecificFunctions:
"""Functions that need to be specific for which platform FWMM is running on"""
is_frozen = False
def __init__(self): pass
def add_self_to_startup(self):
"""Add FWMM to the laptop's startup sequence"""
pass
def remove_self_from_startup(self):
"""Remove FWMM from the laptop's startup sequence"""
pass
class Windows(PlatformSpecificFunctions):
"""Functions specific to Windows systems"""
startup_location = ""
is_frozen = False
def __init__(self):
import winshell # type: ignore
self.startup_location = winshell.startup()
self.is_frozen = getattr(sys, 'frozen', False)
def get_self_start_command(self):
"""Get the command that needs to be added to the startup batch file"""
if self.is_frozen:
return str(Path(sys.executable).resolve())
else:
return str(Path(sys.executable).parent.resolve()) + "/pythonw.exe main.py"
def add_self_to_startup(self) -> bool:
with open(os.path.join(self.startup_location, "FWMM.bat"), "w+") as startup_script:
script = "@echo off"
script += "\ncd " + str(Path(os.path.abspath(__file__)).parent.resolve())
script += "\nstart " + self.get_self_start_command() + " skip-window"
startup_script.write(script)
return True
def remove_self_from_startup(self) -> bool:
Path(os.path.join(self.startup_location, "FWMM.bat")).unlink(True)
return True
class Linux(PlatformSpecificFunctions):
"""Functions specific to Cron-enabled Linux systems"""
startup_location = ""
is_frozen = False
def __init__(self):
self.startup_location = "/etc/systemd/user"
self.is_frozen = getattr(sys, 'frozen', False)
def get_self_start_command(self):
"""Get the command that needs to be added to the startup crontab"""
if self.is_frozen:
return str(Path(sys.executable).resolve())
else:
return str(Path(sys.executable).parent.resolve()) + "/python main.py"
def get_working_directory(self):
"""Get FWMM's current working directory"""
if self.is_frozen:
return str(Path(os.path.abspath(__file__)).parent.parent.resolve())
return str(Path(os.path.abspath(__file__)).parent.resolve())
def add_self_to_startup(self) -> bool:
self.remove_self_from_startup() # Prevent duplicates
from crontab import CronTab
cron = CronTab(user=True)
job = cron.new(command=f"cd '{self.get_working_directory()}' && {self.get_self_start_command()} skip-window", comment="FWMM")
job.every_reboot()
cron.write()
return True
def remove_self_from_startup(self) -> bool:
from crontab import CronTab
cron = CronTab(user=True)
jobs = cron.find_comment("FWMM")
for job in jobs:
cron.remove(job)
cron.write()
return True
def get_class() -> PlatformSpecificFunctions:
"""Get the correct version of PlatformSpecificFunctions for the current system"""
if os.name == "nt": return Windows()
elif os.name == "posix": return Linux()
else: raise OSError("Invalid OS: " + os.name)