Skip to content
Closed
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: 0 additions & 2 deletions hwinfo/host/dmidecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,3 @@ class DmidecodeParser(CommandParser):
# Processor Info
r'Processor\ Information\n\tSocket\ Designation:\ (?P<socket_designation>.*)\n',
]


22 changes: 8 additions & 14 deletions hwinfo/pci/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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'))
Expand All @@ -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 = {}
Expand Down
2 changes: 0 additions & 2 deletions hwinfo/pci/biosdevname.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,3 @@ class BiosdevnameDParser(CommandParser):
r'Driver\ version:\ (?P<driver_version>.*)\n',
r'Firmware\ version:\ (?P<firmware_version>.*)\n',
]


12 changes: 6 additions & 6 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,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):

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
33 changes: 16 additions & 17 deletions hwinfo/tools/inspector.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#!/usr/bin/env python

from __future__ import print_function
from argparse import ArgumentParser
import paramiko
import subprocess
import os
import sys
import tarfile
import tempfile
import shutil
import json
import paramiko

from prettytable import PrettyTable

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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'])
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 Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = []
Expand All @@ -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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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))
15 changes: 9 additions & 6 deletions hwinfo/tools/tests/test_inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand All @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
13 changes: 6 additions & 7 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 @@ -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:
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 tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist=py27,lint
envlist=py27,py37,lint

[testenv]
deps=
Expand Down