From ee63f50648fa520fd8bd60b24c16296ef4405b44 Mon Sep 17 00:00:00 2001 From: Rob Dobson Date: Sat, 12 Jul 2014 18:44:44 +0100 Subject: [PATCH] Adding code for parsing the output of lspci-nnmm. This is going to be the most reliable way to get correct info off a host. Signed-off-by: Rob Dobson --- hwinfo/pci/lspci.py | 13 ++++ .../tests/{lspci_tests.py => test_lspci.py} | 70 +++++++++++++++++++ 2 files changed, 83 insertions(+) rename hwinfo/pci/tests/{lspci_tests.py => test_lspci.py} (59%) diff --git a/hwinfo/pci/lspci.py b/hwinfo/pci/lspci.py index 21b29f1..a6db0e8 100644 --- a/hwinfo/pci/lspci.py +++ b/hwinfo/pci/lspci.py @@ -38,3 +38,16 @@ class LspciNParser(CommandParser): 'pci_vendor_id', 'pci_device_type_id', ] + +class LspciNNMMParser(CommandParser): + """Parser object for the output of lspci -nnmm""" + + #02:00.1 "Ethernet controller [0200]" "Broadcom Corporation [14e4]" "NetXtreme II BCM5716 Gigabit Ethernet [163b]" -r20 "Dell [1028]" "Device [02a3]" + + ITEM_REGEXS = [ + r'(?P([0-9a-fA-F]{2}:[0-9a-fA-F]{2}\.[0-9a-fA-F]))\ "(?P[\w+\ \.\-]+)\ \[(?P[0-9a-fA-F]{4})\]"' \ + + r'\ "(?P[\w+\ \.\-]+)\ \[(?P[0-9a-fA-F]{4})\]"\ "(?P[\w+\ \.\-]+)\ \[(?P[0-9a-fA-F]{4})\]"' \ + + r'\ .*\ "(?P[\w+\ \.\-]+)\ \[(?P[0-9a-fA-F]{4})\]"\ "(?P[\w+\ \.\-]+)\ \[(?P[0-9a-fA-F]{4})\]' + ] + + ITEM_SEPERATOR = "\n" diff --git a/hwinfo/pci/tests/lspci_tests.py b/hwinfo/pci/tests/test_lspci.py similarity index 59% rename from hwinfo/pci/tests/lspci_tests.py rename to hwinfo/pci/tests/test_lspci.py index b7cc520..78d9afa 100644 --- a/hwinfo/pci/tests/lspci_tests.py +++ b/hwinfo/pci/tests/test_lspci.py @@ -105,3 +105,73 @@ def setUp(self): def test_parse_all_devices(self): recs = self.parser.parse_items() self.assertEqual(len(recs), 171) + + +class TestSingleDeviceNNMMParse(unittest.TestCase): + + SAMPLE_DATA = '02:00.0 "Ethernet controller [0200]" "Broadcom Corporation [14e4]" "NetXtreme II BCM5716 Gigabit Ethernet [163b]" -r20 "Dell [1028]" "Device [02a3]' + + DEVICE_REC = { + 'pci_device_bus_id': '02:00.0', + 'pci_device_type_id': '0200', + 'pci_device_type_name': 'Ethernet controller', + 'pci_vendor_name': 'Broadcom Corporation', + 'pci_vendor_id': '14e4', + 'pci_device_id': '163b', + 'pci_device_name': 'NetXtreme II BCM5716 Gigabit Ethernet', + 'pci_subvendor_name': 'Dell', + 'pci_subvendor_id': '1028', + 'pci_subdevice_name': 'Device', + 'pci_subdevice_id': '02a3', + } + + def setUp(self): + self.parser = LspciNNMMParser(self.SAMPLE_DATA) + self.rec = self.parser.parse_items()[0] + + def _assert_rec_key(self, key): + self.assertEquals(self.rec[key], self.DEVICE_REC[key]) + + def test_pci_device_bus_id(self): + self._assert_rec_key('pci_device_bus_id') + + def test_pci_device_type_id(self): + self._assert_rec_key('pci_device_type_id') + + def test_pci_device_type_name(self): + self._assert_rec_key('pci_device_type_name') + + def test_pci_vendor_name(self): + self._assert_rec_key('pci_vendor_name') + + def test_pci_vendor_id(self): + self._assert_rec_key('pci_vendor_id') + + def test_pci_device_id(self): + self._assert_rec_key('pci_device_id') + + def test_pci_device_name(self): + self._assert_rec_key('pci_device_name') + + def test_pci_subvendor_name(self): + self._assert_rec_key('pci_subvendor_name') + + def test_pci_subdevice_name(self): + self._assert_rec_key('pci_subdevice_name') + + def test_pci_subdevice_id(self): + self._assert_rec_key('pci_subdevice_id') + +class TestMultiDeviceNNMMParse(unittest.TestCase): + + SAMPLE_FILE = '%s/lspci-nnmm' % DATA_DIR + + def setUp(self): + fh = open(self.SAMPLE_FILE) + data = fh.read() + fh.close() + self.parser = LspciNNMMParser(data) + + def test_number_of_devices(self): + recs = self.parser.parse_items() + self.assertEqual(len(recs), 37)