"""Shared utilities for retrobios scripts. Single source of truth for platform config loading, hash computation, and file resolution - eliminates DRY violations across scripts. """ from __future__ import annotations import hashlib import json import os import zipfile import zlib from pathlib import Path try: import yaml except ImportError: yaml = None def compute_hashes(filepath: str | Path) -> dict[str, str]: """Compute SHA1, MD5, SHA256, CRC32 for a file.""" sha1 = hashlib.sha1() md5 = hashlib.md5() sha256 = hashlib.sha256() crc = 0 with open(filepath, "rb") as f: for chunk in iter(lambda: f.read(65536), b""): sha1.update(chunk) md5.update(chunk) sha256.update(chunk) crc = zlib.crc32(chunk, crc) return { "sha1": sha1.hexdigest(), "md5": md5.hexdigest(), "sha256": sha256.hexdigest(), "crc32": format(crc & 0xFFFFFFFF, "08x"), } def load_database(db_path: str) -> dict: """Load database.json and return parsed dict.""" with open(db_path) as f: return json.load(f) def md5sum(filepath: str | Path) -> str: """Compute MD5 of a file - matches Batocera's md5sum().""" h = hashlib.md5() with open(filepath, "rb") as f: for chunk in iter(lambda: f.read(65536), b""): h.update(chunk) return h.hexdigest() def md5_composite(filepath: str | Path) -> str: """Compute composite MD5 of a ZIP - matches Recalbox's Zip::Md5Composite(). Sorts filenames alphabetically, reads each file's contents in order, feeds everything into a single MD5 hasher. The result is independent of ZIP compression level or metadata. """ with zipfile.ZipFile(filepath) as zf: names = sorted(n for n in zf.namelist() if not n.endswith("/")) h = hashlib.md5() for name in names: h.update(zf.read(name)) return h.hexdigest() def load_platform_config(platform_name: str, platforms_dir: str = "platforms") -> dict: """Load a platform config with inheritance and shared group resolution. This is the SINGLE implementation used by generate_pack, generate_readme, verify, and auto_fetch. No other copy should exist. """ if yaml is None: raise ImportError("PyYAML required: pip install pyyaml") config_file = os.path.join(platforms_dir, f"{platform_name}.yml") if not os.path.exists(config_file): raise FileNotFoundError(f"Platform config not found: {config_file}") with open(config_file) as f: config = yaml.safe_load(f) or {} # Resolve inheritance if "inherits" in config: parent = load_platform_config(config["inherits"], platforms_dir) merged = {**parent} merged.update({k: v for k, v in config.items() if k not in ("inherits", "overrides")}) if "overrides" in config and "systems" in config["overrides"]: merged.setdefault("systems", {}) for sys_id, override in config["overrides"]["systems"].items(): if sys_id in merged["systems"]: merged["systems"][sys_id] = {**merged["systems"][sys_id], **override} else: merged["systems"][sys_id] = override config = merged # Resolve shared group includes shared_path = os.path.join(platforms_dir, "_shared.yml") if os.path.exists(shared_path): with open(shared_path) as f: shared = yaml.safe_load(f) or {} shared_groups = shared.get("shared_groups", {}) for system in config.get("systems", {}).values(): for group_name in system.get("includes", []): if group_name in shared_groups: existing_names = {f.get("name") for f in system.get("files", [])} for gf in shared_groups[group_name]: if gf.get("name") not in existing_names: system.setdefault("files", []).append(gf) existing_names.add(gf.get("name")) return config def resolve_local_file( file_entry: dict, db: dict, zip_contents: dict | None = None, ) -> tuple[str | None, str]: """Resolve a BIOS file to its local path using database.json. Single source of truth for file resolution, used by both verify.py and generate_pack.py. Does NOT handle storage tiers (external/user_provided) or release assets - callers handle those. Returns (local_path, status) where status is one of: exact, zip_exact, hash_mismatch, not_found. """ sha1 = file_entry.get("sha1") md5_raw = file_entry.get("md5", "") name = file_entry.get("name", "") zipped_file = file_entry.get("zipped_file") md5_list = [m.strip().lower() for m in md5_raw.split(",") if m.strip()] if md5_raw else [] files_db = db.get("files", {}) by_md5 = db.get("indexes", {}).get("by_md5", {}) by_name = db.get("indexes", {}).get("by_name", {}) # 1. SHA1 exact match if sha1 and sha1 in files_db: path = files_db[sha1]["path"] if os.path.exists(path): return path, "exact" # 2. MD5 direct lookup (skip for zipped_file: md5 is inner ROM, not container) if md5_list and not zipped_file: for md5_candidate in md5_list: sha1_match = by_md5.get(md5_candidate) if sha1_match and sha1_match in files_db: path = files_db[sha1_match]["path"] if os.path.exists(path): return path, "exact" if len(md5_candidate) < 32: for db_md5, db_sha1 in by_md5.items(): if db_md5.startswith(md5_candidate) and db_sha1 in files_db: path = files_db[db_sha1]["path"] if os.path.exists(path): return path, "exact" # 3. zipped_file content match via pre-built index if zipped_file and md5_list and zip_contents: for md5_candidate in md5_list: if md5_candidate in zip_contents: zip_sha1 = zip_contents[md5_candidate] if zip_sha1 in files_db: path = files_db[zip_sha1]["path"] if os.path.exists(path): return path, "zip_exact" # 4. No MD5 = any file with that name (existence check) if not md5_list: candidates = [] for match_sha1 in by_name.get(name, []): if match_sha1 in files_db: path = files_db[match_sha1]["path"] if os.path.exists(path): candidates.append(path) if candidates: if zipped_file: candidates = [p for p in candidates if ".zip" in os.path.basename(p)] primary = [p for p in candidates if "/.variants/" not in p] if primary or candidates: return (primary[0] if primary else candidates[0]), "exact" # 5. Name fallback with md5_composite + direct MD5 per candidate md5_set = set(md5_list) candidates = [] for match_sha1 in by_name.get(name, []): if match_sha1 in files_db: entry = files_db[match_sha1] path = entry["path"] if os.path.exists(path): candidates.append((path, entry.get("md5", ""))) if candidates: if zipped_file: candidates = [(p, m) for p, m in candidates if ".zip" in os.path.basename(p)] if md5_set: for path, db_md5 in candidates: if ".zip" in os.path.basename(path): try: composite = md5_composite(path).lower() if composite in md5_set: return path, "exact" except (zipfile.BadZipFile, OSError): pass if db_md5.lower() in md5_set: return path, "exact" primary = [p for p, _ in candidates if "/.variants/" not in p] return (primary[0] if primary else candidates[0][0]), "hash_mismatch" return None, "not_found" def compute_coverage(platform_name: str, platforms_dir: str, db: dict) -> dict: """Compute BIOS coverage for a platform using verify logic.""" from verify import verify_platform config = load_platform_config(platform_name, platforms_dir) result = verify_platform(config, db) present = result["ok"] + result["untested"] pct = (present / result["total"] * 100) if result["total"] > 0 else 0 return { "platform": config.get("platform", platform_name), "total": result["total"], "verified": result["ok"], "untested": result["untested"], "missing": result["missing"], "present": present, "percentage": pct, "mode": config.get("verification_mode", "existence"), "details": result["details"], "config": config, } def safe_extract_zip(zip_path: str, dest_dir: str) -> None: """Extract a ZIP file safely, preventing zip-slip path traversal.""" dest = os.path.realpath(dest_dir) with zipfile.ZipFile(zip_path, "r") as zf: for member in zf.infolist(): member_path = os.path.realpath(os.path.join(dest, member.filename)) if not member_path.startswith(dest + os.sep) and member_path != dest: raise ValueError(f"Zip slip detected: {member.filename}") zf.extract(member, dest)