Skip to content

Commit 432ca5d

Browse files
committed
Adding caching for tarfile reads to improve performance.
Signed-off-by: Rob Dobson <[email protected]>
1 parent f4fae55 commit 432ca5d

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

hwinfo/tools/inspector.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,26 @@ def find_in_tarball(tarball, filename):
5757
if len(matches) > 1:
5858
raise Exception("Error: more than one match for that filename")
5959

60+
if not matches:
61+
raise FileNotFound("Cound not find %s in %s" % (filename, tarball))
6062
return matches.pop()
6163

64+
def read_files_from_tarball(tarball, files):
65+
tar = tarfile.open(tarball)
66+
data = None
67+
tmpdir = tempfile.mkdtemp()
68+
69+
70+
file_data = {}
71+
for f in files:
72+
tar.extract(f, tmpdir)
73+
file_data[os.path.basename(f)] = read_from_file("%s/%s" % (tmpdir, f))
74+
75+
tar.close()
76+
if os.path.exists(tmpdir):
77+
shutil.rmtree(tmpdir)
78+
return file_data
79+
6280
def read_from_tarball(tarball, filename):
6381
tar = tarfile.open(tarball)
6482
data = None
@@ -224,11 +242,34 @@ class HostFromTarball(HostFromLogs):
224242

225243
def __init__(self, filename):
226244
self.tarloc = filename
245+
pre_load_files = [
246+
'lspci-nnm.out',
247+
'lspci-vv.out',
248+
'lspci-n.out',
249+
'dmidecode.out',
250+
'cpuinfo',
251+
'xensource-inventory'
252+
]
253+
254+
self._preload_files(pre_load_files)
255+
256+
def _preload_files(self, filenames):
257+
paths = []
258+
for f in filenames:
259+
try:
260+
filepath = find_in_tarball(self.tarloc, f)
261+
paths.append(filepath)
262+
except FileNotFound:
263+
continue
264+
self.fdata = read_files_from_tarball(self.tarloc, paths)
227265

228266
def _load_from_file(self, filename):
229267
"""Find filename in tar, and load it"""
230-
filepath = find_in_tarball(self.tarloc, filename)
231-
return read_from_tarball(self.tarloc, filepath)
268+
if filename in self.fdata:
269+
return self.fdata[filename]
270+
else:
271+
filepath = find_in_tarball(self.tarloc, filename)
272+
return read_from_tarball(self.tarloc, filepath)
232273

233274
def pci_filter(devices, types):
234275
res = []

0 commit comments

Comments
 (0)