Skip to content

Commit 1e8d14c

Browse files
committed
Merge pull request #3 from rdobson/lspci-nnmm
Adding code for parsing the output of lspci-nnmm.
2 parents 9d24021 + ee63f50 commit 1e8d14c

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

hwinfo/pci/lspci.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,16 @@ class LspciNParser(CommandParser):
3838
'pci_vendor_id',
3939
'pci_device_type_id',
4040
]
41+
42+
class LspciNNMMParser(CommandParser):
43+
"""Parser object for the output of lspci -nnmm"""
44+
45+
#02:00.1 "Ethernet controller [0200]" "Broadcom Corporation [14e4]" "NetXtreme II BCM5716 Gigabit Ethernet [163b]" -r20 "Dell [1028]" "Device [02a3]"
46+
47+
ITEM_REGEXS = [
48+
r'(?P<pci_device_bus_id>([0-9a-fA-F]{2}:[0-9a-fA-F]{2}\.[0-9a-fA-F]))\ "(?P<pci_device_type_name>[\w+\ \.\-]+)\ \[(?P<pci_device_type_id>[0-9a-fA-F]{4})\]"' \
49+
+ r'\ "(?P<pci_vendor_name>[\w+\ \.\-]+)\ \[(?P<pci_vendor_id>[0-9a-fA-F]{4})\]"\ "(?P<pci_device_name>[\w+\ \.\-]+)\ \[(?P<pci_device_id>[0-9a-fA-F]{4})\]"' \
50+
+ r'\ .*\ "(?P<pci_subvendor_name>[\w+\ \.\-]+)\ \[(?P<pci_subvendor_id>[0-9a-fA-F]{4})\]"\ "(?P<pci_subdevice_name>[\w+\ \.\-]+)\ \[(?P<pci_subdevice_id>[0-9a-fA-F]{4})\]'
51+
]
52+
53+
ITEM_SEPERATOR = "\n"

hwinfo/pci/tests/lspci_tests.py renamed to hwinfo/pci/tests/test_lspci.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,73 @@ def setUp(self):
105105
def test_parse_all_devices(self):
106106
recs = self.parser.parse_items()
107107
self.assertEqual(len(recs), 171)
108+
109+
110+
class TestSingleDeviceNNMMParse(unittest.TestCase):
111+
112+
SAMPLE_DATA = '02:00.0 "Ethernet controller [0200]" "Broadcom Corporation [14e4]" "NetXtreme II BCM5716 Gigabit Ethernet [163b]" -r20 "Dell [1028]" "Device [02a3]'
113+
114+
DEVICE_REC = {
115+
'pci_device_bus_id': '02:00.0',
116+
'pci_device_type_id': '0200',
117+
'pci_device_type_name': 'Ethernet controller',
118+
'pci_vendor_name': 'Broadcom Corporation',
119+
'pci_vendor_id': '14e4',
120+
'pci_device_id': '163b',
121+
'pci_device_name': 'NetXtreme II BCM5716 Gigabit Ethernet',
122+
'pci_subvendor_name': 'Dell',
123+
'pci_subvendor_id': '1028',
124+
'pci_subdevice_name': 'Device',
125+
'pci_subdevice_id': '02a3',
126+
}
127+
128+
def setUp(self):
129+
self.parser = LspciNNMMParser(self.SAMPLE_DATA)
130+
self.rec = self.parser.parse_items()[0]
131+
132+
def _assert_rec_key(self, key):
133+
self.assertEquals(self.rec[key], self.DEVICE_REC[key])
134+
135+
def test_pci_device_bus_id(self):
136+
self._assert_rec_key('pci_device_bus_id')
137+
138+
def test_pci_device_type_id(self):
139+
self._assert_rec_key('pci_device_type_id')
140+
141+
def test_pci_device_type_name(self):
142+
self._assert_rec_key('pci_device_type_name')
143+
144+
def test_pci_vendor_name(self):
145+
self._assert_rec_key('pci_vendor_name')
146+
147+
def test_pci_vendor_id(self):
148+
self._assert_rec_key('pci_vendor_id')
149+
150+
def test_pci_device_id(self):
151+
self._assert_rec_key('pci_device_id')
152+
153+
def test_pci_device_name(self):
154+
self._assert_rec_key('pci_device_name')
155+
156+
def test_pci_subvendor_name(self):
157+
self._assert_rec_key('pci_subvendor_name')
158+
159+
def test_pci_subdevice_name(self):
160+
self._assert_rec_key('pci_subdevice_name')
161+
162+
def test_pci_subdevice_id(self):
163+
self._assert_rec_key('pci_subdevice_id')
164+
165+
class TestMultiDeviceNNMMParse(unittest.TestCase):
166+
167+
SAMPLE_FILE = '%s/lspci-nnmm' % DATA_DIR
168+
169+
def setUp(self):
170+
fh = open(self.SAMPLE_FILE)
171+
data = fh.read()
172+
fh.close()
173+
self.parser = LspciNNMMParser(data)
174+
175+
def test_number_of_devices(self):
176+
recs = self.parser.parse_items()
177+
self.assertEqual(len(recs), 37)

0 commit comments

Comments
 (0)