mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-11-22 17:47:48 -06:00
model.database.record_at and sha1_at read one cached path index with normpath keys. They replace records_by_path of resolve.packfiles, the manifest's own sha1 index, the per-call indexes of regionaudit, emulatorcheck and hidden_extras, and the linear scan of held_record. resolve_sha1 drops its by_path argument. A path spelled with ./ or a doubled slash now finds its record everywhere; the database writes none, so the output is the same.
115 lines
3.9 KiB
Python
115 lines
3.9 KiB
Python
"""Loading database.json and the MAME clone map written beside the collection."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
|
|
from core.cli import InputMissing
|
|
from core.paths import REPO_ROOT
|
|
|
|
|
|
class DatabaseUnreadable(InputMissing):
|
|
"""The database a command was pointed at does not exist or is not JSON."""
|
|
|
|
|
|
def load_database(db_path: str) -> dict:
|
|
"""Load database.json and return parsed dict.
|
|
|
|
A missing or damaged file raises DatabaseUnreadable, which run_cli turns
|
|
into one line saying how to build it: `--db nosuch.json` ended at the
|
|
bottom of a stack trace.
|
|
"""
|
|
try:
|
|
with open(db_path) as f:
|
|
return json.load(f)
|
|
except FileNotFoundError:
|
|
# A mistyped --db beside the real database was told to rescan the
|
|
# whole collection into a second file under the typo.
|
|
import difflib
|
|
|
|
typed = Path(db_path)
|
|
close = difflib.get_close_matches(
|
|
typed.name, [p.name for p in typed.parent.glob("*.json")], n=1, cutoff=0.8
|
|
)
|
|
if close:
|
|
raise DatabaseUnreadable(
|
|
f"{db_path} not found. Close match: {typed.with_name(close[0])}"
|
|
) from None
|
|
raise DatabaseUnreadable(
|
|
f"{db_path} not found. Build it with: python scripts/generate_db.py "
|
|
f"--force --bios-dir bios --output {db_path}"
|
|
) from None
|
|
except json.JSONDecodeError as exc:
|
|
raise DatabaseUnreadable(
|
|
f"{db_path} is not valid JSON ({exc}). Rebuild it with: "
|
|
f"python scripts/generate_db.py --force --bios-dir bios --output {db_path}"
|
|
) from None
|
|
|
|
|
|
# One slot: the files dict, its size, and path -> (sha1, record) built from it.
|
|
_PATH_INDEX: list[tuple[dict, int, dict[str, tuple[str, dict]]]] = []
|
|
|
|
|
|
def _path_index(db: dict) -> dict[str, tuple[str, dict]]:
|
|
"""Normalized path -> (sha1, record), built once per database."""
|
|
files = db.get("files", {})
|
|
if _PATH_INDEX:
|
|
cached, size, index = _PATH_INDEX[0]
|
|
if cached is files and size == len(files):
|
|
return index
|
|
index = {
|
|
os.path.normpath(record["path"]): (sha1, record)
|
|
for sha1, record in files.items()
|
|
if record.get("path")
|
|
}
|
|
_PATH_INDEX[:] = [(files, len(files), index)]
|
|
return index
|
|
|
|
|
|
def record_at(db: dict, path: str | None) -> dict | None:
|
|
"""The database record stored at a path, spelled any way normpath reads alike."""
|
|
if not path:
|
|
return None
|
|
found = _path_index(db).get(os.path.normpath(path))
|
|
return found[1] if found else None
|
|
|
|
|
|
def sha1_at(db: dict, path: str | None) -> str | None:
|
|
"""The sha1 the database keys the file at a path under."""
|
|
if not path:
|
|
return None
|
|
found = _path_index(db).get(os.path.normpath(path))
|
|
return found[0] if found else None
|
|
|
|
|
|
_mame_clone_map_cache: dict[str, dict[str, str]] = {}
|
|
|
|
|
|
def get_mame_clone_map(bios_dir: str | None = None) -> dict[str, str]:
|
|
"""The MAME clone map (clone_name -> canonical_name) of a scanned tree.
|
|
|
|
dedup.py writes _mame_clones.json beside the tree it scanned, and the
|
|
map of one tree says nothing about another: read from the repository
|
|
root whatever ``--bios-dir`` said, an alternate tree's renamed clones
|
|
fell out of its packs. Without a tree the repository's own is read.
|
|
"""
|
|
if bios_dir is None:
|
|
bios_dir = os.path.join(
|
|
REPO_ROOT, "bios"
|
|
)
|
|
clone_path = os.path.join(os.path.dirname(os.path.abspath(bios_dir)), "_mame_clones.json")
|
|
cached = _mame_clone_map_cache.get(clone_path)
|
|
if cached is not None:
|
|
return cached
|
|
clone_map: dict[str, str] = {}
|
|
if os.path.exists(clone_path):
|
|
with open(clone_path) as f:
|
|
data = json.load(f)
|
|
for canonical, info in data.items():
|
|
for clone in info.get("clones", []):
|
|
clone_map[clone] = canonical
|
|
_mame_clone_map_cache[clone_path] = clone_map
|
|
return clone_map
|