Advertisement
Guest User

blah

a guest
Jan 16th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.56 KB | None | 0 0
  1. import os
  2. import urllib2
  3. import glob
  4. import subprocess
  5.  
  6. global HOME
  7. HOME = "https://pci-ids.ucw.cz/"
  8.  
  9. class Vendor:
  10.     """
  11.    Class for vendors. This is the top level class
  12.    for the devices belong to a specific vendor.
  13.    self.devices is the device dictionary
  14.    subdevices are in each device.
  15.    """
  16.     def __init__(self, vendorStr):
  17.         """
  18.        Class initializes with the raw line from pci.ids
  19.        Parsing takes place inside __init__
  20.        """
  21.         self.ID = vendorStr.split()[0]
  22.         self.name = vendorStr.replace("%s " % self.ID,"")
  23.         self.devices = {}
  24.  
  25.     def addDevice(self, deviceStr):
  26.         """
  27.        Adds a device to self.devices
  28.        takes the raw line from pci.ids
  29.        """
  30.         s = deviceStr.strip()
  31.         devID = s.split()[0]
  32.         if devID in self.devices:
  33.             pass
  34.         else:
  35.             self.devices[devID] = Device(deviceStr)
  36.  
  37.     def report(self):
  38.         print self.ID, self.name
  39.         for id, dev in self.devices.items():
  40.             dev.report()
  41.  
  42. class Device:
  43.     def __init__(self, deviceStr):
  44.         """
  45.        Class for each device.
  46.        Each vendor has its own devices dictionary.
  47.        """
  48.         s = deviceStr.strip()
  49.         self.ID = s.split()[0]
  50.         self.name = s.replace("%s  " % self.ID,"")
  51.         self.subdevices = {}
  52.  
  53.     def report(self):
  54.         print "\t%s\t%s" % (self.ID, self.name)
  55.         for subID, subdev in self.subdevices.items():
  56.             subdev.report()
  57.  
  58.     def addSubDevice(self, subDeviceStr):
  59.         """
  60.        Adds a subvendor, subdevice to device.
  61.        Uses raw line from pci.ids
  62.        """
  63.         s = subDeviceStr.strip()
  64.         spl = s.split()
  65.         subVendorID  = spl[0]
  66.         subDeviceID  = spl[1]
  67.         subDeviceName = s.split("  ")[-1]
  68.         devID = "%s:%s" % (subVendorID,subDeviceID)
  69.         self.subdevices[devID] = SubDevice(subVendorID,subDeviceID,subDeviceName)
  70.  
  71. class SubDevice:
  72.     """
  73.    Class for subdevices.
  74.    """
  75.     def __init__(self, vendor, device, name):
  76.         """
  77.        Class initializes with vendorid, deviceid and name
  78.        """
  79.         self.vendorID = vendor
  80.         self.deviceID = device
  81.         self.name = name
  82.  
  83.     def report(self):
  84.         print "\t\t%s\t%s\t%s" % (self.vendorID, self.deviceID,self.name)
  85.  
  86. class PCIIds:
  87.     """
  88.    Top class for all pci.ids entries.
  89.    All queries will be asked to this class.
  90.    PCIIds.vendors["0e11"].devices["0046"].subdevices["0e11:4091"].name  =  "Smart Array 6i"
  91.    """
  92.     def __init__(self):
  93.         """
  94.        Prepares the directories.
  95.        Checks local data file.
  96.        Tries to load from local, if not found, downloads from web
  97.        """
  98.         self.version = ""
  99.         self.date = ""
  100.         self.compressed = "pci.ids.bz2"
  101.         subprocess.call(['mkdir -p data'], shell=True)
  102.         self.vendors = {}
  103.         self.contents = None
  104.         self.loadLocal()
  105.         self.parse()
  106.  
  107.     def reportVendors(self):
  108.         """Reports the vendors
  109.        """
  110.         for vid, v in self.vendors.items():
  111.             print v.ID, v.name
  112.  
  113.     def report(self, vendor = None):
  114.         """
  115.        Reports everything for all vendors or a specific vendor
  116.        PCIIds.report()  reports everything
  117.        PCIIDs.report("0e11") reports only "Compaq Computer Corporation"
  118.        """
  119.         if vendor != None:
  120.             self.vendors[vendor].report()
  121.         else:
  122.             for vID, v in self.vendors.items():
  123.                 v.report()
  124.  
  125.     def findDate(self, content):
  126.         for l in content:
  127.             if l.find("Date:") > -1:
  128.                 return l.split()[-2].replace("-", "")
  129.         return None
  130.  
  131.     def parse(self):
  132.         if len(self.contents) < 1:
  133.             print "data/%s-pci.ids not found" % self.date
  134.         else:
  135.             vendorID = ""
  136.             deviceID = ""
  137.             for l in self.contents:
  138.                 if l[0] == "#":
  139.                     continue
  140.                 elif len(l.strip()) == 0:
  141.                     continue
  142.                 else:
  143.                     if l.find("\t\t") == 0:
  144.                         self.vendors[vendorID].devices[deviceID].addSubDevice(l)
  145.                     elif l.find("\t") == 0:
  146.                         deviceID = l.strip().split()[0]
  147.                         self.vendors[vendorID].addDevice(l)
  148.                     else:
  149.                         vendorID = l.split()[0]
  150.                         self.vendors[vendorID] = Vendor(l)
  151.  
  152.     def getLatest(self):
  153.         """
  154.        """
  155.         ver, date, url = self.latestVersion()
  156.         outfile = "data/%s-%s" % (date, self.compressed)
  157.         out = open(outfile, "w")
  158.         out.write(urllib2.urlopen(url).read())
  159.         out.close()
  160.         subprocess.call(['bzip2 -d %s' % outfile], shell=True)
  161.         self.version = ver
  162.         self.date = date
  163.         self.readLocal()
  164.  
  165.  
  166.     def readLocal(self):
  167.         """
  168.        Reads the local file
  169.        """
  170.         self.contents = open("data/pci.ids").readlines()
  171.         self.date = self.findDate(self.contents)
  172.  
  173.     def loadLocal(self):
  174.         """
  175.        Loads database from local. If there is no file,
  176.        it creates a new one from web
  177.        """
  178.         idsfile = glob.glob("data/pci.ids")
  179.         if len(idsfile) == 0:
  180.             self.getLatest()
  181.         else:
  182.             self.date = idsfile[0].split("/")[1].split("-")[0]
  183.             self.readLocal()                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
  184.  
  185.     def latestVersion(self):
  186.         """
  187.        Checks the latest version from web
  188.        """
  189.         webPage = urllib2.urlopen(HOME).readlines()
  190.         for line in webPage:
  191.              if line.find(self.compressed) > -1:
  192.                 for tag in line.split("<"):
  193.                     if tag.find(self.compressed) > -1:
  194.                         path = tag.split('"')[1]
  195.                         ver = path.split("/")[1]
  196.                         url = "%s%s" % (HOME, path)
  197.                         urlUncompressed  = url.replace(".bz2","")
  198.                         con = urllib2.urlopen(urlUncompressed)
  199.                         for i in range(10):
  200.                             l = con.readline()
  201.                             if l.find("Date:") > -1:
  202.                                 date = l.split()[-2].replace("-","")
  203.                                 break
  204.                         return (ver, date, "%s%s" % (HOME, path))
  205.                 break
  206.         return ""
  207.  
  208.  
  209. if __name__ == "__main__":
  210.     id = PCIIds()
  211.     #id.reportVendors()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement