/
tuxconfig.py
executable file
·507 lines (424 loc) · 18.5 KB
/
tuxconfig.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#!/usr/bin/python3
import datetime
import fnmatch
import json
import os
from os.path import join
from cpuinfo import get_cpu_info
import random
import string
import subprocess
import sys
import urllib
import webbrowser
from json import JSONDecodeError
from pathlib import Path
import requests
import socket
from gi.repository import GObject
from shlex import quote as shlex_quote
install_success = False
install_failed = False
class InstallUpdates:
result = ""
completed = 0
device_array = []
def pad_ids(usb_id):
while len(usb_id) < 4:
usb_id = "0" + usb_id
return usb_id
def get_platform():
if get_cpu_info()['arch'] == "i386":
return "i386"
elif get_cpu_info()['arch'] == "X86_64":
return "x86_64"
else:
with open('/proc/cpuinfo') as f:
line = f.read()
if 'Hardware' in line:
hardware_id = line.split(":")[1]
hardware_id.strip(" ")
return hardware_id
import threading
def get_modules_available(device_array,recaptcha_token):
device_string = []
for device in device_array:
device_string.append({device.getDeviceString()})
device_array.append({"recaptcha_token" : recaptcha_token})
response = requests.post(
'https://www.tuxconfig.com/user/get_device_available/',json=json.dumps(device_string))
if response.status_code >= 400 and response.status_code < 404:
print("connection error")
return []
elif response.status_code >= 500:
print("server error")
return []
result = json.loads(response.content)
InstallUpdates.device_array = result
return result
class device_details:
def __init__(self):
self.vendor_id = None
self.device_id = None
self.revision = None
self.device_name = None
self.device_vendor = None
self.driver = None
self.subsystem = None
self.clone_url = None
self.commit = None
self.stars = None
self.pk = None
self.tried = False
self.success = False
self.available = False
self.already_installed = False
self.installed_status = 0
self.install_directory = None
tuxconfig_file = Path('/var/lib/tuxconfig.log')
if tuxconfig_file.is_file():
with open('/var/lib/tuxconfig.log') as file:
for line in file:
checking_device = json.loads(line)
if checking_device['clone_url'] == self.clone_url and checking_device['commit'] == self.commit:
if checking_device['tried'] is True:
self.tried = True
if checking_device['success'] is True:
self.success = True
else:
print("Install marked as unsuccessful")
def setName(self, device_name):
self.device_name = device_name
def setDeviceId(self, vendorId):
self.vendor_id = vendorId
def setVendorId(self, device_id):
self.device_id = device_id
def setVendor(self, device_vendor):
self.device_vendor = device_vendor
def setDriver(self, driver):
self.driver = driver
def setRevision(self, revision):
self.revision = revision
def setSubsystem(self, subsystem):
self.subsystem = subsystem
def setCloneUrl(self, clone_url):
self.subsystem = clone_url
def setCommit(self, commit):
self.commit = commit
def getString(self):
return self.vendor_id + ":" + self.device_id + " " + self.device_vendor + " " + self.device_name + "\n"
def getDeviceString(self):
return self.vendor_id + ":" + self.device_id
def getDeviceName(self):
return self.device_name
def getAvailable(self):
return self.available
def getCommit(self):
return self.commit
def getCloneUrl(self):
return self.clone_url
def getInstalledAlready(self):
return self.success
def getInstallTried(self):
return self.tried
def get_repository_details(self):
try:
string = InstallUpdates.device_array[self.device_id]
json_response = json.loads(string)
current_repository = json_response[0]
drivers_available = 0
for repository in current_repository:
if repository['stars'] > current_repository['stars']:
current_repository = repository
self.available = True
self.clone_url = current_repository['clone_url']
self.commit = current_repository['commit']
self.stars = current_repository['stars']
self.pk = current_repository['pk']
drivers_available += 1
self.already_installed = self.already_installed(self)
if device.tried:
print("Trying next favoured install for " + self.device_vendor + " " + self.device_name)
return current_repository, drivers_available
except JSONDecodeError:
print("cannot parse server request")
return False
def write_to_file(self):
f = open("/var/lib/tuxconfig.log", "w+")
now = datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')
json_string = json.dumps({"vendor_id": self.vendor_id, "device_id": self.device_id, "revision": self.revision,
"vendor_name": self.device_vendor, "device_name": self.device_name,
"driver": self.driver, "clone_url": self.clone_url, "stars": self.stars,
"commit": self.commit,
"pk": self.pk, "tried": self.tried, "success": self.success, "time": now})
f.write(json_string)
f.close()
def already_installed(self):
tuxconfig_file = Path('/var/lib/tuxconfig.log')
if tuxconfig_file.is_file():
with open('/var/lib/tuxconfig.log') as file:
for line in file:
checking_device = json.loads(line)
if checking_device['clone_url'] == self.clone_url and checking_device['commit'] == self.commit:
if checking_device['tried'] is True:
self.tried = True
if checking_device['success'] is True:
self.success = True
else:
print("Install marked as unsuccessful")
def uninstall_module(self):
append_to_logfile(get_module_install_status(3),self.install_directory,self.driver)
self.installed_status = 3
uninstall = subprocess.Popen(["dmks", "remove", self.driver + "--all"], cwd='/tmp/' + self.install_directory,
stdout=subprocess.PIPE)
streamdata = uninstall.communicate()[0]
append_to_logfile(str(streamdata, "utf-8") + "\n",self.install_directory,self.driver)
def install_module(self):
self.install_directory = ''.join(random.choices(string.ascii_letters + string.digits, k=6))
clone_git = subprocess.Popen(["git", "clone", device.clone_url, "/tmp/" + self.install_directory], stdout=subprocess.PIPE)
streamdata = clone_git.communicate()[0]
self.installed_status = 1
if clone_git.returncode > 0:
append_to_logfile(get_module_install_status(1),self.install_directory,self.driver)
append_to_logfile(str(streamdata, "utf-8") + "\n",self.install_directory,self.driver)
self.installed_status = -1
return False
self.installed_status = 2
append_to_logfile(get_module_install_status(2),self.install_directory,self.driver)
set_git_commit = subprocess.Popen(["git", "reset", "--hard", device.commit], cwd='/tmp/' + self.install_directory,
stdout=subprocess.PIPE)
streamdata = set_git_commit.communicate()[0]
if set_git_commit.returncode > 0:
append_to_logfile("Error setting Git branch\n",self.install_directory,self.driver)
self.installed_status = -2
return False
append_to_logfile(str(streamdata, "utf-8") + "\n",self.install_directory,self.driver)
if not os.path.exists("/tmp/" +self.install_directory + "/tuxconfig.conf"):
append_to_logfile("Tuxconfig file not in directory\n",self.install_directory,self.driver)
self.installed_status = -2
return False
install_command = None
test_command = None
module_name = None
with open("/tmp/" + self.install_directory + "/tuxconfig.conf", encoding="UTF-8") as tuxconfig_file:
lines = tuxconfig_file.readlines()
for index, line in enumerate(lines):
line = line.replace("\n", "")
line = line.replace("\"", "")
if "install_command" in line:
line.split("=")
install_command = line.split("=")[1].strip("\"")
if "test_command" in line:
line.split("=")
test_command = line.split("=")[1].strip("\"")
if "module_id" in line:
line.split("=")
module_name = line.split("=")[1].strip("\"")
installed = get_device_installed_list(module_name)
if installed is True:
self.uninstall_module()
append_to_logfile(get_module_install_status(4),self.install_directory,self.driver)
self.installed_status = 4
if install_command.startswith("./"):
install_command = install_command[2:len(install_command)]
install_module = subprocess.Popen(["/tmp/" + self.install_directory + "/" + install_command], stdout=subprocess.PIPE,
cwd="/tmp/" + self.install_directory)
streamdata = install_module.communicate()[0]
if install_module.returncode > 0:
append_to_logfile(get_module_install_status(-3),self.install_directory,self.driver)
append_to_logfile(str(streamdata, "utf-8") + "\n",self.install_directory,self.driver)
return False
append_to_logfile(get_module_install_status(5),self.install_directory,self.driver)
self.installed_status = 5
test_module = subprocess.Popen(["bash", test_command], stdout=subprocess.PIPE,
cwd="/tmp/" + self.install_directory)
streamdata = test_module.communicate()[0]
if test_module.returncode > 0:
append_to_logfile("Error testing new module install\n",self.install_directory,self.driver)
append_to_logfile(str(streamdata, "utf-8") + "\n",self.install_directory,self.driver)
self.installed_status = -4
return False
append_to_logfile(str(streamdata, "utf-8") + "\n",self.install_directory,self.driver)
append_to_logfile(get_module_install_status(6),self.install_directory,self.driver)
return True
def append_to_logfile(message,directory,module_name):
with open(join(directory,"_",module_name,".log"), "a") as logfile:
logfile.write(message)
def poll_update_status(device):
if device.installed_status > 0:
print (get_module_install_status(device.installed_status))
class DaemonStoppableThread(threading.Thread):
def __init__(self,installable_device):
super(DaemonStoppableThread, self).__init__()
self.setDaemon(True)
self.stop_event = threading.Event()
self.sleep_time = 1000
self.device = installable_device
self.target = poll_update_status(self.device)
def get_module_install_status(install_status):
if install_status == 0:
return "Waiting to install"
elif install_status == 1:
return "Cloning repository"
elif install_status == 2:
return "Resetting git head"
elif install_status == 3:
return "Uninstalling current module"
elif install_status == 4:
return "Installing new module"
elif install_status == 5:
return "Running tests on installing module"
elif install_status == -1:
return "Error cloning repository"
elif install_status == -2:
return "Error removing previous installing module"
elif install_status == -3:
return "Error installing module"
elif install_status == -4:
return "Error running tests on installing module"
def get_root():
got_root = 00000000
if os.geteuid() != 0:
print("Please run as using sudo")
text = input("Enter user password") # Python 3
sudo_password = 'echo %s|sudo su'.format(text)
got_root = os.system(shlex_quote(sudo_password))
if got_root != 00000000:
print("That failed, trying su with your root password")
text = input("Enter root password") # Python 3
sudo_password = 'echo %s su'.format(text)
got_root = os.system(shlex_quote(sudo_password))
if got_root != 00000000:
print("You need to be super user to install modules, exiting now.")
exit(1)
def get_device_installed_list(module_name):
get_packages = subprocess.Popen(["dkms status | cut -d\",\" -f 1,2"], stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
output = get_packages.communicate()[0].decode("utf-8")
for package in output.split("\n"):
package.strip(": added")
package_details = package.strip().split(",")
if package_details[0] == module_name:
return package_details[0] + "/" + package_details[1].strip()
return False
def check_connected():
IPaddress = socket.gethostbyname(socket.gethostname())
if IPaddress == "127.0.0.1":
print("Internet not connected.")
return False
else:
return True
def find_device_ids(type):
device_list = []
for file in os.listdir("/sys/bus/" + type + "/devices/"):
result = subprocess.check_output(
["udevadm", "info", os.path.join("/sys/bus/" + type + "/devices/", file)]).decode(
"utf-8").split("\n")
device_id = None
vendor_id = None
revision = None
model_vendor = None
model_name_id = None
model_name = None
driver = None
subsystem = None
for line in result:
line = line[3:len(line)]
if line.startswith("DRIVER="):
driver = line[7:len(line)]
if line.startswith("PRODUCT="):
product = line[12:len(line)]
if line.startswith("PCI_ID=") and type == "pci":
pci_id = line[7:len(line)]
pci_id_array = pci_id.split(":")
if len(pci_id_array) == 2:
device_id = pad_ids(pci_id_array[0])
vendor_id = pad_ids(pci_id_array[1])
if len(pci_id_array) == 3:
revision = pad_ids(pci_id_array[2])
if line.startswith("PRODUCT=") and type == "usb":
pci_id = line[8:len(line)]
pci_id_array = pci_id.split("/")
if len(pci_id_array) >= 2:
device_id = pad_ids(pci_id_array[0])
vendor_id = pad_ids(pci_id_array[1])
if len(pci_id_array) == 3:
revision = pad_ids(pci_id_array[2])
if line.startswith("ID_MODEL="):
model_name_id = line[9:len(line)]
if line.startswith("ID_MODEL_FROM_DATABASE="):
model_name = line[23:len(line)]
if line.startswith("ID_VENDOR_FROM_DATABASE="):
model_vendor = line[24:len(line)]
if line.startswith("SUBSYSTEM="):
subsystem = line[10:len(line)]
if device_id is not None and vendor_id is not None:
device = device_details()
device.setDeviceId(device_id)
device.setVendorId(vendor_id)
device.setVendor(model_vendor)
if model_name_id is None and model_name is None:
continue
elif model_name_id is not None and model_name is None:
device.setName(model_name_id)
elif model_name is not None and model_name_id is None:
device.setName(model_name)
elif model_name is not None and model_name_id is not None:
device.setName(model_name)
device.setRevision(revision)
device.setDriver(driver)
device.setSubsystem(subsystem)
device_list.append(device)
return device_list
if __name__ == '__main__':
print("Getting available drivers")
devices_available = []
pci_devices = find_device_ids("pci")
usb_devices = find_device_ids("usb")
devices_available.extend(pci_devices)
devices_available.extend(usb_devices)
index = 0
device_map = []
empty_list = True
for device in devices_available:
error = device.get_repository_details()
if device.available:
empty_list = False
device_map.append(device)
index += 1
print(str(index) + ") " +device.getString() + " installable from www.tuxconfig.com")
if empty_list is True:
print("No more drivers available for this device")
exit(2)
install_number = input("Select device from list: ") # Python 3
install_number_int = 1
try:
install_number_int = int(install_number)
except:
print("Not a valid choice number")
install_result = False
device_to_install = None
if install_number_int > len(device_map):
print("Not a valid choice number")
exit(5)
else:
for device in device_map:
print("Try install with " + device.stars + " github rating?")
if len(device_map) > 1:
try_install = input("Select device driver to try, number between 1 and " + str(len(device_map))) + ":"
else:
try_install = 1
device_to_install = device_map[int(try_install) - 1]
device_to_install.tried = True
install_result = device_to_install.install_module()
t = DaemonStoppableThread(device_to_install)
t.start()
if install_result is True:
device_to_install.success = True
set_git_commit = subprocess.Popen(
["xdg-open", "https://www.tuxconfig.com/user/get_contributor/" + str(device_to_install.pk)],
stdout=subprocess.PIPE)
else:
device_to_install.success = False
print("install failed")
device_to_install.write_to_file()