Still needed...
[pypcilib.git] / pypci.py
1 import os
2
3 def get_devices():
4     """ This is a replacement to the pypciscan library."""
5
6     ret = {}
7     pci_cmd = os.popen("""/sbin/lspci -nvm | sed -e 's/\t/ /g' -e 's/ Class / /' -e 's/^/"/' -e 's/$/"/' -e 's/$/,/' -e 's/^"",$/],[/'""", 'r')
8     pci_str = "[" + pci_cmd.read() + "]"
9     pci_list = eval(pci_str)
10
11     pci_devlist = []
12     # convert each entry into a dict. and convert strings to ints.
13     for dev in pci_list:
14         rec = {}
15         for field in dev:
16             s = field.split(":")
17             if len(s) > 2:
18                 # There are two 'device' fields in the output. Append
19                 # 'addr' for the bus address, identified by the extra ':'.
20                 end=":".join(s[1:])
21                 key = s[0].lower() + "addr"
22                 value = end.strip()
23             else:
24                 key = s[0].lower()
25                 value = int(s[1].strip(), 16)
26
27             rec[key] = value
28
29         pci_devlist.append(rec)
30
31     ret = {}
32     # convert this list of devices into the format expected by the
33     # consumer of get_devices()
34     for dev in pci_devlist:
35         if 'deviceaddr' not in dev:
36             continue
37
38         if 'sdevice' in dev: subdev = dev['sdevice']
39         else: subdev = 0xffffffffL
40
41         if 'svendor' in dev: subvend = dev['svendor']
42         else: subvend = 0xffffffffL
43
44         if 'progif' in dev: progif = dev['progif']
45         else: progif = 0
46
47         value = (dev['vendor'], dev['device'], subvend, subdev, dev['class'] << 8 | progif)
48         ret[dev['deviceaddr']] = value
49
50     return  ret
51
52 import pypcimap