fix: write the required and pin a refresh sets

This commit is contained in:
Abdessamad Derraz committed 2026-11-24 00:20:03 +01:00
1 parent b365b95632
commit de1a4d8902
2 files changed
+76 -5

No files matched your search

+42 -5
View File
@@ -423,7 +423,10 @@ def write_mame_profile(path: str | Path, data: dict) -> None:
original = p.read_text(encoding="utf-8")
patched = _patch_core_version(original, data.get("core_version", ""))
patched = _patch_scalar(patched, "upstream_commit", data.get("upstream_commit", ""))
patched = _set_scalar(
patched, "upstream_commit", data.get("upstream_commit", ""),
after=("source_commit", "upstream"),
)
patched = _patch_bios_entries(patched, data.get("files", []))
patched = _append_new_entries(patched, data.get("files", []), original)
@@ -448,6 +451,22 @@ def _patch_scalar(text: str, key: str, value: str) -> str:
)
def _set_scalar(text: str, key: str, value: str, after: tuple[str, ...]) -> str:
"""Write a top-level scalar, adding its line after the first key of *after* found.
The merge sets the value whether or not the profile carries the key: a
patch that only rewrites an existing line would report the change and
leave the file without it.
"""
if not value or re.search(rf"^{re.escape(key)}:", text, re.MULTILINE):
return _patch_scalar(text, key, value)
for anchor in after:
match = re.search(rf"^{re.escape(anchor)}:[^\n]*$", text, re.MULTILINE)
if match:
return f'{text[: match.end()]}\n{key}: "{value}"{text[match.end():]}'
raise ValueError(f"no {' or '.join(after)} line to place {key} after")
def _owned_end(lines: list[str], start_line: int) -> int:
"""The last "owned" line of the entry starting at start_line.
@@ -479,6 +498,27 @@ def _patch_source_ref(lines: list[str], start_line: int, last_owned: int, source
return last_owned + 1
def _patch_required(lines: list[str], start_line: int, last_owned: int, required: bool) -> int:
"""Rewrite required in-place, or add it before category when it is false.
An absent line reads as true, so only a false needs a line of its own.
Returns the new last owned line.
"""
value = f" required: {str(required).lower()}"
for j in range(start_line + 1, last_owned + 1):
if re.match(r"^ required:", lines[j]):
lines[j] = value
return last_owned
if required:
return last_owned
at = next(
(j for j in range(start_line + 1, last_owned + 1) if re.match(r"^ category:", lines[j])),
last_owned + 1,
)
lines.insert(at, value)
return last_owned + 1
def _drop_contents(lines: list[str], start_line: int, last_owned: int) -> int:
"""Remove an existing contents block. Returns the new last owned line."""
contents_start = None
@@ -510,10 +550,7 @@ def _patch_entry(lines: list[str], start_line: int, fe: dict) -> None:
# The source settles required for a set the machine runs without
if "required" in fe:
for j in range(start_line + 1, last_owned + 1):
if re.match(r"^ required:", lines[j]):
lines[j] = f" required: {str(fe['required']).lower()}"
break
last_owned = _patch_required(lines, start_line, last_owned, fe["required"])
last_owned = _drop_contents(lines, start_line, last_owned)
+34
View File
@@ -376,6 +376,40 @@ class TheMamePinMovesWithItsRefs(unittest.TestCase):
self.assertEqual(yaml.safe_load(text)["upstream_commit"], "a" * 40)
class TheWrittenFileHoldsWhatTheDiffReports(unittest.TestCase):
"""The diff counted a set refreshed when required moved, but the writer
only rewrote lines already there: a set without a required line, or a
profile without upstream_commit, was reported on every run and never
written."""
def test_a_second_diff_finds_nothing_left_to_write(self):
hashes = _make_mame_hashes(commit="f" * 40, version="0.289")
hashes["bios_sets"] = {"mac2bios": {
"source_file": "src/mame/seta/macs.cpp", "source_line": 776,
"roms": [], "required": False,
}}
with tempfile.TemporaryDirectory() as td:
path = Path(td) / "mame.yml"
path.write_text(
'emulator: MAME\nupstream: "https://github.com/mamedev/mame"\n'
'source_commit: "' + "a" * 40 + '"\ncore_version: "0.288"\nfiles:\n'
" - name: mac2bios.zip\n category: bios_zip\n"
' source_ref: "src/mame/seta/macs.cpp:776"\n',
encoding="utf-8",
)
self.assertEqual(compute_diff(load_profile(path), hashes)["updated"], ["mac2bios"])
_refresh_mame(path, hashes)
written = load_profile(path)
self.assertEqual(compute_diff(written, hashes)["updated"], [])
entry = next(f for f in written["files"] if f["name"] == "mac2bios.zip")
self.assertIs(entry["required"], False)
self.assertEqual(written["upstream_commit"], "f" * 40)
self.assertEqual(written["core_version"], "0.289")
self.assertEqual(
list(written)[:4], ["emulator", "upstream", "source_commit", "upstream_commit"]
)
class TheScraperClonesTheReleaseItLabels(unittest.TestCase):
"""Cloning master and labelling it with the latest release tag gave
master's line numbers and hashes under the release's name."""