mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-12-02 06:18:51 -06:00
refactor: split the hash merge by subject
Writing the merged profile text moves to _hash_write, the generated MAME device and software list sections to _mame_sections.
This commit is contained in:
1 parent
263a64c0e0
commit
65c208b8cd
10 files changed
+827
-803
No files matched your search
@@ -7,25 +7,16 @@ Supports two strategies:
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import re
|
||||
import shutil
|
||||
import textwrap
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
|
||||
from model.profiles import PROFILE_BACKUP_SUFFIX
|
||||
from core.artifacts import write_text_atomic
|
||||
from model.safeparse import yaml_load
|
||||
from model.sources import source_yaml_file
|
||||
|
||||
|
||||
_MAME_RELEASE_RE = re.compile(r"^0\.\d+")
|
||||
_MAMEDEV = "https://github.com/mamedev/mame"
|
||||
DEVICE_SECTION = " # --- Device ROM sets (generated from MAME -listxml) ---"
|
||||
SOFTLIST_SECTION = " # --- Software lists (generated from MAME -listxml) ---"
|
||||
_FILES_END = re.compile(r"^\S", re.M)
|
||||
|
||||
|
||||
def mame_ref_role(profile: dict[str, Any]) -> str:
|
||||
@@ -423,770 +414,3 @@ def _contents_differ(old: list[dict], new: list[dict]) -> bool:
|
||||
if prev.get("sha1") != entry.get("sha1"):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def write_mame_profile(path: str | Path, data: dict) -> None:
|
||||
"""Write merged profile using text-based patching to preserve formatting.
|
||||
|
||||
Instead of yaml.dump (which destroys comments, quoting, indentation),
|
||||
this reads the original file as text, patches specific fields
|
||||
(core_version, contents, source_ref), and appends new entries.
|
||||
"""
|
||||
p = Path(path)
|
||||
backup = p.with_suffix(PROFILE_BACKUP_SUFFIX)
|
||||
shutil.copy2(p, backup)
|
||||
|
||||
original = p.read_text(encoding="utf-8")
|
||||
patched = _patch_core_version(original, data.get("core_version", ""))
|
||||
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)
|
||||
|
||||
write_text_atomic(str(p), patched)
|
||||
|
||||
|
||||
def _patch_core_version(text: str, version: str) -> str:
|
||||
"""Replace core_version value in-place."""
|
||||
return _patch_scalar(text, "core_version", version)
|
||||
|
||||
|
||||
def _patch_scalar(text: str, key: str, value: str) -> str:
|
||||
"""Replace a top-level scalar in-place, quoted. An absent value changes nothing."""
|
||||
if not value:
|
||||
return text
|
||||
return re.sub(
|
||||
rf"^({re.escape(key)}:\s*).*$",
|
||||
rf'\g<1>"{value}"',
|
||||
text,
|
||||
count=1,
|
||||
flags=re.MULTILINE,
|
||||
)
|
||||
|
||||
|
||||
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.
|
||||
|
||||
Owned = indented with 4+ spaces (field lines of this entry).
|
||||
"""
|
||||
last_owned = start_line
|
||||
for j in range(start_line + 1, len(lines)):
|
||||
stripped = lines[j].strip()
|
||||
if not stripped:
|
||||
break # blank line = end of entry
|
||||
if stripped.startswith("#"):
|
||||
break # comment = belongs to next entry
|
||||
if re.match(r"^ - ", lines[j]):
|
||||
break # next list item
|
||||
if re.match(r"^ ", lines[j]) or re.match(r"^ \w", lines[j]):
|
||||
last_owned = j
|
||||
else:
|
||||
break
|
||||
return last_owned
|
||||
|
||||
|
||||
def _patch_source_ref(lines: list[str], start_line: int, last_owned: int, source_ref: str) -> int:
|
||||
"""Patch source_ref in-place, or add it. Returns the new last owned line."""
|
||||
for j in range(start_line + 1, last_owned + 1):
|
||||
if re.match(r"^ source_ref:", lines[j]):
|
||||
lines[j] = f' source_ref: "{source_ref}"'
|
||||
return last_owned
|
||||
lines.insert(last_owned + 1, f' source_ref: "{source_ref}"')
|
||||
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
|
||||
contents_end = None
|
||||
for j in range(start_line + 1, last_owned + 1):
|
||||
if re.match(r"^ contents:", lines[j]):
|
||||
contents_start = j
|
||||
elif contents_start is not None:
|
||||
if re.match(r"^ ", lines[j]):
|
||||
contents_end = j
|
||||
else:
|
||||
break
|
||||
if contents_start is None:
|
||||
return last_owned
|
||||
if contents_end is None:
|
||||
contents_end = contents_start
|
||||
del lines[contents_start : contents_end + 1]
|
||||
return last_owned - (contents_end - contents_start + 1)
|
||||
|
||||
|
||||
def _patch_entry(lines: list[str], start_line: int, fe: dict) -> None:
|
||||
contents = fe.get("contents", [])
|
||||
source_ref = fe.get("source_ref", "")
|
||||
|
||||
last_owned = _owned_end(lines, start_line)
|
||||
|
||||
if source_ref:
|
||||
last_owned = _patch_source_ref(lines, start_line, last_owned, source_ref)
|
||||
|
||||
# The source settles required for a set the machine runs without
|
||||
if "required" in fe:
|
||||
last_owned = _patch_required(lines, start_line, last_owned, fe["required"])
|
||||
|
||||
last_owned = _drop_contents(lines, start_line, last_owned)
|
||||
|
||||
# Insert new contents after last owned line
|
||||
if contents:
|
||||
new_lines = _format_contents(contents).split("\n")
|
||||
for k, cl in enumerate(new_lines):
|
||||
lines.insert(last_owned + 1 + k, cl)
|
||||
|
||||
|
||||
def _patch_bios_entries(text: str, files: list[dict]) -> str:
|
||||
"""Patch contents, source_ref and required of bios_zip entries in-place.
|
||||
|
||||
Processes entries in reverse order to preserve line offsets.
|
||||
Each entry's "owned" lines are: the `- name:` line plus all indented
|
||||
lines that follow (4+ spaces), stopping at blank lines, comments,
|
||||
or the next `- name:`.
|
||||
"""
|
||||
patches: dict[str, dict] = {}
|
||||
for fe in files:
|
||||
if fe.get("category") != "bios_zip":
|
||||
continue
|
||||
patches[fe["name"]] = fe
|
||||
|
||||
if not patches:
|
||||
return text
|
||||
|
||||
lines = text.split("\n")
|
||||
# Find all entry start positions (line indices)
|
||||
entry_starts: list[tuple[int, str]] = []
|
||||
for i, line in enumerate(lines):
|
||||
m = re.match(r"^ - name:\s*(.+?)\s*$", line)
|
||||
if m:
|
||||
entry_starts.append((i, m.group(1).strip('"').strip("'")))
|
||||
|
||||
# Process in reverse so line insertions don't shift indices
|
||||
for idx in range(len(entry_starts) - 1, -1, -1):
|
||||
start_line, entry_name = entry_starts[idx]
|
||||
if entry_name in patches:
|
||||
_patch_entry(lines, start_line, patches[entry_name])
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def _append_new_entries(text: str, files: list[dict], original: str) -> str:
|
||||
"""Append new bios_zip entries (system=None) that aren't in the original."""
|
||||
# Parse original to get existing entry names (more reliable than text search)
|
||||
existing_data = yaml_load(original) or {}
|
||||
existing_names = {f["name"] for f in existing_data.get("files", [])}
|
||||
|
||||
new_entries = []
|
||||
for fe in files:
|
||||
if fe.get("category") != "bios_zip" or fe.get("system") is not None:
|
||||
continue
|
||||
if fe["name"] in existing_names:
|
||||
continue
|
||||
new_entries.append(fe)
|
||||
|
||||
if not new_entries:
|
||||
return text
|
||||
|
||||
lines = []
|
||||
profile_type = str(existing_data.get("type") or "")
|
||||
for fe in new_entries:
|
||||
lines.append(f"\n - name: {_yaml_scalar(fe['name'])}")
|
||||
lines.extend(set_path_lines(profile_type, fe["name"]))
|
||||
lines.append(f" required: {str(fe['required']).lower()}")
|
||||
lines.append(" category: bios_zip")
|
||||
if fe.get("source_ref"):
|
||||
lines.append(f' source_ref: "{fe["source_ref"]}"')
|
||||
if fe.get("contents"):
|
||||
lines.append(_format_contents(fe["contents"]))
|
||||
|
||||
if lines:
|
||||
# New BIOS root sets go before the generated sections, which run to
|
||||
# the end of the files list.
|
||||
at = generated_start(text)
|
||||
if at is None:
|
||||
text = text.rstrip("\n") + "\n" + "\n".join(lines) + "\n"
|
||||
else:
|
||||
text = text[:at].rstrip("\n") + "\n" + "\n".join(lines) + "\n\n" + text[at:]
|
||||
|
||||
return text
|
||||
|
||||
|
||||
def _device_section_start(text: str) -> int | None:
|
||||
"""Offset of the generated device section header, if the profile has one."""
|
||||
match = re.search(rf"^{re.escape(DEVICE_SECTION)}$", text, re.M)
|
||||
return match.start() if match else None
|
||||
|
||||
|
||||
def _softlist_section_start(text: str) -> int | None:
|
||||
"""Offset of the generated software list section header, if any."""
|
||||
match = re.search(rf"^{re.escape(SOFTLIST_SECTION)}$", text, re.M)
|
||||
return match.start() if match else None
|
||||
|
||||
|
||||
def generated_start(text: str) -> int | None:
|
||||
"""Offset of the first generated section: what a reader wrote ends there."""
|
||||
found = [at for at in (_softlist_section_start(text), _device_section_start(text)) if at is not None]
|
||||
return min(found) if found else None
|
||||
|
||||
|
||||
def _device_contents(roms: list[dict], old: list[dict]) -> list[dict]:
|
||||
"""Contents of a device set, keeping the member descriptions a reader wrote."""
|
||||
written = {str(c.get("name")): c.get("description") for c in old or []}
|
||||
contents = _build_contents(roms)
|
||||
for member in contents:
|
||||
if written.get(member["name"]):
|
||||
member["description"] = written[member["name"]]
|
||||
elif member.get("description"):
|
||||
# A biosset description is MAME's text, quoted as written.
|
||||
member["description"] = f"`{member['description']}`"
|
||||
return contents
|
||||
|
||||
|
||||
def _device_note(description: str) -> str:
|
||||
"""The note of a generated entry. The device name is MAME's own text,
|
||||
quoted verbatim in backticks: `Sun-1 Workstation Monitor; PROM 1&3: Rev AJ`
|
||||
is a name, not a sentence."""
|
||||
return f"Device ROM set, loaded when a machine instantiates `{description}`."
|
||||
|
||||
|
||||
def _zip_scalar(name: str) -> str:
|
||||
"""An archive name as the written entries spell it: bare when YAML keeps it."""
|
||||
return name if re.fullmatch(r"[a-z0-9_]+\.zip", name) else _yaml_scalar(name)
|
||||
|
||||
|
||||
_PLAIN_SYSTEM = re.compile(r"^[a-z][a-z0-9-]*$")
|
||||
|
||||
|
||||
def _system_lines(systems: list[str]) -> list[str]:
|
||||
"""`system:` for one system, `systems:` for several, nothing for none."""
|
||||
def scalar(value: str) -> str:
|
||||
return value if _PLAIN_SYSTEM.match(value) else _yaml_scalar(value)
|
||||
|
||||
if not systems:
|
||||
return []
|
||||
if len(systems) == 1:
|
||||
return [f" system: {scalar(systems[0])}"]
|
||||
return [" systems:", *(f" - {scalar(s)}" for s in systems)]
|
||||
|
||||
|
||||
def _entry_systems(entry: dict) -> list[str]:
|
||||
"""The systems an entry states, in the shape _system_lines writes."""
|
||||
if entry.get("system"):
|
||||
return [str(entry["system"])]
|
||||
return [str(s) for s in entry.get("systems") or []]
|
||||
|
||||
|
||||
def _section_names(text: str) -> set[str]:
|
||||
"""Entry names inside the generated device section."""
|
||||
start = _device_section_start(text)
|
||||
if start is None:
|
||||
return set()
|
||||
end = _FILES_END.search(text, text.index("\n", start) + 1)
|
||||
section = text[start : end.start() if end else len(text)]
|
||||
return {
|
||||
m.group(1).strip('"').strip("'")
|
||||
for m in re.finditer(r"^ - name:\s*(.+?)\s*$", section, re.M)
|
||||
}
|
||||
|
||||
|
||||
def _set_section_systems(text: str, systems: dict[str, list[str]]) -> str:
|
||||
"""Rewrite the system fields of the named entries of the device section.
|
||||
|
||||
Any `system:` or `systems:` the entry holds is dropped and the new one
|
||||
written right after its category, where a written entry keeps it.
|
||||
"""
|
||||
if not systems:
|
||||
return text
|
||||
start = _device_section_start(text)
|
||||
if start is None:
|
||||
return text
|
||||
return text[:start] + _rewrite_systems(text[start:], systems)
|
||||
|
||||
|
||||
def _rewrite_systems(section: str, systems: dict[str, list[str]]) -> str:
|
||||
"""Rewrite the system fields of the named entries of one stretch of text."""
|
||||
out: list[str] = []
|
||||
current = ""
|
||||
in_list = False
|
||||
for line in section.split("\n"):
|
||||
entry_head = re.match(r"^ - name:\s*(.+?)\s*$", line)
|
||||
if entry_head:
|
||||
current = entry_head.group(1).strip('"').strip("'")
|
||||
if current in systems:
|
||||
if in_list and line.startswith(" - "):
|
||||
continue
|
||||
in_list = False
|
||||
if re.match(r"^ system:", line):
|
||||
continue
|
||||
if re.match(r"^ systems:\s*$", line):
|
||||
in_list = True
|
||||
continue
|
||||
out.append(line)
|
||||
if current in systems and re.match(r"^ category:", line):
|
||||
out.extend(_system_lines(systems[current]))
|
||||
return "\n".join(out)
|
||||
|
||||
|
||||
def set_path_lines(profile_type: str, name: str) -> list[str]:
|
||||
"""Where each build of a MAME profile reads a set it lists.
|
||||
|
||||
The libretro build searches the content folder, then <system>/mame/bios
|
||||
and <system>/mame/roms (libretro-internal/retro_init.cpp,
|
||||
Set_Path_Option), never the system folder itself. The standalone build
|
||||
reads the top of its rompath.
|
||||
"""
|
||||
if "libretro" not in profile_type:
|
||||
return []
|
||||
lines = [f' path: "mame/bios/{name}"']
|
||||
if "standalone" in profile_type:
|
||||
lines.append(f' standalone_path: "{name}"')
|
||||
return lines
|
||||
|
||||
|
||||
def _format_device_entry(name: str, device: dict, role: str, profile_type: str = "") -> str:
|
||||
"""One generated device entry, in the field order of the written ones."""
|
||||
lines = [
|
||||
f" - name: {_zip_scalar(name)}",
|
||||
*set_path_lines(profile_type, name),
|
||||
" required: false",
|
||||
" category: bios_zip",
|
||||
*_system_lines(device.get("systems") or []),
|
||||
]
|
||||
if device.get("source_line"):
|
||||
lines.append(f' source_ref: "{_build_source_ref(device, role)}"')
|
||||
lines.append(" note: >-")
|
||||
lines.extend(
|
||||
textwrap.wrap(
|
||||
_device_note(device.get("description") or name[:-4]),
|
||||
width=78,
|
||||
initial_indent=" ",
|
||||
subsequent_indent=" ",
|
||||
break_on_hyphens=False,
|
||||
)
|
||||
)
|
||||
lines.append(_format_contents(_device_contents(device["roms"], [])))
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def merge_mame_devices(
|
||||
profile_path: str,
|
||||
devices: dict[str, dict],
|
||||
write: bool = False,
|
||||
) -> dict[str, list[str]]:
|
||||
"""Merge MAME device ROM sets into a profile.
|
||||
|
||||
*devices* maps a device shortname to its listxml description, its dumped
|
||||
ROMs and, when found, source_file and source_line. A set the profile
|
||||
already lists keeps every field a reader wrote: its contents are refreshed
|
||||
with the member descriptions kept, and a source_ref is added only where it
|
||||
has none. A set it lacks is written under the generated section, in name
|
||||
order, with required false. An entry of another category under a device
|
||||
name is reported, never touched.
|
||||
|
||||
A device carrying `systems` (attribute_systems) has them written on its
|
||||
entry when the entry is in the generated section: `system:` for one,
|
||||
`systems:` for several, neither for none. An entry above the section
|
||||
keeps the system a reader gave it.
|
||||
"""
|
||||
path = Path(profile_path)
|
||||
text = path.read_text(encoding="utf-8")
|
||||
profile = yaml_load(text) or {}
|
||||
role = mame_ref_role(profile)
|
||||
entries: dict[str, list[dict]] = {}
|
||||
for entry in profile.get("files") or []:
|
||||
entries.setdefault(str(entry.get("name")), []).append(entry)
|
||||
|
||||
report: dict[str, list[str]] = {
|
||||
"added": [], "updated": [], "unchanged": [], "conflict": [],
|
||||
}
|
||||
contents_patches: list[dict] = []
|
||||
ref_patches: dict[str, str] = {}
|
||||
system_patches: dict[str, list[str]] = {}
|
||||
new_blocks: dict[str, str] = {}
|
||||
generated = _section_names(text)
|
||||
for set_name, device in sorted(devices.items()):
|
||||
name = f"{set_name}.zip"
|
||||
existing = entries.get(name)
|
||||
if not existing:
|
||||
new_blocks[name] = _format_device_entry(
|
||||
name, device, role, str(profile.get("type") or "")
|
||||
)
|
||||
report["added"].append(set_name)
|
||||
continue
|
||||
if any(e.get("category") != "bios_zip" for e in existing):
|
||||
report["conflict"].append(set_name)
|
||||
continue
|
||||
old = existing[0]
|
||||
contents = _device_contents(device["roms"], old.get("contents") or [])
|
||||
changed = False
|
||||
if _contents_differ(old.get("contents") or [], contents):
|
||||
contents_patches.append({"name": name, "category": "bios_zip", "contents": contents})
|
||||
changed = True
|
||||
if not old.get("source_ref") and device.get("source_line"):
|
||||
ref_patches[name] = _build_source_ref(device, role)
|
||||
changed = True
|
||||
if (
|
||||
"systems" in device
|
||||
and name in generated
|
||||
and _entry_systems(old) != list(device["systems"])
|
||||
):
|
||||
system_patches[name] = list(device["systems"])
|
||||
changed = True
|
||||
report["updated" if changed else "unchanged"].append(set_name)
|
||||
|
||||
if write and (contents_patches or ref_patches or system_patches or new_blocks):
|
||||
patched = _add_source_refs(text, ref_patches)
|
||||
patched = _set_section_systems(patched, system_patches)
|
||||
patched = _patch_bios_entries(patched, contents_patches)
|
||||
patched = _insert_device_entries(patched, new_blocks)
|
||||
shutil.copy2(path, path.with_suffix(PROFILE_BACKUP_SUFFIX))
|
||||
write_text_atomic(str(path), patched)
|
||||
return report
|
||||
|
||||
|
||||
def _softlist_section(profile: dict, lists: dict[str, list[str]], role: str) -> str:
|
||||
"""The generated software list section, one entry per list and tree.
|
||||
|
||||
A list takes the fields of each hash/ tree the profile delivers (a
|
||||
bundled tree ships with the emulator, not in a pack) and the path of
|
||||
that tree. Members the section names leave the tree's own entry.
|
||||
"""
|
||||
trees = [
|
||||
entry for entry in profile.get("files") or []
|
||||
if entry.get("type") == "directory"
|
||||
and str(entry.get("name")) == "hash/"
|
||||
and not entry.get("bundled")
|
||||
]
|
||||
blocks: list[str] = []
|
||||
for tree in trees:
|
||||
base = str(tree.get("path") or tree["name"])
|
||||
for name, systems in sorted(lists.items()):
|
||||
lines = [
|
||||
f" - name: {_yaml_scalar(name + '.xml')}",
|
||||
f" path: {_yaml_scalar(base + name + '.xml')}",
|
||||
]
|
||||
if tree.get("mode"):
|
||||
lines.append(f" mode: {tree['mode']}")
|
||||
lines += [
|
||||
" category: game_data",
|
||||
" required: false",
|
||||
*_system_lines(systems),
|
||||
]
|
||||
blocks.append("\n".join(lines))
|
||||
if not blocks:
|
||||
return ""
|
||||
lead = [
|
||||
SOFTLIST_SECTION,
|
||||
" # software_list_device::parse opens <list>.xml for each software list a",
|
||||
" # machine declares. Each list below is filed under the systems whose",
|
||||
" # machines declare it, a list no such machine declares stays with its",
|
||||
" # hash/ tree.",
|
||||
f" # ref: {role}src/emu/softlist_dev.cpp:281-313,",
|
||||
f" # {role}src/frontend/mame/infoxml.cpp:2184-2198",
|
||||
]
|
||||
return "\n".join(lead) + "\n\n" + "\n\n".join(blocks) + "\n"
|
||||
|
||||
|
||||
def merge_mame_support(
|
||||
profile_path: str,
|
||||
lists: dict[str, list[str]],
|
||||
samples: dict[str, list[str]],
|
||||
write: bool = False,
|
||||
) -> dict[str, list[str]]:
|
||||
"""File the software lists and the floppy samples of a MAME profile.
|
||||
|
||||
*lists* maps a software list to the systems whose machines declare it,
|
||||
for the lists the release ships. They are written as the generated
|
||||
software list section, ahead of the device section, and a refresh
|
||||
rewrites the whole section. *samples* maps a floppy sample name to the
|
||||
systems whose drives load it: each written entry named <sample>.wav
|
||||
under a samples/floppy/ path gets them, and one no system loads gets
|
||||
none.
|
||||
"""
|
||||
path = Path(profile_path)
|
||||
text = path.read_text(encoding="utf-8")
|
||||
profile = yaml_load(text) or {}
|
||||
role = mame_ref_role(profile)
|
||||
|
||||
original = text
|
||||
start = _softlist_section_start(text)
|
||||
if start is not None:
|
||||
device = _device_section_start(text)
|
||||
if device is not None and device > start:
|
||||
end = device
|
||||
else:
|
||||
end_match = _FILES_END.search(text, text.index("\n", start) + 1)
|
||||
end = end_match.start() if end_match else len(text)
|
||||
text = text[:start] + text[end:]
|
||||
section = _softlist_section(profile, lists, role)
|
||||
|
||||
head_end = generated_start(text)
|
||||
head = text if head_end is None else text[:head_end]
|
||||
written = yaml_load(head) or {}
|
||||
sample_systems: dict[str, list[str]] = {}
|
||||
for entry in written.get("files") or []:
|
||||
name = str(entry.get("name") or "")
|
||||
if "samples/floppy/" not in str(entry.get("path") or "") or not name.endswith(".wav"):
|
||||
continue
|
||||
wanted = samples.get(name[:-4], [])
|
||||
if _entry_systems(entry) != wanted:
|
||||
sample_systems[name] = wanted
|
||||
head = _rewrite_systems(head, sample_systems) if sample_systems else head
|
||||
rest = "" if head_end is None else text[head_end:]
|
||||
|
||||
if section and not rest:
|
||||
files_at = re.search(r"^files:[ \t]*$", head, re.M)
|
||||
if files_at is None or _FILES_END.search(head, files_at.end()):
|
||||
raise ValueError(
|
||||
"files: must be the last top-level key to hold a software list section"
|
||||
)
|
||||
patched = head.rstrip("\n") + "\n"
|
||||
if section:
|
||||
patched += "\n" + section
|
||||
if rest:
|
||||
patched += "\n" + rest
|
||||
|
||||
if write and patched != original:
|
||||
shutil.copy2(path, path.with_suffix(PROFILE_BACKUP_SUFFIX))
|
||||
write_text_atomic(str(path), patched)
|
||||
return {
|
||||
"lists": sorted(lists),
|
||||
"samples": sorted(sample_systems),
|
||||
"changed": [str(path)] if patched != original else [],
|
||||
}
|
||||
|
||||
|
||||
def _add_source_refs(text: str, refs: dict[str, str]) -> str:
|
||||
"""Give each named entry a source_ref line, right after its category."""
|
||||
if not refs:
|
||||
return text
|
||||
lines = text.split("\n")
|
||||
current = ""
|
||||
out: list[str] = []
|
||||
for line in lines:
|
||||
head = re.match(r"^ - name:\s*(.+?)\s*$", line)
|
||||
if head:
|
||||
current = head.group(1).strip('"').strip("'")
|
||||
out.append(line)
|
||||
if current in refs and re.match(r"^ category:", line):
|
||||
out.append(f' source_ref: "{refs[current]}"')
|
||||
return "\n".join(out)
|
||||
|
||||
|
||||
def _insert_device_entries(text: str, blocks: dict[str, str]) -> str:
|
||||
"""Place new device entries in name order inside the generated section.
|
||||
|
||||
The section opens at its header and runs to the end of the files list.
|
||||
Entries already there stay in the order they are written, each new one
|
||||
goes before the first whose name sorts after it.
|
||||
"""
|
||||
if not blocks:
|
||||
return text
|
||||
start = _device_section_start(text)
|
||||
if start is None:
|
||||
files_at = re.search(r"^files:[ \t]*$", text, re.M)
|
||||
if files_at is None or _FILES_END.search(text, files_at.end()):
|
||||
raise ValueError(
|
||||
"files: must be the last top-level key to hold a device section"
|
||||
)
|
||||
text = text.rstrip("\n") + "\n\n" + DEVICE_SECTION + "\n"
|
||||
start = _device_section_start(text)
|
||||
body_at = text.index("\n", start) + 1
|
||||
end_match = _FILES_END.search(text, body_at)
|
||||
end = end_match.start() if end_match else len(text)
|
||||
section = text[body_at:end]
|
||||
starts = [m.start() for m in re.finditer(r"^ - name:", section, re.M)]
|
||||
lead = (section[: starts[0]] if starts else section).strip("\n")
|
||||
present = [
|
||||
section[a:b].rstrip("\n") for a, b in zip(starts, starts[1:] + [len(section)])
|
||||
]
|
||||
|
||||
def block_name(block: str) -> str:
|
||||
return block.split("\n", 1)[0].split(":", 1)[1].strip().strip('"')
|
||||
|
||||
merged: list[str] = []
|
||||
pending = sorted(blocks)
|
||||
for block in present:
|
||||
while pending and pending[0] < block_name(block):
|
||||
merged.append(blocks[pending.pop(0)])
|
||||
merged.append(block)
|
||||
merged.extend(blocks[name] for name in pending)
|
||||
body = (lead + "\n" if lead else "") + "\n\n".join(merged) + "\n"
|
||||
rest = text[end:]
|
||||
return text[:body_at] + body + ("\n" + rest if rest else "")
|
||||
|
||||
|
||||
def _yaml_scalar(value: object) -> str:
|
||||
"""Quote a scalar so YAML reads back exactly what the source said.
|
||||
|
||||
A MAME ROM named ``01`` written bare re-parses as the integer 1, and the
|
||||
set stops naming the file the driver loads. Quoting is not cosmetic here.
|
||||
"""
|
||||
return json.dumps(str(value), ensure_ascii=False)
|
||||
|
||||
|
||||
def _format_contents(contents: list[dict]) -> str:
|
||||
"""Format a contents list as YAML text."""
|
||||
lines = [" contents:"]
|
||||
for rom in contents:
|
||||
lines.append(f" - name: {_yaml_scalar(rom['name'])}")
|
||||
if rom.get("description"):
|
||||
lines.append(f" description: {_yaml_scalar(rom['description'])}")
|
||||
if rom.get("size"):
|
||||
lines.append(f" size: {rom['size']}")
|
||||
if rom.get("crc32"):
|
||||
lines.append(f' crc32: "{rom["crc32"]}"')
|
||||
if rom.get("sha1"):
|
||||
lines.append(f' sha1: "{rom["sha1"]}"')
|
||||
if rom.get("bad_dump"):
|
||||
lines.append(" bad_dump: true")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
# Content values an FBNeo entry carries, in the order a new one is written.
|
||||
_FBNEO_VALUES = ("size", "crc32", "sha1")
|
||||
_ENTRY_START = re.compile(r"^ - name: ", re.M)
|
||||
_FIELD_LINE = re.compile(r"^ (?P<key>[a-z0-9_]+):\s*(?P<value>[^#\n]*?)\s*(?P<comment>#.*)?$")
|
||||
|
||||
|
||||
def _patch_fbneo_values(text: str, changed: dict[tuple[str, str], dict]) -> str:
|
||||
"""Rewrite size/crc32/sha1 inside the blocks of the changed entries."""
|
||||
starts = [m.start() for m in _ENTRY_START.finditer(text)] + [len(text)]
|
||||
out = [text[: starts[0]]]
|
||||
for begin, end in zip(starts, starts[1:]):
|
||||
block = text[begin:end]
|
||||
fields = {}
|
||||
for line in block.splitlines()[1:]:
|
||||
match = _FIELD_LINE.match(line)
|
||||
if match:
|
||||
fields[match["key"]] = match["value"].strip().strip('"')
|
||||
name = block.splitlines()[0].removeprefix(" - name:").strip().strip('"')
|
||||
entry = changed.get((fields.get("archive", ""), name))
|
||||
if entry is not None:
|
||||
block = _patch_block_values(block, entry)
|
||||
out.append(block)
|
||||
return "".join(out)
|
||||
|
||||
|
||||
def _patch_block_values(block: str, entry: dict) -> str:
|
||||
lines = block.split("\n")
|
||||
for key in _FBNEO_VALUES:
|
||||
value = entry.get(key)
|
||||
if not value:
|
||||
continue
|
||||
rendered = str(value) if key == "size" else f'"{value}"'
|
||||
for i, line in enumerate(lines):
|
||||
match = _FIELD_LINE.match(line)
|
||||
if match and match["key"] == key:
|
||||
if match["value"].strip().strip('"') != str(value):
|
||||
# A size comment spells the old value, so it goes with it.
|
||||
lines[i] = f" {key}: {rendered}"
|
||||
break
|
||||
else:
|
||||
anchor = max(
|
||||
(i for i, line in enumerate(lines)
|
||||
if (m := _FIELD_LINE.match(line)) and m["key"] in ("archive", *_FBNEO_VALUES)),
|
||||
default=0,
|
||||
)
|
||||
lines.insert(anchor + 1, f" {key}: {rendered}")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def write_fbneo_profile(path: str | Path, data: dict) -> None:
|
||||
"""Write merged FBNeo profile using text-based patching.
|
||||
|
||||
FBNeo profiles have individual ROM entries with archive: field.
|
||||
Patches the content values of existing entries that changed and
|
||||
appends new ROM entries. core_version is the pin's, never written here.
|
||||
Comments and curated source_refs stay as written.
|
||||
"""
|
||||
p = Path(path)
|
||||
backup = p.with_suffix(PROFILE_BACKUP_SUFFIX)
|
||||
shutil.copy2(p, backup)
|
||||
|
||||
original = p.read_text(encoding="utf-8")
|
||||
patched = original
|
||||
|
||||
# Identify new ROM entries by comparing parsed data keys, not text search
|
||||
existing_data = yaml_load(original) or {}
|
||||
existing_by_key = {
|
||||
(f["archive"], f["name"]): f
|
||||
for f in existing_data.get("files", [])
|
||||
if f.get("archive")
|
||||
}
|
||||
existing_keys = set(existing_by_key)
|
||||
changed = {
|
||||
(f["archive"], f["name"]): f
|
||||
for f in data.get("files", [])
|
||||
if f.get("archive")
|
||||
and (f["archive"], f["name"]) in existing_by_key
|
||||
and any(
|
||||
str(f.get(k) or "") != str(existing_by_key[(f["archive"], f["name"])].get(k) or "")
|
||||
for k in _FBNEO_VALUES
|
||||
if f.get(k)
|
||||
)
|
||||
}
|
||||
if changed:
|
||||
patched = _patch_fbneo_values(patched, changed)
|
||||
new_roms = [
|
||||
f
|
||||
for f in data.get("files", [])
|
||||
if f.get("archive") and (f["archive"], f["name"]) not in existing_keys
|
||||
]
|
||||
|
||||
if new_roms:
|
||||
lines = []
|
||||
for fe in new_roms:
|
||||
lines.append(f" - name: {_yaml_scalar(fe['name'])}")
|
||||
lines.append(f" archive: {_yaml_scalar(fe['archive'])}")
|
||||
lines.append(f" required: {str(fe.get('required', True)).lower()}")
|
||||
if fe.get("size"):
|
||||
lines.append(f" size: {fe['size']}")
|
||||
if fe.get("crc32"):
|
||||
lines.append(f' crc32: "{fe["crc32"]}"')
|
||||
if fe.get("source_ref"):
|
||||
lines.append(f' source_ref: "{fe["source_ref"]}"')
|
||||
lines.append("")
|
||||
patched = patched.rstrip("\n") + "\n\n" + "\n".join(lines)
|
||||
|
||||
write_text_atomic(str(p), patched)
|
||||
@@ -0,0 +1,412 @@
|
||||
"""Text patches that write merged hashes into a profile and keep its formatting."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import re
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
from core.artifacts import write_text_atomic
|
||||
from model.profiles import PROFILE_BACKUP_SUFFIX
|
||||
from model.safeparse import yaml_load
|
||||
|
||||
|
||||
DEVICE_SECTION = " # --- Device ROM sets (generated from MAME -listxml) ---"
|
||||
SOFTLIST_SECTION = " # --- Software lists (generated from MAME -listxml) ---"
|
||||
|
||||
|
||||
def write_mame_profile(path: str | Path, data: dict) -> None:
|
||||
"""Write merged profile using text-based patching to preserve formatting.
|
||||
|
||||
Instead of yaml.dump (which destroys comments, quoting, indentation),
|
||||
this reads the original file as text, patches specific fields
|
||||
(core_version, contents, source_ref), and appends new entries.
|
||||
"""
|
||||
p = Path(path)
|
||||
backup = p.with_suffix(PROFILE_BACKUP_SUFFIX)
|
||||
shutil.copy2(p, backup)
|
||||
|
||||
original = p.read_text(encoding="utf-8")
|
||||
patched = _patch_core_version(original, data.get("core_version", ""))
|
||||
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)
|
||||
|
||||
write_text_atomic(str(p), patched)
|
||||
|
||||
|
||||
def _patch_core_version(text: str, version: str) -> str:
|
||||
"""Replace core_version value in-place."""
|
||||
return _patch_scalar(text, "core_version", version)
|
||||
|
||||
|
||||
def _patch_scalar(text: str, key: str, value: str) -> str:
|
||||
"""Replace a top-level scalar in-place, quoted. An absent value changes nothing."""
|
||||
if not value:
|
||||
return text
|
||||
return re.sub(
|
||||
rf"^({re.escape(key)}:\s*).*$",
|
||||
rf'\g<1>"{value}"',
|
||||
text,
|
||||
count=1,
|
||||
flags=re.MULTILINE,
|
||||
)
|
||||
|
||||
|
||||
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.
|
||||
|
||||
Owned = indented with 4+ spaces (field lines of this entry).
|
||||
"""
|
||||
last_owned = start_line
|
||||
for j in range(start_line + 1, len(lines)):
|
||||
stripped = lines[j].strip()
|
||||
if not stripped:
|
||||
break # blank line = end of entry
|
||||
if stripped.startswith("#"):
|
||||
break # comment = belongs to next entry
|
||||
if re.match(r"^ - ", lines[j]):
|
||||
break # next list item
|
||||
if re.match(r"^ ", lines[j]) or re.match(r"^ \w", lines[j]):
|
||||
last_owned = j
|
||||
else:
|
||||
break
|
||||
return last_owned
|
||||
|
||||
|
||||
def _patch_source_ref(lines: list[str], start_line: int, last_owned: int, source_ref: str) -> int:
|
||||
"""Patch source_ref in-place, or add it. Returns the new last owned line."""
|
||||
for j in range(start_line + 1, last_owned + 1):
|
||||
if re.match(r"^ source_ref:", lines[j]):
|
||||
lines[j] = f' source_ref: "{source_ref}"'
|
||||
return last_owned
|
||||
lines.insert(last_owned + 1, f' source_ref: "{source_ref}"')
|
||||
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
|
||||
contents_end = None
|
||||
for j in range(start_line + 1, last_owned + 1):
|
||||
if re.match(r"^ contents:", lines[j]):
|
||||
contents_start = j
|
||||
elif contents_start is not None:
|
||||
if re.match(r"^ ", lines[j]):
|
||||
contents_end = j
|
||||
else:
|
||||
break
|
||||
if contents_start is None:
|
||||
return last_owned
|
||||
if contents_end is None:
|
||||
contents_end = contents_start
|
||||
del lines[contents_start : contents_end + 1]
|
||||
return last_owned - (contents_end - contents_start + 1)
|
||||
|
||||
|
||||
def _patch_entry(lines: list[str], start_line: int, fe: dict) -> None:
|
||||
contents = fe.get("contents", [])
|
||||
source_ref = fe.get("source_ref", "")
|
||||
|
||||
last_owned = _owned_end(lines, start_line)
|
||||
|
||||
if source_ref:
|
||||
last_owned = _patch_source_ref(lines, start_line, last_owned, source_ref)
|
||||
|
||||
# The source settles required for a set the machine runs without
|
||||
if "required" in fe:
|
||||
last_owned = _patch_required(lines, start_line, last_owned, fe["required"])
|
||||
|
||||
last_owned = _drop_contents(lines, start_line, last_owned)
|
||||
|
||||
# Insert new contents after last owned line
|
||||
if contents:
|
||||
new_lines = _format_contents(contents).split("\n")
|
||||
for k, cl in enumerate(new_lines):
|
||||
lines.insert(last_owned + 1 + k, cl)
|
||||
|
||||
|
||||
def _patch_bios_entries(text: str, files: list[dict]) -> str:
|
||||
"""Patch contents, source_ref and required of bios_zip entries in-place.
|
||||
|
||||
Processes entries in reverse order to preserve line offsets.
|
||||
Each entry's "owned" lines are: the `- name:` line plus all indented
|
||||
lines that follow (4+ spaces), stopping at blank lines, comments,
|
||||
or the next `- name:`.
|
||||
"""
|
||||
patches: dict[str, dict] = {}
|
||||
for fe in files:
|
||||
if fe.get("category") != "bios_zip":
|
||||
continue
|
||||
patches[fe["name"]] = fe
|
||||
|
||||
if not patches:
|
||||
return text
|
||||
|
||||
lines = text.split("\n")
|
||||
# Find all entry start positions (line indices)
|
||||
entry_starts: list[tuple[int, str]] = []
|
||||
for i, line in enumerate(lines):
|
||||
m = re.match(r"^ - name:\s*(.+?)\s*$", line)
|
||||
if m:
|
||||
entry_starts.append((i, m.group(1).strip('"').strip("'")))
|
||||
|
||||
# Process in reverse so line insertions don't shift indices
|
||||
for idx in range(len(entry_starts) - 1, -1, -1):
|
||||
start_line, entry_name = entry_starts[idx]
|
||||
if entry_name in patches:
|
||||
_patch_entry(lines, start_line, patches[entry_name])
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def _append_new_entries(text: str, files: list[dict], original: str) -> str:
|
||||
"""Append new bios_zip entries (system=None) that aren't in the original."""
|
||||
# Parse original to get existing entry names (more reliable than text search)
|
||||
existing_data = yaml_load(original) or {}
|
||||
existing_names = {f["name"] for f in existing_data.get("files", [])}
|
||||
|
||||
new_entries = []
|
||||
for fe in files:
|
||||
if fe.get("category") != "bios_zip" or fe.get("system") is not None:
|
||||
continue
|
||||
if fe["name"] in existing_names:
|
||||
continue
|
||||
new_entries.append(fe)
|
||||
|
||||
if not new_entries:
|
||||
return text
|
||||
|
||||
lines = []
|
||||
profile_type = str(existing_data.get("type") or "")
|
||||
for fe in new_entries:
|
||||
lines.append(f"\n - name: {_yaml_scalar(fe['name'])}")
|
||||
lines.extend(set_path_lines(profile_type, fe["name"]))
|
||||
lines.append(f" required: {str(fe['required']).lower()}")
|
||||
lines.append(" category: bios_zip")
|
||||
if fe.get("source_ref"):
|
||||
lines.append(f' source_ref: "{fe["source_ref"]}"')
|
||||
if fe.get("contents"):
|
||||
lines.append(_format_contents(fe["contents"]))
|
||||
|
||||
if lines:
|
||||
# New BIOS root sets go before the generated sections, which run to
|
||||
# the end of the files list.
|
||||
at = generated_start(text)
|
||||
if at is None:
|
||||
text = text.rstrip("\n") + "\n" + "\n".join(lines) + "\n"
|
||||
else:
|
||||
text = text[:at].rstrip("\n") + "\n" + "\n".join(lines) + "\n\n" + text[at:]
|
||||
|
||||
return text
|
||||
|
||||
|
||||
def _device_section_start(text: str) -> int | None:
|
||||
"""Offset of the generated device section header, if the profile has one."""
|
||||
match = re.search(rf"^{re.escape(DEVICE_SECTION)}$", text, re.M)
|
||||
return match.start() if match else None
|
||||
|
||||
|
||||
def _softlist_section_start(text: str) -> int | None:
|
||||
"""Offset of the generated software list section header, if any."""
|
||||
match = re.search(rf"^{re.escape(SOFTLIST_SECTION)}$", text, re.M)
|
||||
return match.start() if match else None
|
||||
|
||||
|
||||
def generated_start(text: str) -> int | None:
|
||||
"""Offset of the first generated section: what a reader wrote ends there."""
|
||||
found = [at for at in (_softlist_section_start(text), _device_section_start(text)) if at is not None]
|
||||
return min(found) if found else None
|
||||
|
||||
|
||||
def set_path_lines(profile_type: str, name: str) -> list[str]:
|
||||
"""Where each build of a MAME profile reads a set it lists.
|
||||
|
||||
The libretro build searches the content folder, then <system>/mame/bios
|
||||
and <system>/mame/roms (libretro-internal/retro_init.cpp,
|
||||
Set_Path_Option), never the system folder itself. The standalone build
|
||||
reads the top of its rompath.
|
||||
"""
|
||||
if "libretro" not in profile_type:
|
||||
return []
|
||||
lines = [f' path: "mame/bios/{name}"']
|
||||
if "standalone" in profile_type:
|
||||
lines.append(f' standalone_path: "{name}"')
|
||||
return lines
|
||||
|
||||
|
||||
def _yaml_scalar(value: object) -> str:
|
||||
"""Quote a scalar so YAML reads back exactly what the source said.
|
||||
|
||||
A MAME ROM named ``01`` written bare re-parses as the integer 1, and the
|
||||
set stops naming the file the driver loads. Quoting is not cosmetic here.
|
||||
"""
|
||||
return json.dumps(str(value), ensure_ascii=False)
|
||||
|
||||
|
||||
def _format_contents(contents: list[dict]) -> str:
|
||||
"""Format a contents list as YAML text."""
|
||||
lines = [" contents:"]
|
||||
for rom in contents:
|
||||
lines.append(f" - name: {_yaml_scalar(rom['name'])}")
|
||||
if rom.get("description"):
|
||||
lines.append(f" description: {_yaml_scalar(rom['description'])}")
|
||||
if rom.get("size"):
|
||||
lines.append(f" size: {rom['size']}")
|
||||
if rom.get("crc32"):
|
||||
lines.append(f' crc32: "{rom["crc32"]}"')
|
||||
if rom.get("sha1"):
|
||||
lines.append(f' sha1: "{rom["sha1"]}"')
|
||||
if rom.get("bad_dump"):
|
||||
lines.append(" bad_dump: true")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
# Content values an FBNeo entry carries, in the order a new one is written.
|
||||
_FBNEO_VALUES = ("size", "crc32", "sha1")
|
||||
_ENTRY_START = re.compile(r"^ - name: ", re.M)
|
||||
_FIELD_LINE = re.compile(r"^ (?P<key>[a-z0-9_]+):\s*(?P<value>[^#\n]*?)\s*(?P<comment>#.*)?$")
|
||||
|
||||
|
||||
def _patch_fbneo_values(text: str, changed: dict[tuple[str, str], dict]) -> str:
|
||||
"""Rewrite size/crc32/sha1 inside the blocks of the changed entries."""
|
||||
starts = [m.start() for m in _ENTRY_START.finditer(text)] + [len(text)]
|
||||
out = [text[: starts[0]]]
|
||||
for begin, end in zip(starts, starts[1:]):
|
||||
block = text[begin:end]
|
||||
fields = {}
|
||||
for line in block.splitlines()[1:]:
|
||||
match = _FIELD_LINE.match(line)
|
||||
if match:
|
||||
fields[match["key"]] = match["value"].strip().strip('"')
|
||||
name = block.splitlines()[0].removeprefix(" - name:").strip().strip('"')
|
||||
entry = changed.get((fields.get("archive", ""), name))
|
||||
if entry is not None:
|
||||
block = _patch_block_values(block, entry)
|
||||
out.append(block)
|
||||
return "".join(out)
|
||||
|
||||
|
||||
def _patch_block_values(block: str, entry: dict) -> str:
|
||||
lines = block.split("\n")
|
||||
for key in _FBNEO_VALUES:
|
||||
value = entry.get(key)
|
||||
if not value:
|
||||
continue
|
||||
rendered = str(value) if key == "size" else f'"{value}"'
|
||||
for i, line in enumerate(lines):
|
||||
match = _FIELD_LINE.match(line)
|
||||
if match and match["key"] == key:
|
||||
if match["value"].strip().strip('"') != str(value):
|
||||
# A size comment spells the old value, so it goes with it.
|
||||
lines[i] = f" {key}: {rendered}"
|
||||
break
|
||||
else:
|
||||
anchor = max(
|
||||
(i for i, line in enumerate(lines)
|
||||
if (m := _FIELD_LINE.match(line)) and m["key"] in ("archive", *_FBNEO_VALUES)),
|
||||
default=0,
|
||||
)
|
||||
lines.insert(anchor + 1, f" {key}: {rendered}")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def write_fbneo_profile(path: str | Path, data: dict) -> None:
|
||||
"""Write merged FBNeo profile using text-based patching.
|
||||
|
||||
FBNeo profiles have individual ROM entries with archive: field.
|
||||
Patches the content values of existing entries that changed and
|
||||
appends new ROM entries. core_version is the pin's, never written here.
|
||||
Comments and curated source_refs stay as written.
|
||||
"""
|
||||
p = Path(path)
|
||||
backup = p.with_suffix(PROFILE_BACKUP_SUFFIX)
|
||||
shutil.copy2(p, backup)
|
||||
|
||||
original = p.read_text(encoding="utf-8")
|
||||
patched = original
|
||||
|
||||
# Identify new ROM entries by comparing parsed data keys, not text search
|
||||
existing_data = yaml_load(original) or {}
|
||||
existing_by_key = {
|
||||
(f["archive"], f["name"]): f
|
||||
for f in existing_data.get("files", [])
|
||||
if f.get("archive")
|
||||
}
|
||||
existing_keys = set(existing_by_key)
|
||||
changed = {
|
||||
(f["archive"], f["name"]): f
|
||||
for f in data.get("files", [])
|
||||
if f.get("archive")
|
||||
and (f["archive"], f["name"]) in existing_by_key
|
||||
and any(
|
||||
str(f.get(k) or "") != str(existing_by_key[(f["archive"], f["name"])].get(k) or "")
|
||||
for k in _FBNEO_VALUES
|
||||
if f.get(k)
|
||||
)
|
||||
}
|
||||
if changed:
|
||||
patched = _patch_fbneo_values(patched, changed)
|
||||
new_roms = [
|
||||
f
|
||||
for f in data.get("files", [])
|
||||
if f.get("archive") and (f["archive"], f["name"]) not in existing_keys
|
||||
]
|
||||
|
||||
if new_roms:
|
||||
lines = []
|
||||
for fe in new_roms:
|
||||
lines.append(f" - name: {_yaml_scalar(fe['name'])}")
|
||||
lines.append(f" archive: {_yaml_scalar(fe['archive'])}")
|
||||
lines.append(f" required: {str(fe.get('required', True)).lower()}")
|
||||
if fe.get("size"):
|
||||
lines.append(f" size: {fe['size']}")
|
||||
if fe.get("crc32"):
|
||||
lines.append(f' crc32: "{fe["crc32"]}"')
|
||||
if fe.get("source_ref"):
|
||||
lines.append(f' source_ref: "{fe["source_ref"]}"')
|
||||
lines.append("")
|
||||
patched = patched.rstrip("\n") + "\n\n" + "\n".join(lines)
|
||||
|
||||
write_text_atomic(str(p), patched)
|
||||
@@ -0,0 +1,398 @@
|
||||
"""The generated device and software list sections of a MAME profile."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import shutil
|
||||
import textwrap
|
||||
from pathlib import Path
|
||||
|
||||
from core.artifacts import write_text_atomic
|
||||
from model.profiles import PROFILE_BACKUP_SUFFIX
|
||||
from model.safeparse import yaml_load
|
||||
from ._hash_merge import _build_contents, _build_source_ref, _contents_differ, mame_ref_role
|
||||
from ._hash_write import (
|
||||
DEVICE_SECTION,
|
||||
SOFTLIST_SECTION,
|
||||
_device_section_start,
|
||||
_format_contents,
|
||||
_patch_bios_entries,
|
||||
_softlist_section_start,
|
||||
_yaml_scalar,
|
||||
generated_start,
|
||||
set_path_lines,
|
||||
)
|
||||
|
||||
|
||||
_FILES_END = re.compile(r"^\S", re.M)
|
||||
|
||||
|
||||
def _device_contents(roms: list[dict], old: list[dict]) -> list[dict]:
|
||||
"""Contents of a device set, keeping the member descriptions a reader wrote."""
|
||||
written = {str(c.get("name")): c.get("description") for c in old or []}
|
||||
contents = _build_contents(roms)
|
||||
for member in contents:
|
||||
if written.get(member["name"]):
|
||||
member["description"] = written[member["name"]]
|
||||
elif member.get("description"):
|
||||
# A biosset description is MAME's text, quoted as written.
|
||||
member["description"] = f"`{member['description']}`"
|
||||
return contents
|
||||
|
||||
|
||||
def _device_note(description: str) -> str:
|
||||
"""The note of a generated entry. The device name is MAME's own text,
|
||||
quoted verbatim in backticks: `Sun-1 Workstation Monitor; PROM 1&3: Rev AJ`
|
||||
is a name, not a sentence."""
|
||||
return f"Device ROM set, loaded when a machine instantiates `{description}`."
|
||||
|
||||
|
||||
def _zip_scalar(name: str) -> str:
|
||||
"""An archive name as the written entries spell it: bare when YAML keeps it."""
|
||||
return name if re.fullmatch(r"[a-z0-9_]+\.zip", name) else _yaml_scalar(name)
|
||||
|
||||
|
||||
_PLAIN_SYSTEM = re.compile(r"^[a-z][a-z0-9-]*$")
|
||||
|
||||
|
||||
def _system_lines(systems: list[str]) -> list[str]:
|
||||
"""`system:` for one system, `systems:` for several, nothing for none."""
|
||||
def scalar(value: str) -> str:
|
||||
return value if _PLAIN_SYSTEM.match(value) else _yaml_scalar(value)
|
||||
|
||||
if not systems:
|
||||
return []
|
||||
if len(systems) == 1:
|
||||
return [f" system: {scalar(systems[0])}"]
|
||||
return [" systems:", *(f" - {scalar(s)}" for s in systems)]
|
||||
|
||||
|
||||
def _entry_systems(entry: dict) -> list[str]:
|
||||
"""The systems an entry states, in the shape _system_lines writes."""
|
||||
if entry.get("system"):
|
||||
return [str(entry["system"])]
|
||||
return [str(s) for s in entry.get("systems") or []]
|
||||
|
||||
|
||||
def _section_names(text: str) -> set[str]:
|
||||
"""Entry names inside the generated device section."""
|
||||
start = _device_section_start(text)
|
||||
if start is None:
|
||||
return set()
|
||||
end = _FILES_END.search(text, text.index("\n", start) + 1)
|
||||
section = text[start : end.start() if end else len(text)]
|
||||
return {
|
||||
m.group(1).strip('"').strip("'")
|
||||
for m in re.finditer(r"^ - name:\s*(.+?)\s*$", section, re.M)
|
||||
}
|
||||
|
||||
|
||||
def _set_section_systems(text: str, systems: dict[str, list[str]]) -> str:
|
||||
"""Rewrite the system fields of the named entries of the device section.
|
||||
|
||||
Any `system:` or `systems:` the entry holds is dropped and the new one
|
||||
written right after its category, where a written entry keeps it.
|
||||
"""
|
||||
if not systems:
|
||||
return text
|
||||
start = _device_section_start(text)
|
||||
if start is None:
|
||||
return text
|
||||
return text[:start] + _rewrite_systems(text[start:], systems)
|
||||
|
||||
|
||||
def _rewrite_systems(section: str, systems: dict[str, list[str]]) -> str:
|
||||
"""Rewrite the system fields of the named entries of one stretch of text."""
|
||||
out: list[str] = []
|
||||
current = ""
|
||||
in_list = False
|
||||
for line in section.split("\n"):
|
||||
entry_head = re.match(r"^ - name:\s*(.+?)\s*$", line)
|
||||
if entry_head:
|
||||
current = entry_head.group(1).strip('"').strip("'")
|
||||
if current in systems:
|
||||
if in_list and line.startswith(" - "):
|
||||
continue
|
||||
in_list = False
|
||||
if re.match(r"^ system:", line):
|
||||
continue
|
||||
if re.match(r"^ systems:\s*$", line):
|
||||
in_list = True
|
||||
continue
|
||||
out.append(line)
|
||||
if current in systems and re.match(r"^ category:", line):
|
||||
out.extend(_system_lines(systems[current]))
|
||||
return "\n".join(out)
|
||||
|
||||
|
||||
def _format_device_entry(name: str, device: dict, role: str, profile_type: str = "") -> str:
|
||||
"""One generated device entry, in the field order of the written ones."""
|
||||
lines = [
|
||||
f" - name: {_zip_scalar(name)}",
|
||||
*set_path_lines(profile_type, name),
|
||||
" required: false",
|
||||
" category: bios_zip",
|
||||
*_system_lines(device.get("systems") or []),
|
||||
]
|
||||
if device.get("source_line"):
|
||||
lines.append(f' source_ref: "{_build_source_ref(device, role)}"')
|
||||
lines.append(" note: >-")
|
||||
lines.extend(
|
||||
textwrap.wrap(
|
||||
_device_note(device.get("description") or name[:-4]),
|
||||
width=78,
|
||||
initial_indent=" ",
|
||||
subsequent_indent=" ",
|
||||
break_on_hyphens=False,
|
||||
)
|
||||
)
|
||||
lines.append(_format_contents(_device_contents(device["roms"], [])))
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def merge_mame_devices(
|
||||
profile_path: str,
|
||||
devices: dict[str, dict],
|
||||
write: bool = False,
|
||||
) -> dict[str, list[str]]:
|
||||
"""Merge MAME device ROM sets into a profile.
|
||||
|
||||
*devices* maps a device shortname to its listxml description, its dumped
|
||||
ROMs and, when found, source_file and source_line. A set the profile
|
||||
already lists keeps every field a reader wrote: its contents are refreshed
|
||||
with the member descriptions kept, and a source_ref is added only where it
|
||||
has none. A set it lacks is written under the generated section, in name
|
||||
order, with required false. An entry of another category under a device
|
||||
name is reported, never touched.
|
||||
|
||||
A device carrying `systems` (attribute_systems) has them written on its
|
||||
entry when the entry is in the generated section: `system:` for one,
|
||||
`systems:` for several, neither for none. An entry above the section
|
||||
keeps the system a reader gave it.
|
||||
"""
|
||||
path = Path(profile_path)
|
||||
text = path.read_text(encoding="utf-8")
|
||||
profile = yaml_load(text) or {}
|
||||
role = mame_ref_role(profile)
|
||||
entries: dict[str, list[dict]] = {}
|
||||
for entry in profile.get("files") or []:
|
||||
entries.setdefault(str(entry.get("name")), []).append(entry)
|
||||
|
||||
report: dict[str, list[str]] = {
|
||||
"added": [], "updated": [], "unchanged": [], "conflict": [],
|
||||
}
|
||||
contents_patches: list[dict] = []
|
||||
ref_patches: dict[str, str] = {}
|
||||
system_patches: dict[str, list[str]] = {}
|
||||
new_blocks: dict[str, str] = {}
|
||||
generated = _section_names(text)
|
||||
for set_name, device in sorted(devices.items()):
|
||||
name = f"{set_name}.zip"
|
||||
existing = entries.get(name)
|
||||
if not existing:
|
||||
new_blocks[name] = _format_device_entry(
|
||||
name, device, role, str(profile.get("type") or "")
|
||||
)
|
||||
report["added"].append(set_name)
|
||||
continue
|
||||
if any(e.get("category") != "bios_zip" for e in existing):
|
||||
report["conflict"].append(set_name)
|
||||
continue
|
||||
old = existing[0]
|
||||
contents = _device_contents(device["roms"], old.get("contents") or [])
|
||||
changed = False
|
||||
if _contents_differ(old.get("contents") or [], contents):
|
||||
contents_patches.append({"name": name, "category": "bios_zip", "contents": contents})
|
||||
changed = True
|
||||
if not old.get("source_ref") and device.get("source_line"):
|
||||
ref_patches[name] = _build_source_ref(device, role)
|
||||
changed = True
|
||||
if (
|
||||
"systems" in device
|
||||
and name in generated
|
||||
and _entry_systems(old) != list(device["systems"])
|
||||
):
|
||||
system_patches[name] = list(device["systems"])
|
||||
changed = True
|
||||
report["updated" if changed else "unchanged"].append(set_name)
|
||||
|
||||
if write and (contents_patches or ref_patches or system_patches or new_blocks):
|
||||
patched = _add_source_refs(text, ref_patches)
|
||||
patched = _set_section_systems(patched, system_patches)
|
||||
patched = _patch_bios_entries(patched, contents_patches)
|
||||
patched = _insert_device_entries(patched, new_blocks)
|
||||
shutil.copy2(path, path.with_suffix(PROFILE_BACKUP_SUFFIX))
|
||||
write_text_atomic(str(path), patched)
|
||||
return report
|
||||
|
||||
|
||||
def _softlist_section(profile: dict, lists: dict[str, list[str]], role: str) -> str:
|
||||
"""The generated software list section, one entry per list and tree.
|
||||
|
||||
A list takes the fields of each hash/ tree the profile delivers (a
|
||||
bundled tree ships with the emulator, not in a pack) and the path of
|
||||
that tree. Members the section names leave the tree's own entry.
|
||||
"""
|
||||
trees = [
|
||||
entry for entry in profile.get("files") or []
|
||||
if entry.get("type") == "directory"
|
||||
and str(entry.get("name")) == "hash/"
|
||||
and not entry.get("bundled")
|
||||
]
|
||||
blocks: list[str] = []
|
||||
for tree in trees:
|
||||
base = str(tree.get("path") or tree["name"])
|
||||
for name, systems in sorted(lists.items()):
|
||||
lines = [
|
||||
f" - name: {_yaml_scalar(name + '.xml')}",
|
||||
f" path: {_yaml_scalar(base + name + '.xml')}",
|
||||
]
|
||||
if tree.get("mode"):
|
||||
lines.append(f" mode: {tree['mode']}")
|
||||
lines += [
|
||||
" category: game_data",
|
||||
" required: false",
|
||||
*_system_lines(systems),
|
||||
]
|
||||
blocks.append("\n".join(lines))
|
||||
if not blocks:
|
||||
return ""
|
||||
lead = [
|
||||
SOFTLIST_SECTION,
|
||||
" # software_list_device::parse opens <list>.xml for each software list a",
|
||||
" # machine declares. Each list below is filed under the systems whose",
|
||||
" # machines declare it, a list no such machine declares stays with its",
|
||||
" # hash/ tree.",
|
||||
f" # ref: {role}src/emu/softlist_dev.cpp:281-313,",
|
||||
f" # {role}src/frontend/mame/infoxml.cpp:2184-2198",
|
||||
]
|
||||
return "\n".join(lead) + "\n\n" + "\n\n".join(blocks) + "\n"
|
||||
|
||||
|
||||
def merge_mame_support(
|
||||
profile_path: str,
|
||||
lists: dict[str, list[str]],
|
||||
samples: dict[str, list[str]],
|
||||
write: bool = False,
|
||||
) -> dict[str, list[str]]:
|
||||
"""File the software lists and the floppy samples of a MAME profile.
|
||||
|
||||
*lists* maps a software list to the systems whose machines declare it,
|
||||
for the lists the release ships. They are written as the generated
|
||||
software list section, ahead of the device section, and a refresh
|
||||
rewrites the whole section. *samples* maps a floppy sample name to the
|
||||
systems whose drives load it: each written entry named <sample>.wav
|
||||
under a samples/floppy/ path gets them, and one no system loads gets
|
||||
none.
|
||||
"""
|
||||
path = Path(profile_path)
|
||||
text = path.read_text(encoding="utf-8")
|
||||
profile = yaml_load(text) or {}
|
||||
role = mame_ref_role(profile)
|
||||
|
||||
original = text
|
||||
start = _softlist_section_start(text)
|
||||
if start is not None:
|
||||
device = _device_section_start(text)
|
||||
if device is not None and device > start:
|
||||
end = device
|
||||
else:
|
||||
end_match = _FILES_END.search(text, text.index("\n", start) + 1)
|
||||
end = end_match.start() if end_match else len(text)
|
||||
text = text[:start] + text[end:]
|
||||
section = _softlist_section(profile, lists, role)
|
||||
|
||||
head_end = generated_start(text)
|
||||
head = text if head_end is None else text[:head_end]
|
||||
written = yaml_load(head) or {}
|
||||
sample_systems: dict[str, list[str]] = {}
|
||||
for entry in written.get("files") or []:
|
||||
name = str(entry.get("name") or "")
|
||||
if "samples/floppy/" not in str(entry.get("path") or "") or not name.endswith(".wav"):
|
||||
continue
|
||||
wanted = samples.get(name[:-4], [])
|
||||
if _entry_systems(entry) != wanted:
|
||||
sample_systems[name] = wanted
|
||||
head = _rewrite_systems(head, sample_systems) if sample_systems else head
|
||||
rest = "" if head_end is None else text[head_end:]
|
||||
|
||||
if section and not rest:
|
||||
files_at = re.search(r"^files:[ \t]*$", head, re.M)
|
||||
if files_at is None or _FILES_END.search(head, files_at.end()):
|
||||
raise ValueError(
|
||||
"files: must be the last top-level key to hold a software list section"
|
||||
)
|
||||
patched = head.rstrip("\n") + "\n"
|
||||
if section:
|
||||
patched += "\n" + section
|
||||
if rest:
|
||||
patched += "\n" + rest
|
||||
|
||||
if write and patched != original:
|
||||
shutil.copy2(path, path.with_suffix(PROFILE_BACKUP_SUFFIX))
|
||||
write_text_atomic(str(path), patched)
|
||||
return {
|
||||
"lists": sorted(lists),
|
||||
"samples": sorted(sample_systems),
|
||||
"changed": [str(path)] if patched != original else [],
|
||||
}
|
||||
|
||||
|
||||
def _add_source_refs(text: str, refs: dict[str, str]) -> str:
|
||||
"""Give each named entry a source_ref line, right after its category."""
|
||||
if not refs:
|
||||
return text
|
||||
lines = text.split("\n")
|
||||
current = ""
|
||||
out: list[str] = []
|
||||
for line in lines:
|
||||
head = re.match(r"^ - name:\s*(.+?)\s*$", line)
|
||||
if head:
|
||||
current = head.group(1).strip('"').strip("'")
|
||||
out.append(line)
|
||||
if current in refs and re.match(r"^ category:", line):
|
||||
out.append(f' source_ref: "{refs[current]}"')
|
||||
return "\n".join(out)
|
||||
|
||||
|
||||
def _insert_device_entries(text: str, blocks: dict[str, str]) -> str:
|
||||
"""Place new device entries in name order inside the generated section.
|
||||
|
||||
The section opens at its header and runs to the end of the files list.
|
||||
Entries already there stay in the order they are written, each new one
|
||||
goes before the first whose name sorts after it.
|
||||
"""
|
||||
if not blocks:
|
||||
return text
|
||||
start = _device_section_start(text)
|
||||
if start is None:
|
||||
files_at = re.search(r"^files:[ \t]*$", text, re.M)
|
||||
if files_at is None or _FILES_END.search(text, files_at.end()):
|
||||
raise ValueError(
|
||||
"files: must be the last top-level key to hold a device section"
|
||||
)
|
||||
text = text.rstrip("\n") + "\n\n" + DEVICE_SECTION + "\n"
|
||||
start = _device_section_start(text)
|
||||
body_at = text.index("\n", start) + 1
|
||||
end_match = _FILES_END.search(text, body_at)
|
||||
end = end_match.start() if end_match else len(text)
|
||||
section = text[body_at:end]
|
||||
starts = [m.start() for m in re.finditer(r"^ - name:", section, re.M)]
|
||||
lead = (section[: starts[0]] if starts else section).strip("\n")
|
||||
present = [
|
||||
section[a:b].rstrip("\n") for a, b in zip(starts, starts[1:] + [len(section)])
|
||||
]
|
||||
|
||||
def block_name(block: str) -> str:
|
||||
return block.split("\n", 1)[0].split(":", 1)[1].strip().strip('"')
|
||||
|
||||
merged: list[str] = []
|
||||
pending = sorted(blocks)
|
||||
for block in present:
|
||||
while pending and pending[0] < block_name(block):
|
||||
merged.append(blocks[pending.pop(0)])
|
||||
merged.append(block)
|
||||
merged.extend(blocks[name] for name in pending)
|
||||
body = (lead + "\n" if lead else "") + "\n\n".join(merged) + "\n"
|
||||
rest = text[end:]
|
||||
return text[:body_at] + body + ("\n" + rest if rest else "")
|
||||
@@ -25,12 +25,8 @@ from model.profiles import source_yaml_paths
|
||||
from core.artifacts import write_text_atomic
|
||||
from model.safeparse import yaml_load
|
||||
|
||||
from scripts.scraper._hash_merge import (
|
||||
compute_diff,
|
||||
load_profile,
|
||||
merge_fbneo_profile,
|
||||
write_fbneo_profile,
|
||||
)
|
||||
from scripts.scraper._hash_merge import compute_diff, load_profile, merge_fbneo_profile
|
||||
from scripts.scraper._hash_write import write_fbneo_profile
|
||||
from scripts.scraper.fbneo_parser import parse_fbneo_source_tree
|
||||
from scripts.scraper.sparse_clone import fresh_cache, remove_clone, sparse_clone
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ from typing import IO, NamedTuple
|
||||
|
||||
from model.safeparse import yaml_load
|
||||
|
||||
from ._hash_merge import generated_start
|
||||
from ._hash_write import generated_start
|
||||
from .clex import blank_comments
|
||||
from .listxml import LISTXML_CHUNK, attrs, iter_elements, rom_members
|
||||
from .mame_parser import _collect_rom_macros, _expand_macros, _parse_rom_entries
|
||||
|
||||
@@ -37,12 +37,11 @@ from ._hash_merge import (
|
||||
_build_source_ref,
|
||||
compute_diff,
|
||||
load_profile,
|
||||
merge_mame_devices,
|
||||
merge_mame_profile,
|
||||
merge_mame_support,
|
||||
settled_required,
|
||||
write_mame_profile,
|
||||
)
|
||||
from ._mame_sections import merge_mame_devices, merge_mame_support
|
||||
from ._hash_write import write_mame_profile
|
||||
from .mame_devices import (
|
||||
attribute_softlists,
|
||||
attribute_systems,
|
||||
|
||||
@@ -18,9 +18,8 @@ from scripts.scraper._hash_merge import (
|
||||
load_profile,
|
||||
merge_fbneo_profile,
|
||||
merge_mame_profile,
|
||||
write_fbneo_profile,
|
||||
write_mame_profile,
|
||||
)
|
||||
from scripts.scraper._hash_write import write_fbneo_profile, write_mame_profile
|
||||
|
||||
|
||||
def _refresh_mame(path: Path, hashes: dict) -> None:
|
||||
@@ -256,10 +255,7 @@ class TestMameMerge(unittest.TestCase):
|
||||
The manager set loads "01", but written unquoted it re-parses as the
|
||||
integer 1 and the profile stops naming the file the driver opens.
|
||||
"""
|
||||
from scripts.scraper._hash_merge import (
|
||||
_append_new_entries,
|
||||
_format_contents,
|
||||
)
|
||||
from scripts.scraper._hash_write import _append_new_entries, _format_contents
|
||||
|
||||
contents = [
|
||||
{"name": "01", "description": "ROM bank 0-1", "size": 8192,
|
||||
|
||||
@@ -17,16 +17,9 @@ import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from scripts.model.safeparse import yaml_load
|
||||
from scripts.scraper._hash_merge import (
|
||||
DEVICE_SECTION,
|
||||
SOFTLIST_SECTION,
|
||||
load_profile,
|
||||
mame_ref_role,
|
||||
merge_mame_devices,
|
||||
merge_mame_profile,
|
||||
merge_mame_support,
|
||||
write_mame_profile,
|
||||
)
|
||||
from scripts.scraper._hash_write import DEVICE_SECTION, SOFTLIST_SECTION, write_mame_profile
|
||||
from scripts.scraper._hash_merge import load_profile, mame_ref_role, merge_mame_profile
|
||||
from scripts.scraper._mame_sections import merge_mame_devices, merge_mame_support
|
||||
from scripts.scraper.listxml import open_listxml
|
||||
from scripts.scraper.mame_devices import (
|
||||
ABSENT,
|
||||
|
||||
@@ -434,6 +434,10 @@ The merge preserves fields the hash data does not generate (system, note, requir
|
||||
and leaves entries not present in the hash data untouched. Uses text-level YAML
|
||||
manipulation rather than load-dump to maintain human-readable formatting.
|
||||
|
||||
`_hash_merge` computes the merge and the diff, `_hash_write` writes the result
|
||||
back into the profile text, and `_mame_sections` keeps the device and software
|
||||
list sections generated from MAME `-listxml`.
|
||||
|
||||
## Testing
|
||||
|
||||
### Development workflow
|
||||
|
||||
+3
-1
@@ -1730,7 +1730,9 @@ the GitHub and text fetches, and the shared CLI), `dat_parser.py` (clrmamepro DA
|
||||
`mame_parser.py` (MAME C source BIOS root set parser),
|
||||
`fbneo_parser.py` (FBNeo C source BIOS set parser),
|
||||
`dat_fetch.py` (upstream MAME `-listxml` and FBNeo DATs, cached under `.cache/dats/`),
|
||||
`_hash_merge.py` (text-based YAML patching that preserves formatting).
|
||||
`_hash_merge.py` (fetched hashes merged into a profile), `_hash_write.py` (text-based YAML
|
||||
patching that preserves formatting), `_mame_sections.py` (the generated MAME device and
|
||||
software list sections).
|
||||
|
||||
Adding a scraper: inherit `BaseScraper`, implement `fetch_requirements()` and
|
||||
`validate_format()` (a class missing either cannot be instantiated), call
|
||||
|
||||
Reference in new issue
Block a user