Files
libretro/scripts/fileless_audit.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

170 lines
6.3 KiB
Python

#!/usr/bin/env python3
"""Find profiles that declare no files whose source now asks for a directory.
A profile with an empty `files:` list asserts that the emulator loads nothing
from disk. That assertion ages: a core that embedded everything can grow a
path, and nothing in the repository notices, because there is no file to go
missing and no ref to drift. virtualjaguar carried "No external BIOS files are
required or loaded by this core" while its source had grown eleven filenames
read from the system directory.
The signal is the request for a directory to read from. A libretro core asks
the frontend with RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY. Other shapes ask the
environment or build a path from a home directory. Finding one in a file the
profile itself cites does not prove a file is loaded, and this reports rather
than concludes: it names the profiles worth a reading, so the other hundred and
fifty need none.
"""
from __future__ import annotations
import argparse
import os
import sys
from pathlib import Path
sys.path.insert(0, os.path.dirname(__file__))
from core import upstream
from core.cli import add_repo_inputs, require_directory
from profiling.citations import collect_citations
from profiling.repos import select_views
from model.sourceref import ref_role, split_source_ref
from model.profiles import ProfileSelectionError, load_emulator_profiles, resolve_profile_name
from model.sources import source_yaml_file
# Ways a program asks for somewhere to read from.
SIGNALS = (
"RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY",
"GET_SYSTEM_DIRECTORY",
"system_directory",
"get_system_directory",
)
SOURCE_SUFFIX = (".c", ".cpp", ".cc", ".cxx", ".h", ".hpp", ".m", ".mm")
def cited_paths(document: dict) -> list[str]:
"""Source paths the profile points at, in document order, deduplicated."""
seen: list[str] = []
for citation in collect_citations(document):
for part in split_source_ref(citation.ref):
path = ref_role(part.path)[1]
if path.endswith(SOURCE_SUFFIX) and path not in seen:
seen.append(path)
return seen
def audit(
name: str, emulators_dir: str, cache_dir: str, offline: bool
) -> list[tuple[str, str]] | None:
"""Signals found in the sources a fileless profile cites."""
document = source_yaml_file(os.path.join(emulators_dir, f"{name}.yml")) or {}
if document.get("files"):
return None
if document.get("type") == "alias":
# The same binary under another name: its files are its target's.
return None
if str(document.get("exclusion_note") or "").strip():
# Someone read this one and wrote down why nothing is declared. The
# check exists to find the profiles nobody has answered yet, and an
# answer that goes stale shows up as a drifting ref instead.
return None
if document.get("data_directories"):
# The load is declared, as a directory rather than a file. dinothawr
# reads system_dir/dinothawr/ and says so there. That is coverage, not
# an assertion waiting to age.
return None
views = select_views(document, cache_dir, offline)
if not views:
return []
hits: list[tuple[str, str]] = []
for path in cited_paths(document)[:40]:
for view in views:
lines = upstream.fetch_file(view.repo, view.pin, path, cache_dir, offline)
if lines is None:
continue
for signal in SIGNALS:
if any(signal in line for line in lines):
hits.append((path, signal))
break
break
return hits
def _settled_before_reading(document: dict) -> bool:
"""A profile answered without its sources: files, an exclusion note, or
a declared data directory (see audit)."""
return bool(
document.get("files")
or document.get("type") == "alias"
or str(document.get("exclusion_note") or "").strip()
or document.get("data_directories")
)
def main() -> None:
parser = argparse.ArgumentParser(allow_abbrev=False, description=__doc__.splitlines()[0])
parser.add_argument("emulators", nargs="*", help="profiles to read")
parser.add_argument(
"--all", action="store_true",
help="every profile that declares no files and has no answer yet",
)
add_repo_inputs(parser, "emulators")
parser.add_argument(
"--cache-dir", default=".cache",
help="download cache directory (default: %(default)s)",
)
parser.add_argument(
"--offline", action="store_true",
help="read only what the cache holds, no network access",
)
args = parser.parse_args()
if args.all == bool(args.emulators):
parser.error("name profiles or pass --all, not both")
require_directory(args.emulators_dir, "--emulators-dir")
profiles = load_emulator_profiles(args.emulators_dir, skip_aliases=False) if args.emulators else {}
try:
names = [
resolve_profile_name(name, profiles, "python scripts/verify.py --list-emulators")
for name in args.emulators
]
except ProfileSelectionError as exc:
raise SystemExit(f"ERROR: {exc}") from None
if args.all:
known = sorted(path.stem for path in Path(args.emulators_dir).glob("*.yml"))
names = []
for name in known:
if not _settled_before_reading(
source_yaml_file(Path(args.emulators_dir) / f"{name}.yml") or {}
):
names.append(name)
flagged = unreachable = 0
for name in names:
try:
hits = audit(name, args.emulators_dir, args.cache_dir, args.offline)
except upstream.UpstreamError as exc:
print(f"{name}: unreachable, {exc}", file=sys.stderr)
unreachable += 1
continue
if hits is None or not hits:
continue
flagged += 1
print(f"{name}: declares no files, yet its source asks for a directory")
for path, signal in hits:
print(f" {path}: {signal}")
print(
f"{len(names)} profile(s) read, {flagged} to read again"
+ (f", {unreachable} unreachable" if unreachable else ""),
file=sys.stderr,
)
# An unreachable source is not a clean one.
raise SystemExit(1 if flagged or unreachable else 0)
if __name__ == "__main__":
from core.cli import run_cli
run_cli(main)