mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-11-23 10:07:48 -06:00
core.ziptools.zip_members lists the name, size and crc32 of each member once per archive revision. The site lookup and cards, the gap report, the pack check, the romset importer, the recipe identification and build, provenance, derivations and the atom pool read it instead of opening each archive again. Each reader also tested a .zip suffix and skipped the .variants/ copies (x.zip.1a2b3c4d): the site lookup listed no member of a copy, the gap report's member index never saw one, the pack check never compared a copy's members, and the pack builders copied a copy without its CRC test. They now use is_archive_path. The inner ROM index of the resolver reads copies too, and a copy answers only for a member no primary holds.
322 lines
13 KiB
Python
322 lines
13 KiB
Python
"""Dump-catalog coverage: which catalogued dumps the collection holds, and the profiles a missing one would fill."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import sys
|
|
import zipfile
|
|
from pathlib import Path
|
|
|
|
from core.hashing import parse_hash_list
|
|
from core.paths import REPO_ROOT
|
|
from core.ziptools import is_archive_path, zip_members
|
|
from resolve.content import size_fits
|
|
|
|
|
|
HASH_FIELDS = ("sha1", "md5", "sha256")
|
|
|
|
|
|
def _crc_key(value: str) -> str:
|
|
return value.removeprefix("0x").zfill(8)
|
|
|
|
|
|
def _fits(size: int | None, entry: dict) -> bool:
|
|
"""Whether a size meets what a profile entry states about it."""
|
|
return size is not None and size_fits(entry, size)
|
|
|
|
|
|
def profile_needs(profiles: dict, db: dict) -> dict:
|
|
"""The files emulator profiles read that the collection does not hold.
|
|
|
|
Keyed by every hash a profile accepts, and, for an entry that declares
|
|
a size and no hash, by its names casefolded. An entry with neither
|
|
cannot be told apart from any same-named dump and is left out, as is a
|
|
file the emulator installs itself (`bundled:`) on every platform.
|
|
"""
|
|
files = db.get("files", {})
|
|
indexes = db.get("indexes", {})
|
|
held_hashes = {
|
|
"sha1": set(files),
|
|
"md5": set(indexes.get("by_md5", {})),
|
|
"sha256": set(indexes.get("by_sha256", {})),
|
|
}
|
|
held_crc = {
|
|
(_crc_key(str(f.get("crc32", "")).lower()), f.get("size"))
|
|
for f in files.values() if f.get("crc32")
|
|
}
|
|
# Held under its own name or under an alias the database indexes, as
|
|
# the resolver and verify read it: np2pi's KANJI2.ROM is the PC-88
|
|
# n88knj2.rom, and its own name alone listed it missing.
|
|
sizes_by_name: dict[str, set[int]] = {}
|
|
for record in files.values():
|
|
sizes_by_name.setdefault(str(record.get("name", "")).casefold(), set()).add(
|
|
record.get("size")
|
|
)
|
|
for alias, sha1s in indexes.get("by_name", {}).items():
|
|
held = sizes_by_name.setdefault(str(alias).casefold(), set())
|
|
held.update(files[sha1].get("size") for sha1 in sha1s if sha1 in files)
|
|
|
|
by_hash: dict[tuple, list[dict]] = {}
|
|
by_name: dict[str, list[tuple[dict, dict]]] = {}
|
|
for emulator, profile in sorted(profiles.items()):
|
|
for entry in profile.get("files", []) or []:
|
|
name = str(entry.get("name", ""))
|
|
if not name or name.endswith("/"):
|
|
continue
|
|
# Installed with the emulator, so not an acquisition target,
|
|
# unless a platform package leaves it out.
|
|
if entry.get("bundled") and not entry.get("not_bundled_on"):
|
|
continue
|
|
need = {"emulator": emulator, "name": name,
|
|
"required": bool(entry.get("required"))}
|
|
hashes = {(field, v) for field in HASH_FIELDS for v in parse_hash_list(entry.get(field))}
|
|
crcs = {_crc_key(v) for v in parse_hash_list(entry.get("crc32"))}
|
|
declared = entry.get("size")
|
|
sizes = [] if declared is None else (
|
|
declared if isinstance(declared, list) else [declared]
|
|
)
|
|
crc_keys = {(crc, int(size)) for crc in crcs for size in sizes}
|
|
if hashes or crc_keys:
|
|
if any(v in held_hashes[field] for field, v in hashes) or (
|
|
crc_keys & held_crc
|
|
):
|
|
continue
|
|
for key in hashes | {("crc32", crc, size) for crc, size in crc_keys}:
|
|
by_hash.setdefault(key, []).append(need)
|
|
continue
|
|
if all(entry.get(k) is None for k in ("size", "min_size", "max_size")):
|
|
continue
|
|
names = [name, *(entry.get("aliases") or [])]
|
|
# An unsourceable entry is not held by a homonym: xm7's
|
|
# KANJI2.ROM is the Fujitsu font, the held one the PC-88's.
|
|
if not entry.get("unsourceable") and any(
|
|
_fits(held, entry)
|
|
for alias in names
|
|
for held in sizes_by_name.get(str(alias).casefold(), ())
|
|
):
|
|
continue
|
|
for alias in names:
|
|
by_name.setdefault(str(alias).casefold(), []).append((need, entry))
|
|
return {"by_hash": by_hash, "by_name": by_name}
|
|
|
|
|
|
def needs_filled(entry: dict, needs: dict) -> list[dict]:
|
|
"""The profile needs a catalog dump answers, required ones first."""
|
|
found: dict[tuple[str, str], dict] = {}
|
|
keys: list[tuple[object, ...]] = [
|
|
(field, v) for field in HASH_FIELDS for v in parse_hash_list(entry.get(field))
|
|
]
|
|
if entry.get("size") is not None:
|
|
keys += [("crc32", _crc_key(v), entry["size"]) for v in parse_hash_list(entry.get("crc32"))]
|
|
for key in keys:
|
|
for need in needs["by_hash"].get(key, []):
|
|
found.setdefault((need["emulator"], need["name"]), {**need, "by": key[0]})
|
|
size = entry.get("size")
|
|
name = str(entry.get("name", "")).casefold()
|
|
for need, declared in needs["by_name"].get(name, []):
|
|
if _fits(size, declared):
|
|
found.setdefault(
|
|
(need["emulator"], need["name"]), {**need, "by": "name and size"}
|
|
)
|
|
return sorted(found.values(), key=lambda n: (not n["required"], n["emulator"], n["name"]))
|
|
|
|
|
|
def archive_members(db: dict) -> dict[tuple[str, int], list[tuple[str, str]]]:
|
|
"""(crc32, size) of every member of the collection's ZIPs -> (zip, member).
|
|
|
|
Read from the central directories alone: a romset member is a dump the
|
|
collection already holds, and listing it as an acquisition target sent
|
|
searches after astrocdw.zip's bioswhit.bin and the Gamate BIOS.
|
|
"""
|
|
members: dict[tuple[str, int], list[tuple[str, str]]] = {}
|
|
for entry in db.get("files", {}).values():
|
|
path = entry.get("path", "")
|
|
if not is_archive_path(path) or not os.path.exists(path):
|
|
continue
|
|
try:
|
|
for member in zip_members(path):
|
|
key = (member.crc32, member.size)
|
|
members.setdefault(key, []).append((path, member.name))
|
|
except (zipfile.BadZipFile, OSError) as exc:
|
|
print(f" WARNING: {path}: {exc}", file=sys.stderr)
|
|
return members
|
|
|
|
|
|
def _held_in_archive(entry: dict, members: dict) -> bool:
|
|
"""Whether a catalog entry is a member of one of the collection's ZIPs.
|
|
|
|
crc32 and size only nominate candidates. A declared sha1 must match the
|
|
member's bytes.
|
|
"""
|
|
crc = str(entry.get("crc32") or "").lower()
|
|
size = entry.get("size")
|
|
if not crc or size is None:
|
|
return False
|
|
candidates = members.get((crc.zfill(8), int(size)), [])
|
|
sha1 = str(entry.get("sha1") or "").lower()
|
|
if not sha1:
|
|
return bool(candidates)
|
|
for path, name in candidates:
|
|
try:
|
|
with zipfile.ZipFile(path) as archive:
|
|
if hashlib.sha1(archive.read(name)).hexdigest() == sha1:
|
|
return True
|
|
except (zipfile.BadZipFile, OSError, KeyError) as exc:
|
|
print(f" WARNING: {path}:{name}: {exc}", file=sys.stderr)
|
|
return False
|
|
|
|
|
|
RECIPES_DIR = REPO_ROOT / "recipes"
|
|
|
|
|
|
def romset_index(recipes_dir: Path = RECIPES_DIR) -> dict[tuple[str, int], list[dict]]:
|
|
"""(crc32, size) of every member of an imported romset -> where it sits.
|
|
|
|
A dump the collection lacks may still ship inside an emulator's romset:
|
|
17 TOSEC ZX Spectrum ROMs are members of MAME's spectrum.zip. Naming the
|
|
set and the versions that carry it turns a dead end into a download.
|
|
"""
|
|
index: dict[tuple[str, int], list[dict]] = {}
|
|
for snapshot in sorted(recipes_dir.glob("*.json")):
|
|
with snapshot.open(encoding="utf-8") as handle:
|
|
data = json.load(handle)
|
|
source = data.get("source", snapshot.stem)
|
|
for entry in data.get("entries", []):
|
|
dats = entry.get("dats") or [entry.get("dat", "")]
|
|
for member in entry.get("members", []) or []:
|
|
crc, size = str(member.get("crc32") or "").lower(), member.get("size")
|
|
if not crc or size is None:
|
|
continue
|
|
index.setdefault((crc.zfill(8), int(size)), []).append({
|
|
"source": source,
|
|
"archive": entry.get("name", ""),
|
|
"member": member.get("name", ""),
|
|
"sha1": member.get("sha1", ""),
|
|
"dats": dats,
|
|
})
|
|
return index
|
|
|
|
|
|
def romsets_holding(entry: dict, index: dict) -> list[dict]:
|
|
"""The imported romsets whose members are this dump.
|
|
|
|
A member and an entry that both carry a sha1 must agree on it: crc32
|
|
and size alone match by accident across 40 000 ROMs.
|
|
"""
|
|
crc, size = str(entry.get("crc32") or "").lower(), entry.get("size")
|
|
if not crc or size is None:
|
|
return []
|
|
sha1 = str(entry.get("sha1") or "").lower()
|
|
# A set whose contents changed between versions is stored once per
|
|
# distinct content: one line per set and member, with every version.
|
|
merged: dict[tuple[str, str, str], dict] = {}
|
|
for hit in index.get((crc.zfill(8), int(size)), []):
|
|
if sha1 and hit["sha1"] and hit["sha1"].lower() != sha1:
|
|
continue
|
|
key = (hit["source"], hit["archive"], hit["member"])
|
|
held = merged.setdefault(key, {**hit, "dats": []})
|
|
held["dats"] = sorted(set(held["dats"]) | set(hit["dats"]))
|
|
return list(merged.values())
|
|
|
|
|
|
def dump_identity(entry: dict) -> tuple:
|
|
"""What makes two catalog listings one dump: the strongest hash stated."""
|
|
if entry.get("sha1"):
|
|
return ("sha1", str(entry["sha1"]).lower())
|
|
if entry.get("md5"):
|
|
return ("md5", str(entry["md5"]).lower(), entry.get("size"))
|
|
if entry.get("crc32"):
|
|
return ("crc32", str(entry["crc32"]).lower(), entry.get("size"))
|
|
return ("name", entry.get("dat", ""), entry.get("name", ""))
|
|
|
|
|
|
def distinct_missing(report: dict) -> int:
|
|
"""Missing dumps across every catalog, each counted once."""
|
|
return len({dump_identity(e) for data in report.values() for e in data["missing"]})
|
|
|
|
|
|
def build_report(
|
|
db: dict,
|
|
snapshots: dict,
|
|
members: dict | None = None,
|
|
romsets: dict | None = None,
|
|
needs: dict | None = None,
|
|
) -> dict:
|
|
"""Compare each snapshot against the collection.
|
|
|
|
A DAT counts as covered when the collection holds at least one of
|
|
its entries. Missing entries from covered DATs are acquisition
|
|
targets. Missing entries from DATs the collection does not cover at
|
|
all are out of scope and only counted. No-Intro tags every non-game
|
|
dump "[BIOS]", including digital title distribution, so without this
|
|
split the target list is swamped by content the project never ships.
|
|
"""
|
|
by_sha1 = db.get("files", {})
|
|
# The members a romset holds are held too, whoever asks: the site built
|
|
# its Wanted page without them and asked the public for the Astrocade
|
|
# and Gamate BIOS that astrocdw.zip and gamate.zip already carry.
|
|
members = archive_members(db) if members is None else members
|
|
romsets = romset_index() if romsets is None else romsets
|
|
by_md5_size = {
|
|
(entry.get("md5", ""), entry.get("size", 0)) for entry in by_sha1.values()
|
|
}
|
|
# Redump lists some dumps by crc32 alone (ps2-0101jd-20030110, DTL-H10100):
|
|
# collected as a loose file it would stay MISSING for good without this.
|
|
by_crc32_size = {
|
|
(str(entry.get("crc32", "")).lower(), entry.get("size", 0))
|
|
for entry in by_sha1.values()
|
|
if entry.get("crc32")
|
|
}
|
|
|
|
report = {}
|
|
for source, snapshot in sorted(snapshots.items()):
|
|
matched: set = set()
|
|
covered_dats = set()
|
|
unmatched = []
|
|
for entry in snapshot["entries"]:
|
|
if (
|
|
entry.get("sha1") in by_sha1
|
|
or (entry.get("md5"), entry.get("size")) in by_md5_size
|
|
or (str(entry.get("crc32") or "").lower(), entry.get("size")) in by_crc32_size
|
|
or _held_in_archive(entry, members)
|
|
):
|
|
matched.add(dump_identity(entry))
|
|
covered_dats.add(entry.get("dat", ""))
|
|
else:
|
|
unmatched.append(entry)
|
|
# One dump listed by several DATs (No-Intro's three FM-7 DATs share
|
|
# DICROM.rom, KANJI.rom...) is one dump: counted per listing, the
|
|
# report announced 2 485 missing for 2 284 distinct ones.
|
|
merged: dict = {}
|
|
for entry in unmatched:
|
|
if entry.get("dat", "") not in covered_dats:
|
|
continue
|
|
held = merged.get(dump_identity(entry))
|
|
if held is None:
|
|
merged[dump_identity(entry)] = {**entry, "dats": [entry.get("dat", "")]}
|
|
elif entry.get("dat", "") not in held["dats"]:
|
|
held["dats"].append(entry.get("dat", ""))
|
|
missing = list(merged.values())
|
|
for entry in missing:
|
|
holders = romsets_holding(entry, romsets)
|
|
if holders:
|
|
entry["romsets"] = holders
|
|
filled = needs_filled(entry, needs) if needs else []
|
|
if filled:
|
|
entry["needed_by"] = filled
|
|
out_of_scope = len({
|
|
dump_identity(e) for e in unmatched if e.get("dat", "") not in covered_dats
|
|
})
|
|
report[source] = {
|
|
"imported_at": snapshot.get("imported_at", ""),
|
|
"dats": snapshot.get("dats", {}),
|
|
"total": len(snapshot["entries"]),
|
|
"matched": len(matched),
|
|
"covered_dats": sorted(covered_dats),
|
|
"missing": missing,
|
|
"out_of_scope": out_of_scope,
|
|
}
|
|
return report
|