From bce9abedc7a80798239542ea729ab97b13b34f9a Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Sat, 21 Nov 2026 16:16:58 +0100 Subject: [PATCH] fix: read profile repositories from one model rule The site permalinks read only source and upstream, so a profile whose forge is gone (yuzu answers 451) linked nothing although profile_sync judged it through its source_mirror. declared_repositories, pin_field and the declared pin now live in model.profilerepos, read by both. --- scripts/model/profilerepos.py | 51 ++++++++++++++++++++++++++++++ scripts/profiling/citations.py | 2 +- scripts/profiling/realign.py | 2 +- scripts/profiling/rebase.py | 2 +- scripts/profiling/repos.py | 37 ++-------------------- scripts/profiling/resolver.py | 2 +- scripts/profiling/version_basis.py | 3 +- scripts/sitegen/sources.py | 50 ++++++++++++----------------- tests/test_profile_repositories.py | 2 +- tests/test_site_exports.py | 11 +++++++ tests/test_version_basis.py | 2 +- 11 files changed, 94 insertions(+), 70 deletions(-) create mode 100644 scripts/model/profilerepos.py diff --git a/scripts/model/profilerepos.py b/scripts/model/profilerepos.py new file mode 100644 index 000000000..b5a4c6ac4 --- /dev/null +++ b/scripts/model/profilerepos.py @@ -0,0 +1,51 @@ +"""The repositories a profile declares and the field that pins each one.""" + +from __future__ import annotations + +# Read in this order: a declared repository decides attribution before the +# mirror that only keeps a gone forge's history reachable. +REPOSITORY_FIELDS = ("source", "upstream", "source_mirror") + + +def declared_repositories(profile: dict) -> list[tuple[str, str, str]]: + """Every repository URL the profile declares, source before upstream, + as (field, build mode, url). + + `source` and `upstream` are usually plain strings. `ymir` keys them by + build mode instead, because its standalone and libretro builds live in + different repositories. + + `source_mirror` comes last and is the only thing that keeps a profile + checkable once its forge is gone: yuzu and suyu answer 451, citron's + host stopped resolving. Being last, it never decides attribution while + a declared repository still answers. + """ + urls: list[tuple[str, str, str]] = [] + for field_name in REPOSITORY_FIELDS: + value = profile.get(field_name) + if isinstance(value, dict): + urls.extend((field_name, str(k), str(v)) for k, v in value.items() if v) + elif value: + urls.append((field_name, "", str(value))) + return urls + + +def pin_field(field: str) -> str: + """The `_commit` that pins a repository field. A mirror carries + the source's history, commits and branches alike: source_commit pins it.""" + return "source" if field == "source_mirror" else field + + +def declared_pin(profile: dict, field: str, mode: str = "") -> tuple[str, str]: + """The commit `_commit` declares for a build mode, and the field + it was read from; ('', origin) when the profile declares none. + + A profile whose builds live in different repositories keys its pin by + build mode, the way `ymir` keys `source` and `core_version`. Without a + declared pin, the revision is the last commit by `profiled_date`, which + only a forge (or its cache) can answer. + """ + pinned = profile.get(f"{field}_commit") + if isinstance(pinned, dict): + return str(pinned.get(mode) or ""), f"{field}_commit[{mode}]" + return str(pinned or ""), f"{field}_commit" diff --git a/scripts/profiling/citations.py b/scripts/profiling/citations.py index bfaba0627..244b8c1df 100644 --- a/scripts/profiling/citations.py +++ b/scripts/profiling/citations.py @@ -16,7 +16,7 @@ import yaml from core import upstream from core.hashing import parse_hash_list from model.sourceref import RefPart, prose_runs, ref_prose_text, ref_role, source_ref_values -from profiling.repos import declared_repositories +from model.profilerepos import declared_repositories HASH_FIELDS = ("sha1", "md5", "crc32", "sha256", "known_hash_adler32") diff --git a/scripts/profiling/realign.py b/scripts/profiling/realign.py index 91deba0bf..038b60e7d 100644 --- a/scripts/profiling/realign.py +++ b/scripts/profiling/realign.py @@ -16,7 +16,7 @@ from model.sourceref import RefPart from profiling.anchoring import anchor_block from profiling.citations import Citation, citation_key, collect_citations, is_external_citation from profiling.proseedit import apply_prose_edits, run_after_moves -from profiling.repos import declared_repositories +from model.profilerepos import declared_repositories from profiling.yamledit import apply_edit diff --git a/scripts/profiling/rebase.py b/scripts/profiling/rebase.py index 2fa3d3afa..d1dd0efdc 100644 --- a/scripts/profiling/rebase.py +++ b/scripts/profiling/rebase.py @@ -13,7 +13,7 @@ from profiling.anchoring import PartResult, REBASE_STATUSES, REVIEW_STATUSES from profiling.citations import Citation, citation_key, collect_citations from profiling.proseedit import apply_prose_edits, run_after_moves, set_scalar from profiling.reports import EntryReport, ProfileReport, original_part, rendered_part -from profiling.repos import declared_repositories +from model.profilerepos import declared_repositories from profiling.yamledit import YamlWriteError, apply_edit, find_field_line, replace_field_line diff --git a/scripts/profiling/repos.py b/scripts/profiling/repos.py index b2f728382..3eb8ef9cc 100644 --- a/scripts/profiling/repos.py +++ b/scripts/profiling/repos.py @@ -7,6 +7,7 @@ from collections.abc import Iterable from dataclasses import dataclass, replace from core import upstream +from model.profilerepos import declared_pin, declared_repositories, pin_field PIN = "pin" @@ -44,12 +45,6 @@ def select_repo(profile: dict) -> upstream.Repo | None: return None -def pin_field(field: str) -> str: - """The `_commit` that pins a repository field. A mirror carries - the source's history, commits and branches alike: source_commit pins it.""" - return "source" if field == "source_mirror" else field - - def resolve_pin( profile: dict, repo: upstream.Repo, cache_dir: str, offline: bool, field: str = "source", mode: str = "", branch: str | None = None, @@ -61,14 +56,9 @@ def resolve_pin( A profile whose builds live in different repositories keys its pin by build mode, the way `ymir` keys `source` and `core_version`. """ - pinned = profile.get(f"{field}_commit") - if isinstance(pinned, dict): - pinned = pinned.get(mode) - origin = f"{field}_commit[{mode}]" - else: - origin = f"{field}_commit" + pinned, origin = declared_pin(profile, field, mode) if pinned: - return str(pinned), origin + return pinned, origin date = str(profile.get("profiled_date") or "") if not date: return None, None @@ -231,27 +221,6 @@ def _follow_fork_base( return followed -def declared_repositories(profile: dict) -> list[tuple[str, str, str]]: - """Every repository URL the profile declares, source before upstream. - - `source` and `upstream` are usually plain strings. `ymir` keys them by - build mode instead, because its standalone and libretro builds live in - different repositories. - - `source_mirror` comes last and is the only thing that keeps a profile - checkable once its forge is gone: yuzu and suyu answer 451, citron's - host stopped resolving. Being last, it never decides attribution while - a declared repository still answers. - """ - urls: list[tuple[str, str, str]] = [] - for field_name in ("source", "upstream", "source_mirror"): - value = profile.get(field_name) - if isinstance(value, dict): - urls.extend((field_name, str(k), str(v)) for k, v in value.items() if v) - elif value: - urls.append((field_name, "", str(value))) - return urls - def branch_in_url(url: str) -> str | None: """Branch named by a forge URL pointing inside a repository. diff --git a/scripts/profiling/resolver.py b/scripts/profiling/resolver.py index 67ea1905f..1bcc0e948 100644 --- a/scripts/profiling/resolver.py +++ b/scripts/profiling/resolver.py @@ -39,12 +39,12 @@ from profiling.citations import ( symlink_target, ) from profiling.reports import EntryReport, ProfileReport +from model.profilerepos import declared_repositories from profiling.repos import ( PIN, RepoView, reads_own_revision, build_revision, - declared_repositories, detect_pinned_tag, moved_repositories, resolve_through_gitlinks, diff --git a/scripts/profiling/version_basis.py b/scripts/profiling/version_basis.py index b02143e3b..904d665ba 100644 --- a/scripts/profiling/version_basis.py +++ b/scripts/profiling/version_basis.py @@ -33,7 +33,8 @@ from collections.abc import Callable from dataclasses import dataclass from core import upstream -from profiling.repos import pin_field, resolve_pin +from model.profilerepos import pin_field +from profiling.repos import resolve_pin KINDS = ("file", "tag", "alias", "package", "none", "unverified") # Values copied from a .info or a branch name, which name no version. diff --git a/scripts/sitegen/sources.py b/scripts/sitegen/sources.py index 9662a45d0..28037bf12 100644 --- a/scripts/sitegen/sources.py +++ b/scripts/sitegen/sources.py @@ -24,8 +24,14 @@ from model.sourceref import ( source_ref_values, split_source_ref, ) +from model.profilerepos import ( + REPOSITORY_FIELDS, + declared_pin, + declared_repositories, + pin_field, +) from model.sourcerevision import parse_source -from sitegen.render import by_mode, escape_tags +from sitegen.render import escape_tags def _repo_pins(profile: dict, field: str, label: str) -> list[tuple[str, str, str]]: @@ -35,32 +41,18 @@ def _repo_pins(profile: dict, field: str, label: str) -> list[tuple[str, str, st and the revision by build mode. Reading them apart would pin a libretro fork to the standalone revision and produce a permalink into a tree that never held the cited line. Each tuple is (url, pin, source_commit - fallback for that same mode). + fallback for that same mode). A mirror is pinned by source_commit. """ - raw = profile.get(field, "") - pin_field = profile.get(f"{field}_commit") or "" - source_field = profile.get("source_commit") or "" - - if not isinstance(raw, dict): - if not raw: - return [] - return [ - ( - str(raw), - by_mode(pin_field, label), - by_mode(source_field, label), - ) - ] - + declared = [(mode, url) for name, mode, url in declared_repositories(profile) + if name == field] # The requested build mode first, so it wins when several repositories # would otherwise be equally good candidates. - ordered = ([label] if label and label in raw else []) + [ - key for key in raw if key != label - ] + declared.sort(key=lambda pair: bool(pair[0]) and pair[0] != label) + pinned_by = pin_field(field) return [ - (str(raw[key]), by_mode(pin_field, key), by_mode(source_field, key)) - for key in ordered - if raw.get(key) + (url, declared_pin(profile, pinned_by, mode or label)[0], + declared_pin(profile, "source", mode or label)[0]) + for mode, url in declared ] # Forges the pages link into but upstream.py does not read from. Each is @@ -110,7 +102,7 @@ def _forge_sources(profile: dict, label: str = "") -> list[tuple[upstream.Repo, seen: set[tuple[str, str, str]] = set() source_repos: set[tuple[str, str]] = set() - for field in ("source", "upstream"): + for field in REPOSITORY_FIELDS: for value, pin, fallback in _repo_pins(profile, field, label): repo = browse_repo(value) if repo is None: @@ -124,7 +116,7 @@ def _forge_sources(profile: dict, label: str = "") -> list[tuple[upstream.Repo, if key not in seen: candidates.append((repo, pin)) seen.add(key) - if field == "source": + if pin_field(field) == "source": source_repos.add(repo_key) return candidates @@ -269,7 +261,7 @@ def profile_trees(profile: dict) -> list[tuple[upstream.Repo, str]]: """Every repository and revision a page of the profile can link into.""" found: list[tuple[upstream.Repo, str]] = [] labels = {""} | { - str(mode) for field in ("source", "upstream", "source_commit", "upstream_commit") + str(mode) for field in (*REPOSITORY_FIELDS, "source_commit", "upstream_commit") if isinstance(profile.get(field), dict) for mode in profile[field]} for label in sorted(labels): for pair in _forge_sources(profile, label) + list(role_sources(profile, label).values()): @@ -463,11 +455,11 @@ def role_label(profile: dict, role: str, path: str = "") -> str: def role_sources(profile: dict, label: str = "") -> dict[str, tuple[upstream.Repo, str]]: """The forge repository and pin behind each role a citation can name.""" found: dict[str, tuple[upstream.Repo, str]] = {} - for field in ("source", "upstream"): + for field in REPOSITORY_FIELDS: for value, pin, _fallback in _repo_pins(profile, field, label): repo = browse_repo(value) - if repo is not None and pin and field not in found: - found[field] = (repo, pin) + if repo is not None and pin: + found.setdefault(pin_field(field), (repo, pin)) return found diff --git a/tests/test_profile_repositories.py b/tests/test_profile_repositories.py index ef476313a..d2e8fb7a1 100644 --- a/tests/test_profile_repositories.py +++ b/tests/test_profile_repositories.py @@ -19,7 +19,7 @@ from pathlib import Path import yaml from core import upstream -from profiling.repos import declared_repositories +from model.profilerepos import declared_repositories REPO_ROOT = Path(__file__).resolve().parent.parent diff --git a/tests/test_site_exports.py b/tests/test_site_exports.py index 31ff8c7b1..3e0d984ea 100644 --- a/tests/test_site_exports.py +++ b/tests/test_site_exports.py @@ -287,6 +287,17 @@ class PerModeSourcePins(unittest.TestCase): [("vendor/emu", "c" * 40)], ) + def test_a_mirror_is_read_at_the_source_commit_as_profile_sync_reads_it(self): + profile = { + "source": "https://github.com/gone/emu", + "source_mirror": "https://github.com/keeper/emu", + "source_commit": "d" * 40, + } + self.assertEqual( + [(repo.slug, pin) for repo, pin in _forge_sources(profile)], + [("gone/emu", "d" * 40), ("keeper/emu", "d" * 40)], + ) + def test_sqlite_export_accepts_a_per_mode_commit(self): from core.artifacts import json_text as _json_text diff --git a/tests/test_version_basis.py b/tests/test_version_basis.py index f00b3471e..55281d213 100644 --- a/tests/test_version_basis.py +++ b/tests/test_version_basis.py @@ -28,7 +28,7 @@ from profiling import resolver from profiling import yamledit from core import upstream from profiling import version_basis -from profiling.repos import pin_field +from model.profilerepos import pin_field from model.profiles import load_emulator_profiles from profiling.reports import ProfileReport from profiling.bump import bump_commit