-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathudev_test.py
executable file
·181 lines (151 loc) · 4.77 KB
/
udev_test.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
#!/usr/bin/env python3
# coding=utf-8
"""
pyudev tests.
simple tests for the correct udev matchings.
use with interactive flag:
"""
import time
import os
import json
import pyudev
##########################################
context = pyudev.Context()
device = None
# devices = context.list_devices()
# for device in devices.match_subsystem('block') \
# .match_property('ID_BUS', 'usb'):
# print("-"*42)
# print(device)
# # for prop in device.properties:
# # print(prop)
# print("ID_BUS: ", device.properties.get("ID_BUS"))
# print("ID_USB_INTERFACES: ", device.properties.get("ID_USB_INTERFACES"))
# print("ID_SERIAL: ", device.properties.get("ID_SERIAL"))
# # print("-"*42)
# for device in context.list_devices(
# # sys_name='usb'
# # subsystem='usb',
# # SUBSYSTEMS='usb',
# # this works:
# subsystem='block',
# DEVTYPE='partition'
# ):
# print(device)
# for dev in devices.match(subsystem='block', DEVTYPE='partition'): print(dev)
print("Version 1", "-"*42)
for dev in context.list_devices(
subsystem='block'
).match_property("ID_BUS", "usb"):
print(dev)
print("Version 2", "-"*42)
for dev in context.list_devices(
subsystem='block',
ID_BUS="usb"
):
print(dev)
print("Version 3", "-"*42)
for dev in context.list_devices(
subsystem='block',
ID_BUS="usb",
DEVTYPE='partition'
):
print(dev)
print("problem is that all properties are matche as OR :-(")
##########################################
print("-"*42)
print("start monitoring for block devices with partition:")
monitor = pyudev.Monitor.from_netlink(context)
monitor.filter_by('block', device_type="partition")
def print_properties(device):
"""Print all properties with values for an given device."""
for prop in device.properties:
print("{}:{}".format(
prop,
device[prop]
))
# def print_attributes(device):
# """Print all attributes with values for an given device."""
# for attr in device.attributes:
# print("{}:{}".format(
# attr,
# device[attr]
# ))
def log_event(action, device):
"""Print an event."""
print('background event {0.action}: {0.device_path}'.format(device))
if 'ID_BUS' in device:
if device['ID_BUS'] == 'usb':
print("-"*42)
# for prop in device.attributes:
# print(prop)
usb_port_path = device.find_parent('usb').device_path
head, tail = os.path.split(usb_port_path)
levels = tail.replace(':1.0', '').split('.')
# tree = {}
# current_level = tree
# for level in levels:
# current_level[level] = {}
# current_level = current_level[level]
print(levels)
tree = {
'device_path': device.device_path
}
for level in reversed(levels):
new_level = {}
new_level[level] = tree
tree = new_level
print(
(
42*'*' + "\n"
"{}\n"
"ID_BUS: {}\n"
"device_node: {}\n"
"DEVNAME: {}\n"
"ID_FS_LABEL: {}\n"
"ID_FS_LABEL_ENC: {}\n"
"ID_SERIAL: {}\n"
"usb_port_path: {}\n"
"USB Port: {}\n"
"USB Port levels: {}\n"
"USB Port Tree: \n{}\n"
).format(
device,
device.properties.get("ID_BUS"),
device.device_node,
device.properties.get("DEVNAME"),
device.properties.get("ID_FS_LABEL"),
device.properties.get("ID_FS_LABEL_ENC"),
device.properties.get("ID_SERIAL"),
usb_port_path,
tail,
levels,
json.dumps(
tree,
sort_keys=True,
indent=4,
separators=(',', ': ')
),
)
)
# print("properties", "-"*42)
# print_properties(device)
# print("attributes", "-"*42)
# print_attributes(device)
print("-"*42)
observer = pyudev.MonitorObserver(monitor, log_event)
observer.start()
# just wait
flag_run = True
try:
while flag_run:
time.sleep(1)
except KeyboardInterrupt:
print("\nstop script.")
flag_run = False
# get device:
# device = pyudev.Devices.from_path(
# context,
# '/sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2.2/2-1.2.2.4/'
# '2-1.2.2.4:1.0/host7/target7:0:0/7:0:0:0/block/sdc/sdc1'
# )