From 6f3500d8c887ae1cb45bf5e648e02ab29002d394 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Sat, 21 Nov 2026 20:32:33 +0100 Subject: [PATCH] fix: vary a primary only with its own copies Campaign.variant_files listed every .variants/ copy the database named after the entry, so QUASI88's PC-88 font.rom was varied with a PC-98 font filed under the same name. resolve.localfile.variant_copies keeps the copies in the primary's own .variants/ folder that pass the entry's declared hashes and shape checks; the campaign reads it. --- scripts/probes/campaign.py | 15 ++---- scripts/probes/entries.py | 12 +++++ scripts/resolve/localfile.py | 46 +++++++++++++++++++ tests/test_variant_copies.py | 88 ++++++++++++++++++++++++++++++++++++ 4 files changed, 149 insertions(+), 12 deletions(-) create mode 100644 tests/test_variant_copies.py diff --git a/scripts/probes/campaign.py b/scripts/probes/campaign.py index d277323bc..ff573d037 100644 --- a/scripts/probes/campaign.py +++ b/scripts/probes/campaign.py @@ -31,6 +31,7 @@ from probes.cores import Core, Cores from probes.entries import ( entry_dest, held_file, + held_variants, is_bios, profile_digest, region_matches, @@ -178,18 +179,8 @@ class Campaign: return held_file(entry, self.db, self.paths.root) def variant_files(self, entry: dict, primary: Path) -> list[Path]: - """The .variants/ copies of an entry's file the collection holds.""" - names = [entry["name"], *(entry.get("aliases") or ())] - out = [] - index = self.db.get("indexes", {}).get("by_name", {}) - for name in names: - for sha1 in index.get(name, ()): - path = (self.db["files"].get(sha1) or {}).get("path", "") - if "/.variants/" in path: - local = self.paths.root / path - if local.is_file() and local.resolve() != primary.resolve() and local not in out: - out.append(local) - return out + """The .variants/ copies standing in for an entry's primary.""" + return held_variants(entry, self.db, self.paths.root, primary) # -- guards --------------------------------------------------------------- diff --git a/scripts/probes/entries.py b/scripts/probes/entries.py index e576200b0..43efa9da9 100644 --- a/scripts/probes/entries.py +++ b/scripts/probes/entries.py @@ -35,6 +35,18 @@ def held_file(entry: dict, db: dict, root: Path) -> Path | None: return local if local.is_file() else None +def held_variants(entry: dict, db: dict, root: Path, primary: Path) -> list[Path]: + """The .variants/ copies standing in for the primary held_file chose, in + the checkout at root.""" + from resolve.localfile import variant_copies + + hint = entry.get("path") if isinstance(entry.get("path"), str) else "" + stored = primary.relative_to(root) if primary.is_relative_to(root) else primary + with working_directory(root): + paths = variant_copies(entry, db, str(stored), dest_hint=hint or "") + return [path if path.is_absolute() else root / path for path in map(Path, paths)] + + def entry_dest(entry: dict) -> str: """Where the core reads the entry, relative to its system directory.""" path = entry.get("path") diff --git a/scripts/resolve/localfile.py b/scripts/resolve/localfile.py index 2ad099be1..edf4a10b4 100644 --- a/scripts/resolve/localfile.py +++ b/scripts/resolve/localfile.py @@ -359,6 +359,52 @@ def held_record_for(file_entry: dict, db: dict) -> str | None: ) +def variant_copies( + file_entry: dict, db: dict, primary: str, dest_hint: str = "" +) -> list[str]: + """The .variants/ copies that stand in for the primary an entry resolved to. + + A primary's alternates are filed in its own directory's .variants/ under + its file name or one the entry answers to: the same name under another + system (the PC-98 font.rom beside QUASI88's PC-88 one) is another file. + A copy passes the shape the entry's code checks and, when the entry + declares hashes, agrees with every one of them, as the resolver requires. + """ + q = _lookup( + file_entry, db, _entry_hint(file_entry, dest_hint), [ + (cut_md5_owner(value, db.get("indexes", {}).get("by_md5", {})) or value) + if len(value) < 32 else value + for value in parse_hash_list(file_entry.get("md5")) + ], + composite_only=file_entry.get("zip_digest") == "composite", + ) + primary = os.path.normpath(primary) + folder = os.path.dirname(primary) + # A primary that is itself a .variants/ copy shares that directory. + home = folder if os.path.basename(folder) == ".variants" else os.path.join(folder, ".variants") + primary_name = variant_base_name(os.path.basename(primary)) + names = [primary_name, *(n for n in q.names_to_try if n != primary_name)] + hashed = bool( + q.sha1_candidates or q.sha256_candidates or q.crc_candidates + or (q.md5_list and not q.zipped_file) + ) + out: list[str] = [] + for name in names: + for match_sha1 in q.by_name.get(name, ()): + path = q.files_db.get(match_sha1, {}).get("path", "") + if ( + os.path.dirname(os.path.normpath(path)) == home + and os.path.normpath(path) != primary + and variant_base_name(os.path.basename(path)) in names + and (not hashed or _record_match_status(q, match_sha1)) + and _name_size_ok(q, match_sha1) + and os.path.exists(path) + and path not in out + ): + out.append(path) + return out + + def _held_record(q: _Lookup, match_sha1: str) -> Resolution | None: """The record's path and evidence, when it satisfies the entry and is on disk.""" path = q.files_db[match_sha1]["path"] diff --git a/tests/test_variant_copies.py b/tests/test_variant_copies.py new file mode 100644 index 000000000..4c88114c7 --- /dev/null +++ b/tests/test_variant_copies.py @@ -0,0 +1,88 @@ +"""The other copies of an entry come from the primary's own .variants/ folder. + +The campaign listed every .variants/ copy the database named after the entry, +so QUASI88's PC-88 font.rom was varied with a PC-98 font filed under the +same name in another system's folder. +""" + +from __future__ import annotations + +import tests + +import hashlib +import shutil +import tempfile +import unittest +from pathlib import Path + +from probes.entries import held_file, held_variants +from resolve.localfile import variant_copies + + +def _record(root: Path, path: str, body: bytes) -> tuple[str, dict]: + target = root / path + target.parent.mkdir(parents=True, exist_ok=True) + target.write_bytes(body) + sha1 = hashlib.sha1(body).hexdigest() + return sha1, {"path": path, "name": target.name, "size": len(body), + "md5": hashlib.md5(body).hexdigest(), "crc32": "", "sha256": ""} + + +class VariantCopies(unittest.TestCase): + def setUp(self) -> None: + self.root = Path(tempfile.mkdtemp()) + files = dict([ + _record(self.root, "bios/NEC/PC-88/font.rom", b"8" * 2048), + _record(self.root, "bios/NEC/PC-88/.variants/font.rom.11111111", b"9" * 2048), + _record(self.root, "bios/NEC/PC-88/.variants/font.rom.22222222", b"7" * 2048), + _record(self.root, "bios/NEC/PC-98/font.rom", b"P" * 4096), + _record(self.root, "bios/NEC/PC-98/.variants/font.rom.425a896a", b"Q" * 6144), + ]) + self.db = {"files": files, "indexes": { + "by_name": {"font.rom": list(files)}, "by_md5": {}, "by_crc32": {}, + "by_sha256": {}, "by_path_suffix": {}, + }} + self.entry = {"name": "font.rom", "path": "quasi88/font.rom", "system": "nec-pc-88"} + + def tearDown(self) -> None: + shutil.rmtree(self.root, ignore_errors=True) + + def test_only_the_primary_folder_answers(self) -> None: + primary = self.root / "bios/NEC/PC-88/font.rom" + self.assertEqual( + held_variants(self.entry, self.db, self.root, primary), + [self.root / "bios/NEC/PC-88/.variants/font.rom.11111111", + self.root / "bios/NEC/PC-88/.variants/font.rom.22222222"], + ) + + def test_a_variant_primary_shares_its_folder(self) -> None: + primary = self.root / "bios/NEC/PC-88/.variants/font.rom.11111111" + self.assertEqual( + held_variants(self.entry, self.db, self.root, primary), + [self.root / "bios/NEC/PC-88/.variants/font.rom.22222222"], + ) + + def test_a_declared_hash_refuses_a_copy_it_contradicts(self) -> None: + entry = {**self.entry, "md5": hashlib.md5(b"8" * 2048).hexdigest()} + primary = self.root / "bios/NEC/PC-88/font.rom" + self.assertEqual(held_variants(entry, self.db, self.root, primary), []) + accepted = {**self.entry, "md5": [hashlib.md5(b"8" * 2048).hexdigest(), + hashlib.md5(b"9" * 2048).hexdigest()]} + self.assertEqual(len(held_variants(accepted, self.db, self.root, primary)), 1) + + def test_a_size_the_code_checks_refuses_a_copy(self) -> None: + entry = {**self.entry, "size": 4096, "validation": ["size"]} + self.assertEqual( + variant_copies(entry, self.db, "bios/NEC/PC-98/font.rom"), [], + ) + + def test_held_file_and_variants_agree(self) -> None: + primary = held_file(self.entry, self.db, self.root) + self.assertIsNotNone(primary) + assert primary is not None + for copy in held_variants(self.entry, self.db, self.root, primary): + self.assertEqual(copy.parent.parent, primary.parent) + + +if __name__ == "__main__": + unittest.main()