00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 from smolt_config import get_config_attr
00023
00024 class myVendor(object):
00025 def __init__(self):
00026 self.name = ""
00027 self.num = ""
00028 self.devices = {}
00029
00030 class myDevice(object):
00031 def __init__(self):
00032 self.name = ""
00033 self.num = ""
00034 self.subvendors = {}
00035 self.vendor = ""
00036
00037 class DeviceMap:
00038 vendors = {}
00039
00040 def __init__(self, bus='pci'):
00041 self.vendors['pci'] = self.device_map('pci')
00042 self.vendors['usb'] = self.device_map('usb')
00043
00044 def device_map(self, bus='pci'):
00045 import os
00046 HWDATA_DIRS = [ get_config_attr("HWDATA_DIR"), '/usr/share/hwdata','/usr/share/misc','/usr/share/' ]
00047 for hwd_file in HWDATA_DIRS:
00048 fn = "%s/%s.ids" % (hwd_file, bus)
00049 if os.path.isfile(fn + ".gz"):
00050 import gzip
00051 try:
00052 fo = gzip.open(fn + ".gz", 'r')
00053 break
00054 except IOError:
00055 pass
00056 else:
00057 try:
00058 fo = open(fn, 'r')
00059 break
00060 except IOError:
00061 pass
00062 else:
00063 raise Exception('Hardware data file not found. Please set the location HWDATA_DIR in config.py')
00064
00065
00066
00067 vendors = {}
00068 curvendor = None
00069 curdevice = None
00070 for line in fo.readlines():
00071 if line.startswith('#') or not line.strip():
00072 continue
00073 elif not line.startswith('\t'):
00074 curvendor = myVendor()
00075 try:
00076 curvendor.num = int(line[0:4], 16)
00077 except:
00078 continue
00079 curvendor.name = line[6:-1]
00080 vendors[curvendor.num] = curvendor
00081 continue
00082
00083 elif line.startswith('\t\t'):
00084 line = line.replace('\t', '')
00085 thisdev = myDevice()
00086 try:
00087 thisdev.vendor = int(line[0:4], 16)
00088 except:
00089 continue
00090 try:
00091 thisdev.num = int(line[5:9], 16)
00092 except:
00093 continue
00094 thisdev.name = line[11:-1]
00095 subvend = myVendor()
00096 try:
00097 subvend.num = thisdev.vendor
00098 except:
00099 continue
00100 subvend.name = ""
00101
00102 if not curdevice.subvendors.has_key(subvend.num):
00103 curdevice.subvendors[subvend.num] = subvend
00104 subvend.devices[thisdev.num] = thisdev
00105 else:
00106 subvend = curdevice.subvendors[subvend.num]
00107 subvend.devices[thisdev.num] = thisdev
00108
00109 continue
00110
00111 elif line.startswith('\t'):
00112 line = line.replace('\t', '')
00113 curdevice = myDevice()
00114 try:
00115 curdevice.num = int(line[0:4], 16)
00116 except:
00117 continue
00118 curdevice.name = line[6:-1]
00119 try:
00120 curdevice.vendor = int(curvendor.num)
00121 except:
00122 continue
00123 curvendor.devices[curdevice.num] = curdevice
00124 continue
00125 else:
00126 print line
00127 continue
00128 fo.close()
00129
00130
00131
00132 return vendors
00133
00134 def vendor(self, vend, subvend=None, alt='N/A', bus='pci'):
00135 try:
00136 vend = int(vend)
00137 except:
00138 return alt
00139 if vend == 0:
00140 return alt
00141 try:
00142 return self.vendors[bus][vend].devices[dev].subvendors[subvend].name + "a"
00143 except:
00144 try:
00145 return self.vendors[bus][vend].name
00146 except:
00147 return alt
00148
00149 def device(self, vend, dev, subvend=None, subdevice=None, alt='N/A', bus='pci'):
00150 try:
00151 vend = int(vend)
00152 except:
00153 pass
00154 try:
00155 dev = int(dev)
00156 except:
00157 pass
00158 try:
00159 subvend = int(subvend)
00160 except:
00161 pass
00162 try:
00163 subdevice = int(subdevice)
00164 except:
00165 pass
00166 try:
00167 return self.vendors[bus][vend].devices[dev].name
00168 except:
00169 try:
00170 self.vendors[bus][vend].devices[dev].name
00171 except:
00172 return alt
00173
00174 def subdevice(self, vend, dev, subvend, subdevice, alt='N/A', bus='pci'):
00175
00176 try:
00177 vend = int(vend)
00178 except:
00179 pass
00180 if vend == 0:
00181 return alt
00182 try:
00183 dev = int(dev)
00184 except:
00185 pass
00186 try:
00187 subvend = int(subvend)
00188 except:
00189 pass
00190 try:
00191 subdevice = int(subdevice)
00192 except:
00193 pass
00194 try:
00195 var = self.vendors[bus][vend].devices[dev].subvendors[subvend].devices[subdevice].name
00196 return var
00197 except:
00198 try:
00199 return self.vendors[bus][vend].devices[dev].name
00200 except:
00201 return alt