Skip to content

CP-41972: Update HWinfo to support python3 #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: python
env:
- TOXENV=py27
- TOXENV=py36

install:
- pip install tox coveralls
Expand Down
16 changes: 8 additions & 8 deletions hwinfo/pci/tests/test_lspci.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -43,12 +43,12 @@ 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):

SAMPLE_DEVICE_FILE = "%s/lspci_vv" % DATA_DIR
Expand All @@ -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
Expand All @@ -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')
Expand Down Expand Up @@ -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')
Expand Down
26 changes: 14 additions & 12 deletions hwinfo/tools/inspector.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

from argparse import ArgumentParser
import paramiko
Expand Down Expand Up @@ -39,10 +39,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().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):
Expand Down Expand Up @@ -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.
Expand All @@ -170,6 +170,8 @@ def get_info(self):

def get_cpu_info(self):
data = self.get_cpuinfo_data()
if data.startswith("b"):
data = data[2:]
parser = cpuinfo.CPUInfoParser(data)
return parser.parse_items()

Expand Down Expand Up @@ -198,13 +200,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):
Expand Down Expand Up @@ -234,7 +236,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
Expand Down Expand Up @@ -302,7 +304,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

Expand Down Expand Up @@ -345,7 +347,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):
Expand Down Expand Up @@ -429,6 +431,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))
4 changes: 2 additions & 2 deletions hwinfo/tools/tests/test_inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import mock
import sys
from mock import patch
from StringIO import StringIO
from io import StringIO

from hwinfo.tools import inspector
import dummy_data
Expand Down Expand Up @@ -180,7 +180,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):
Expand Down
4 changes: 2 additions & 2 deletions hwinfo/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -60,7 +60,7 @@ def parse_items(self):
return [self.parse_item(self.DATA)]
else:
recs = []
for data in self.DATA.decode().split(self.ITEM_SEPERATOR):
for data in self.DATA.split(self.ITEM_SEPERATOR)[:-1]:
rec = self.parse_item(data)
recs.append(rec)
return recs
Expand Down
16 changes: 8 additions & 8 deletions hwinfo/util/tests/parser_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<num>\w+)']
rec = cp.parse_item(data)
self.assertEquals(rec['num'], '845')
cp = CommandParser()
cp.ITEM_REGEXS = [r'four:\ (?P<num>\w+)']
rec = cp.parse_item(data)
self.assertEqual(rec['num'], '845')

def test_parse_items(self):
data = """
Expand All @@ -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<encap>[\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)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

from setuptools import setup, find_packages

Expand Down