diff options
author | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2020-04-06 13:32:41 +0200 |
---|---|---|
committer | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2020-04-06 13:33:26 +0200 |
commit | 233f5642a34bf46d54978c8663fe927ee4065df3 (patch) | |
tree | 297f29fe996075bb4d08e91678e444cf2cdc2aa4 | |
parent | Add RTEMS build specification (diff) | |
download | rtems-central-233f5642a34bf46d54978c8663fe927ee4065df3.tar.bz2 |
items: Use a cache file directory
This allows read-only source trees.
-rw-r--r-- | config.ini | 2 | ||||
-rw-r--r-- | rtemsqual/items.py | 23 | ||||
-rw-r--r-- | tests/test_glossary.py | 14 | ||||
-rw-r--r-- | tests/test_items.py | 11 |
4 files changed, 27 insertions, 23 deletions
@@ -1,5 +1,5 @@ spec: - cache-file: spec.pickle + cache-directory: cache paths: - spec - external/rtems/spec diff --git a/rtemsqual/items.py b/rtemsqual/items.py index a943663d..c1db9026 100644 --- a/rtemsqual/items.py +++ b/rtemsqual/items.py @@ -98,7 +98,7 @@ class ItemCache(object): """ Returns the list of top-level specification items. """ return self._top_level - def _load_items_in_dir(self, path: str, path_cache_file: str, + def _load_items_in_dir(self, path: str, cache_file: str, update_cache: bool) -> None: data_by_uid = {} # type: Dict[str, Any] if update_cache: @@ -108,10 +108,11 @@ class ItemCache(object): uid = os.path.basename(name).replace(".yml", "") with open(path2, "r") as yaml_src: data_by_uid[uid] = yaml.safe_load(yaml_src.read()) - with open(path_cache_file, "wb") as out: + os.makedirs(os.path.dirname(cache_file), exist_ok=True) + with open(cache_file, "wb") as out: pickle.dump(data_by_uid, out) else: - with open(path_cache_file, "rb") as pickle_src: + with open(cache_file, "rb") as pickle_src: data_by_uid = pickle.load(pickle_src) for uid, data in data_by_uid.items(): item = Item(uid, data) @@ -119,10 +120,12 @@ class ItemCache(object): if not item["links"]: self._top_level[uid] = item - def _load_items_recursive(self, path: str, cache_file: str) -> None: - path_cache_file = os.path.join(path, cache_file) + def _load_items_recursive(self, path: str, cache_dir: str) -> None: + mid = os.path.abspath(path) + mid = mid.replace(os.path.commonprefix([cache_dir, mid]), "") + cache_file = os.path.join(cache_dir, mid, "spec.pickle") try: - mtime = os.path.getmtime(path_cache_file) + mtime = os.path.getmtime(cache_file) update_cache = False except FileNotFoundError: update_cache = True @@ -132,8 +135,8 @@ class ItemCache(object): update_cache = update_cache or mtime <= os.path.getmtime(path2) else: if stat.S_ISDIR(os.lstat(path2).st_mode): - self._load_items_recursive(path2, cache_file) - self._load_items_in_dir(path, path_cache_file, update_cache) + self._load_items_recursive(path2, cache_dir) + self._load_items_in_dir(path, cache_file, update_cache) def _init_parents(self) -> None: for item in self._items.values(): @@ -145,8 +148,8 @@ class ItemCache(object): parent.add_child(item) def _load_items(self, config: Any) -> None: - cache_file = config["cache-file"] + cache_dir = os.path.abspath(config["cache-directory"]) for path in config["paths"]: - self._load_items_recursive(path, cache_file) + self._load_items_recursive(path, cache_dir) self._init_parents() self._init_children() diff --git a/tests/test_glossary.py b/tests/test_glossary.py index fa9e7bcf..2faa4831 100644 --- a/tests/test_glossary.py +++ b/tests/test_glossary.py @@ -34,20 +34,20 @@ from rtemsqual.items import ItemCache class TestGlossary(object): def test_glossary(self, tmpdir): item_cache_config = {} - cache_file = tmpdir + "/spec.pickle" - item_cache_config["cache-file"] = cache_file - spec_dir = tmpdir + "/spec" - shutil.copytree(os.path.dirname(__file__) + "/spec-glossary", spec_dir) - item_cache_config["paths"] = [spec_dir] + item_cache_config["cache-directory"] = "cache" + spec_src = os.path.join(os.path.dirname(__file__), "spec-glossary") + spec_dst = os.path.join(tmpdir, "spec") + shutil.copytree(spec_src, spec_dst) + item_cache_config["paths"] = [os.path.normpath(spec_dst)] ic = ItemCache(item_cache_config) glossary_config = {} glossary_config["project-groups"] = ["g"] - project_glossary = tmpdir + "/project/glossary.rst" + project_glossary = os.path.join(tmpdir, "project", "glossary.rst") glossary_config["project-target"] = project_glossary doc = {} doc["rest-source-paths"] = [str(tmpdir)] - document_glossary = tmpdir + "/document/glossary.rst" + document_glossary = os.path.join(tmpdir, "document", "glossary.rst") doc["target"] = document_glossary glossary_config["documents"] = [doc] generate(glossary_config, ic) diff --git a/tests/test_items.py b/tests/test_items.py index af97437c..613bd0be 100644 --- a/tests/test_items.py +++ b/tests/test_items.py @@ -66,15 +66,16 @@ class TestItemCache(object): def test_load(self, tmpdir): config = {} - cache_file = "spec.pickle" - config["cache-file"] = cache_file + cache_dir = os.path.join(tmpdir, "cache") + config["cache-directory"] = os.path.normpath(cache_dir) spec_src = os.path.join(os.path.dirname(__file__), "spec-item-cache") spec_dst = os.path.join(tmpdir, "spec") shutil.copytree(spec_src, spec_dst) - config["paths"] = [str(spec_dst)] + config["paths"] = [os.path.normpath(spec_dst)] ic = ItemCache(config) - assert os.path.exists(os.path.join(spec_dst, cache_file)) - assert os.path.exists(os.path.join(spec_dst, "d", cache_file)) + assert os.path.exists(os.path.join(cache_dir, "spec", "spec.pickle")) + assert os.path.exists( + os.path.join(cache_dir, "spec", "d", "spec.pickle")) assert ic["c"]["v"] == "c" assert ic["p"]["v"] == "p" t = ic.top_level |