refactor: split alias collection and dedup

This commit is contained in:
Abdessamad Derraz committed 2026-11-17 07:52:20 +01:00
1 parent 86f32f6afb
commit b4cf8a969f
2 files changed
+379 -334

No files matched your search

+118 -94
View File
@@ -145,7 +145,7 @@ def _plan_group_removals(
# True duplicates: same filename in multiple directories
true_dupes_to_remove = []
for name, name_paths in by_name.items():
for name_paths in by_name.values():
if len(name_paths) > 1:
name_paths.sort(key=path_priority)
true_dupes_to_remove.extend(name_paths[1:])
@@ -194,6 +194,115 @@ def _plan_group_removals(
return true_dupes_to_remove
def _read_clone_map(clone_path: str) -> object:
"""The clone map an earlier run wrote, {} when there is none."""
# Read before anything is removed: the map is cumulative, and a run that
# stopped on an unreadable one after deleting clones would lose them.
try:
with open(clone_path) as f:
return json.load(f)
except FileNotFoundError:
return {}
except json.JSONDecodeError as e:
raise SystemExit(
f"{clone_path} is not valid JSON ({e}). Restore it from git "
"before deduplicating again"
) from e
def _remove_copies(canonical: str, true_dupes_to_remove: list[str], dry_run: bool) -> list[str]:
"""Remove (or name, in a dry run) each copy beside the canonical file."""
removed: list[str] = []
for dup in true_dupes_to_remove:
if dup == canonical:
continue
if not os.path.exists(dup):
continue
if dry_run:
print(f" WOULD REMOVE: {dup}")
else:
try:
os.remove(dup)
except OSError as e:
print(f" WARNING: cannot remove {dup}: {e}")
continue
# Clean up empty .variants/ directories
parent = os.path.dirname(dup)
if os.path.basename(parent) == ".variants" and not os.listdir(parent):
os.rmdir(parent)
removed.append(dup)
return removed
def _settle_group(paths: list[str], true_dupes_to_remove: list[str], dry_run: bool) -> dict:
"""Keep the best copy of one content and remove the others."""
# Find the best canonical across all paths
all_paths = [p for p in paths if p not in true_dupes_to_remove]
if not all_paths:
# All copies were marked for removal: keep the best one
all_paths_sorted = sorted(paths, key=path_priority)
all_paths = [all_paths_sorted[0]]
true_dupes_to_remove = [p for p in paths if p != all_paths[0]]
canonical = sorted(all_paths, key=path_priority)[0]
canonical_name = os.path.basename(canonical)
all_names = set(os.path.basename(p) for p in paths)
alias_names = sorted(all_names - {canonical_name})
size = os.path.getsize(canonical)
removed = _remove_copies(canonical, true_dupes_to_remove, dry_run)
if alias_names or true_dupes_to_remove:
action = "Would remove" if dry_run else "Removed"
dn = os.path.basename(canonical)
print(f" {dn} (keep: {canonical})")
if true_dupes_to_remove:
print(f" {action} {len(true_dupes_to_remove)} copies")
if alias_names:
print(f" MAME clones: {alias_names}")
return {
"canonical": canonical,
"removed": removed,
"aliases": alias_names,
"saved": size * len(removed),
}
def _remove_empty_dirs(bios_dir: str) -> int:
"""Remove the directories the removals left empty, bottom up."""
empty_cleaned = 0
for root, dirs, files in os.walk(bios_dir, topdown=False):
if not files and not dirs and root != bios_dir:
os.rmdir(root)
empty_cleaned += 1
return empty_cleaned
def _record_clone_map(
mame_clones: dict[str, dict], previous: object, clone_path: str, dry_run: bool
) -> None:
"""Write the clone map, merged with what earlier runs recorded."""
# A group is only visible while both copies are on disk, and this run
# has just deleted the clone. Writing only what was seen this time
# therefore erases every mapping an earlier run recorded, and the
# canonical zip stops answering to the names it stands in for. The
# older entries stay valid: their canonical file is still here.
if not dry_run and isinstance(previous, dict):
merged = dict(previous)
merged.update(mame_clones)
mame_clones = collapse_clone_chains(merged)
if dry_run:
print(f"\nWould write MAME clone map: {clone_path}")
else:
write_text_atomic(clone_path, json.dumps(mame_clones, indent=2, sort_keys=True))
print(f"\nWrote MAME clone map: {clone_path}")
print(
f" {len(mame_clones)} canonical ZIPs with "
f"{sum(len(v['clones']) for v in mame_clones.values())} clones"
)
def deduplicate(bios_dir: str, dry_run: bool = False) -> dict:
"""Remove true duplicates, map MAME device clones.
@@ -208,21 +317,10 @@ def deduplicate(bios_dir: str, dry_run: bool = False) -> dict:
clone_path = os.path.join(
os.path.dirname(os.path.abspath(bios_dir)), "_mame_clones.json"
)
# Read before anything is removed: the map is cumulative, and a run that
# stopped on an unreadable one after deleting clones would lose them.
try:
with open(clone_path) as f:
previous = json.load(f)
except FileNotFoundError:
previous = {}
except json.JSONDecodeError as e:
raise SystemExit(
f"{clone_path} is not valid JSON ({e}). Restore it from git "
"before deduplicating again"
) from e
previous = _read_clone_map(clone_path)
sha1_groups = scan_duplicates(bios_dir)
results = {}
results: dict[str, dict] = {}
total_removed = 0
total_saved = 0
mame_clones: dict[str, dict] = {} # canonical_name -> {sha1, clones: [names]}
@@ -236,65 +334,13 @@ def deduplicate(bios_dir: str, dry_run: bool = False) -> dict:
if not true_dupes_to_remove:
continue
# Find the best canonical across all paths
all_paths = [p for p in paths if p not in true_dupes_to_remove]
if not all_paths:
# All copies were marked for removal: keep the best one
all_paths_sorted = sorted(paths, key=path_priority)
all_paths = [all_paths_sorted[0]]
true_dupes_to_remove = [p for p in paths if p != all_paths[0]]
canonical = sorted(all_paths, key=path_priority)[0]
canonical_name = os.path.basename(canonical)
all_names = set(os.path.basename(p) for p in paths)
alias_names = sorted(all_names - {canonical_name})
size = os.path.getsize(canonical)
results[sha1] = {
"canonical": canonical,
"removed": [],
"aliases": alias_names,
}
for dup in true_dupes_to_remove:
if dup == canonical:
continue
if not os.path.exists(dup):
continue
if dry_run:
print(f" WOULD REMOVE: {dup}")
else:
try:
os.remove(dup)
except OSError as e:
print(f" WARNING: cannot remove {dup}: {e}")
continue
# Clean up empty .variants/ directories
parent = os.path.dirname(dup)
if os.path.basename(parent) == ".variants" and not os.listdir(parent):
os.rmdir(parent)
results[sha1]["removed"].append(dup)
total_removed += 1
total_saved += size
if alias_names or true_dupes_to_remove:
action = "Would remove" if dry_run else "Removed"
dn = os.path.basename(canonical)
print(f" {dn} (keep: {canonical})")
if true_dupes_to_remove:
print(f" {action} {len(true_dupes_to_remove)} copies")
if alias_names:
print(f" MAME clones: {alias_names}")
settled = _settle_group(paths, true_dupes_to_remove, dry_run)
total_removed += len(settled["removed"])
total_saved += settled.pop("saved")
results[sha1] = settled
# Clean up empty directories
if not dry_run:
empty_cleaned = 0
for root, dirs, files in os.walk(bios_dir, topdown=False):
if not files and not dirs and root != bios_dir:
os.rmdir(root)
empty_cleaned += 1
empty_cleaned = 0 if dry_run else _remove_empty_dirs(bios_dir)
prefix = "Would remove" if dry_run else "Removed"
print(f"\n{prefix}: {total_removed} files")
@@ -305,29 +351,7 @@ def deduplicate(bios_dir: str, dry_run: bool = False) -> dict:
print(f"Cleaned {empty_cleaned} empty directories")
if mame_clones:
# A group is only visible while both copies are on disk, and this run
# has just deleted the clone. Writing only what was seen this time
# therefore erases every mapping an earlier run recorded, and the
# canonical zip stops answering to the names it stands in for. The
# older entries stay valid: their canonical file is still here.
if not dry_run:
if isinstance(previous, dict):
merged = dict(previous)
merged.update(mame_clones)
mame_clones = collapse_clone_chains(merged)
if dry_run:
print(f"\nWould write MAME clone map: {clone_path}")
print(
f" {len(mame_clones)} canonical ZIPs with "
f"{sum(len(v['clones']) for v in mame_clones.values())} clones"
)
else:
write_text_atomic(clone_path, json.dumps(mame_clones, indent=2, sort_keys=True))
print(f"\nWrote MAME clone map: {clone_path}")
print(
f" {len(mame_clones)} canonical ZIPs with "
f"{sum(len(v['clones']) for v in mame_clones.values())} clones"
)
_record_clone_map(mame_clones, previous, clone_path, dry_run)
return results
+261 -240
View File
@@ -16,6 +16,7 @@ import os
import shutil
import sys
import tempfile
from dataclasses import dataclass, field
from datetime import datetime, timezone
from pathlib import Path
@@ -129,10 +130,52 @@ def _takes_primary(current: str, candidate: str) -> bool:
return _emulator_tree(current) and not _emulator_tree(candidate)
def _add_scanned(
files: dict[str, dict],
aliases: dict[str, list[dict]],
filepath: Path,
rel_path: str,
size: int,
hashes: dict,
) -> None:
"""Record a scanned file as its content's primary, or as an alias of it."""
sha1 = hashes["sha1"]
entry = {
"path": rel_path,
"name": _canonical_name(filepath),
"size": size,
**hashes,
}
if sha1 not in files:
files[sha1] = entry
elif _takes_primary(files[sha1]["path"], rel_path):
aliases.setdefault(sha1, []).append(
{"name": files[sha1]["name"], "path": files[sha1]["path"]}
)
files[sha1] = entry
else:
aliases.setdefault(sha1, []).append({"name": entry["name"], "path": rel_path})
def _cached_hashes(cached: dict | None, mtime: float, size: int) -> dict | None:
"""The cache's digests of a file that has not changed since."""
if cached is None:
return None
fresh = cached.get("mtime") == mtime and cached.get("size") == size
# Rebuilding the hash dict by hand dropped whichever digest the
# list forgot, and the entry was then written back to the cache
# without it, so the loss survived every later run. Requiring the
# full set instead turns a partial cache entry into a miss, which
# heals it.
if fresh and all(name in cached for name in CACHED_HASHES):
return {name: cached[name] for name in CACHED_HASHES}
return None
def scan_bios_dir(bios_dir: Path, cache: dict, force: bool) -> tuple[dict, dict, dict]:
"""Scan bios directory and compute hashes, using cache when possible."""
files = {}
aliases = {}
files: dict[str, dict] = {}
aliases: dict[str, list[dict]] = {}
new_cache = {}
for filepath in sorted(bios_dir.rglob("*")):
@@ -147,76 +190,10 @@ def scan_bios_dir(bios_dir: Path, cache: dict, force: bool) -> tuple[dict, dict,
size = stat.st_size
cache_key = rel_path
if not force and cache_key in cache:
cached = cache[cache_key]
fresh = cached.get("mtime") == mtime and cached.get("size") == size
# Rebuilding the hash dict by hand dropped whichever digest the
# list forgot, and the entry was then written back to the cache
# without it, so the loss survived every later run. Requiring the
# full set instead turns a partial cache entry into a miss, which
# heals it.
if fresh and all(name in cached for name in CACHED_HASHES):
hashes = {name: cached[name] for name in CACHED_HASHES}
sha1 = hashes["sha1"]
if sha1 in files:
if _takes_primary(files[sha1]["path"], rel_path):
if sha1 not in aliases:
aliases[sha1] = []
aliases[sha1].append(
{"name": files[sha1]["name"], "path": files[sha1]["path"]}
)
files[sha1] = {
"path": rel_path,
"name": _canonical_name(filepath),
"size": size,
**hashes,
}
else:
if sha1 not in aliases:
aliases[sha1] = []
aliases[sha1].append(
{"name": _canonical_name(filepath), "path": rel_path}
)
else:
entry = {
"path": rel_path,
"name": _canonical_name(filepath),
"size": size,
**hashes,
}
files[sha1] = entry
new_cache[cache_key] = {**hashes, "mtime": mtime, "size": size}
continue
hashes = _settled_hashes(filepath, cache.get(cache_key), mtime, size)
sha1 = hashes["sha1"]
if sha1 in files:
if _takes_primary(files[sha1]["path"], rel_path):
if sha1 not in aliases:
aliases[sha1] = []
aliases[sha1].append(
{"name": files[sha1]["name"], "path": files[sha1]["path"]}
)
files[sha1] = {
"path": rel_path,
"name": _canonical_name(filepath),
"size": size,
**hashes,
}
else:
if sha1 not in aliases:
aliases[sha1] = []
aliases[sha1].append(
{"name": _canonical_name(filepath), "path": rel_path}
)
else:
entry = {
"path": rel_path,
"name": _canonical_name(filepath),
"size": size,
**hashes,
}
files[sha1] = entry
hashes = None if force else _cached_hashes(cache.get(cache_key), mtime, size)
if hashes is None:
hashes = _settled_hashes(filepath, cache.get(cache_key), mtime, size)
_add_scanned(files, aliases, filepath, rel_path, size, hashes)
new_cache[cache_key] = {**hashes, "mtime": mtime, "size": size}
return files, aliases, new_cache
@@ -244,11 +221,11 @@ def _path_suffixes(rel_path: str) -> list[str]:
def build_indexes(files: dict, aliases: dict) -> dict:
"""Build secondary indexes for fast lookup."""
by_md5 = {}
by_name = {}
by_crc32 = {}
by_sha256 = {}
by_path_suffix = {}
by_md5: dict[str, str] = {}
by_name: dict[str, list[str]] = {}
by_crc32: dict[str, str] = {}
by_sha256: dict[str, str] = {}
by_path_suffix: dict[str, list[str]] = {}
for sha1, entry in files.items():
by_md5[entry["md5"]] = sha1
@@ -624,25 +601,38 @@ class UnreadableSource(Exception):
self.sources = sources
def _collect_all_aliases(
files: dict,
copies: dict | None = None,
shadowed: list[tuple[str, set[str], list[str]]] | None = None,
) -> dict:
"""Collect alternate filenames from platform YAMLs, core-info, and known aliases.
@dataclass
class _AliasBook:
"""The alternate names collected so far, and what designates a content."""
Registers alternate names so generate_pack can resolve files stored under different names.
``copies`` maps a sha1 to the other files the scan found with that content.
``shadowed`` receives, per profile entry, its name, the contents its hashes
designate and the aliases a primary's name already carries.
"""
files: dict
md5_to_sha1: dict[str, str]
name_to_sha1: dict[str, str]
# The contents held under each name, every copy counted.
holders: dict[str, set[str]]
# The files a name designates once case is folded.
families: dict[str, set[str]]
# A name only .variants/ copies carry is not a primary's.
named_primaries: set[str]
aliases: dict[str, list[dict]] = field(default_factory=dict)
def add(self, name: str, matched_sha1: str) -> None:
if not name or name in self.named_primaries:
return
if matched_sha1 not in self.aliases:
self.aliases[matched_sha1] = []
existing = {a["name"] for a in self.aliases[matched_sha1]}
if name not in existing:
self.aliases[matched_sha1].append({"name": name, "path": ""})
def _alias_book(files: dict, copies: dict | None) -> _AliasBook:
"""Index the scanned files by md5, name and folded name."""
md5_to_sha1 = {}
name_to_sha1 = {}
name_count: dict[str, int] = {}
for sha1, entry in files.items():
md5_to_sha1[entry["md5"]] = sha1
name_to_sha1[entry["name"]] = sha1
name_count[entry["name"]] = name_count.get(entry["name"], 0) + 1
# The contents held under each name, every copy counted and not only the
# primary: which copy of identical bytes is primary follows path order.
# Keyed on primaries alone, project64's 64DD_IPL_v12_JPN.bin stopped
@@ -670,8 +660,6 @@ def _collect_all_aliases(
for copy in copy_list:
_hold(copy["name"], copy["path"], sha1)
aliases = {}
# A name only .variants/ copies carry is not a primary's: those copies are
# the other revisions of whatever the name designates. Retroarch pins
# bios_E.sms to v1.3, and with the name refused, Genesis Plus GX got the
@@ -679,177 +667,210 @@ def _collect_all_aliases(
named_primaries = {
entry["name"] for entry in files.values() if "/.variants/" not in entry["path"]
}
return _AliasBook(files, md5_to_sha1, name_to_sha1, holders, families, named_primaries)
def _add_alias(name: str, matched_sha1: str) -> None:
if not name or name in named_primaries:
return
if matched_sha1 not in aliases:
aliases[matched_sha1] = []
existing = {a["name"] for a in aliases[matched_sha1]}
if name not in existing:
aliases[matched_sha1].append({"name": name, "path": ""})
def _platform_aliases(book: _AliasBook, unreadable: list[str]) -> None:
"""Register the names platform YAMLs give a held content."""
platforms_dir = Path("platforms")
if not platforms_dir.is_dir():
return
for platform_name in list_registered_platforms(
str(platforms_dir)
):
config_file = platforms_dir / f"{platform_name}.yml"
try:
with open(config_file) as f:
config = yaml_load(f) or {}
except (yaml.YAMLError, OSError) as e:
unreadable.append(f"{config_file}: {e}")
continue
for system in config.get("systems", {}).values():
for file_entry in system.get("files", []):
name = file_entry.get("name", "")
sha1 = file_entry.get("sha1", "")
md5 = file_entry.get("md5", "")
# A zipped_file entry verifies a ROM INSIDE the
# archive, so its hash describes the member and not
# the file the name designates. Registering the name
# against that hash made d2fdc.zip an alias of a
# loose 256-byte state-machine-16.rom, which three
# platforms were then served in place of the archive.
if file_entry.get("zipped_file"):
continue
matched = None
if sha1 and sha1 in book.files:
matched = sha1
elif md5 and md5 in book.md5_to_sha1:
matched = book.md5_to_sha1[md5]
if matched:
book.add(name, matched)
def _profile_entry_aliases(
book: _AliasBook,
file_entry: dict,
primary_names: set[str],
shadowed: list[tuple[str, set[str], list[str]]] | None,
) -> None:
"""Register the aliases one profile entry gives the contents it designates."""
entry_name = file_entry.get("name", "")
entry_aliases = _own_aliases(file_entry, primary_names)
# A profile may accept several revisions: each held one
# is designated by the entry, none of them by guess.
matched: set[str] = {
value for value in _as_list(file_entry.get("sha1"))
if value in book.files
}
matched |= {
book.md5_to_sha1[value]
for value in _as_list(file_entry.get("md5"))
if value in book.md5_to_sha1
}
if matched:
# Proven by content, the profile's own name designates
# the file whatever the collection calls it.
entry_aliases.insert(0, entry_name)
if not entry_aliases:
return
if shadowed is not None:
taken = [
alias for alias in entry_aliases
if alias != entry_name and alias in book.named_primaries
]
if taken:
shadowed.append((entry_name, set(matched), taken))
held = book.holders.get(entry_name, set())
if (
not matched
and len(held) == 1
and len(book.families[entry_name.casefold()]) == 1
):
# A name carried by several contents names none of them:
# quasi88's disk.rom aliases went to whichever
# disk.rom the scan met last, a Tandy CoCo ROM.
matched = set(held)
for sha in sorted(matched):
for alias_name in entry_aliases:
book.add(alias_name, sha)
def _profile_aliases(
book: _AliasBook,
shadowed: list[tuple[str, set[str], list[str]]] | None,
unreadable: list[str],
) -> None:
"""Register the aliases emulator YAMLs give (aliases field on file entries)."""
emulators_dir = Path("emulators")
if not emulators_dir.is_dir():
return
for emu_file in source_yaml_paths(emulators_dir):
try:
with open(emu_file) as f:
emu_config = yaml_load(f) or {}
except (yaml.YAMLError, OSError) as e:
unreadable.append(f"{emu_file}: {e}")
continue
# A profile whose entries name each other (dosbox-x opens
# MT32_CONTROL.ROM and CM32L_CONTROL.ROM alike, so each entry
# aliases the other) states that ITS slot takes either name.
# Indexed globally that became evidence for every emulator:
# ScummVM's MT-32 slot received the CM-32L ROMs.
primary_names = _primary_names(emu_config.get("files", []))
for file_entry in emu_config.get("files", []):
_profile_entry_aliases(book, file_entry, primary_names, shadowed)
# Identical content named differently across platforms/cores
KNOWN_ALIAS_GROUPS = [
# ColecoVision - all these are the same 8KB BIOS
["colecovision.rom", "coleco.rom", "BIOS.col", "bioscv.rom"],
# Game Boy - DMG boot ROM
["gb_bios.bin", "dmg_boot.bin", "dmg_rom.bin", "dmg0_rom.bin"],
# Game Boy Color - CGB boot ROM
["gbc_bios.bin", "cgb_boot.bin", "cgb0_boot.bin", "cgb_agb_boot.bin"],
# Super Game Boy
["sgb_bios.bin", "sgb_boot.bin", "sgb.boot.rom"],
["sgb2_bios.bin", "sgb2_boot.bin", "sgb2.boot.rom"],
["sgb1.program.rom", "SGB1.sfc/program.rom"],
["sgb2.program.rom", "SGB2.sfc/program.rom"],
# Nintendo DS. DraStic opens nds_bios_arm9.bin, nds_bios_arm7.bin
# and nds_firmware.bin and checks their size only: the collection
# answers those names with its DS dumps.
["NDS_Bios7.bin", "bios7.bin", "nds7.bin", "nds_bios_arm7.bin"],
["NDS_Bios9.bin", "bios9.bin", "nds9.bin", "nds_bios_arm9.bin"],
["NDS_Lite_Firmware.bin", "nds_firmware.bin"],
["dsi_sd_card.bin", "nds_sd_card.bin"],
# MSX
["MSX.ROM", "MSX.rom", "Machines/Shared Roms/MSX.rom"],
# NEC PC-98
["N88KNJ1.ROM", "n88knj1.rom", "quasi88/n88knj1.rom"],
# Enterprise
["zt19uk.rom", "zt19hfnt.rom", "ep128emu/roms/zt19hfnt.rom"],
# ZX Spectrum
["48.rom", "zx48.rom"],
# SquirrelJME - all JARs are the same
[
"squirreljme.sqc",
"squirreljme.jar",
"squirreljme-fast.jar",
"squirreljme-slow.jar",
"squirreljme-slow-test.jar",
"squirreljme-0.3.0.jar",
"squirreljme-0.3.0-fast.jar",
"squirreljme-0.3.0-slow.jar",
"squirreljme-0.3.0-slow-test.jar",
],
# Arcade - FBNeo spectrum
["spectrum.zip", "fbneo/spectrum.zip", "spec48k.zip"],
]
def _collect_all_aliases(
files: dict,
copies: dict | None = None,
shadowed: list[tuple[str, set[str], list[str]]] | None = None,
) -> dict:
"""Collect alternate filenames from platform YAMLs, core-info, and known aliases.
Registers alternate names so generate_pack can resolve files stored under different names.
``copies`` maps a sha1 to the other files the scan found with that content.
``shadowed`` receives, per profile entry, its name, the contents its hashes
designate and the aliases a primary's name already carries.
"""
book = _alias_book(files, copies)
# A source that does not parse is reported and the run refused: written
# without it, the database lost every alias the file proves and the
# command exited 0 (dosbox-x's wqy fonts answered not_found).
unreadable: list[str] = []
platforms_dir = Path("platforms")
if platforms_dir.is_dir():
for platform_name in list_registered_platforms(
str(platforms_dir)
):
config_file = platforms_dir / f"{platform_name}.yml"
try:
with open(config_file) as f:
config = yaml_load(f) or {}
except (yaml.YAMLError, OSError) as e:
unreadable.append(f"{config_file}: {e}")
continue
for sys_id, system in config.get("systems", {}).items():
for file_entry in system.get("files", []):
name = file_entry.get("name", "")
sha1 = file_entry.get("sha1", "")
md5 = file_entry.get("md5", "")
# A zipped_file entry verifies a ROM INSIDE the
# archive, so its hash describes the member and not
# the file the name designates. Registering the name
# against that hash made d2fdc.zip an alias of a
# loose 256-byte state-machine-16.rom, which three
# platforms were then served in place of the archive.
if file_entry.get("zipped_file"):
continue
matched = None
if sha1 and sha1 in files:
matched = sha1
elif md5 and md5 in md5_to_sha1:
matched = md5_to_sha1[md5]
if matched:
_add_alias(name, matched)
_platform_aliases(book, unreadable)
# core-info is not read here: it was the only network call of a build
# that must give the same database offline, and the one name it added
# (gearcoleco's writer.rom) is a profile entry proven by its sha1, which
# the profile pass below now registers.
# Collect aliases from emulator YAMLs (aliases field on file entries)
emulators_dir = Path("emulators")
if emulators_dir.is_dir():
for emu_file in source_yaml_paths(emulators_dir):
try:
with open(emu_file) as f:
emu_config = yaml_load(f) or {}
except (yaml.YAMLError, OSError) as e:
unreadable.append(f"{emu_file}: {e}")
continue
# A profile whose entries name each other (dosbox-x opens
# MT32_CONTROL.ROM and CM32L_CONTROL.ROM alike, so each entry
# aliases the other) states that ITS slot takes either name.
# Indexed globally that became evidence for every emulator:
# ScummVM's MT-32 slot received the CM-32L ROMs.
primary_names = _primary_names(emu_config.get("files", []))
for file_entry in emu_config.get("files", []):
entry_name = file_entry.get("name", "")
entry_aliases = _own_aliases(file_entry, primary_names)
# A profile may accept several revisions: each held one
# is designated by the entry, none of them by guess.
matched: set[str] = {
value for value in _as_list(file_entry.get("sha1"))
if value in files
}
matched |= {
md5_to_sha1[value]
for value in _as_list(file_entry.get("md5"))
if value in md5_to_sha1
}
if matched:
# Proven by content, the profile's own name designates
# the file whatever the collection calls it.
entry_aliases.insert(0, entry_name)
if not entry_aliases:
continue
if shadowed is not None:
taken = [
alias for alias in entry_aliases
if alias != entry_name and alias in named_primaries
]
if taken:
shadowed.append((entry_name, set(matched), taken))
held = holders.get(entry_name, set())
if (
not matched
and len(held) == 1
and len(families[entry_name.casefold()]) == 1
):
# A name carried by several contents names none of them:
# quasi88's disk.rom aliases went to whichever
# disk.rom the scan met last, a Tandy CoCo ROM.
matched = set(held)
for sha in sorted(matched):
for alias_name in entry_aliases:
_add_alias(alias_name, sha)
# Identical content named differently across platforms/cores
KNOWN_ALIAS_GROUPS = [
# ColecoVision - all these are the same 8KB BIOS
["colecovision.rom", "coleco.rom", "BIOS.col", "bioscv.rom"],
# Game Boy - DMG boot ROM
["gb_bios.bin", "dmg_boot.bin", "dmg_rom.bin", "dmg0_rom.bin"],
# Game Boy Color - CGB boot ROM
["gbc_bios.bin", "cgb_boot.bin", "cgb0_boot.bin", "cgb_agb_boot.bin"],
# Super Game Boy
["sgb_bios.bin", "sgb_boot.bin", "sgb.boot.rom"],
["sgb2_bios.bin", "sgb2_boot.bin", "sgb2.boot.rom"],
["sgb1.program.rom", "SGB1.sfc/program.rom"],
["sgb2.program.rom", "SGB2.sfc/program.rom"],
# Nintendo DS. DraStic opens nds_bios_arm9.bin, nds_bios_arm7.bin
# and nds_firmware.bin and checks their size only: the collection
# answers those names with its DS dumps.
["NDS_Bios7.bin", "bios7.bin", "nds7.bin", "nds_bios_arm7.bin"],
["NDS_Bios9.bin", "bios9.bin", "nds9.bin", "nds_bios_arm9.bin"],
["NDS_Lite_Firmware.bin", "nds_firmware.bin"],
["dsi_sd_card.bin", "nds_sd_card.bin"],
# MSX
["MSX.ROM", "MSX.rom", "Machines/Shared Roms/MSX.rom"],
# NEC PC-98
["N88KNJ1.ROM", "n88knj1.rom", "quasi88/n88knj1.rom"],
# Enterprise
["zt19uk.rom", "zt19hfnt.rom", "ep128emu/roms/zt19hfnt.rom"],
# ZX Spectrum
["48.rom", "zx48.rom"],
# SquirrelJME - all JARs are the same
[
"squirreljme.sqc",
"squirreljme.jar",
"squirreljme-fast.jar",
"squirreljme-slow.jar",
"squirreljme-slow-test.jar",
"squirreljme-0.3.0.jar",
"squirreljme-0.3.0-fast.jar",
"squirreljme-0.3.0-slow.jar",
"squirreljme-0.3.0-slow-test.jar",
],
# Arcade - FBNeo spectrum
["spectrum.zip", "fbneo/spectrum.zip", "spec48k.zip"],
]
_profile_aliases(book, shadowed, unreadable)
for group in KNOWN_ALIAS_GROUPS:
matched_sha1 = None
for name in group:
if name in name_to_sha1:
matched_sha1 = name_to_sha1[name]
if name in book.name_to_sha1:
matched_sha1 = book.name_to_sha1[name]
break
if not matched_sha1:
continue
for name in group:
_add_alias(name, matched_sha1)
book.add(name, matched_sha1)
if unreadable:
raise UnreadableSource(unreadable)
return aliases
return book.aliases
if __name__ == "__main__":