From 6e9f525216a3e6a21d96d8ab723b65114e333355 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sun, 2 Dec 2018 15:29:07 +0000 Subject: [PATCH] Ship local copy of hwdata module This is not really available over PIP Signed-off-by: Michael Tremer --- Makefile.am | 1 + requirements.txt | 1 - src/backend/fireinfo.py | 2 +- src/backend/hwdata.py | 223 ++++++++++++++++++++++++++++++++++++++++ src/web/fireinfo.py | 1 - 5 files changed, 225 insertions(+), 3 deletions(-) create mode 100644 src/backend/hwdata.py diff --git a/Makefile.am b/Makefile.am index d96b35b3..2cac0644 100644 --- a/Makefile.am +++ b/Makefile.am @@ -56,6 +56,7 @@ backend_PYTHON = \ src/backend/decorators.py \ src/backend/fireinfo.py \ src/backend/geoip.py \ + src/backend/hwdata.py \ src/backend/iuse.py \ src/backend/memcached.py \ src/backend/mirrors.py \ diff --git a/requirements.txt b/requirements.txt index da6d09ee..cab22856 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,7 +10,6 @@ idna==2.7 iso3166==0.9 ldap3==2.5.1 markdown2==2.3.6 -pciutils==2.3.7 phonenumbers==8.9.15 Pillow==5.3.0 psycopg2-binary==2.7.5 diff --git a/src/backend/fireinfo.py b/src/backend/fireinfo.py index b14edd91..4ca5c935 100644 --- a/src/backend/fireinfo.py +++ b/src/backend/fireinfo.py @@ -1,12 +1,12 @@ #!/usr/bin/python import datetime -import hwdata import iso3166 import logging import re from . import database +from . import hwdata from . import util from .misc import Object diff --git a/src/backend/hwdata.py b/src/backend/hwdata.py new file mode 100644 index 00000000..24a31c98 --- /dev/null +++ b/src/backend/hwdata.py @@ -0,0 +1,223 @@ +# +# Copyright (c) 1999--2012 Red Hat, Inc. +# +# This software is licensed to you under the GNU General Public License, +# version 2 (GPLv2). There is NO WARRANTY for this software, express or +# implied, including the implied warranties of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 +# along with this software; if not, see +# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. +# +# Red Hat trademarks are not licensed under GPLv2. No permission is +# granted to use or replicate Red Hat trademarks that are incorporated +# in this software or its documentation. +# + +""" Query hwdata database and return decription of vendor and/or device. """ + +# pylint: disable=misplaced-bare-raise + +class USB(object): + """ Interace to usb.ids from hwdata package """ + filename = '/usr/share/hwdata/usb.ids' + devices = None + + def __init__(self, filename=None): + """ Load pci.ids from file to internal data structure. + parameter 'filename' can specify location of this file + """ + if filename: + self.filename = filename + else: + self.filename = USB.filename + self.cache = 1 + + if self.cache and not USB.devices: + # parse usb.ids + USB.devices = {} + try: + f = open(self.filename, encoding='ISO8859-1') + except IOError: + return + lineno = 0 + vendor = None + device = None + for line in f.readlines(): + lineno += 1 + l = line.split() + if line.startswith('#'): + if line.startswith('# List of known device classes, subclasses and protocols'): + break # end of database of devices, rest is protocols, types etc. + else: + continue + elif len(l) == 0: + continue + elif line.startswith('\t\t'): + interface_id = l[0].lower() + if len(l) > 2: + interface_name = ' '.join(l[1:]) + else: + interface_name = '' + try: + USB.devices[vendor][1][device][0][interface_id] = interface_name + except TypeError: + sys.stderr.write("Unknown line at line {0} in {1}.\n".format(lineno, self.filename)) + elif line.startswith('\t'): + device = l[0].lower() + device_name = ' '.join(l[1:]) + USB.devices[vendor][1][device] = [device_name, {}] + else: + vendor = l[0].lower() + vendor_name = ' '.join(l[1:]) + if vendor not in USB.devices: + USB.devices[vendor] = [vendor_name, {}] + else: # this should not happen + USB.devices[vendor][0] = vendor_name + + def get_vendor(self, vendor): + """ Return description of vendor. Parameter is two byte code in hexa. + If vendor is unknown None is returned. + """ + vendor = vendor.lower() + if self.cache: + if vendor in USB.devices: + return USB.devices[vendor][0] + else: + return None + else: + raise # not implemented yet + + def get_device(self, vendor, device): + """ Return description of device. Parameters are two byte code variables in hexa. + If device is unknown None is returned. + """ + vendor = vendor.lower() + device = device.lower() + if self.cache: + if vendor in USB.devices: + if device in USB.devices[vendor][1]: + return USB.devices[vendor][1][device][0] + else: + return None + else: + return None + else: + raise # not implemented yet + +class PCI(object): + """ Interace to pci.ids from hwdata package """ + filename = '/usr/share/hwdata/pci.ids' + devices = None + + def __init__(self, filename=None): + """ Load pci.ids from file to internal data structure. + parameter 'filename' can specify location of this file + """ + if filename: + self.filename = filename + else: + self.filename = PCI.filename + self.cache = 1 + + if self.cache and not PCI.devices: + # parse pci.ids + PCI.devices = {} + try: + f = open(self.filename, encoding='ISO8859-1') + except IOError: + return + vendor = None + for line in f.readlines(): + l = line.split() + if line.startswith('#'): + continue + elif len(l) == 0: + continue + elif line.startswith('\t\t'): + continue + elif line.startswith('\t'): + device = l[0].lower() + device_name = ' '.join(l[1:]) + PCI.devices[vendor][1][device] = device_name + else: + vendor = l[0].lower() + vendor_name = ' '.join(l[1:]) + if not vendor in list(PCI.devices.keys()): + PCI.devices[vendor] = [vendor_name, {}] + else: # this should not happen + PCI.devices[vendor][0] = vendor_name + + def get_vendor(self, vendor): + """ Return description of vendor. Parameter is two byte code in hexa. + If vendor is unknown None is returned. + """ + vendor = vendor.lower() + if self.cache: + if vendor in list(PCI.devices.keys()): + return PCI.devices[vendor][0] + else: + return None + else: + raise # not implemented yet + + def get_device(self, vendor, device): + """ Return description of device. Parameters are two byte code variables in hexa. + If device is unknown None is returned. + """ + vendor = vendor.lower() + device = device.lower() + if self.cache: + if vendor in list(PCI.devices.keys()): + if device in list(PCI.devices[vendor][1].keys()): + return PCI.devices[vendor][1][device] + else: + return None + else: + return None + else: + raise # not implemented yet + +class PNP(object): + """ Interace to pnp.ids from hwdata package """ + filename = '/usr/share/hwdata/pnp.ids' + VENDORS = None + + def __init__(self, filename=None): + """ Load pnp.ids from file to internal data structure. + parameter 'filename' can specify location of this file + """ + if filename: + self.filename = filename + else: + self.filename = PNP.filename + self.cache = 1 + + if self.cache and not PNP.VENDORS: + # parse pnp.ids + PNP.VENDORS = {} + try: + f = open(self.filename, encoding='ISO8859-1') + except IOError: + return + for line in f.readlines(): + l = line.split() + if line.startswith('#'): + continue + elif len(l) == 0: + continue + else: + vendor_id = l[0].upper() + PNP.VENDORS[vendor_id] = ' '.join(l[1:]) + + def get_vendor(self, vendor_id): + """ Return description of vendor. Parameter is 3 character long id of vendor. + If vendor is unknown None is returned. + """ + vendor_id = vendor_id.upper() + if self.cache: + if vendor_id in list(PNP.VENDORS.keys()): + return PNP.VENDORS[vendor_id] + else: + return None + else: + raise # not implemented yet diff --git a/src/web/fireinfo.py b/src/web/fireinfo.py index cbb1518d..60fe3c63 100644 --- a/src/web/fireinfo.py +++ b/src/web/fireinfo.py @@ -1,7 +1,6 @@ #!/usr/bin/python import datetime -import hwdata import logging import re import json -- 2.39.2