diff --git a/scripts/packs/coverage.py b/scripts/packs/coverage.py index 74103973e..ab2858789 100644 --- a/scripts/packs/coverage.py +++ b/scripts/packs/coverage.py @@ -32,6 +32,7 @@ from resolve.cores import platform_scope_systems, resolve_platform_cores from resolve.launchers import launcher_unproven from resolve.nativemode import digest_algorithm, platform_mode, reads_file_contents from resolve.validation import ( + accepted_hashes, build_validation_index, agnostic_substitute, baseline_verdict, @@ -478,6 +479,8 @@ class _PlatformTally: ) if disagreement: result["discrepancy"] = f"{self.platform} says OK but {disagreement}" + if accepted := accepted_hashes(disagreement): + result["accepted"] = accepted return result, local_path def _aggregate( diff --git a/scripts/packs/verdicts.py b/scripts/packs/verdicts.py index d0066da82..538c6786b 100644 --- a/scripts/packs/verdicts.py +++ b/scripts/packs/verdicts.py @@ -6,6 +6,7 @@ from core.hashing import compute_hashes, md5sum, parse_hash_list from core.ziptools import check_inside_zip from resolve.nativemode import SIZE_AND_ANY_HASH, reads_file_contents from resolve.validation import ( + accepted_hashes, check_file_validation, existence_discrepancy, frontend_digest_matches, @@ -79,11 +80,15 @@ def verify_entry_existence( file_entry, local_path, db, validation_index, "bios", None, destination, owners, ) + accepted = accepted_hashes(disagreement) else: check = check_file_validation(local_path, name, validation_index) disagreement = f"{', '.join(check[1])} says {check[0]}" if check else None + accepted = accepted_hashes(check[0]) if check else {} if disagreement: result["discrepancy"] = f"file present (OK) but {disagreement}" + if accepted: + result["accepted"] = accepted return result diff --git a/scripts/resolve/validation.py b/scripts/resolve/validation.py index 9cd442835..326b63d10 100644 --- a/scripts/resolve/validation.py +++ b/scripts/resolve/validation.py @@ -49,6 +49,36 @@ _CONTENT_REASONS = { "masked_sha1": "masked_sha1 mismatch: the cleared image is not the one the code names", } + +class Refusal(str): + """A hash check's refusal: the message for people, the accepted values as data. + + ``accepted`` maps the algorithm to the values the check takes, in the + order the message lists them. Reports that look the dumps up read it, + never the message, which is free to change. + """ + + accepted: dict[str, list[str]] + + def __new__(cls, message: str, accepted: Mapping[str, Sequence[str]]) -> Refusal: + refusal = super().__new__(cls, message) + refusal.accepted = {algo: list(values) for algo, values in accepted.items()} + return refusal + + +def _hash_refusal(hash_type: str, got: str, accepted: Collection[str]) -> Refusal: + """The refusal of a hash check that does not take ``got``.""" + values = sorted(accepted) + return Refusal(f"{hash_type} mismatch: got {got}, accepted [{','.join(values)}]", + {hash_type: values}) + + +def accepted_hashes(reason: str | None) -> dict[str, list[str]]: + """The values a refused check accepts, by algorithm, empty when it + refused on anything but a hash.""" + return dict(reason.accepted) if isinstance(reason, Refusal) else {} + + # All reproducible validation types. _HASH_CHECKS = frozenset({"crc32", "md5", "sha1", "adler32"}) @@ -134,8 +164,7 @@ def _rule_failure( if not hashes: hashes.update(compute_hashes(local_path)) if hashes[hash_type].lower() not in rule[hash_type]: - accepted = ",".join(sorted(rule[hash_type])) - return f"{hash_type} mismatch: got {hashes[hash_type]}, accepted [{accepted}]" + return _hash_refusal(hash_type, hashes[hash_type], rule[hash_type]) if rule["adler32"] and "adler32" not in skip: if rule["adler32_byteswap"]: actual = _adler32_byteswapped(local_path) @@ -417,7 +446,11 @@ def _merged_reason(reasons: list[str]) -> str: if len(parsed) == len(reasons) and len({(m.group(1), m.group(2)) for m in parsed}) == 1: accepted = sorted({v for m in parsed for v in m.group(3).split(",") if v}, key=lambda v: (len(v), v)) - return f"{parsed[0].group(1)}: got {parsed[0].group(2)}, accepted [{','.join(accepted)}]" + message = f"{parsed[0].group(1)}: got {parsed[0].group(2)}, accepted [{','.join(accepted)}]" + algorithms = {algo for reason in reasons for algo in accepted_hashes(reason)} + if len(algorithms) == 1 and all(isinstance(reason, Refusal) for reason in reasons): + return Refusal(message, {algorithms.pop(): accepted}) + return message return reasons[0] @@ -544,13 +577,8 @@ def _merged_hash_failure( hash_type in checks and entry[hash_type] and hashes[hash_type].lower() not in entry[hash_type] ): - expected = ",".join(sorted(entry[hash_type])) emus = _emulators_for_check(hash_type, pe) - return ( - f"{hash_type} mismatch: got {hashes[hash_type]}, " - f"accepted [{expected}]", - emus, - ) + return _hash_refusal(hash_type, hashes[hash_type], entry[hash_type]), emus if entry["adler32"]: actual_adler = hashes["adler32"].lower() if entry.get("adler32_byteswap"): @@ -1159,7 +1187,8 @@ def validated_choice( if better: return better, None reason, failing = failed - return local_path, f"{', '.join(failing)} says {reason}" + said = f"{', '.join(failing)} says {reason}" + return local_path, Refusal(said, reason.accepted) if isinstance(reason, Refusal) else said def existence_discrepancy(file_entry: dict, local_path: str, platform_display: str) -> str: diff --git a/scripts/sitegen/gaps.py b/scripts/sitegen/gaps.py index 94464737a..107aabb4f 100644 --- a/scripts/sitegen/gaps.py +++ b/scripts/sitegen/gaps.py @@ -4,7 +4,6 @@ from __future__ import annotations import dataclasses import html -import re from dataclasses import dataclass from typing import TYPE_CHECKING @@ -32,28 +31,27 @@ if TYPE_CHECKING: # Contributing page -_ACCEPTED_RE = re.compile(r"\b(sha1|sha256|md5|crc32) mismatch: got \S+, accepted \[([^\]]*)\]") - - -def _held_accepted(discrepancy: str, db: dict | None) -> str: +def _held_accepted(accepted: dict[str, list[str]], db: dict | None) -> str: """Where the collection keeps a dump the emulator accepts, if it does. The table printed the hash the emulator wants and stopped there, while the collection held that exact dump for every one of the ten rows. + ``accepted`` is the verify detail's field, the values by algorithm. """ - match = _ACCEPTED_RE.search(discrepancy or "") - if not match or not db: + if not accepted or not db: return "" - algo, accepted = match.group(1), match.group(2) indexes = db.get("indexes", {}) - for value in re.split(r"[\s,]+", accepted.lower()): - if algo == "sha1": - sha1 = value if value in db.get("files", {}) else None - else: - sha1 = indexes.get(f"by_{algo}", {}).get(value) - path = db.get("files", {}).get(sha1 or "", {}).get("path") - if path: - return path + files = db.get("files", {}) + for algo, values in accepted.items(): + for value in values: + value = value.lower() + if algo == "sha1": + sha1 = value if value in files else None + else: + sha1 = indexes.get(f"by_{algo}", {}).get(value) + path = files.get(sha1 or "", {}).get("path") + if path: + return path return "" @@ -165,6 +163,7 @@ def _render_problem_files( entry = mismatch_files.setdefault(fname, { "name": fname, "platforms": [], "discrepancy": p["discrepancy"], + "accepted": p.get("accepted") or {}, }) entry["platforms"].append(p["platform"]) @@ -251,7 +250,7 @@ def _render_problem_files( for fname in sorted(mismatch_files): f = mismatch_files[fname] plats = ", ".join(sorted(set(f["platforms"]))) - held = _held_accepted(f["discrepancy"], db) + held = _held_accepted(f["accepted"], db) cell = f"[{code_span(held)}]({fetch_url(held)})" if held else "-" lines.append(f"| `{fname}` | {plats} | {_reason_cell(f['discrepancy'])} | {cell} |") lines.append("") @@ -396,6 +395,7 @@ def _problem_record(pname: str, cov: dict, d: dict, entry: dict) -> dict: "category": d.get("category", "bios"), "reason": d.get("reason", ""), "discrepancy": d.get("discrepancy", ""), + "accepted": d.get("accepted") or {}, "system": d.get("system", ""), # The untested table said "declared hash contradicted by the local # file" and named neither: the reader could not look for the dump diff --git a/tests/test_site_exports.py b/tests/test_site_exports.py index 5778687e9..4cdd50028 100644 --- a/tests/test_site_exports.py +++ b/tests/test_site_exports.py @@ -1055,12 +1055,32 @@ class AMismatchNamesTheDumpTheCollectionHolds(unittest.TestCase): db = {"files": {"s1": {"path": "bios/Microsoft/MSX/CLK/msx-japanese.rom"}}, "indexes": {"by_sha256": {"ab" * 32: "s1"}, "by_crc32": {"84f68bf9": "s1"}}} - text = ("RetroBat says OK but ares says sha256 mismatch: got " + "cd" * 32 - + ", accepted [" + "ab" * 32 + "]") - self.assertEqual(_held_accepted(text, db), "bios/Microsoft/MSX/CLK/msx-japanese.rom") - self.assertEqual(_held_accepted("xroar says crc32 mismatch: got 1, accepted [84f68bf9]", db), - "bios/Microsoft/MSX/CLK/msx-japanese.rom") - self.assertEqual(_held_accepted("size mismatch: got 1, accepted [256]", db), "") + held = "bios/Microsoft/MSX/CLK/msx-japanese.rom" + self.assertEqual(_held_accepted({"sha256": ["AB" * 32]}, db), held) + self.assertEqual(_held_accepted({"crc32": ["00000000", "84f68bf9"]}, db), held) + self.assertEqual(_held_accepted({}, db), "") + + def test_verify_carries_what_the_emulator_accepts(self): + """The accepted values travel as data: a reworded message cannot + empty the column.""" + import hashlib + import tempfile + from pathlib import Path + + from packs.verdicts import verify_entry_existence + from resolve.validation import build_validation_index + + wanted = "84f68bf9" + index = build_validation_index({"xroar": {"files": [ + {"name": "a.rom", "crc32": wanted, "validation": ["crc32"]}]}}) + with tempfile.TemporaryDirectory() as tmp: + path = Path(tmp) / "a.rom" + path.write_bytes(b"x") + for db in (None, {"files": {hashlib.sha1(b"x").hexdigest(): {"path": "a.rom"}}, + "indexes": {}}): + result = verify_entry_existence({"name": "a.rom"}, str(path), index, db) + self.assertIn("crc32 mismatch", result["discrepancy"]) + self.assertEqual(result["accepted"], {"crc32": [wanted]}) class ADataDirectoryFileCanBeLookedUp(unittest.TestCase):