Skip to content

Commit 2449c48

Browse files
authored
Merge pull request #5 from Iceburgino/patch-1
Some improvements to the monitor script
2 parents a6d2404 + 9e2e8c1 commit 2449c48

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

monitor.py

+38-17
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
#!/usr/bin/python3
2-
import os
2+
import sys
33
import signal
44
import subprocess
55

66
print("")
77
print("Using both an USB 3.0 and an USB 2.0 device (could be a thumb drive,")
8-
print("an audio device or any other simple USB device), plug and unplug the")
8+
print("an audio device or any other simple USB device), plug or unplug the")
99
print("device in the ports that you are interested for VM passthrough.")
1010
print("")
1111
print("Press Control + C when finished. The app will then print the device")
1212
print("path of the USB ports. Also make sure that 'udevadm' is installed.")
1313
print("")
14-
input("Press ENTER to continue or abort with CTRL+C...")
15-
print("")
1614
print("Monitoring USB ports...")
1715

1816
###########################
@@ -39,29 +37,52 @@ def handle(sig, _):
3937
listout.append(line)
4038

4139
proc.wait()
42-
# print(listout)
4340

4441
######################################
4542
# This gets an unique list of DEVPATHs
4643
######################################
4744

45+
46+
4847
# function to get unique values
4948

49+
def unique(input_list):
50+
51+
# leave only unique entries
52+
return list(dict.fromkeys(input_list))
5053

51-
def unique(list1):
5254

53-
# intilize a null list
54-
unique_list = []
5555

56-
# traverse for all elements
57-
for x in list1:
58-
# check if exists in unique_list or not
59-
if x not in unique_list:
60-
unique_list.append(x)
56+
# function to remove the netries that are not useful for udev
6157

62-
return unique_list
58+
def remove_unnecessary(input_list):
6359

60+
# copy to avoid modifying the input list
61+
output_list = list(input_list)
6462

65-
uniq = unique(listout)
66-
stringlist = [x.decode('utf-8') for x in uniq]
67-
print(*stringlist, sep='')
63+
# traverse for all elements
64+
for element in output_list:
65+
# remove long entries as they are not useful for udev
66+
for potential_prefix in output_list:
67+
if element != potential_prefix and element.startswith(potential_prefix):
68+
output_list.remove(element)
69+
70+
return output_list
71+
72+
73+
if __name__ == '__main__':
74+
listout = [x.decode('utf-8').strip() for x in listout]
75+
uniq = unique(listout)
76+
filtered = remove_unnecessary(uniq)
77+
78+
print("\nFound these USB ports:")
79+
print(*filtered, sep='\n')
80+
print("")
81+
82+
orig_stdout = sys.stdout
83+
with open("usb.portlist", "w+") as f:
84+
sys.stdout = f
85+
print(*filtered, sep='\n')
86+
sys.stdout = orig_stdout
87+
88+
print("Results were saved to 'usb.portlist'.")

0 commit comments

Comments
 (0)