diff --git a/hwinfo/host/dmidecode.py b/hwinfo/host/dmidecode.py index a791903..0ce9a94 100644 --- a/hwinfo/host/dmidecode.py +++ b/hwinfo/host/dmidecode.py @@ -19,5 +19,3 @@ class DmidecodeParser(CommandParser): # Processor Info r'Processor\ Information\n\tSocket\ Designation:\ (?P.*)\n', ] - - diff --git a/hwinfo/pci/__init__.py b/hwinfo/pci/__init__.py index 9590cfe..0e98cd7 100644 --- a/hwinfo/pci/__init__.py +++ b/hwinfo/pci/__init__.py @@ -10,17 +10,14 @@ def __init__(self, record): def lookup_value(self, k): if k in self.rec: return self.rec[k] - else: - return None + return None def _fmt(self, value, wrap=None): if not value: return self.NONE_VALUE - else: - if wrap: - return "%s%s%s" % (wrap, value, wrap) - else: - return value + if wrap: + return "%s%s%s" % (wrap, value, wrap) + return value def get_device_name(self): wrap = None @@ -35,8 +32,7 @@ def get_device_name(self): # If the input has come from lspci, this is the value for # not being able to find a key in the pciids db. return '[Device %s]' % self.get_device_id() - else: - return self._fmt(name, wrap) + return self._fmt(name, wrap) def get_device_id(self): @@ -64,8 +60,7 @@ def get_subdevice_name(self): # If the input has come from lspci, this is the value for # not being able to find a key in the pciids db. return '[Device %s]' % self.get_subdevice_id() - else: - return self._fmt(name, wrap) + return self._fmt(name, wrap) def get_subdevice_id(self): return self._fmt(self.lookup_value('pci_subdevice_id')) @@ -88,14 +83,13 @@ def get_pci_class(self): return self._fmt(self.lookup_value('pci_device_class')) def is_subdevice(self): - return self.lookup_value('pci_subvendor_id') and self.lookup_value('pci_subdevice_id') or self.lookup_value('pci_device_sub_string') + return self.lookup_value('pci_subdevice_id') if self.lookup_value('pci_subvendor_id') else self.lookup_value('pci_device_sub_string') def get_info(self): if self.is_subdevice(): return "%s %s (%s %s)" % (self.get_subvendor_name(), self.get_subdevice_name(), self.get_vendor_name(), self.get_device_name()) - else: - return "%s %s" % (self.get_vendor_name(), self.get_device_name()) + return "%s %s" % (self.get_vendor_name(), self.get_device_name()) def get_rec(self): rec = {} diff --git a/hwinfo/pci/biosdevname.py b/hwinfo/pci/biosdevname.py index e7cba4d..4404a33 100644 --- a/hwinfo/pci/biosdevname.py +++ b/hwinfo/pci/biosdevname.py @@ -13,5 +13,3 @@ class BiosdevnameDParser(CommandParser): r'Driver\ version:\ (?P.*)\n', r'Firmware\ version:\ (?P.*)\n', ] - - diff --git a/hwinfo/pci/tests/test_lspci.py b/hwinfo/pci/tests/test_lspci.py index faae49d..d1547f4 100644 --- a/hwinfo/pci/tests/test_lspci.py +++ b/hwinfo/pci/tests/test_lspci.py @@ -27,7 +27,7 @@ def setUp(self): self.parser = LspciVVParser(data) def _assert_rec_key(self, rec, key): - self.assertEquals(rec[key], self.DEVICE_REC[key]) + self.assertEqual(rec[key], self.DEVICE_REC[key]) def test_pci_device_string(self): rec = self.parser.parse_items().pop() @@ -43,11 +43,11 @@ def test_pci_device_class_name(self): def test_pci_device_sub_string(self): rec = self.parser.parse_items().pop() - self._assert_rec_key(rec, 'pci_device_sub_string') + self._assert_rec_key(rec, 'pci_device_sub_string') def test_pci_device_vpd_product_name(self): rec = self.parser.parse_items().pop() - self._assert_rec_key(rec, 'pci_device_vpd_product_name') + self._assert_rec_key(rec, 'pci_device_vpd_product_name') class TestMultiDeviceVVParse(unittest.TestCase): @@ -64,7 +64,7 @@ def test_parse_all_devices(self): self.assertEqual(len(recs), 58) found = False for rec in recs: - print rec + print(rec) if rec['pci_device_bus_id'] == '02:00.0': self.assertEqual(rec['pci_device_class_name'], 'VGA compatible controller') found = True @@ -86,7 +86,7 @@ def setUp(self): self.rec = self.parser.parse_items().pop() def _assert_rec_key(self, key): - self.assertEquals(self.rec[key], self.DEVICE_REC[key]) + self.assertEqual(self.rec[key], self.DEVICE_REC[key]) def test_pci_device_bus_id(self): self._assert_rec_key('pci_device_bus_id') @@ -138,7 +138,7 @@ def setUp(self): self.rec = self.parser.parse_items()[0] def _assert_rec_key(self, key): - self.assertEquals(self.rec[key], self.DEVICE_REC[key]) + self.assertEqual(self.rec[key], self.DEVICE_REC[key]) def test_pci_device_bus_id(self): self._assert_rec_key('pci_device_bus_id') diff --git a/hwinfo/tools/inspector.py b/hwinfo/tools/inspector.py index fe71687..fbf8374 100644 --- a/hwinfo/tools/inspector.py +++ b/hwinfo/tools/inspector.py @@ -1,7 +1,7 @@ #!/usr/bin/env python +from __future__ import print_function from argparse import ArgumentParser -import paramiko import subprocess import os import sys @@ -9,6 +9,7 @@ import tempfile import shutil import json +import paramiko from prettytable import PrettyTable @@ -39,10 +40,10 @@ def local_command(cmd): process = subprocess.Popen(cmdstr, stdout=subprocess.PIPE, shell=True) stdout, stderr = process.communicate() if process.returncode == 0: - return str(stdout).strip() + return (stdout.decode('utf-8')).strip() else: - print "RC: %s" % process.returncode - print stdout + print("RC: %s" % process.returncode) + print(stdout) raise Exception("stderr: %s" % str(stderr)) def find_in_tarball(tarball, filename): @@ -121,8 +122,7 @@ def is_remote(self): def exec_command(self, cmd): if self.is_remote(): return remote_command(self.client, cmd) - else: - return local_command(cmd) + return local_command(cmd) def get_lspci_data(self): return self.exec_command(['lspci', '-nnmm']) @@ -160,7 +160,7 @@ def get_info(self): try: os_rec = self.get_os_info() - for k, v in os_rec.iteritems(): + for k, v in os_rec.items(): rec[k] = v except Exception: #Ignore failures. Only supports XS right now. @@ -198,13 +198,13 @@ def combine_recs(rec_list, key): for rec in rec_list: rec_key = rec[key] if rec_key in final_recs: - for k, v in rec.iteritems(): + for k, v in rec.items(): if k in final_recs[rec_key] and final_recs[rec_key][k] != v: raise Exception("Mis-match for key '%s'" % k) final_recs[rec_key][k] = v else: final_recs[rec_key] = rec - return final_recs.values() + return list(final_recs.values()) class HostFromLogs(Host): @@ -234,7 +234,7 @@ def get_pci_devices(self): return devs except FileNotFound: # Fall back to looking for the file lspci-vv.out - print "***lspci-nnm.out found. Falling back to looking for lspci-vv.out and lspci-n.out.***" + print("***lspci-nnm.out found. Falling back to looking for lspci-vv.out and lspci-n.out.***") lspci_vv_recs = parse_data(LspciVVParser, self._load_from_file('lspci-vv.out')) lspci_n_recs = parse_data(LspciNParser, self._load_from_file('lspci-n.out')) all_recs = lspci_vv_recs + lspci_n_recs @@ -273,9 +273,8 @@ def _load_from_file(self, filename): """Find filename in tar, and load it""" if filename in self.fdata: return self.fdata[filename] - else: - filepath = find_in_tarball(self.tarloc, filename) - return read_from_tarball(self.tarloc, filepath) + filepath = find_in_tarball(self.tarloc, filename) + return read_from_tarball(self.tarloc, filepath) def pci_filter(devices, types): res = [] @@ -302,7 +301,7 @@ def rec_to_table(rec): table = PrettyTable(["Key", "Value"]) table.align['Key'] = 'l' table.align['Value'] = 'l' - for k, v in rec.iteritems(): + for k, v in rec.items(): table.add_row([k, v]) return table @@ -345,7 +344,7 @@ def create_unit(title, content): def validate_args(args): if args.machine != 'localhost': if not args.username or not args.password: - print "Error: you must specify a username and password to query a remote machine." + print("Error: you must specify a username and password to query a remote machine.") sys.exit(1) def system_info(host, options): @@ -429,6 +428,6 @@ def main(): options = filter_choices if args.export: - print export_system_info(host, options) + print(export_system_info(host, options)) else: - print system_info(host, options) + print(system_info(host, options)) diff --git a/hwinfo/tools/tests/test_inspector.py b/hwinfo/tools/tests/test_inspector.py index 125d88c..9763e0e 100644 --- a/hwinfo/tools/tests/test_inspector.py +++ b/hwinfo/tools/tests/test_inspector.py @@ -2,7 +2,10 @@ import mock import sys from mock import patch -from StringIO import StringIO +if sys.version_info[0] == 2: + from StringIO import StringIO +else: + from io import StringIO from hwinfo.tools import inspector import dummy_data @@ -134,14 +137,14 @@ def test_ssh_connect_error(self, ssh_client_cls): client.exec_command.return_value = self.stdout, self.stdin, StringIO("Error") with self.assertRaises(Exception) as context: inspector.remote_command(client, 'ls') - self.assertEqual(context.exception.message, "stderr: ['Error']") + self.assertEqual(context.exception.args[0], "stderr: ['Error']") class LocalCommandTests(unittest.TestCase): @patch('subprocess.Popen') def test_local_call(self, mock_popen_cls): mprocess =mock_popen_cls.return_value = mock.MagicMock() - mprocess.communicate.return_value = 'test', None + mprocess.communicate.return_value = ('test').encode(), None mprocess.returncode = 0 stdout = inspector.local_command("echo 'test'") self.assertEqual(stdout, 'test') @@ -153,7 +156,7 @@ def test_local_call_error(self, mock_popen_cls): mprocess.returncode = 1 with self.assertRaises(Exception) as context: stdout = inspector.local_command("echo 'test'") - self.assertEqual(context.exception.message, "stderr: my error") + self.assertEqual(context.exception.args[0], "stderr: my error") class PCIFilterTests(unittest.TestCase): @@ -180,7 +183,7 @@ def test_pci_filter_match_all(self): def test_pci_filter_match_two(self): devs = inspector.pci_filter(self.devices, ['02']) for dev in devs: - print dev.get_pci_class() + print(dev.get_pci_class()) self.assertEqual(len(devs), 2) def test_pci_filter_match_one(self): @@ -333,7 +336,7 @@ def test_colliding_values(self): ] with self.assertRaises(Exception) as context: combined_recs = inspector.combine_recs(recs, 'name') - self.assertEqual(context.exception.message, "Mis-match for key 'valuea'") + self.assertEqual(context.exception.args[0], "Mis-match for key 'valuea'") class CLITests(unittest.TestCase): diff --git a/hwinfo/util/__init__.py b/hwinfo/util/__init__.py index 100a2bf..2f8cc8b 100644 --- a/hwinfo/util/__init__.py +++ b/hwinfo/util/__init__.py @@ -12,7 +12,7 @@ def combine_dicts(recs): new_rec = {} for rec in recs: - for k, v in rec.iteritems(): + for k, v in rec.items(): if k in new_rec: new_rec[k] = "%s, %s" % (new_rec[k], v) else: @@ -58,12 +58,11 @@ def parse_item(self, item): def parse_items(self): if not self.ITEM_SEPERATOR: return [self.parse_item(self.DATA)] - else: - recs = [] - for data in self.DATA.decode().split(self.ITEM_SEPERATOR): - rec = self.parse_item(data) - recs.append(rec) - return recs + recs = [] + for data in str(self.DATA).split(self.ITEM_SEPERATOR): + rec = self.parse_item(data) + recs.append(rec) + return recs def parse(self): if self.ITEM_SEPERATOR: diff --git a/hwinfo/util/tests/parser_tests.py b/hwinfo/util/tests/parser_tests.py index e513390..62ebf0e 100644 --- a/hwinfo/util/tests/parser_tests.py +++ b/hwinfo/util/tests/parser_tests.py @@ -7,10 +7,10 @@ class TestCommandParser(unittest.TestCase): def test_parse_item(self): data = 'one two three four: 845 six' - cp = CommandParser() - cp.ITEM_REGEXS = [r'four:\ (?P\w+)'] - rec = cp.parse_item(data) - self.assertEquals(rec['num'], '845') + cp = CommandParser() + cp.ITEM_REGEXS = [r'four:\ (?P\w+)'] + rec = cp.parse_item(data) + self.assertEqual(rec['num'], '845') def test_parse_items(self): data = """ @@ -34,10 +34,10 @@ def test_parse_items(self): RX bytes:3684827 (3.6 MB) TX bytes:3684827 (3.6 MB) """ regexs = [r'Link encap:(?P[\w]+)'] - cp = CommandParser(data, regexs, seperator='\n\n') - recs = cp.parse_items() - to_match = ['Ethernet', 'Local'] - for rec in recs: + cp = CommandParser(data, regexs, seperator='\n\n') + recs = cp.parse_items() + to_match = ['Ethernet', 'Local'] + for rec in recs: val = rec['encap'] to_match.remove(val) diff --git a/tox.ini b/tox.ini index 1f4be45..43305fa 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist=py27,lint +envlist=py27,py37,lint [testenv] deps=