-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstaller
executable file
·204 lines (150 loc) · 5.39 KB
/
installer
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python3
"""
"""
import sysconfig
import shutil, os, subprocess
#taken from colorama
sRED = '\033[31m'
sGREEN = '\033[32m'
sRESET = '\033[0m'
service_name = 'mouzwheeler'
file_chmod = {'/usr/local/bin/mouzwheeler': 0o550}
def main():
_show_header()
_check_requirements()
_install_packages()
_copy_files('_/')
_register_service()
_final_notes()
def _show_header():
print('')
print(f'###===> INSTALLER of {service_name}', end='')
print('\n')
#sys.stdout.flush()
#time.sleep(0.5)
def _check_requirements():
sysreq_functions = [func for func in globals().values() if callable(func) and func.__name__.startswith("_syscheck_")]
print("Checking system...\n")
for func in sysreq_functions:
ret = func()
fname = func.__name__.split('_')[-1]
print(f'{fname}: ', end='')
if ret is not True:
print(f'{sRED}{ret}{sRESET}')
exit(1)
print(f'{sGREEN}OK{sRESET}')
print('')
def _install_packages():
def _is_python_externally_managed():
p3_path = sysconfig.get_path("stdlib", sysconfig.get_default_scheme())
return os.path.exists(os.path.join(p3_path, 'EXTERNALLY-MANAGED'))
def _has_apt():
res = cmd('which apt-get')
if res.returncode > 0:
return False
bin_apt = res.stdout.strip()
res = cmd_safe(bin_apt, ['--version'])
return not res.returncode > 0
def _has_pip():
res = cmd('pip --version')
return not res.returncode > 0
def get_packages_apt():
aptfile = 'requirements_apt.txt'
if not os.path.exists(aptfile):
return False
prefix = 'python3-'
packages = []
with open(aptfile, 'r') as f:
packages = [f'{prefix}{line.rstrip()}' for line in f.readlines()]
return packages
apt_packagelist = get_packages_apt()
# Nothing to do
if not os.path.exists('requirements.txt') and not apt_packagelist:
return False
# Packages are managed by the package manager
if _is_python_externally_managed():
if _has_apt() and apt_packagelist:
args = '-y install'.split(" ") + get_packages_apt()
res = cmd_safe('apt-get',args)
print(res.stdout, res.stderr)
return not res.returncode
elif not _has_apt():
print(f'{sRED}APT package manager not found! Install required packages (requirements*.txt) yourself!{sRESET}')
return False
else:
return not cmd('pip install -r requirements.txt').returncode
def _copy_files(fromdir:str='_/'):
print("Copying files...\n")
def get_files_recursively(directory):
file_paths = []
for root, dirs, files in os.walk(directory):
for file in files:
file_path = os.path.join(root, file)
file_paths.append(file_path)
return file_paths
def _create_dir(dir:str):
try:
os.makedirs(path, exist_ok=True)
except OSError as e:
if e.errno == 13:
print(f"Permission denied: {e.strerror}")
else:
print(f"An error occurred: {e.strerror}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
def _copy_file(src:str, dst:str):
try:
shutil.copy(src, dst)
except IOError as e:
print(f"Error copying file: {e}")
except Exception as e:
print(f"Unexpected error: {e}")
files = get_files_recursively(fromdir)
for file in files:
path, filename = os.path.split(file)
filename_dest = '/' + os.path.relpath(file, fromdir)
path_dest = os.path.dirname(filename_dest)
# file = 0 str(40) "_/etc/systemd/system/mouzwheeler.service"
# path_dest = 1 str(19) "/etc/systemd/system"
# filename = 2 str(19) "mouzwheeler.service"
# filename_dest = 3 str(39) "/etc/systemd/system/mouzwheeler.service"
print(f'{file} => {path_dest}')
_create_dir(path_dest)
_copy_file(file, os.path.join(path_dest, filename))
if filename_dest in file_chmod:
os.chmod(filename_dest, file_chmod[filename_dest])
print('')
def _register_service():
print("Registering service...\n")
cmdList = [
['Reloading systemd config files','systemctl daemon-reload'],
[f'Enabling service: {service_name}', f'systemctl enable {service_name}'],
[f'Starting service: {service_name}',f'systemctl start {service_name}']
]
for cmd in cmdList:
print(cmd[0])
result = subprocess.run(cmd[1], shell=True, capture_output=True, text=True)
if result.returncode > 0:
print('error!')
print('\nDONE!')
def _final_notes():
print(f'\nEdit {sGREEN}/etc/default/mouzwheeler{sRESET} to fit your needs!')
def cmd_safe(command:str, args:list):
arglist = args
arglist.insert(0, command)
res = subprocess.run(arglist, shell=False, capture_output=True, text=True)
return res
def cmd(command:str):
"""
Quote arguments with shlex!
"""
res = subprocess.run(command, shell=True, capture_output=True, text=True)
return res
####
#### System checks
####
def _syscheck_root():
return os.geteuid() == 0 or 'Execute as root!'
def _syscheck_systemd():
return 'systemd' in os.readlink('/sbin/init') or 'Only works on SystemD enabled system'
main()