From b9441c545d64adbafb5dd52efa1378138394244a Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Sat, 21 Nov 2026 04:31:07 +0100 Subject: [PATCH] fix: read variant zip copies as archives --- scripts/acquire/derivations.py | 4 +++- scripts/acquire/provenance.py | 3 ++- scripts/core/ziptools.py | 7 +++++++ scripts/romrecipes/build.py | 2 +- scripts/romrecipes/ledger.py | 8 +------- scripts/romrecipes/pool.py | 8 ++++++-- tests/test_provenance.py | 13 +++++++++++++ tests/test_romset_recipes.py | 19 +++++++++++++++++++ 8 files changed, 52 insertions(+), 12 deletions(-) diff --git a/scripts/acquire/derivations.py b/scripts/acquire/derivations.py index fcd1202c3..83a83ea35 100644 --- a/scripts/acquire/derivations.py +++ b/scripts/acquire/derivations.py @@ -18,6 +18,8 @@ import zipfile from collections import defaultdict from collections.abc import Callable, Iterable, Sequence +from core.ziptools import is_archive_path + # A held file larger than this is not read: no chip image is near it. MAX_SOURCE_SIZE = 64 * 1024 * 1024 @@ -153,7 +155,7 @@ def _held_sources(db: dict) -> list[dict]: continue if 0 < record.get("size", 0) <= MAX_SOURCE_SIZE: sources.append({**record, "group": os.path.dirname(path)}) - if not path.lower().endswith(".zip"): + if not is_archive_path(path): continue try: with zipfile.ZipFile(path) as zf: diff --git a/scripts/acquire/provenance.py b/scripts/acquire/provenance.py index 49114b18d..aed681bbe 100644 --- a/scripts/acquire/provenance.py +++ b/scripts/acquire/provenance.py @@ -11,6 +11,7 @@ from pathlib import Path from core.hashing import parse_hash_list from core.paths import REPO_ROOT +from core.ziptools import is_archive_path from resolve.content import size_fits @@ -132,7 +133,7 @@ def archive_members(db: dict) -> dict[tuple[str, int], list[tuple[str, str]]]: members: dict[tuple[str, int], list[tuple[str, str]]] = {} for entry in db.get("files", {}).values(): path = entry.get("path", "") - if not path.endswith(".zip") or not os.path.exists(path): + if not is_archive_path(path) or not os.path.exists(path): continue try: with zipfile.ZipFile(path) as archive: diff --git a/scripts/core/ziptools.py b/scripts/core/ziptools.py index db1d8a589..e90ccc7dd 100644 --- a/scripts/core/ziptools.py +++ b/scripts/core/ziptools.py @@ -20,6 +20,13 @@ from core.artifacts import process_umask, write_text_atomic from core.hashing import MAX_HASHED_MEMBER, md5sum from core.paths import REPO_ROOT +_ARCHIVE_PATH = re.compile(r"\.zip(\.[0-9a-f]{8})?$", re.IGNORECASE) + + +def is_archive_path(path: str) -> bool: + """A ZIP by its name: ``x.zip`` or a ``.variants/`` copy ``x.zip.1a2b3c4d``.""" + return bool(_ARCHIVE_PATH.search(path)) + def check_inside_zip(container: str, file_name: str, expected_md5: str) -> str: """Check a ROM inside a ZIP, as Batocera's checkInsideZip() does. diff --git a/scripts/romrecipes/build.py b/scripts/romrecipes/build.py index dddc04e50..ba5fe0c81 100644 --- a/scripts/romrecipes/build.py +++ b/scripts/romrecipes/build.py @@ -10,7 +10,7 @@ from pathlib import Path from core.hashing import compute_hashes from core.paths import REPO_ROOT from core.torrentzip import write_torrentzip, zlib_ng_version -from romrecipes.ledger import is_archive_path +from core.ziptools import is_archive_path from romrecipes.members import member_sha1, member_opener diff --git a/scripts/romrecipes/ledger.py b/scripts/romrecipes/ledger.py index aacdf544a..174b647e8 100644 --- a/scripts/romrecipes/ledger.py +++ b/scripts/romrecipes/ledger.py @@ -30,22 +30,16 @@ before moving it into place. from __future__ import annotations import json -import re from pathlib import Path from core.paths import REPO_ROOT +from core.ziptools import is_archive_path ARCHIVES_FILE = REPO_ROOT / "recipes" / "archives.json" # The writer contract: build_torrentzip's layout over stock zlib. A change to # the layout is a new builder name and a new proof of every record. BUILDER = "torrentzip-1" -_ARCHIVE_PATH = re.compile(r"\.zip(\.[0-9a-f]{8})?$", re.IGNORECASE) - - -def is_archive_path(path: str) -> bool: - """A ZIP by its name: ``x.zip`` or a ``.variants/`` copy ``x.zip.1a2b3c4d``.""" - return bool(_ARCHIVE_PATH.search(path)) def database_archives(db: dict) -> dict[str, dict]: diff --git a/scripts/romrecipes/pool.py b/scripts/romrecipes/pool.py index e226cdd55..6648f9e06 100644 --- a/scripts/romrecipes/pool.py +++ b/scripts/romrecipes/pool.py @@ -10,6 +10,7 @@ from pathlib import Path from core.paths import REPO_ROOT from core.torrentzip import build_torrentzip +from core.ziptools import is_archive_path from romrecipes.romsets import Recipe @@ -36,7 +37,10 @@ class AtomPool: # same archives, and every build re-reads and re-deflates its ROMs. self._built: dict[tuple[tuple[str, str], ...], bytes | None] = {} self.unreadable: list[str] = [] - for archive in sorted(bios_dir.rglob("*.zip")): + # .variants/ copies (x.zip.1a2b3c4d) hold members no primary holds, + # bbcm.zip.4cc3e25a's fa2b881f among them; primaries answer first. + archives = (p for p in bios_dir.rglob("*") if is_archive_path(p.name) and p.is_file()) + for archive in sorted(archives, key=lambda p: (".variants" in p.parts, p)): try: with zipfile.ZipFile(archive) as handle: for info in handle.infolist(): @@ -51,7 +55,7 @@ class AtomPool: root = bios_dir.resolve() for entry in ((db or {}).get("files") or {}).values(): crc, path = str(entry.get("crc32") or "").lower(), entry.get("path", "") - if not crc or not path or path.lower().endswith(".zip"): + if not crc or not path or is_archive_path(path): continue loose = Path(path) if root in loose.resolve().parents and loose.is_file(): diff --git a/tests/test_provenance.py b/tests/test_provenance.py index 841a16b3d..b0a551612 100644 --- a/tests/test_provenance.py +++ b/tests/test_provenance.py @@ -576,6 +576,19 @@ class ArchiveMembersAreHeld(unittest.TestCase): self.assertEqual(report["no-intro"]["matched"], 1) self.assertEqual([e["name"] for e in report["no-intro"]["missing"]], ["other.bin"]) + def test_member_of_a_variant_copy_counts_as_held(self): + """A .variants/x.zip. copy was skipped: dumps only it holds + stayed acquisition targets.""" + from scripts.acquire.provenance import archive_members + + with tempfile.TemporaryDirectory() as tmp: + copy = Path(tmp) / "bbcm.zip.4cc3e25a" + with zipfile.ZipFile(copy, "w") as zf: + zf.writestr("mos.rom", b"mos") + members = archive_members({"files": {"z": {"path": str(copy)}}}) + self.assertEqual([m for ms in members.values() for _p, m in ms], ["mos.rom"]) + + class OneDumpIsCountedOnce(unittest.TestCase): """No-Intro's three FM-7 DATs each list DICROM.rom and KANJI.rom: the report said 2 485 missing where 2 284 distinct dumps were missing, and diff --git a/tests/test_romset_recipes.py b/tests/test_romset_recipes.py index d955bbc2a..487e6e381 100644 --- a/tests/test_romset_recipes.py +++ b/tests/test_romset_recipes.py @@ -149,6 +149,25 @@ class PoolAndBuild(unittest.TestCase): self.assertIsNone(reconstruct("0" * 32, {"v1": [("a.rom", "ffffffff")]}, self.pool)) +class AVariantCopyIsAnArchive(unittest.TestCase): + """AtomPool listed bios/**/*.zip: bbcm.zip.4cc3e25a's members were never + atoms, so --missing called a recipe unbuildable that --build rebuilt, + and the copy itself was indexed as a loose ROM.""" + + def test_its_members_are_atoms_and_the_copy_is_no_rom(self): + with tempfile.TemporaryDirectory(dir=TMP_ROOT) as directory: + bios = Path(directory) + copy = bios / "Acorn" / ".variants" / "bbcm.zip.4cc3e25a" + copy.parent.mkdir(parents=True) + with zipfile.ZipFile(copy, "w") as archive: + archive.writestr("a.rom", ROM_A) + container = f"{zlib.crc32(copy.read_bytes()):08x}" + db = {"files": {"s": {"path": str(copy), "crc32": container}}} + pool = AtomPool(bios, db=db) + self.assertEqual(pool.read(_crc(ROM_A)), ROM_A) + self.assertNotIn(container, pool) + + class ReportsOverAFixture(unittest.TestCase): """A whole run over a synthetic collection, database and platform."""