From 226433bac265f645b40a93f867e343808d7cce11 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Mon, 23 Nov 2026 23:46:45 +0100 Subject: [PATCH] fix: hold a directory by its members alone --- scripts/packs/consumers.py | 32 +++++--------------- scripts/packs/crossref.py | 14 +++++---- scripts/packs/supplemental.py | 42 ++++++++------------------- scripts/sitegen/emulatorcards.py | 5 +--- tests/test_cross_reference_sources.py | 16 ++++++++-- tests/test_site_availability.py | 5 ++-- 6 files changed, 45 insertions(+), 69 deletions(-) diff --git a/scripts/packs/consumers.py b/scripts/packs/consumers.py index 168b734ca..d0efc097c 100644 --- a/scripts/packs/consumers.py +++ b/scripts/packs/consumers.py @@ -16,42 +16,24 @@ from resolve.localfile import resolve_local_file from resolve.validation import inner_rom_check, with_archive_members -_tree_cache: dict[tuple[str, str], list[str]] = {} - - -def _trees(data_root: str, bios_root: str) -> list[str]: - """Every directory under data/ and bios/, as a path with a trailing slash.""" - key = (data_root, bios_root) - if key not in _tree_cache: - _tree_cache[key] = sorted( - f"{path.as_posix()}/" - for root in (data_root, bios_root) if Path(root).is_dir() - for path in Path(root).rglob("*") - if path.is_dir() - and not any(p.startswith(".") for p in path.relative_to(root).parts) - ) - return _tree_cache[key] - - def held_copy( entry: dict, data_root: str = "data", bios_root: str = BIOS_DIR ) -> tuple[str, str] | None: - """The data/ file, directory or bios/ ZIP member holding an entry. + """The data/ file or bios/ ZIP member holding a file entry. The database indexes the files under bios/, and an entry held otherwise (specmate.rom as a member of spectrum_opus.zip, Dolphin's fonts in its data directory) read as held with no word of where. Returns (path, - member), the member empty for a file or a directory. A file entry that - declares a hash is held only by a copy carrying it, as data_hit judges. + member), the member empty for a file. A file entry that declares a hash + is held only by a copy carrying it, as data_hit judges. A directory + entry is never answered here: its copies are the files + FileConsumers.directory_members finds, the ones verify and the pack + read, and a folder of the same name elsewhere held another emulator's + tree (SuperTux's data/ named MAME's plugins/data/). """ names = [entry.get("name", ""), entry.get("path") or "", *(entry.get("aliases") or [])] names = [str(n) for n in names if n] if entry.get("type") == "directory" or any(n.endswith("/") for n in names[:1]): - for name in names: - tail = "/" + name.strip("/") + "/" - for tree in _trees(data_root, bios_root): - if tree.endswith(tail): - return tree, "" return None declared = declared_content(entry) locations = supplemental_locations(data_root, bios_root) diff --git a/scripts/packs/crossref.py b/scripts/packs/crossref.py index ab66f3a7a..279c6a860 100644 --- a/scripts/packs/crossref.py +++ b/scripts/packs/crossref.py @@ -248,12 +248,14 @@ def entry_source(f: dict, index: dict) -> str | None: fname = f.get("name", "") path_field = f.get("path") or "" # A directory is held when the collection holds files under it, the rule - # the pack and verify --emulator expand it by: a bare-name lookup missed - # cdogs' .wolf3d/, whose 695 files every pack carries. - if f.get("type") == "directory" and directory_members( - {"indexes": {"by_path_suffix": by_path_suffix}}, path_field or fname - ): - return "bios" + # the pack and verify --emulator expand it by, and by nothing else: a + # bare-name lookup missed cdogs' .wolf3d/, whose 695 files every pack + # carries, and called SuperTux's data/ held by MAME's plugins/data/. + if f.get("type") == "directory" or fname.endswith("/"): + members = directory_members( + {"indexes": {"by_path_suffix": by_path_suffix}}, path_field or fname + ) + return "bios" if members else None found = None for candidate in [fname, *([path_field] if path_field != fname else []), *(f.get("aliases") or [])]: diff --git a/scripts/packs/supplemental.py b/scripts/packs/supplemental.py index cbc61de49..fbabf9f89 100644 --- a/scripts/packs/supplemental.py +++ b/scripts/packs/supplemental.py @@ -35,38 +35,21 @@ class SupplementalIndex: def build_supplemental_index( data_root: str = "data", bios_root: str = BIOS_DIR ) -> SupplementalIndex: - """Index the filenames and directory names in data/ and inside bios/ ZIPs. + """Index the filenames in data/ and inside bios/ ZIPs. - A directory is indexed only with its trailing slash: a bare name matched a - file entry of the same name, and BasiliskII's required `ROM` read as held - because cpcemu keeps a directory called ROM. + No directory is indexed: whether the collection holds a directory entry + is resolve.directories.directory_members alone. A folder name proved + nothing, and SuperTux's data/ read as held by MAME's plugins/data/. """ names: set[str] = set() root_path = Path(data_root) if root_path.is_dir(): for fpath in root_path.rglob("*"): - if fpath.name.startswith("."): - continue - if fpath.is_file(): + if not fpath.name.startswith(".") and fpath.is_file(): names.add(fpath.name) names.add(fpath.name.lower()) - else: - names.add(fpath.name + "/") - names.add(fpath.name.lower() + "/") - if fpath.is_dir(): - # Also index relative path from data/subdir/ for directory entries - parts = fpath.relative_to(root_path).parts - if len(parts) > 1: - rel = "/".join(parts[1:]) - names.add(rel + "/") - names.add(rel.lower() + "/") bios_path = Path(bios_root) if bios_path.is_dir(): - # Index directory names for directory-type entries (e.g., "nestopia/samples/moepro/") - for dpath in bios_path.rglob("*"): - if dpath.is_dir() and not dpath.name.startswith("."): - names.add(dpath.name + "/") - names.add(dpath.name.lower() + "/") names |= _zip_member_names(bios_path) return SupplementalIndex(frozenset(names), data_root, str(bios_root)) @@ -113,15 +96,14 @@ def _zip_member_names(bios_path: Path) -> set[str]: def data_hit( fname: str, basename: str | None, file_entry: dict | None, data_names: SupplementalIndex ) -> bool: - """Whether data/ or a ZIP holds the name: a directory entry looks for a - directory, a file entry for a file. A file entry that declares a hash is - held only by a copy carrying it: touchhle's LiberationMono-Regular.ttf - read as held because idtech4a++'s extras.zip has another font of that - name.""" - looked_up = [fname, fname.lower()] + ([basename, basename.lower()] if basename else []) + """Whether data/ or a ZIP holds a file of the name. A directory entry is + never held here (directory_members decides it). A file entry that + declares a hash is held only by a copy carrying it: touchhle's + LiberationMono-Regular.ttf read as held because idtech4a++'s extras.zip + has another font of that name.""" if fname.endswith("/") or (file_entry or {}).get("type") == "directory": - looked_up = [name.rstrip("/") + "/" for name in looked_up] - return any(name in data_names for name in looked_up) + return False + looked_up = [fname, fname.lower()] + ([basename, basename.lower()] if basename else []) if not any(name in data_names for name in looked_up): return False declared = declared_content(file_entry or {}) diff --git a/scripts/sitegen/emulatorcards.py b/scripts/sitegen/emulatorcards.py index 9f703fe4b..a168414f2 100644 --- a/scripts/sitegen/emulatorcards.py +++ b/scripts/sitegen/emulatorcards.py @@ -794,7 +794,7 @@ def _emulator_card_folds( def _held_copy_line(copy: tuple[str, str], sources: CardSources) -> str: """Where a copy the database does not index sits: a ZIP member under - bios/, or a file or tree of a data directory the packs fetch.""" + bios/, or a file of a data directory the packs fetch.""" consumers = sources.consumers path, member = copy parts = path.rstrip("/").split("/") @@ -807,9 +807,6 @@ def _held_copy_line(copy: tuple[str, str], sources: CardSources) -> str: return f"Collection copy: member {code_span(member)} of {archive}" if parts[0] == "data" and len(parts) >= 2: ref, inside = parts[1], "/".join(parts[2:]) - if path.endswith("/"): - where = code_span(inside + "/") if inside else "the tree" - return f"Collection copy: {where} in the {code_span(ref)} data directory" info = (sources.data_members.get(ref) or {}).get(inside) or {} shown = code_span(inside) if info.get("sha1"): diff --git a/tests/test_cross_reference_sources.py b/tests/test_cross_reference_sources.py index 1f8db1289..90eca1f75 100644 --- a/tests/test_cross_reference_sources.py +++ b/tests/test_cross_reference_sources.py @@ -24,7 +24,7 @@ REPO_ROOT = Path(__file__).resolve().parent.parent def _index(**over) -> dict: - base = { + base: dict = { "by_name": {}, "by_name_lower": {}, "by_path_suffix": {}, "by_md5": {}, "by_sha256": {}, "by_crc32": {}, "db_files": {}, "data_names": set(), } @@ -82,7 +82,19 @@ class GapReportSources(unittest.TestCase): names = build_supplemental_index(str(Path(tmp) / "data"), str(Path(tmp) / "bios")) self.assertIsNone(entry_source({"name": "ROM"}, _index(data_names=names))) directory = {"name": "ROM", "type": "directory"} - self.assertEqual(entry_source(directory, _index(data_names=names)), "data") + self.assertIsNone(entry_source(directory, _index(data_names=names))) + + def test_a_folder_name_does_not_hold_a_directory(self): + """SuperTux's data/ read as held by MAME's plugins/data/ while verify + and the pack, which read directory_members, called it missing.""" + with tempfile.TemporaryDirectory() as tmp: + (Path(tmp) / "bios" / "Arcade" / "MAME" / "plugins" / "data").mkdir(parents=True) + names = build_supplemental_index(str(Path(tmp) / "data"), str(Path(tmp) / "bios")) + entry = {"name": "SuperTux data", "path": "data/", "type": "directory"} + self.assertIsNone(entry_source(entry, _index(data_names=names))) + held = _index(data_names=names, by_path_suffix={ + "data/levels/a.stl": ["a" * 40], "supertux/data/levels/a.stl": ["a" * 40]}) + self.assertEqual(entry_source(entry, held), "bios") def test_a_system_spelled_otherwise_is_still_declared(self): profiles = {"gpgx": {"emulator": "gpgx", "systems": ["sega-megacd"], diff --git a/tests/test_site_availability.py b/tests/test_site_availability.py index 2d457089e..f00cc38ba 100644 --- a/tests/test_site_availability.py +++ b/tests/test_site_availability.py @@ -94,8 +94,9 @@ class AHeldEntrySaysWhere(unittest.TestCase): self.assertIsNone(held_copy({"name": "specmate.rom", "sha1": "0" * 40}, **roots)) self.assertEqual(held_copy({"name": "font_western.bin"}, **roots), (str(data / "dolphin-sys/GC/font_western.bin"), "")) - self.assertEqual(held_copy({"name": "GC/", "type": "directory"}, **roots), - (f"{(data / 'dolphin-sys/GC').as_posix()}/", "")) + # A directory is held by the files directory_members finds, never + # by a folder of its name. + self.assertIsNone(held_copy({"name": "GC/", "type": "directory"}, **roots)) consumers = FileConsumers({"files": {"z" * 40: { "path": "bios/Arcade/MAME/spectrum_opus.zip"}}}, data_dir_registry={})