From 95cbb5af1d8236eb5c2e3e05c854a269abb48972 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Tue, 24 Nov 2026 03:04:20 +0100 Subject: [PATCH] fix: read archive members under the collection --- scripts/core/ziptools.py | 18 ++++++++---- scripts/exporter/native.py | 5 ++-- scripts/orchestrate/compare.py | 5 +++- scripts/orchestrate/emulatorpacks.py | 6 ++-- scripts/packs/coverage.py | 4 ++- scripts/packs/emulatorcheck.py | 4 ++- scripts/packs/emulatorpack.py | 4 ++- scripts/packs/integrity.py | 10 ++++--- scripts/packs/packmodes.py | 6 ++-- scripts/packs/slotscan.py | 8 ++++-- scripts/sitegen/whichpack.py | 2 +- tests/test_agnostic_scope.py | 2 +- tests/test_audit_regressions.py | 6 ++-- tests/test_cache_independent_counts.py | 2 +- tests/test_e2e.py | 6 ++-- tests/test_file_consumers.py | 2 +- tests/test_game_data_homonyms.py | 2 +- tests/test_install_expert.py | 2 +- tests/test_install_tui.py | 2 +- tests/test_owner_resolution.py | 2 +- tests/test_site_exports.py | 2 +- tests/test_site_render.py | 2 +- tests/test_slots.py | 2 +- tests/test_split_extras.py | 4 +-- tests/test_split_names.py | 2 +- tests/test_variant_archives.py | 2 +- tests/test_verify_emulator_evidence.py | 2 +- tests/test_zipped_member.py | 39 ++++++++++++++++++++++---- 28 files changed, 104 insertions(+), 49 deletions(-) diff --git a/scripts/core/ziptools.py b/scripts/core/ziptools.py index d323d0634..beec5d598 100644 --- a/scripts/core/ziptools.py +++ b/scripts/core/ziptools.py @@ -16,6 +16,7 @@ import stat import sys import tempfile import zipfile +from pathlib import Path from core.artifacts import process_umask, write_text_atomic from core.hashing import MAX_HASHED_MEMBER, md5sum @@ -199,15 +200,18 @@ def _members_md5(path: str) -> list[list] | None: def build_zip_contents_index( db: dict, + root: Path, max_entry_size: int = MAX_HASHED_MEMBER, cache_path: str | None = MEMBER_CACHE, ) -> dict: """Build {inner_rom_md5: zip_file_sha1} for ROMs inside ZIP files. - A ``.variants/`` copy counts (``neogeo.zip.1a2b3c4d`` is an archive), and - a member a primary archive holds answers with the primary, as the - resolver prefers it. Cached in-process, and on disk per archive sha1 - when cache_path is set. + Each database path is read under ``root``, the folder that holds the + collection's bios tree (model.dbpaths.collection_root). A ``.variants/`` + copy counts (``neogeo.zip.1a2b3c4d`` is an archive), and a member a + primary archive holds answers with the primary, as the resolver prefers + it. Cached in-process, and on disk per archive sha1 when cache_path is + set. """ global _zip_contents_cache @@ -217,7 +221,10 @@ def build_zip_contents_index( variant_entries: list[tuple[str, str]] = [] for sha1, entry in db.get("files", {}).items(): path = entry["path"] - if is_archive_path(path) and os.path.exists(path): + if not is_archive_path(path): + continue + path = str(root / path) + if os.path.exists(path): primary = path.lower().endswith(".zip") (zip_entries if primary else variant_entries).append((path, sha1)) primaries = len(zip_entries) @@ -227,6 +234,7 @@ def build_zip_contents_index( # second call with a smaller bound got members the first one admitted, # and a regenerated database got the sha1 of the old one. fingerprint = ( + str(root), max_entry_size, frozenset((path, os.path.getmtime(path), sha1) for path, sha1 in zip_entries), ) diff --git a/scripts/exporter/native.py b/scripts/exporter/native.py index 1dbee0769..897cdd431 100644 --- a/scripts/exporter/native.py +++ b/scripts/exporter/native.py @@ -5,6 +5,7 @@ file from the platform's original, and a report lists every change.""" from __future__ import annotations +from model.dbpaths import collection_root import contextlib import functools import json @@ -14,7 +15,7 @@ from pathlib import Path import yaml from core.artifacts import write_bytes_atomic, write_if_changed -from core.paths import EMULATORS_DIR +from core.paths import BIOS_DIR, EMULATORS_DIR from core.text import count_text from core.ziptools import build_zip_contents_index from .base_exporter import BaseExporter @@ -141,7 +142,7 @@ def refresh_cache( def _collection(db_path: str) -> tuple[dict, dict]: """The database and its archive index, read once for every platform.""" db = load_database(db_path) - return db, build_zip_contents_index(db) + return db, build_zip_contents_index(db, collection_root(Path(BIOS_DIR))) def doubled_in(scraped: dict | None, platforms_dir: str) -> list[dict]: diff --git a/scripts/orchestrate/compare.py b/scripts/orchestrate/compare.py index 775dd7c58..ed49ff2cc 100644 --- a/scripts/orchestrate/compare.py +++ b/scripts/orchestrate/compare.py @@ -2,6 +2,9 @@ from __future__ import annotations +from pathlib import Path +from core.paths import BIOS_DIR +from model.dbpaths import collection_root from model.registry import load_data_dir_registry @@ -29,5 +32,5 @@ def comparison_inputs( db = load_database(db_path) groups = group_identical_platforms(list_registered_platforms(platforms_dir), platforms_dir) - return (db, build_zip_contents_index(db), load_emulator_profiles(emulators_dir), + return (db, build_zip_contents_index(db, collection_root(Path(BIOS_DIR))), load_emulator_profiles(emulators_dir), load_data_dir_registry(platforms_dir), groups) diff --git a/scripts/orchestrate/emulatorpacks.py b/scripts/orchestrate/emulatorpacks.py index dde6b6a61..309c018bc 100644 --- a/scripts/orchestrate/emulatorpacks.py +++ b/scripts/orchestrate/emulatorpacks.py @@ -2,10 +2,12 @@ from __future__ import annotations +from pathlib import Path +from model.dbpaths import collection_root import contextlib import io -from core.paths import DB_FILE, EMULATORS_DIR, PLATFORMS_DIR +from core.paths import BIOS_DIR, DB_FILE, EMULATORS_DIR, PLATFORMS_DIR from model.profiles import reads_own_files from orchestrate.compare import set_difference_lines from orchestrate.labels import STEP_LABELS, step_title @@ -66,7 +68,7 @@ def check_emulator_packs( print(f"\n--- {step_title(STEP_LABELS['consistency'])}, emulator packs ---") db = load_database(db_path) - zip_contents = build_zip_contents_index(db) + zip_contents = build_zip_contents_index(db, collection_root(Path(BIOS_DIR))) profiles = load_emulator_profiles(emulators_dir, skip_aliases=False) def compare(names: list[str], standalone: bool, label: str, **narrowing: object) -> bool | None: diff --git a/scripts/packs/coverage.py b/scripts/packs/coverage.py index 013f9a082..15ec7e8f2 100644 --- a/scripts/packs/coverage.py +++ b/scripts/packs/coverage.py @@ -2,6 +2,8 @@ from __future__ import annotations +from pathlib import Path +from model.dbpaths import collection_root import os from dataclasses import dataclass, field from typing import TYPE_CHECKING, TypedDict @@ -816,7 +818,7 @@ def verify_platform( for sys in config.get("systems", {}).values() for fe in sys.get("files", []) ) - zip_contents = build_zip_contents_index(db) if has_zipped else {} + zip_contents = build_zip_contents_index(db, collection_root(Path(BIOS_DIR))) if has_zipped else {} # Loaded before the slot overrides: a caller passing no profiles (the # README's coverage) got no override and reported a discrepancy at a slot diff --git a/scripts/packs/emulatorcheck.py b/scripts/packs/emulatorcheck.py index bb6ad5c84..e88f5b901 100644 --- a/scripts/packs/emulatorcheck.py +++ b/scripts/packs/emulatorcheck.py @@ -2,6 +2,8 @@ from __future__ import annotations +from pathlib import Path +from model.dbpaths import collection_root import os from collections.abc import Callable from dataclasses import dataclass, field @@ -510,7 +512,7 @@ def verify_emulator( # The member index only answers a zipped_file entry, and decompressing # every archive took 23 of the 24 seconds of a run that had none. zip_contents = ( - build_zip_contents_index(db) + build_zip_contents_index(db, collection_root(Path(BIOS_DIR))) if any(f.get("zipped_file") for _name, profile in selected for f in profile.get("files") or []) else {} diff --git a/scripts/packs/emulatorpack.py b/scripts/packs/emulatorpack.py index f667fdee0..0dab1a46d 100644 --- a/scripts/packs/emulatorpack.py +++ b/scripts/packs/emulatorpack.py @@ -2,6 +2,8 @@ from __future__ import annotations +from pathlib import Path +from model.dbpaths import collection_root import contextlib import functools import os @@ -481,7 +483,7 @@ def build_emulator_pack( all_profiles = load_emulator_profiles(emulators_dir, skip_aliases=False) zip_contents = options.get("zip_contents") if zip_contents is None: - zip_contents = build_zip_contents_index(db) + zip_contents = build_zip_contents_index(db, collection_root(Path(bios_dir))) try: selected = select_emulator_profiles(profile_names, all_profiles, standalone) diff --git a/scripts/packs/integrity.py b/scripts/packs/integrity.py index 090b1a46b..7fdca615f 100644 --- a/scripts/packs/integrity.py +++ b/scripts/packs/integrity.py @@ -6,6 +6,8 @@ coverage report that disagree describe different collections.""" from __future__ import annotations +from pathlib import Path +from model.dbpaths import collection_root from collections import Counter from dataclasses import dataclass, field from typing import TYPE_CHECKING, TypedDict @@ -214,7 +216,7 @@ def verify_pack( by_md5 = db.get("indexes", {}).get("by_md5", {}) # MD5 -> SHA1 # Built on the first archive no hash recognises, once per pack: the # index opens every archive of the collection. - held_inside = _once(lambda: build_zip_contents_index(db)) + held_inside = _once(lambda: build_zip_contents_index(db, collection_root(Path(BIOS_DIR)))) by_name = db.get("indexes", {}).get("by_name", {}) # name -> [SHA1] # Data directory file index @@ -581,7 +583,7 @@ def _builder_extras( config, expect.opts.emulators_dir, db, emu_profiles, shipped, expect.target_cores, expect.region_drops or None, resolver=ExtraResolver( - db, BIOS_DIR, build_zip_contents_index(db), data_registry, + db, BIOS_DIR, build_zip_contents_index(db, collection_root(Path(BIOS_DIR))), data_registry, build_validation_index(platform_profiles), destination_owners(platform_profiles), True, profiles=emu_profiles, ), @@ -707,7 +709,7 @@ def _region_drops( config, emu_profiles, target_cores ) held: set[str] = set() - held_zip_contents = build_zip_contents_index(db) if db is not None else {} + held_zip_contents = build_zip_contents_index(db, collection_root(Path(BIOS_DIR))) if db is not None else {} region_groups, _extra_dests = platform_region_groups( config, expected_systems, @@ -803,7 +805,7 @@ def _count_baseline( for fe in declarations if fe.get("group") } - exclusion_index = build_zip_contents_index(db) if db is not None else {} + exclusion_index = build_zip_contents_index(db, collection_root(Path(BIOS_DIR))) if db is not None else {} categories = EntryCategories(expect.config, expect.emu_profiles, data_registry) for expected, declarations in baseline_groups.items(): if expected in view.zip_set: diff --git a/scripts/packs/packmodes.py b/scripts/packs/packmodes.py index 7c786570e..772573bdb 100644 --- a/scripts/packs/packmodes.py +++ b/scripts/packs/packmodes.py @@ -2,6 +2,8 @@ from __future__ import annotations +from pathlib import Path +from model.dbpaths import collection_root import argparse import contextlib import os @@ -97,7 +99,7 @@ def _run_hash_mode(args: argparse.Namespace) -> None: if print_hash_lookup(lookup_hashes(hashes, db, args.bios_dir)): sys.exit(1) return - zip_contents = build_zip_contents_index(db) + zip_contents = build_zip_contents_index(db, collection_root(Path(args.bios_dir))) try: with pack_output_lock(args.output_dir): outcome = generate_md5_pack( @@ -222,7 +224,7 @@ def run_pack_command( return args db = load_database(args.db) - zip_contents = build_zip_contents_index(db) + zip_contents = build_zip_contents_index(db, collection_root(Path(args.bios_dir))) if args.emulator or (args.system and not args.platform and not args.all): _run_profile_mode(args, db, zip_contents) return args diff --git a/scripts/packs/slotscan.py b/scripts/packs/slotscan.py index b7962fb97..49af31da7 100644 --- a/scripts/packs/slotscan.py +++ b/scripts/packs/slotscan.py @@ -2,7 +2,9 @@ from __future__ import annotations -from core.paths import PLATFORMS_DIR +from pathlib import Path +from model.dbpaths import collection_root +from core.paths import BIOS_DIR, PLATFORMS_DIR from core.ziptools import build_zip_contents_index from model.platforms import load_platform_config from model.registry import load_data_dir_registry @@ -31,7 +33,7 @@ def scan_platform( db, config.get("base_destination", ""), platform_launches(config, profiles), - build_zip_contents_index(db) if zip_index is None else zip_index, + build_zip_contents_index(db, collection_root(Path(BIOS_DIR))) if zip_index is None else zip_index, load_data_dir_registry(platforms_dir) if data_registry is None else data_registry, ) @@ -41,7 +43,7 @@ def scan_platforms( target_cores: dict[str, set[str] | None], data_registry: dict, ) -> tuple[dict[str, list[Conflict]], dict[str, list[Collision]]]: """The platforms with a contested destination, and those declaring one twice.""" - zip_index = build_zip_contents_index(db) + zip_index = build_zip_contents_index(db, collection_root(Path(BIOS_DIR))) found: dict[str, list[Conflict]] = {} collided: dict[str, list[Collision]] = {} for name in names: diff --git a/scripts/sitegen/whichpack.py b/scripts/sitegen/whichpack.py index 920fd2750..4a8e7fc42 100644 --- a/scripts/sitegen/whichpack.py +++ b/scripts/sitegen/whichpack.py @@ -32,7 +32,7 @@ from sitegen.releasepacks import NO_PACK, download_links, release_state def build_file_consumers(db: dict, coverages: dict, profiles: dict) -> FileConsumers: """Which platforms and emulators read each collected file.""" - consumers = FileConsumers(db, build_zip_contents_index(db)) + consumers = FileConsumers(db, build_zip_contents_index(db, siteinputs.current().db.parent)) consumers.add_platforms({name: cov["config"] for name, cov in coverages.items()}) consumers.add_emulators(profiles) consumers.add_manifests(siteinputs.manifests_dir()) diff --git a/tests/test_agnostic_scope.py b/tests/test_agnostic_scope.py index 35fcce727..bc234d016 100644 --- a/tests/test_agnostic_scope.py +++ b/tests/test_agnostic_scope.py @@ -163,7 +163,7 @@ class BundledHashIsProof(unittest.TestCase): os.chdir(REPO_ROOT) self.addCleanup(os.chdir, previous) db = load_database("database.json") - zips = build_zip_contents_index(db) + zips = build_zip_contents_index(db, REPO_ROOT) registry = load_data_dir_registry("platforms") wrong = [] for name, profile in sorted(load_emulator_profiles("emulators", skip_aliases=False).items()): diff --git a/tests/test_audit_regressions.py b/tests/test_audit_regressions.py index 81945eca1..9ffefeb48 100644 --- a/tests/test_audit_regressions.py +++ b/tests/test_audit_regressions.py @@ -3891,11 +3891,11 @@ class TheZipIndexCacheKnowsItsInputs(unittest.TestCase): archive.writestr("big.bin", b"x" * 4096) ziptools._zip_contents_cache = None db = {"files": {"a" * 40: {"path": str(path)}}} - self.assertEqual(len(ziptools.build_zip_contents_index(db)), 1) - self.assertEqual(ziptools.build_zip_contents_index(db, max_entry_size=10), {}) + self.assertEqual(len(ziptools.build_zip_contents_index(db, Path(directory))), 1) + self.assertEqual(ziptools.build_zip_contents_index(db, Path(directory), max_entry_size=10), {}) other = {"files": {"b" * 40: {"path": str(path)}}} self.assertEqual( - set(ziptools.build_zip_contents_index(other).values()), {"b" * 40} + set(ziptools.build_zip_contents_index(other, Path(directory)).values()), {"b" * 40} ) ziptools._zip_contents_cache = None diff --git a/tests/test_cache_independent_counts.py b/tests/test_cache_independent_counts.py index d5086d66c..0cd6716e9 100644 --- a/tests/test_cache_independent_counts.py +++ b/tests/test_cache_independent_counts.py @@ -40,7 +40,7 @@ class DeclaredFilesResolveWithoutDataCaches(unittest.TestCase): os.chdir(REPO_ROOT) try: db = load_database("database.json") - zip_contents = build_zip_contents_index(db) + zip_contents = build_zip_contents_index(db, REPO_ROOT) offenders = [] for platform in list_registered_platforms(str(platforms_dir)): config = load_platform_config(platform, str(platforms_dir)) diff --git a/tests/test_e2e.py b/tests/test_e2e.py index 50c0ddc4e..7a81abc40 100644 --- a/tests/test_e2e.py +++ b/tests/test_e2e.py @@ -823,7 +823,7 @@ class TestE2E(unittest.TestCase): self.assertNotIn(".variants", path) def test_09_resolve_zip_contents(self): - zc = build_zip_contents_index(self.db) + zc = build_zip_contents_index(self.db, Path(self.root)) inner_md5 = self.files["good.zip"]["inner_md5s"]["inner.rom"] entry = {"name": "good.zip", "md5": inner_md5, "zipped_file": "inner.rom"} path, status = resolve_local_file(entry, self.db, zc) @@ -2618,7 +2618,7 @@ class TestE2E(unittest.TestCase): from model.profiles import load_emulator_profiles from packs.platformpack import generate_split_packs - zip_contents = build_zip_contents_index(db) + zip_contents = build_zip_contents_index(db, Path(tmpdir)) emu_profiles = load_emulator_profiles(emu_dir) zip_paths = generate_split_packs( @@ -2885,7 +2885,7 @@ class TestE2E(unittest.TestCase): from core.ziptools import build_zip_contents_index from packs.md5pack import generate_md5_pack - zip_contents = build_zip_contents_index(db) + zip_contents = build_zip_contents_index(db, Path(tmpdir)) zip_path = generate_md5_pack( hashes=[("md5", h["md5"])], diff --git a/tests/test_file_consumers.py b/tests/test_file_consumers.py index eb239972b..9e3f0628e 100644 --- a/tests/test_file_consumers.py +++ b/tests/test_file_consumers.py @@ -254,7 +254,7 @@ class EveryReaderIsTheEmulatorPackFile(unittest.TestCase): os.chdir(REPO_ROOT) self.addCleanup(os.chdir, previous) db = json.loads(db_path.read_text(encoding="utf-8")) - zips = build_zip_contents_index(db) + zips = build_zip_contents_index(db, REPO_ROOT) registry = load_data_dir_registry(str(REPO_ROOT / "platforms")) profiles = load_emulator_profiles(str(REPO_ROOT / "emulators")) consumers = FileConsumers(db, zips, registry) diff --git a/tests/test_game_data_homonyms.py b/tests/test_game_data_homonyms.py index e9b472152..b09ba64d0 100644 --- a/tests/test_game_data_homonyms.py +++ b/tests/test_game_data_homonyms.py @@ -438,7 +438,7 @@ class CollectionCarriesNoGameDataHomonym(unittest.TestCase): try: db = load_database(str(database)) profiles = load_emulator_profiles("emulators", skip_aliases=False) - zip_index = build_zip_contents_index(db) + zip_index = build_zip_contents_index(db, REPO_ROOT) registry = load_data_dir_registry("platforms") claims = [] for name, profile in sorted(profiles.items()): diff --git a/tests/test_install_expert.py b/tests/test_install_expert.py index 8417a212d..1ec5e7ad6 100644 --- a/tests/test_install_expert.py +++ b/tests/test_install_expert.py @@ -60,7 +60,7 @@ class RealListParity(unittest.TestCase): cls.config = staticmethod( lambda plat: load_platform_config(plat, str(REPO_ROOT / "platforms"))) cls.db = json.loads(db_path.read_text(encoding="utf-8")) - cls.zip_contents = build_zip_contents_index(cls.db) + cls.zip_contents = build_zip_contents_index(cls.db, REPO_ROOT) cls.profiles = load_emulator_profiles(str(REPO_ROOT / "emulators")) cls.by_path = {entry["path"]: sha1 for sha1, entry in cls.db["files"].items()} cls.platforms = list_registered_platforms(str(REPO_ROOT / "platforms")) diff --git a/tests/test_install_tui.py b/tests/test_install_tui.py index cd4975ce2..1a0c55993 100644 --- a/tests/test_install_tui.py +++ b/tests/test_install_tui.py @@ -16594,7 +16594,7 @@ class EmulatorChoiceMatchesTheEmulatorPack(unittest.TestCase): from packs import emulatorpack cls.packs_archive, cls.emulatorpack = packs_archive, emulatorpack cls.db = json.loads(db_path.read_text(encoding="utf-8")) - cls.zip_contents = build_zip_contents_index(cls.db) + cls.zip_contents = build_zip_contents_index(cls.db, REPO_ROOT) cls.by_path = {entry["path"]: sha1 for sha1, entry in cls.db["files"].items()} def pack(self, emulator: str) -> dict[str, str]: diff --git a/tests/test_owner_resolution.py b/tests/test_owner_resolution.py index f2ae0d3de..7aac7495d 100644 --- a/tests/test_owner_resolution.py +++ b/tests/test_owner_resolution.py @@ -38,7 +38,7 @@ class ReadersNameTheOwner(unittest.TestCase): def test_the_site_credits_the_copy_the_pack_ships(self): consumers = FileConsumers( - self.db, build_zip_contents_index(self.db) + self.db, build_zip_contents_index(self.db, REPO_ROOT) ) consumers.add_emulators(self.profiles) sha1 = self.sha1_by_path.get("bios/Arcade/MAME/neogeo.zip") diff --git a/tests/test_site_exports.py b/tests/test_site_exports.py index 294fe9315..4d4891db8 100644 --- a/tests/test_site_exports.py +++ b/tests/test_site_exports.py @@ -739,7 +739,7 @@ class AHashFindsEveryPageThatNamesIt(unittest.TestCase): cwd = Path.cwd() try: os.chdir(root) - consumers = FileConsumers(db, build_zip_contents_index(db)) + consumers = FileConsumers(db, build_zip_contents_index(db, root)) archive = consumers.sha1_of(entry) finally: os.chdir(cwd) diff --git a/tests/test_site_render.py b/tests/test_site_render.py index 112da12cc..75aa598e4 100644 --- a/tests/test_site_render.py +++ b/tests/test_site_render.py @@ -207,7 +207,7 @@ class DirectoryMembersNameTheirEmulator(unittest.TestCase): if cls.SHA1 not in cls.db.get("files", {}): raise unittest.SkipTest("the collection does not hold the alephone plugin") cls.profile = load_emulator_profiles(str(REPO_ROOT / "emulators"))["alephone"] - cls.consumers = FileConsumers(cls.db, build_zip_contents_index(cls.db)) + cls.consumers = FileConsumers(cls.db, build_zip_contents_index(cls.db, REPO_ROOT)) cls.consumers.add_emulators({"alephone": cls.profile}) def test_a_member_names_the_emulator_and_its_directory_entry(self): diff --git a/tests/test_slots.py b/tests/test_slots.py index fc1d874c6..ed74ca3d1 100644 --- a/tests/test_slots.py +++ b/tests/test_slots.py @@ -821,7 +821,7 @@ class ManifestFollowsTheArbitration(unittest.TestCase): config = load_platform_config("retroarch", str(repo / "platforms")) profiles = load_emulator_profiles(str(repo / "emulators")) overrides = packs_slots.pack_overrides( - config, profiles, db, build_zip_contents_index(db), + config, profiles, db, build_zip_contents_index(db, repo), load_data_dir_registry(str(repo / "platforms")), ) # RetroArch serves a core's own copy at contested slots (ATARIOSB.ROM, diff --git a/tests/test_split_extras.py b/tests/test_split_extras.py index 27d77d3e4..a9e9d7f3f 100644 --- a/tests/test_split_extras.py +++ b/tests/test_split_extras.py @@ -69,7 +69,7 @@ class SplitPartsCoverTheExtras(unittest.TestCase): parts = generate_split_packs( "plat", str(root / "platforms"), db, str(root / "bios"), str(root / "dist"), emulators_dir=str(root / "emulators"), - zip_contents=build_zip_contents_index(db), + zip_contents=build_zip_contents_index(db, root), emu_profiles=load_emulator_profiles(str(root / "emulators")), ) members = {n for p in parts for n in zipfile.ZipFile(p).namelist()} @@ -120,7 +120,7 @@ class SplitPartsHoldWhatTheFullPackHolds(unittest.TestCase): (root / "emulators" / f"{name}.yml").write_text(yaml.dump(profile)) args = dict( emulators_dir=str(root / "emulators"), - zip_contents=build_zip_contents_index(db), + zip_contents=build_zip_contents_index(db, root), emu_profiles=load_emulator_profiles(str(root / "emulators")), offline=True, ) diff --git a/tests/test_split_names.py b/tests/test_split_names.py index 47bf45df3..296d2c398 100644 --- a/tests/test_split_names.py +++ b/tests/test_split_names.py @@ -79,7 +79,7 @@ class SplitPartNames(unittest.TestCase): return generate_split_packs( "longplat", str(self.platforms), self.db, self.tmp.name, str(self.out), group_by=group_by, emulators_dir=str(self.emulators), - zip_contents=build_zip_contents_index(self.db), emu_profiles={}, + zip_contents=build_zip_contents_index(self.db, Path(self.tmp.name)), emu_profiles={}, ) def test_a_large_group_is_written(self): diff --git a/tests/test_variant_archives.py b/tests/test_variant_archives.py index ee114a361..e8ae130d3 100644 --- a/tests/test_variant_archives.py +++ b/tests/test_variant_archives.py @@ -65,7 +65,7 @@ class VariantArchives(unittest.TestCase): "p": {"path": str(self.primary)}, "v": {"path": str(self.variant)}, }} - index = build_zip_contents_index(db, cache_path=None) + index = build_zip_contents_index(db, self.root, cache_path=None) self.assertEqual(index[hashlib.md5(b"a" * 16).hexdigest()], "p") self.assertEqual(index[hashlib.md5(b"v" * 16).hexdigest()], "v") diff --git a/tests/test_verify_emulator_evidence.py b/tests/test_verify_emulator_evidence.py index 4a4d0e3b6..dde6f34dc 100644 --- a/tests/test_verify_emulator_evidence.py +++ b/tests/test_verify_emulator_evidence.py @@ -377,7 +377,7 @@ class RepositoryWideEvidence(unittest.TestCase): self.skipTest("no database.json") db = load_database(str(db_path)) profiles = load_emulator_profiles(str(REPO_ROOT / "emulators")) - zip_contents = build_zip_contents_index(db) + zip_contents = build_zip_contents_index(db, REPO_ROOT) registry = load_data_dir_registry(str(REPO_ROOT / "platforms")) offenders: list[str] = [] diff --git a/tests/test_zipped_member.py b/tests/test_zipped_member.py index 65993990b..db68c8440 100644 --- a/tests/test_zipped_member.py +++ b/tests/test_zipped_member.py @@ -50,7 +50,7 @@ class ZippedMemberIsChecked(unittest.TestCase): })) pack = generate_pack( "plat", str(root / "platforms"), db, str(root / "bios"), str(root / "dist"), - emulators_dir=str(root / "emulators"), zip_contents=build_zip_contents_index(db), + emulators_dir=str(root / "emulators"), zip_contents=build_zip_contents_index(db, root), emu_profiles={}, offline=True, ) names = zipfile.ZipFile(pack).namelist() if pack else [] @@ -82,7 +82,7 @@ class VariantArchivesAreIndexed(unittest.TestCase): files[hashlib.sha1(data).hexdigest()] = {"path": str(path), "name": path.name} sha1_of = {entry["path"]: sha1 for sha1, entry in files.items()} ziptools._zip_contents_cache = None - index = build_zip_contents_index({"files": files}, cache_path=None) + index = build_zip_contents_index({"files": files}, root, cache_path=None) self.assertEqual(index[hashlib.md5(b"own").hexdigest()], sha1_of[str(variant)]) self.assertEqual(index[hashlib.md5(b"shared").hexdigest()], sha1_of[str(primary)]) @@ -159,13 +159,13 @@ class TheMemberIndexIsCachedByContent(unittest.TestCase): sha1 = hashlib.sha1(archive.read_bytes()).hexdigest() db = {"files": {sha1: {"path": str(archive), "name": "set.zip"}}} cache = str(Path(tmp) / "members.json") - first = ziptools.build_zip_contents_index(db, cache_path=cache) + first = ziptools.build_zip_contents_index(db, Path(tmp), cache_path=cache) ziptools._zip_contents_cache = None with mock.patch.object(ziptools, "_members_md5", side_effect=AssertionError("archive reopened")): - second = ziptools.build_zip_contents_index(db, cache_path=cache) + second = ziptools.build_zip_contents_index(db, Path(tmp), cache_path=cache) ziptools._zip_contents_cache = None - bounded = ziptools.build_zip_contents_index(db, max_entry_size=32, + bounded = ziptools.build_zip_contents_index(db, Path(tmp), max_entry_size=32, cache_path=cache) ziptools._zip_contents_cache = None self.assertEqual(first, second) @@ -173,5 +173,34 @@ class TheMemberIndexIsCachedByContent(unittest.TestCase): hashlib.md5(b"b" * 64).hexdigest()}) self.assertEqual(set(bounded), {hashlib.md5(b"a" * 16).hexdigest()}) + +class TheMemberIndexReadsUnderTheCollection(unittest.TestCase): + """The index tested each database path against the working directory: a + site built from another collection's database read this checkout's + archives, and a run started elsewhere found none.""" + + def test_paths_are_read_under_the_root_whatever_the_working_directory(self): + import os + + from core import ziptools + + with tempfile.TemporaryDirectory() as tmp, tempfile.TemporaryDirectory() as elsewhere: + root = Path(tmp) + archive = root / "bios" / "set.zip" + archive.parent.mkdir() + with zipfile.ZipFile(archive, "w") as zf: + zf.writestr("own.rom", b"own") + db = {"files": {"s" * 40: {"path": "bios/set.zip", "name": "set.zip"}}} + previous = os.getcwd() + os.chdir(elsewhere) + self.addCleanup(os.chdir, previous) + ziptools._zip_contents_cache = None + self.addCleanup(setattr, ziptools, "_zip_contents_cache", None) + found = build_zip_contents_index(db, root, cache_path=None) + self.assertEqual(found, {hashlib.md5(b"own").hexdigest(): "s" * 40}) + # Another root is another answer, never the cached one. + self.assertEqual(build_zip_contents_index(db, Path(elsewhere), cache_path=None), {}) + + if __name__ == "__main__": unittest.main()