yet another attempt
[pypcilib.git] / pypci.py
1 # Copyright 2008 The Trustees of Princeton University
2 # Author: Daniel Hokka Zakrisson
3 # $Id$
4 # vim:ts=4:expandtab
5
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met: 
9
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 #       
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 #       
18 # * Neither the name of the copyright holder nor the names of its
19 # contributors may be used to endorse or promote products derived
20 # from this software without specific prior written permission.
21 #       
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PRINCETON
26 # UNIVERSITY OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 # OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
32 # WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 # POSSIBILITY OF SUCH DAMAGE. 
34
35 PCI_BASE_CLASS_NETWORK=0x02L
36 PCI_BASE_CLASS_STORAGE=0x01L
37 PCI_ANY=0xffffffffL
38
39 def get_devices():
40     """ This is a replacement to the pypciscan library."""
41     import os
42     pci_cmd = os.popen("""/sbin/lspci -nvm | sed -e 's/\t/ /g' -e 's/ Class / /' -e 's/^/"/' -e 's/$/"/' -e 's/$/,/' -e 's/^"",$/],[/'""", 'r')
43     pci_str = "[" + pci_cmd.read() + "]"
44     pci_list = eval(pci_str)
45
46     pci_devlist = []
47     # convert each entry into a dict. and convert strings to ints.
48     for dev in pci_list:
49         rec = {}
50         for field in dev:
51             s = field.split(":")
52             if len(s) > 2:
53                 # There are two 'device' fields in the output. Append
54                 # 'addr' for the bus address, identified by the extra ':'.
55                 end=":".join(s[1:])
56                 value = end.strip()
57                 key = s[0].lower() + "addr"
58             else:
59                 value = int(s[1].strip(), 16)
60                 key = s[0].lower()
61
62             rec[key] = value
63
64         pci_devlist.append(rec)
65
66     ret = {}
67     # convert this list of devices into the format expected by the
68     # consumer of get_devices()
69     for dev in pci_devlist:
70         if 'deviceaddr' not in dev:
71             continue
72
73         subdev = dev.get('sdevice',PCI_ANY)
74         subvend = dev.get('svendor',PCI_ANY)
75         progif = dev.get('progif',0)
76
77         value = (dev['vendor'], dev['device'], subvend, subdev, dev['class'] << 8 | progif)
78         ret[dev['deviceaddr']] = value
79
80     return  ret
81
82 # for convenience for the clients of pypci
83 import pypcimap