Skip to content

Commit 31e4b88

Browse files
authored
Merge pull request #17 from vivekkumac/improvements
python-hwinfo 0.1.7
2 parents c27c76b + a03424c commit 31e4b88

File tree

6 files changed

+73
-53
lines changed

6 files changed

+73
-53
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ For example, to parse a list of PCI devices:
3131

3232
from hwinfo.pci import PCIDevice
3333
from hwinfo.pci.lspci import LspciNNMMParser
34+
from subprocess import check_output
3435

3536
# Obtain the output of lspci -nnmm from somewhere
36-
lspci_output = exec_command('lspci -nnmm')
37+
lspci_output = check_output(["lspci", "-nnmm"])
3738

3839
# Parse the output using the LspciNNMMParser object
3940
parser = LspciNNMMParser(lspci_output)
@@ -84,11 +85,11 @@ For inspecting the hardware present on a local machine, execute:
8485

8586
Ethernet Controller Info:
8687

87-
+-------------------+-----------+------------------------------------+-----------+----------------+--------------+----------------+--------------+
88-
| vendor_name | vendor_id | device_name | device_id | subvendor_name | subvendor_id | subdevice_name | subdevice_id |
89-
+-------------------+-----------+------------------------------------+-----------+----------------+--------------+----------------+--------------+
90-
| Intel Corporation | 8086 | 82579LM Gigabit Network Connection | 1502 | Dell | 1028 | [Device 05d2] | 05d2 |
91-
+-------------------+-----------+------------------------------------+-----------+----------------+--------------+----------------+--------------+
88+
+---------------+-------------------+-----------+------------------------------------+-----------+----------------+--------------+----------------+--------------+
89+
| device_bus_id | vendor_name | vendor_id | device_name | device_id | subvendor_name | subvendor_id | subdevice_name | subdevice_id |
90+
+---------------+-------------------+-----------+------------------------------------+-----------+----------------+--------------+----------------+--------------+
91+
| 02:00.0 | Intel Corporation | 8086 | 82579LM Gigabit Network Connection | 1502 | Dell | 1028 | [Device 05d2] | 05d2 |
92+
+---------------+-------------------+-----------+------------------------------------+-----------+----------------+--------------+----------------+--------------+
9293

9394
Storage Controller Info:
9495

hwinfo/pci/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ def get_device_name(self):
4242
def get_device_id(self):
4343
return self._fmt(self.lookup_value('pci_device_id'))
4444

45+
def get_device_bus_id(self):
46+
return self._fmt(self.lookup_value('pci_device_bus_id'))
47+
4548
def get_vendor_name(self):
4649
return self._fmt(self.lookup_value('pci_vendor_name'))
4750

@@ -96,6 +99,7 @@ def get_info(self):
9699

97100
def get_rec(self):
98101
rec = {}
102+
rec['device_bus_id'] = self.get_device_bus_id()
99103
rec['vendor_name'] = self.get_vendor_name()
100104
rec['device_name'] = self.get_device_name()
101105
rec['vendor_id'] = self.get_vendor_id()

hwinfo/tools/inspector.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ def tabulate_recs(recs, header):
315315

316316
def tabulate_pci_recs(recs):
317317
header = [
318+
'device_bus_id',
318319
'vendor_name',
319320
'vendor_id',
320321
'device_name',
@@ -338,39 +339,38 @@ def tabulate_cpu_recs(recs):
338339
]
339340
return tabulate_recs(recs, header)
340341

341-
def print_unit(title, content):
342-
print "%s" % title
343-
print ""
344-
print content
345-
print ""
346-
342+
def create_unit(title, content):
343+
return "\n%s\n\n%s\n" % (str(title), str(content))
347344

348345
def validate_args(args):
349346
if args.machine != 'localhost':
350347
if not args.username or not args.password:
351348
print "Error: you must specify a username and password to query a remote machine."
352349
sys.exit(1)
353350

354-
def print_system_info(host, options):
351+
def system_info(host, options):
352+
info = []
355353

356354
if 'bios' in options:
357-
print_unit("Bios Info:", rec_to_table(host.get_info()))
355+
info.append(create_unit("Bios Info:", rec_to_table(host.get_info())))
358356

359357
if 'cpu' in options:
360-
print_unit("CPU Info:", tabulate_cpu_recs(host.get_cpu_info()))
358+
info.append(create_unit("CPU Info:", tabulate_cpu_recs(host.get_cpu_info())))
361359

362360
if 'nic' in options:
363361
devices = pci_filter_for_nics(host.get_pci_devices())
364-
print_unit("Ethernet Controller Info:", tabulate_pci_recs([dev.get_rec() for dev in devices]))
362+
info.append(create_unit("Ethernet Controller Info:", tabulate_pci_recs([dev.get_rec() for dev in devices])))
365363

366364
if 'storage' in options:
367365
devices = pci_filter_for_storage(host.get_pci_devices())
368-
print_unit("Storage Controller Info:", tabulate_pci_recs([dev.get_rec() for dev in devices]))
366+
info.append(create_unit("Storage Controller Info:", tabulate_pci_recs([dev.get_rec() for dev in devices])))
369367

370368
if 'gpu' in options:
371369
devices = pci_filter_for_gpu(host.get_pci_devices())
372370
if devices:
373-
print_unit("GPU Info:", tabulate_pci_recs([dev.get_rec() for dev in devices]))
371+
info.append(create_unit("GPU Info:", tabulate_pci_recs([dev.get_rec() for dev in devices])))
372+
373+
return "".join(info).strip()
374374

375375
def export_system_info(host, options):
376376
rec = {}
@@ -393,7 +393,7 @@ def export_system_info(host, options):
393393
devices = pci_filter_for_gpu(host.get_pci_devices())
394394
rec["gpus"] = [dev.get_rec() for dev in devices]
395395

396-
print json.dumps(rec, indent=4, separators=(',', ': '))
396+
return json.dumps(rec, indent=4, separators=(',', ': '))
397397

398398
def main():
399399
"""Entry Point"""
@@ -429,6 +429,6 @@ def main():
429429
options = filter_choices
430430

431431
if args.export:
432-
export_system_info(host, options)
432+
print export_system_info(host, options)
433433
else:
434-
print_system_info(host, options)
434+
print system_info(host, options)

0 commit comments

Comments
 (0)