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
|
# Copyright 2010-2011 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
__metaclass__ = type
__all__ = [
"UdevadmParser",
]
import re
import string
from lpresults.xunit.parsers.bit import (
get_bitmask,
test_bit,
)
from lpresults.xunit.parsers.input import Input
from lpresults.xunit.parsers.pci import Pci
from lpresults.xunit.parsers.usb import Usb
PCI_RE = re.compile(
r"^pci:"
r"v(?P<vendor_id>[%(hex)s]{8})"
r"d(?P<product_id>[%(hex)s]{8})"
r"sv(?P<subvendor_id>[%(hex)s]{8})"
r"sd(?P<subproduct_id>[%(hex)s]{8})"
r"bc(?P<class>[%(hex)s]{2})"
r"sc(?P<subclass>[%(hex)s]{2})"
r"i(?P<interface>[%(hex)s]{2})"
% {"hex": string.hexdigits})
PNP_RE = re.compile(
r"^acpi:"
r"(?P<vendor_name>[%(upper)s]{3})"
r"(?P<product_id>[%(hex)s]{4}):"
% {"upper": string.uppercase, "hex": string.hexdigits})
USB_RE = re.compile(
r"^usb:"
r"v(?P<vendor_id>[%(hex)s]{4})"
r"p(?P<product_id>[%(hex)s]{4})"
r"d(?P<revision>[%(hex)s]{4})"
r"dc(?P<class>[%(hex)s]{2})"
r"dsc(?P<subclass>[%(hex)s]{2})"
r"dp(?P<protocol>[%(hex)s]{2})"
r"ic(?P<interface_class>[%(hex)s]{2})"
r"isc(?P<interface_subclass>[%(hex)s]{2})"
r"ip(?P<interface_protocol>[%(hex)s]{2})"
% {"hex": string.hexdigits})
SCSI_RE = re.compile(
r"^scsi:"
r"t-0x(?P<type>[%(hex)s]{2})"
% {"hex": string.hexdigits})
class UdevadmDevice:
__slots__ = ("_environment", "_bits", "_stack",)
def __init__(self, environment, bits=None, stack=[]):
self._environment = environment
self._bits = bits
self._stack = stack
@property
def bus(self):
# Change the bus from 'acpi' to 'pnp' for some devices
if PNP_RE.match(self._environment.get("MODALIAS", "")) \
and self.path.endswith(":00"):
return u"pnp"
# Change the bus from 'block' to parent
if self._environment.get("DEVTYPE") == "disk" and self._stack:
return self._stack[-1]._environment.get("SUBSYSTEM")
bus = self._environment.get("SUBSYSTEM")
if bus == "pnp":
return None
return bus
@property
def category(self):
if "IFINDEX" in self._environment:
return u"NETWORK"
if "PCI_CLASS" in self._environment:
pci_class_string = self._environment["PCI_CLASS"]
pci_class = int(pci_class_string, 16)
# Strip prog_if if defined
if pci_class > 0xFFFF:
pci_class >>= 8
subclass_id = pci_class & 0xFF
class_id = (pci_class >> 8) & 0xFF
if class_id == Pci.BASE_CLASS_NETWORK:
if subclass_id == Pci.CLASS_NETWORK_WIRELESS:
return u"WIRELESS"
else:
return u"NETWORK"
if class_id == Pci.BASE_CLASS_DISPLAY:
if subclass_id == Pci.CLASS_DISPLAY_VGA:
return u"VIDEO"
else:
return u"OTHER"
if class_id == Pci.BASE_CLASS_SERIAL \
and subclass_id == Pci.CLASS_SERIAL_USB:
return u"USB"
if class_id == Pci.BASE_CLASS_STORAGE:
if subclass_id == Pci.CLASS_STORAGE_SCSI:
return u"SCSI"
if subclass_id == Pci.CLASS_STORAGE_IDE:
return u"IDE"
if subclass_id == Pci.CLASS_STORAGE_FLOPPY:
return u"FLOPPY"
if subclass_id == Pci.CLASS_STORAGE_RAID:
return u"RAID"
if class_id == Pci.BASE_CLASS_COMMUNICATION \
and subclass_id == Pci.CLASS_COMMUNICATION_MODEM:
return u"MODEM"
if class_id == Pci.BASE_CLASS_INPUT \
and subclass_id == Pci.CLASS_INPUT_SCANNER:
return u"SCANNER"
if class_id == Pci.BASE_CLASS_MULTIMEDIA:
if subclass_id == Pci.CLASS_MULTIMEDIA_VIDEO:
return u"CAPTURE"
if subclass_id == Pci.CLASS_MULTIMEDIA_AUDIO \
or subclass_id == Pci.CLASS_MULTIMEDIA_AUDIO_DEVICE:
return u"AUDIO"
if class_id == Pci.BASE_CLASS_SERIAL \
and subclass_id == Pci.CLASS_SERIAL_FIREWIRE:
return u"FIREWIRE"
if class_id == Pci.BASE_CLASS_BRIDGE \
and (subclass_id == Pci.CLASS_BRIDGE_PCMCIA \
or subclass_id == Pci.CLASS_BRIDGE_CARDBUS):
return u"SOCKET"
if "TYPE" in self._environment and "INTERFACE" in self._environment:
interface_class, interface_subclass, interface_protocol = (
int(i) for i in self._environment["INTERFACE"].split("/"))
if interface_class == Usb.BASE_CLASS_AUDIO:
return u"AUDIO"
if interface_class == Usb.BASE_CLASS_PRINTER:
return u"PRINTER"
if interface_class == Usb.BASE_CLASS_STORAGE:
if interface_subclass == Usb.CLASS_STORAGE_FLOPPY:
return u"FLOPPY"
if interface_subclass == Usb.CLASS_STORAGE_SCSI:
return u"SCSI"
if interface_class == Usb.BASE_CLASS_VIDEO:
return u"CAPTURE"
if interface_class == Usb.BASE_CLASS_WIRELESS:
if interface_protocol == Usb.PROTOCOL_BLUETOOTH:
return u"BLUETOOTH"
else:
return u"WIRELESS"
if "KEY" in self._environment:
key = self._environment["KEY"].strip("=")
bitmask = get_bitmask(key)
for i in range(Input.KEY_Q, Input.KEY_P + 1):
if not test_bit(i, bitmask, self._bits):
break
else:
return u"KEYBOARD"
if test_bit(Input.BTN_TOUCH, bitmask, self._bits):
return u"TOUCH"
if test_bit(Input.BTN_MOUSE, bitmask, self._bits):
return u"MOUSE"
if "ID_TYPE" in self._environment:
id_type = self._environment["ID_TYPE"]
if id_type == "cd":
return u"CDROM"
if id_type == "disk":
return u"DISK"
if id_type == "video":
return u"VIDEO"
if "DEVTYPE" in self._environment:
devtype = self._environment["DEVTYPE"]
if devtype == "disk":
if "ID_CDROM" in self._environment:
return u"CDROM"
if "ID_DRIVE_FLOPPY" in self._environment:
return u"FLOPPY"
if devtype == "scsi_device":
match = SCSI_RE.match(self._environment.get("MODALIAS", ""))
type = int(match.group("type"), 16) if match else -1
# Check FLASH drives, see /lib/udev/rules.d/80-udisks.rules
if type in (0, 7, 14) \
and not any(d.driver == "rts_pstor" for d in self._stack):
return u"DISK"
if type == 1:
return u"TAPE"
if type == 2:
return u"PRINTER"
if type in (4, 5):
return u"CDROM"
if type == 6:
return u"SCANNER"
if type == 12:
return u"RAID"
if "DRIVER" in self._environment:
if self._environment["DRIVER"] == "floppy":
return u"FLOPPY"
if self.product:
return u"OTHER"
return None
@property
def driver(self):
if "DRIVER" in self._environment:
return self._environment["DRIVER"]
# Check parent device for driver
if self._stack:
parent = self._stack[-1]
if "DRIVER" in parent._environment:
return parent._environment["DRIVER"]
return None
@property
def path(self):
return self._environment.get("DEVPATH")
@property
def product_id(self):
# pci
match = PCI_RE.match(self._environment.get("MODALIAS", ""))
if match:
return int(match.group("product_id"), 16)
# usb
match = USB_RE.match(self._environment.get("MODALIAS", ""))
if match:
return int(match.group("product_id"), 16)
# pnp
match = PNP_RE.match(self._environment.get("MODALIAS", ""))
if match:
product_id = int(match.group("product_id"), 16)
# Ignore interrupt controllers
if product_id > 0x0100:
return product_id
return None
@property
def vendor_id(self):
# pci
match = PCI_RE.match(self._environment.get("MODALIAS", ""))
if match:
return int(match.group("vendor_id"), 16)
# usb
match = USB_RE.match(self._environment.get("MODALIAS", ""))
if match:
return int(match.group("vendor_id"), 16)
return None
@property
def subproduct_id(self):
if "PCI_SUBSYS_ID" in self._environment:
pci_subsys_id = self._environment["PCI_SUBSYS_ID"]
subvendor_id, subproduct_id = pci_subsys_id.split(":")
return int(subproduct_id, 16)
return None
@property
def subvendor_id(self):
if "PCI_SUBSYS_ID" in self._environment:
pci_subsys_id = self._environment["PCI_SUBSYS_ID"]
subvendor_id, subproduct_id = pci_subsys_id.split(":")
return int(subvendor_id, 16)
return None
@property
def product(self):
for element in ("NAME",
"RFKILL_NAME",
"POWER_SUPPLY_MODEL_NAME"):
if element in self._environment:
return self._environment[element].strip('"')
# disk
if self._environment.get("DEVTYPE") == "scsi_device":
for device in reversed(self._stack):
if device._environment.get("ID_BUS") == "usb":
return decode_id(device._environment["ID_MODEL_ENC"])
if self._environment.get("DEVTYPE") == "disk" \
and self._environment.get("ID_BUS") == "ata":
return decode_id(self._environment["ID_MODEL_ENC"])
# floppy
if self.driver == "floppy":
return u"Platform Device"
return None
@property
def vendor(self):
if "RFKILL_NAME" in self._environment:
return None
if "POWER_SUPPLY_MANUFACTURER" in self._environment:
return self._environment["POWER_SUPPLY_MANUFACTURER"]
# pnp
match = PNP_RE.match(self._environment.get("MODALIAS", ""))
if match:
return match.group("vendor_name")
# disk
if self._environment.get("DEVTYPE") == "scsi_device":
for device in reversed(self._stack):
if device._environment.get("ID_BUS") == "usb":
return decode_id(device._environment["ID_VENDOR_ENC"])
return None
class UdevadmParser:
"""Parser for the udevadm command."""
device_factory = UdevadmDevice
def __init__(self, stream, bits=None):
self.stream = stream
self.bits = bits
def _ignoreDevice(self, device):
# Ignore devices without bus information
if not device.bus:
return True
# Ignore devices without product information
if not device.product and device.product_id is None:
return True
# Ignore invalid subsystem information
if ((device.subproduct_id is None
and device.subvendor_id is not None)
or (device.subproduct_id is not None
and device.subvendor_id is None)):
return True
# Ignore ACPI devices
if device.bus == "acpi":
return True
return False
def getAttributes(self, path):
return {}
def run(self, result):
# Some attribute lines have a space character after the
# ':', others don't have it (see udevadm-info.c).
line_pattern = re.compile(r"(?P<key>[A-Z]):\s*(?P<value>.*)")
multi_pattern = re.compile(r"(?P<key>[^=]+)=(?P<value>.*)")
stack = []
output = self.stream.read()
for record in re.split("\n{2,}", output):
record = record.strip()
if not record:
continue
# Determine path and environment
path = None
element = None
environment = {}
for line in record.split("\n"):
line_match = line_pattern.match(line)
if not line_match:
if environment:
# Append to last environment element
environment[element] += line
continue
key = line_match.group("key")
value = line_match.group("value")
if key == "P":
path = value
elif key == "E":
key_match = multi_pattern.match(value)
if not key_match:
raise Exception(
"Device property not supported: %s" % value)
element = key_match.group("key")
environment[element] = key_match.group("value")
# Update stack
while stack:
if stack[-1].path + "/" in path:
break
stack.pop()
# Set default DEVPATH
environment.setdefault("DEVPATH", path)
device = self.device_factory(environment, self.bits, list(stack))
if not self._ignoreDevice(device):
result.addDevice(device)
stack.append(device)
def decode_id(id):
encoded_id = id.encode("utf-8")
decoded_id = encoded_id.decode("string-escape").decode("utf-8")
return decoded_id.strip()
|