Skip to content

Commit ad763fc

Browse files
committed
Adding ability to read from a tarball.
Signed-off-by: Rob Dobson <[email protected]>
1 parent d144c88 commit ad763fc

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

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)