]> git.ipfire.org Git - oddments/fireinfo.git/blob - fireinfo/device.py
Rewrite hypervisor detection.
[oddments/fireinfo.git] / fireinfo / device.py
1 #!/usr/bin/python
2 ###############################################################################
3 # #
4 # Fireinfo #
5 # Copyright (C) 2010, 2011 IPFire Team (www.ipfire.org) #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 import os.path
23
24 class Device(object):
25 """
26 This is an abstract class that represents all devices in the system.
27 Every single device has its own instance of this class.
28 """
29
30 def __init__(self, path):
31 """
32 Collect all information about the device by reading the
33 "uevent" file and parsing it.
34 """
35
36 # Save the path in /sys to the device.
37 self.path = path
38
39 # Datastructure to store information we read.
40 self._uevent = {}
41
42 # Open the uevent file and parse all lines.
43 with open(os.path.join(path, "uevent")) as f:
44 for line in f.readlines():
45 key, val = line.split("=")
46 self._uevent[key] = val.rstrip().lower()
47
48 @property
49 def driver(self):
50 """
51 Get the driver/kernel module that device is driven by or return None.
52 """
53 return self._uevent.get("DRIVER", None)
54
55
56 class PCIDevice(Device):
57 """
58 A class that represents all PCI (and PCIe) devices in a system.
59 """
60
61 subsystem = "pci"
62
63 @property
64 def model(self):
65 """
66 Return the PCI model id of this device.
67 """
68 return self._uevent['PCI_ID'].split(":")[1]
69
70 @property
71 def vendor(self):
72 """
73 Return the PCI vendor id of this device.
74 """
75 return self._uevent['PCI_ID'].split(":")[0]
76
77 @property
78 def deviceclass(self):
79 """
80 Return the PCI device class of this device.
81 """
82 return self._uevent['PCI_CLASS']
83
84 @property
85 def sub_vendor(self):
86 """
87 Return the PCI vendor sub id of this device.
88 """
89 return self._uevent["PCI_SUBSYS_ID"].split(":")[0]
90
91 @property
92 def sub_model(self):
93 """
94 Return the PCI model sub id of this device.
95 """
96 return self._uevent["PCI_SUBSYS_ID"].split(":")[1]
97
98
99 class USBDevice(Device):
100 """
101 A class that represents all USB devices in a system.
102 """
103
104 subsystem = "usb"
105
106 def pad(self, s):
107 """
108 A function to pad ids that have no leading zeroes.
109 """
110 while len(s) < 4:
111 s = "0"+s
112 return s
113
114 @property
115 def vendor(self):
116 """
117 Return the USB vendor id of this device.
118 """
119 return self.pad(self._uevent['PRODUCT'].split("/")[0])
120
121 @property
122 def model(self):
123 """
124 Return the USB model id of this device.
125 """
126 return self.pad(self._uevent['PRODUCT'].split("/")[1])
127
128 @property
129 def deviceclass(self):
130 """
131 Return the USB device class of this device.
132 """
133 return self._uevent.get("INTERFACE", None)