Skip to content

Commit

Permalink
Adding caching for tarfile reads to improve performance.
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Dobson <[email protected]>
  • Loading branch information
rdobson committed Oct 23, 2014
1 parent f4fae55 commit 432ca5d
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions hwinfo/tools/inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,26 @@ def find_in_tarball(tarball, filename):
if len(matches) > 1:
raise Exception("Error: more than one match for that filename")

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

def read_files_from_tarball(tarball, files):
tar = tarfile.open(tarball)
data = None
tmpdir = tempfile.mkdtemp()


file_data = {}
for f in files:
tar.extract(f, tmpdir)
file_data[os.path.basename(f)] = read_from_file("%s/%s" % (tmpdir, f))

tar.close()
if os.path.exists(tmpdir):
shutil.rmtree(tmpdir)
return file_data

def read_from_tarball(tarball, filename):
tar = tarfile.open(tarball)
data = None
Expand Down Expand Up @@ -224,11 +242,34 @@ class HostFromTarball(HostFromLogs):

def __init__(self, filename):
self.tarloc = filename
pre_load_files = [
'lspci-nnm.out',
'lspci-vv.out',
'lspci-n.out',
'dmidecode.out',
'cpuinfo',
'xensource-inventory'
]

self._preload_files(pre_load_files)

def _preload_files(self, filenames):
paths = []
for f in filenames:
try:
filepath = find_in_tarball(self.tarloc, f)
paths.append(filepath)
except FileNotFound:
continue
self.fdata = read_files_from_tarball(self.tarloc, paths)

def _load_from_file(self, filename):
"""Find filename in tar, and load it"""
filepath = find_in_tarball(self.tarloc, filename)
return read_from_tarball(self.tarloc, filepath)
if filename in self.fdata:
return self.fdata[filename]
else:
filepath = find_in_tarball(self.tarloc, filename)
return read_from_tarball(self.tarloc, filepath)

def pci_filter(devices, types):
res = []
Expand Down

0 comments on commit 432ca5d

Please sign in to comment.