Add scripts to create myops-getqueryview:
[myops.git] / web / collect / client / sysinfo / pypcimap.py
1 #!/usr/bin/python -tt
2 # Copyright 2007 The Trustees of Princeton University
3 # Author: Daniel Hokka Zakrisson
4 # $Id$
5 # vim:ts=4:expandtab
6
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met: 
10
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 #       
14 # * Redistributions in binary form must reproduce the above
15 # copyright notice, this list of conditions and the following
16 # disclaimer in the documentation and/or other materials provided
17 # with the distribution.
18 #       
19 # * Neither the name of the copyright holder nor the names of its
20 # contributors may be used to endorse or promote products derived
21 # from this software without specific prior written permission.
22 #       
23 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PRINCETON
27 # UNIVERSITY OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
30 # OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
33 # WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 # POSSIBILITY OF SUCH DAMAGE. 
35
36 import os
37 import re
38
39 # These are modules which are only returned if no other driver is available
40 greylist = ["ata_generic", "eepro100", "8139cp"]
41
42 class PCIMap:
43     """Encapsulates modules.pcimap"""
44     def __init__(self, filename):
45         self.list = []
46         self.read(filename)
47     def get(self, tuple):
48         """Returns a list of candidate modules for the PCI device specified in tuple"""
49         ret = []
50         for i in self.list:
51             if ((i[1] == tuple[0] or i[1] == 0xffffffffL) and
52                 (i[2] == tuple[1] or i[2] == 0xffffffffL) and
53                 (i[3] == tuple[2] or i[3] == 0xffffffffL) and
54                 (i[4] == tuple[3] or i[4] == 0xffffffffL) and
55                 (i[5] == (tuple[4] & i[6]))):
56                 ret.append(i[0])
57         for i in greylist:
58             if i in ret and len(ret) > 1:
59                 ret.remove(i)
60         return ret
61     def add(self, list):
62         # FIXME: check values
63         self.list.append(list)
64     def read(self, filename):
65         f = file(filename)
66         pattern = re.compile("(\\S+)\\s+0x([0-9A-Fa-f]+)\\s0x([0-9A-Fa-f]+)\\s0x([0-9A-Fa-f]+)\\s0x([0-9A-Fa-f]+)\\s0x([0-9A-Fa-f]+)\\s0x([0-9A-Fa-f]+)\\s0x([0-9A-Fa-f]+)\\n")
67         while True:
68             line = f.readline()
69             if line == "":
70                 break
71             if line[0] == '#' or line[0] == '\n':
72                 continue
73             match = pattern.match(line)
74             if not match:
75                 continue
76             self.add([match.group(1),
77                 int(match.group(2), 16),
78                 int(match.group(3), 16),
79                 int(match.group(4), 16),
80                 int(match.group(5), 16),
81                 int(match.group(6), 16),
82                 int(match.group(7), 16),
83                 int(match.group(8), 16)])
84         f.close()