Files
libretro/scripts/model/registry.py
T
Abdessamad Derraz 036a6ebf29 refactor: reuse the shared helpers left copied
Copies of helpers the first pass created, replaced by the helper:
- source_yaml_file for our YAML sources (generate_db read them in the
  locale's encoding, a syntax error named no file);
- data_dir_registry_at in registry_bundles, platform_registry_at (new,
  beside load_platform_registry) in the manifest;
- DATA_DIRS_FILE for the registry path five modules spelled;
- load_optional_database, sha1_at, VERSIONS_FILE, installer_module;
- DerivedView where a cache was keyed by id() of a dict;
- count_text and series where a plural or a list was written by hand.
2026-11-21 10:31:29 +01:00

100 lines
3.8 KiB
Python

"""The platform registry and the data directory registry."""
from __future__ import annotations
import json
import os
from pathlib import Path
from core.paths import PLATFORMS_DIR
from core.cli import InputMissing
from model.sources import source_yaml_file
def data_dir_registry_at(registry_path: str | os.PathLike[str], required: bool = False) -> dict:
"""The `data_directories:` mapping of a data directory registry file.
An absent file is an empty registry, or with ``required`` an input the
command cannot run without.
"""
if not os.path.exists(registry_path):
if required:
raise InputMissing(f"Registry not found: {registry_path}")
return {}
return (source_yaml_file(registry_path) or {}).get("data_directories") or {}
def load_data_dir_registry(platforms_dir: str | Path = PLATFORMS_DIR, required: bool = False) -> dict:
"""Load the data directory registry from _data_dirs.yml."""
return data_dir_registry_at(os.path.join(platforms_dir, "_data_dirs.yml"), required)
def platform_registry_at(registry_path: str | os.PathLike[str], required: bool = False) -> dict:
"""The `platforms:` mapping of a platform registry file.
An absent file is an empty registry, or with ``required`` an input the
command cannot run without.
"""
if not os.path.exists(registry_path):
if required:
raise InputMissing(f"{registry_path} not found")
return {}
return (source_yaml_file(registry_path) or {}).get("platforms") or {}
def load_platform_registry(platforms_dir: str | Path = PLATFORMS_DIR, required: bool = False) -> dict:
"""Return the `platforms:` mapping from _registry.yml."""
return platform_registry_at(os.path.join(platforms_dir, "_registry.yml"), required)
def list_registered_platforms(platforms_dir: str = PLATFORMS_DIR) -> list[str]:
"""List platforms registered in _registry.yml.
Only registered platforms generate packs and appear in CI.
Unregistered YAMLs (e.g., emulatorjs.yml) are base configs for inheritance.
A missing registry, or a registered platform whose file is gone, raises:
left out quietly, verify --all --strict and every --all build passed
having checked none of it, and install.py kept serving its last manifest.
"""
registry_path = os.path.join(platforms_dir, "_registry.yml")
if not os.path.exists(registry_path):
raise InputMissing(f"no platform registry at {registry_path}")
registry = source_yaml_file(registry_path) or {}
platforms = []
for name, meta in sorted(registry.get("platforms", {}).items()):
config_path = os.path.join(platforms_dir, meta.get("config", f"{name}.yml"))
if not os.path.exists(config_path):
raise FileNotFoundError(
f"{name} is registered in {registry_path} but {config_path} does not exist"
)
platforms.append(name)
return platforms
def list_release_pack_platforms(platforms_dir: str = PLATFORMS_DIR) -> list[str]:
"""Registered platforms a release publishes a ZIP pack for.
`release_pack: true` in _registry.yml is the whole list: the build of
`--all`, the pack checks, the release record and every download table
read it. The other platforms keep their install manifest and are served
by the installer.
"""
registry = load_platform_registry(platforms_dir)
return [
name
for name in list_registered_platforms(platforms_dir)
if registry.get(name, {}).get("release_pack") is True
]
VERSIONS_FILE = "data/.versions.json"
def load_versions(versions_path: str | os.PathLike[str] = VERSIONS_FILE) -> dict[str, dict]:
"""What refresh_data_dirs recorded of each tree's last fetch."""
path = Path(versions_path)
if not path.is_file():
return {}
return json.loads(path.read_text(encoding="utf-8"))