Skip to content

Commit 10f7958

Browse files
committed
Merge pull request #13 from rdobson/extract_tar
Extract tar
2 parents bab0a3a + ad763fc commit 10f7958

File tree

4 files changed

+112
-1
lines changed

4 files changed

+112
-1
lines changed

hwinfo/pci/lspci.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class ParserException(Exception):
66
pass
77

8-
LABEL_REGEX = r'[\w+\ \.\,\+\&\-\/\[\]\(\)]+'
8+
LABEL_REGEX = r'[\w+\ \.\,\:\+\&\-\/\[\]\(\)]+'
99
CODE_REGEX = r'[0-9a-fA-F]{4}'
1010
BUSID_REGEX = r'[0-9a-fA-F]{2}:[0-9a-fA-F]{2}\.[0-9a-fA-F]'
1111

hwinfo/pci/tests/test_lspci.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,24 @@ class EmulexNicDeviceParse(TestSingleDeviceNNMMParse):
225225
'pci_subdevice_id': 'e70b',
226226
}
227227

228+
class EmulexHbDeviceParse(TestSingleDeviceNNMMParse):
229+
230+
SAMPLE_DATA = '07:00.0 "Fibre Channel [0c04]" "Emulex Corporation [10df]" "Saturn-X: LightPulse Fibre Channel Host Adapter [f100]" -r03 "Hewlett-Packard Company [103c]" "Device [3282]"'
231+
232+
DEVICE_REC = {
233+
'pci_device_bus_id': '07:00.0',
234+
'pci_device_class': '0c04',
235+
'pci_device_class_name': 'Fibre Channel',
236+
'pci_vendor_name': 'Emulex Corporation',
237+
'pci_vendor_id': '10df',
238+
'pci_device_id': 'f100',
239+
'pci_device_name': 'Saturn-X: LightPulse Fibre Channel Host Adapter',
240+
'pci_subvendor_name': 'Hewlett-Packard Company',
241+
'pci_subvendor_id': '103c',
242+
'pci_subdevice_name': 'Device',
243+
'pci_subdevice_id': '3282',
244+
}
245+
228246
class LsiSASDeviceParse(TestSingleDeviceNNMMParse):
229247

230248
SAMPLE_DATA = '06:00.0 "Serial Attached SCSI controller [0107]" "LSI Logic / Symbios Logic [1000]" "SAS2004 PCI-Express Fusion-MPT SAS-2 [Spitfire] [0070]" -r03 "IBM [1014]" "Device [03f8]"'

hwinfo/tools/inspector.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
import subprocess
77
import os
88
import sys
9+
import tarfile
10+
import random
11+
import string
12+
import tempfile
13+
import shutil
914

1015
from hwinfo.pci import PCIDevice
1116
from hwinfo.pci.lspci import *
@@ -40,6 +45,34 @@ def local_command(cmd):
4045
print stdout
4146
raise Exception("stderr: %s" % str(stderr))
4247

48+
def find_in_tarball(tarball, filename):
49+
tar = tarfile.open(tarball)
50+
members = tar.getmembers()
51+
tar.close()
52+
matches = []
53+
for m in members:
54+
if m.name.endswith(filename):
55+
matches.append(m.name)
56+
57+
if len(matches) > 1:
58+
raise Exception("Error: more than one match for that filename")
59+
60+
return matches.pop()
61+
62+
def read_from_tarball(tarball, filename):
63+
tar = tarfile.open(tarball)
64+
data = None
65+
tmpdir = tempfile.mkdtemp()
66+
67+
tar.extract(filename, tmpdir)
68+
data = read_from_file("%s/%s" % (tmpdir, filename))
69+
70+
tar.close()
71+
if os.path.exists(tmpdir):
72+
shutil.rmtree(tmpdir)
73+
74+
return data
75+
4376
class Host(object):
4477

4578
client = None
@@ -155,6 +188,15 @@ def get_pci_devices(self):
155188
recs = combine_recs(all_recs, 'pci_device_bus_id')
156189
return [PCIDevice(rec) for rec in recs]
157190

191+
class HostFromTarball(HostFromLogs):
192+
193+
def __init__(self, filename):
194+
self.tarloc = filename
195+
196+
def _load_from_file(self, filename):
197+
"""Find filename in tar, and load it"""
198+
filepath = find_in_tarball(self.tarloc, filename)
199+
return read_from_tarball(self.tarloc, filepath)
158200

159201
def pci_filter(devices, types):
160202
res = []

hwinfo/tools/tests/test_inspector.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,57 @@ def test_is_remote(self, get_ssh_client):
4848
host = inspector.Host('test', 'user', 'pass')
4949
self.assertEqual(host.is_remote(), True)
5050

51+
52+
class HostFromTarballTests(unittest.TestCase):
53+
54+
@patch('hwinfo.tools.inspector.find_in_tarball')
55+
@patch('hwinfo.tools.inspector.read_from_tarball')
56+
def test__load_from_file(self, read_from_tarball, find_in_tarball):
57+
tarball_name = 'test.tar.gz'
58+
filename = 'foobar.txt'
59+
filepath = find_in_tarball.return_value = \
60+
'somedir/anotherdir/%s' % filename
61+
62+
host = inspector.HostFromTarball('test.tar.gz')
63+
host._load_from_file(filename)
64+
65+
find_in_tarball.assert_called_once_with(tarball_name, filename)
66+
read_from_tarball.assert_called_once_with(tarball_name, filepath)
67+
68+
class UtilTests(unittest.TestCase):
69+
70+
@patch('tarfile.open')
71+
def test_find_in_tarball(self, tar_open):
72+
mtarfh = tar_open.return_value = mock.MagicMock()
73+
filename = 'foo.bar'
74+
filepath = 'testing/path/%s' % filename
75+
76+
paths = [filepath, 'something/else/foo.pdf', 'extra/file.bin']
77+
78+
mtar_infos = []
79+
for p in paths:
80+
m = mock.MagicMock()
81+
m.name = p
82+
mtar_infos.append(m)
83+
84+
mtarfh.getmembers.return_value = mtar_infos
85+
86+
path = inspector.find_in_tarball(mtarfh, filename)
87+
self.assertEqual(filepath, path)
88+
89+
90+
@patch('hwinfo.tools.inspector.read_from_file')
91+
@patch('tarfile.open')
92+
def test_read_from_tarball(self, tar_open, read_from_file):
93+
mtarfh = tar_open.return_value = mock.MagicMock()
94+
filepath = 'something/else/foo.pdf'
95+
96+
mread = read_from_file.return_value = 'blah blah blah'
97+
98+
data = inspector.read_from_tarball('testtar.tar.gz', filepath)
99+
self.assertEqual(mread, data)
100+
101+
51102
class RemoteCommandTests(unittest.TestCase):
52103

53104
def setUp(self):

0 commit comments

Comments
 (0)