[Libosinfo] [PATCH osinfo-db 06/10] tests: util: Cache xml file list and parsed OS objects
Cole Robinson
crobinso at redhat.com
Tue Mar 19 20:01:56 UTC 2019
These should be reusable across test cases. Cache the lookup results on
the first access
Signed-off-by: Cole Robinson <crobinso at redhat.com>
---
tests/util.py | 42 ++++++++++++++++++++++++++----------------
1 file changed, 26 insertions(+), 16 deletions(-)
diff --git a/tests/util.py b/tests/util.py
index 15ec3cc..418b9d2 100644
--- a/tests/util.py
+++ b/tests/util.py
@@ -15,31 +15,41 @@ class _DataFiles():
def __init__(self):
self.datadir = os.environ['INTERNAL_OSINFO_DB_DATA_DIR']
self.schema = os.path.join(self.datadir, 'schema', 'osinfo.rng')
+ self._all_xml_cache = []
+ self._oses_cache = []
if not os.path.exists(self.datadir):
raise RuntimeError("INTERNAL_OSINFO_DB_DATA_DIR=%s "
"doesn't exist" % self.datadir)
- def _get_files(self, directory):
- files = []
- root = os.path.join(self.datadir, directory)
- for (dirpath, _, filenames) in os.walk(root):
- for filename in filenames:
- if not filename.endswith('.xml'):
- continue
- files.append(os.path.join(dirpath, filename))
- return files
+ def _get_all_xml(self):
+ """
+ Get and cache the full list of all DATA_DIR .xml paths
+ """
+ if not self._all_xml_cache:
+ for (dirpath, _, filenames) in os.walk(self.datadir):
+ for filename in filenames:
+ if not filename.endswith('.xml'):
+ continue
+ self._all_xml_cache.append(os.path.join(dirpath, filename))
+ return self._all_xml_cache
+
+ def _filter_xml(self, dirname):
+ """
+ Filter XML paths by those in $DATA_DIR/$dirname
+ """
+ return [p for p in self._get_all_xml() if
+ p.startswith(os.path.join(self.datadir, dirname))]
def oses(self):
- ret = []
- files = self._get_files('os')
- for path in files:
- osroot = ET.parse(path).getroot().find('os')
- ret.append(osinfo.Os(osroot))
- return ret
+ if not self._oses_cache:
+ for path in self._filter_xml('os'):
+ osroot = ET.parse(path).getroot().find('os')
+ self._oses_cache.append(osinfo.Os(osroot))
+ return self._oses_cache
def xmls(self):
- return self._get_files('')
+ return self._get_all_xml()
DataFiles = _DataFiles()
--
2.21.0
More information about the Libosinfo
mailing list