-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransferManager.py
159 lines (126 loc) · 5.81 KB
/
TransferManager.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
import subprocess
import tkinter as tk
def list_devices():
# Execute the 'list --local' command and capture the output
output = subprocess.check_output(['usbip', 'list', '--local']).decode('utf-8')
# Extract the device names from the output
device_names = []
for line in output.split('\n'):
if 'Bus=' in line:
device_name = line.split(' ')[-1].strip()
device_names.append(device_name)
return device_names
def bind_device(device_name):
# Execute the 'bind' command for the specified device
subprocess.check_call(['sudo', 'usbip', 'bind', '-b', device_name])
def unbind_device(device_name):
# Execute the 'unbind' command for the specified device
subprocess.check_call(['sudo', 'usbip', 'unbind', '-b', device_name])
class App(tk.Tk):
def __init__(self):
super().__init__()
# Create a dictionary to store the device names and their state (bound/unbound)
self.device_states = {}
# Create a listbox to display the device names
self.device_listbox = tk.Listbox(self)
self.device_listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# Create a button to bind the selected device
self.bind_button = tk.Button(self, text='Bind', command=self.bind_device)
self.bind_button.pack(side=tk.TOP, fill=tk.X)
# Create a button to unbind the selected device
self.unbind_button = tk.Button(self, text='Unbind', command=self.unbind_device)
self.unbind_button.pack(side=tk.TOP, fill=tk.X)
# Populate the device listbox
self.update_device_listbox()
def update_device_listbox(self):
# Get the list of device names
device_names = list_devices()
# Remove any devices that are no longer connected
for device_name in list(self.device_states.keys()):
if device_name not in device_names:
del self.device_states[device_name]
# Add any new devices that have been connected
for device_name in device_names:
if device_name not in self.device_states:
self.device_states[device_name] = False
# Clear the device listbox
self.device_listbox.delete(0, tk.END)
# Populate the device listbox with the device names and their state
for device_name, state in self.device_states.items():
if state:
self.device_listbox.insert(tk.END, device_name + ' (Bound)', 'bound')
else:
self.device_listbox.insert(tk.END, device_name + ' (Unbound)', 'unbound')
# Set the background color of the device listbox items based on their state
self.device_listbox.tag_configure('bound', background='green')
self.device_listbox.tag_configure('unbound', background='red')
# Schedule the next update after 1 second
self.after(1000, self.update_device_listbox)
def bind_device(self):
# Get the selected device name from the device listbox
selection = self.device_listbox.curselection()
if len(selection) != 1:
return
device_name = self.device_listbox.get(selection[0]).split(' ')[0]
# Bind the selected device
selected_devices = [devices[i] for i in checked_items]
for device in selected_devices:
device_name = device.split()[0]
print(f"Binding {device_name}...")
os.system(f"sudo usbip bind -b {device_name}")
bound_devices.append(device_name)
# Update the checkboxes
for i, device in enumerate(devices):
device_name = device.split()[0]
if device_name in bound_devices:
device_checkboxes[i].config(fg="green")
else:
device_checkboxes[i].config(fg="red")
def unbind_devices():
"""Unbinds the selected devices."""
global bound_devices
checked_items = get_checked_items()
# Unbind the selected device
selected_devices = [devices[i] for i in checked_items]
for device in selected_devices:
device_name = device.split()[0]
print(f"Unbinding {device_name}...")
os.system(f"sudo usbip unbind -b {device_name}")
bound_devices.remove(device_name)
# Update the checkboxes
for i, device in enumerate(devices):
device_name = device.split()[0]
if device_name in bound_devices:
device_checkboxes[i].config(fg="green")
else:
device_checkboxes[i].config(fg="red")
# Set up the GUI
root = tk.Tk()
root.title("USB/IP Bindings")
root.geometry("400x400")
# Create the device list
devices_label = tk.Label(root, text="Select Devices to Bind/Unbind:")
devices_label.pack()
device_frame = tk.Frame(root)
device_frame.pack()
devices = get_devices()
device_checkboxes = []
for device in devices:
device_name = device.split()[0]
checkbox = tk.Checkbutton(device_frame, text=device, onvalue=1, offvalue=0)
if device_name in bound_devices:
checkbox.config(fg="green")
checkbox.select()
else:
checkbox.config(fg="red")
checkbox.pack(anchor="w")
device_checkboxes.append(checkbox)
# Create the bind/unbind buttons
button_frame = tk.Frame(root)
button_frame.pack()
bind_button = tk.Button(button_frame, text="Bind Selected Devices", command=bind_devices)
bind_button.pack(side="left", padx=10)
unbind_button = tk.Button(button_frame, text="Unbind Selected Devices", command=unbind_devices)
unbind_button.pack(side="left", padx=10)
# Run the GUI
root.mainloop()