1
1
#!/usr/bin/python3
2
- import os
2
+ import sys
3
3
import signal
4
4
import subprocess
5
5
6
6
print ("" )
7
7
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" )
9
9
print ("device in the ports that you are interested for VM passthrough." )
10
10
print ("" )
11
11
print ("Press Control + C when finished. The app will then print the device" )
12
12
print ("path of the USB ports. Also make sure that 'udevadm' is installed." )
13
13
print ("" )
14
- input ("Press ENTER to continue or abort with CTRL+C..." )
15
- print ("" )
16
14
print ("Monitoring USB ports..." )
17
15
18
16
###########################
@@ -39,29 +37,52 @@ def handle(sig, _):
39
37
listout .append (line )
40
38
41
39
proc .wait ()
42
- # print(listout)
43
40
44
41
######################################
45
42
# This gets an unique list of DEVPATHs
46
43
######################################
47
44
45
+
46
+
48
47
# function to get unique values
49
48
49
+ def unique (input_list ):
50
+
51
+ # leave only unique entries
52
+ return list (dict .fromkeys (input_list ))
50
53
51
- def unique (list1 ):
52
54
53
- # intilize a null list
54
- unique_list = []
55
55
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
61
57
62
- return unique_list
58
+ def remove_unnecessary ( input_list ):
63
59
60
+ # copy to avoid modifying the input list
61
+ output_list = list (input_list )
64
62
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 ("\n Found 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