removed debug print statement.
[pypcilib.git] / pypci.py
1 import os
2 try:
3     from pypciscan import get_devices
4 except:
5     def get_devices():
6         """ This is a replacement to the version in pypciscan library for 3.3 and lower bootcds 
7         that will help maintain backward compatibility.  This version has limitations wrt accuracy
8         that the library does not.  In particular it is limited to the output of
9         lspci and 'forces' all devices to appear on the '0000' domain, rather than
10         where they actually are."""
11
12         ret = {}
13         pci_cmd = os.popen("""/sbin/lspci -nvm | sed -e 's/\t/ /g' -e 's/ Class //' -e 's/^/"/' -e 's/$/"/' -e 's/$/,/' -e 's/^"",$/],[/'""", 'r')
14         pci_str = "[" + pci_cmd.read() + "]"
15         pci_list = eval(pci_str)
16
17         pci_devlist = []
18                 # convert each entry into a dict. and convert strings to ints.
19         for dev in pci_list:
20             rec = {}
21             for field in dev:
22                 s = field.split(":")
23                 if len(s) > 2:
24                     # There are two 'device' fields in the output. Append
25                     # 'addr' for the bus address, identified by the extra ':'.
26                     end=":".join(s[1:])
27                     key = s[0].lower() + "addr"
28                     value = end.strip()
29                 else:
30                     key = s[0].lower()
31                     value = int(s[1].strip(), 16)
32
33                 rec[key] = value
34
35             pci_devlist.append(rec)
36
37         ret = {}
38                 # convert this list of devices into the format expected by the
39                 # consumer of get_devices()
40         for dev in pci_devlist:
41             if 'device' not in dev:
42                 continue
43
44             if 'sdevice' in dev: subdev = dev['sdevice']
45             else: subdev = 0xffffffffL
46
47             if 'svendor' in dev: subvend = dev['svendor']
48             else: subvend = 0xffffffffL
49
50             key = "0000:%s" % dev['deviceaddr']
51             value = (dev['vendor'], dev['device'], subvend, subdev, dev['class'] << 8)
52             ret[key] = value
53
54         return  ret
55
56 import pypcimap