diff --git a/scripts/scraper/_hash_merge.py b/scripts/scraper/_hash_merge.py index 6cdcfd71e..023576d91 100644 --- a/scripts/scraper/_hash_merge.py +++ b/scripts/scraper/_hash_merge.py @@ -62,6 +62,23 @@ def settled_required(entry: dict, set_data: dict) -> bool: return current +def mame_pin(profile: dict[str, Any], hashes: dict[str, Any]) -> dict[str, Any]: + """The core_version and upstream_commit a refresh gives a MAME profile.""" + # The version follows the refs only for profiles already labelled with a + # MAME release. A derivative pins its own line: same_cdi ("Git") and a7800 + # ("5.2") would otherwise be relabelled with a MAME version they were + # never built from. + if not _MAME_RELEASE_RE.match(str(profile.get("core_version") or "")): + return {} + pin = {"core_version": hashes.get("version", profile.get("core_version"))} + # The refs about to be written are line numbers of that release's + # mamedev/mame tree: the pin moves with them, or the profile cites lines + # its declared revision does not hold. + if profile.get("upstream") == _MAMEDEV and hashes.get("commit"): + pin["upstream_commit"] = hashes["commit"] + return pin + + def merge_mame_profile( profile: dict[str, Any], hashes: dict[str, Any], @@ -79,18 +96,7 @@ def merge_mame_profile( is write_mame_profile's. """ profile = dict(profile) - - # The version follows the refs only for profiles already labelled with a - # MAME release. A derivative pins its own line: same_cdi ("Git") and a7800 - # ("5.2") would otherwise be relabelled with a MAME version they were - # never built from. - if _MAME_RELEASE_RE.match(str(profile.get("core_version") or "")): - profile["core_version"] = hashes.get("version", profile.get("core_version")) - # The refs about to be written are line numbers of that release's - # mamedev/mame tree: the pin moves with them, or the profile cites - # lines its declared revision does not hold. - if profile.get("upstream") == _MAMEDEV and hashes.get("commit"): - profile["upstream_commit"] = hashes["commit"] + profile.update(mame_pin(profile, hashes)) files = profile.get("files", []) bios_zip, non_bios = _split_files(files, lambda f: f.get("category") == "bios_zip") @@ -273,12 +279,21 @@ def diff_mame( 1 for s in existing_by_name if s in bios_sets ) + # A release that leaves every BIOS root set as it was still moves the pin. + # merge_mame_profile writes it, so the write gate has to see it. + pin = { + field: (profile.get(field), value) + for field, value in mame_pin(profile, hashes).items() + if profile.get(field) != value + } + return { "added": added, "updated": updated, "removed": [], "unchanged": unchanged, "out_of_scope": out_of_scope, + "pin": pin, } diff --git a/scripts/scraper/mame_hash_scraper.py b/scripts/scraper/mame_hash_scraper.py index 99056a023..93950000e 100644 --- a/scripts/scraper/mame_hash_scraper.py +++ b/scripts/scraper/mame_hash_scraper.py @@ -176,12 +176,15 @@ def _format_diff( removed = diff.get("removed", []) unchanged = diff.get("unchanged", 0) - if not added and not updated and not removed: + pin = diff.get("pin", {}) + + if not added and not updated and not removed and not pin: lines.append(f" {name}:") lines.append(" no changes") return lines lines.append(f" {name}:") + lines.extend(f" ^ {field} moves from {old} to {new}" for field, (old, new) in pin.items()) if show_added: bios_sets = hashes.get("bios_sets", {}) @@ -272,7 +275,7 @@ def _run(args: argparse.Namespace) -> None: if not args.dry_run: updated = diff.get("updated", []) added = diff.get("added", []) if is_main else [] - if added or updated: + if added or updated or diff.get("pin"): write_mame_profile( profile_path, merge_mame_profile(profile, hashes, add_new=is_main), ) diff --git a/tests/test_hash_merge.py b/tests/test_hash_merge.py index 826a27eb0..a509c3966 100644 --- a/tests/test_hash_merge.py +++ b/tests/test_hash_merge.py @@ -594,6 +594,28 @@ class TestDiff(unittest.TestCase): self.assertEqual(len(diff["removed"]), 0) self.assertEqual(diff["unchanged"], 0) + def test_diff_mame_reports_the_pin_the_merge_writes(self) -> None: + """A release that changes no set still moves core_version and upstream_commit.""" + hashes = _make_mame_hashes() + profile = merge_mame_profile( + _make_mame_profile(upstream="https://github.com/mamedev/mame"), hashes, + ) + hashes = {**hashes, "version": "0.287", "commit": "cccc"} + + diff = compute_diff(profile, hashes, mode="mame") + merged = merge_mame_profile(profile, hashes) + + self.assertEqual(diff["updated"], []) + self.assertEqual(diff["pin"], { + "core_version": ("0.286", "0.287"), + "upstream_commit": ("abc123", "cccc"), + }) + self.assertEqual( + {field: merged[field] for field in diff["pin"]}, + {field: new for field, (_old, new) in diff["pin"].items()}, + ) + self.assertEqual(compute_diff(merged, hashes, mode="mame")["pin"], {}) + def test_diff_mame_out_of_scope(self) -> None: """Items in profile but not in scraper output = out of scope, not removed.""" hashes = _make_mame_hashes() diff --git a/tests/test_mame_devices.py b/tests/test_mame_devices.py index e227b1e1c..00ecc8e94 100644 --- a/tests/test_mame_devices.py +++ b/tests/test_mame_devices.py @@ -1035,7 +1035,7 @@ class TheDevicesFollowTheRelease(unittest.TestCase): if moves: profile.write_text('core_version: "0.290"\nfiles: []\n') - merged = [] + merged: list = [] with mock.patch.multiple( scraper, _EMULATORS_DIR=Path(directory), @@ -1059,6 +1059,34 @@ class TheDevicesFollowTheRelease(unittest.TestCase): self.assertEqual(self._run(moves=False), []) self.assertEqual(self._run(moves=True, dry_run=True), []) + def test_a_release_with_no_set_change_still_moves_the_pin(self) -> None: + """No BIOS root set changed: the pin moves anyway, and the devices follow.""" + import argparse + from unittest import mock + + from scripts.scraper import mame_hash_scraper as scraper + + with tempfile.TemporaryDirectory() as directory: + profile = Path(directory) / "mame.yml" + profile.write_text('core_version: "0.289"\nfiles: []\n') + merged: list = [] + out = io.StringIO() + with mock.patch.multiple( + scraper, + _EMULATORS_DIR=Path(directory), + _fetch_hashes=lambda force: { + "bios_sets": {}, "version": "0.290", "commit": "cccc", + }, + _find_mame_profiles=lambda: [profile], + fetch_release_listxml=lambda version: Path(f"mame{version}lx.zip"), + _merge_devices=merged.append, + ), mock.patch("sys.stdout", new=out): + scraper._run(argparse.Namespace(force=False, json=False, dry_run=False)) + self.assertIn('core_version: "0.290"', profile.read_text()) + self.assertIn("core_version moves from 0.289 to 0.290", out.getvalue()) + (args,) = merged + self.assertEqual(args.listxml, Path("mame0.290lx.zip")) + if __name__ == "__main__": unittest.main()