refactor: move profile_sync logic into profiling

This commit is contained in:
Abdessamad Derraz committed 2026-11-21 14:59:03 +01:00
1 parent 22a81c384d
commit 75a69a4a42
13 files changed
+366 -300

No files matched your search

+23
View File
@@ -0,0 +1,23 @@
"""The anonymous GitHub quota a profile pass has to fit in."""
from __future__ import annotations
import sys
from core.github import github_token
ANON_QUOTA = 60
def check_quota(count: int, offline: bool) -> None:
"""Refuse a run the anonymous quota cannot carry to completion."""
if offline or github_token():
return
if count * 5 > ANON_QUOTA:
print(
f"{count} profiles need roughly {count * 5} API calls, the anonymous "
f"quota is {ANON_QUOTA} per hour. Log in with `gh auth login`, set "
"GITHUB_TOKEN, or use --offline.",
file=sys.stderr,
)
raise SystemExit(1)
+39
View File
@@ -0,0 +1,39 @@
"""--realign-prose over a selection of profiles."""
from __future__ import annotations
import argparse
import sys
from pathlib import Path
from core import upstream
from profiling.realign import realign_prose
from profiling.run import WRITTEN_PROFILES
from profiling.sourcepaths import refresh_source_paths
from profiling.yamledit import YamlWriteError
def run_realign(args: argparse.Namespace, selected: dict) -> None:
"""--realign-prose over the selected profiles, exit 1 when one failed."""
failed: set[str] = set()
for name in selected:
profile_path = Path(args.emulators_dir) / f"{name}.yml"
if not profile_path.is_file():
continue
try:
before = profile_path.read_bytes()
for line in realign_prose(
profile_path, args.cache_dir, args.offline, args.dry_run
):
print(f"{name}: {line}")
if profile_path.read_bytes() != before:
WRITTEN_PROFILES.add(name)
except upstream.UpstreamError as exc:
print(f"{name}: {exc}", file=sys.stderr)
failed.add(name)
except YamlWriteError as exc:
print(f"{name}: write refused: {exc}", file=sys.stderr)
failed.add(name)
refresh_source_paths(args)
if failed:
raise SystemExit(1)
+31 -1
View File
@@ -25,7 +25,7 @@ from profiling.reports import (
)
from profiling.repos import select_repo
from profiling.resolver import build_report
from profiling.triage import print_triage
from profiling.triage import PROGRESS_SECONDS, Progress, print_interrupted, print_triage
from profiling.versions import (
newer_release,
latest_passed_release,
@@ -342,6 +342,36 @@ def read_one_profile(
failed.add(name)
def read_selected(
args: argparse.Namespace, selected: dict, writes: bool
) -> tuple[list[ProfileReport], set[str], bool]:
"""Every selected profile's pass: the reports, the profiles that failed,
and whether the pass ran to its end."""
# A profile that could not be compared or written is not a profile with
# nothing to review: it stays in every output and fails the run.
failed: set[str] = set()
reports: list[ProfileReport] = []
quiet = args.offline or len(selected) < 2
if not quiet:
print(
f"reading {len(selected)} profiles against their forges, a line every "
f"{PROGRESS_SECONDS} s. --offline answers from the cache alone.",
file=sys.stderr, flush=True,
)
progress = Progress(len(selected), "read", quiet)
complete = True
try:
for name, profile in selected.items():
progress.step(len(reports), name)
read_one_profile(args, name, profile, reports, failed, writes)
except KeyboardInterrupt:
# What was read is still worth printing: a ranking of 200 profiles
# beats none after 15 minutes.
print_interrupted(len(reports), len(selected), "the output covers those read")
complete = False
return reports, failed, complete
# Profiles this run wrote. Their new pins name trees the site's record of
# cited source paths does not hold yet.
WRITTEN_PROFILES: set[str] = set()
+46
View File
@@ -0,0 +1,46 @@
"""The site's record of cited source paths, rebuilt after a pass wrote profiles."""
from __future__ import annotations
import argparse
import os
import sys
from model.profiles import load_emulator_profiles
from profiling.run import WRITTEN_PROFILES
def refresh_source_paths(args: argparse.Namespace) -> None:
"""Rebuild the site's record of cited source paths after a write.
Each bump left website/src/data/source-paths.json behind its pins, and
the site's freshness test stayed red until someone ran sitesources.py
--fetch by hand. A run that writes nothing, or makes no request, leaves
it alone.
"""
if not WRITTEN_PROFILES or args.dry_run or args.offline or os.environ.get("RETROBIOS_OFFLINE"):
return
from core.artifacts import ArtifactLockBusy
from model.profiles import emulator_profiles_cache
from sitegen import sources
# The run loaded the profiles before writing them, and the loader caches
# by directory: read again, the record described the pins just replaced
# (linapple, then ten profiles in one advance).
emulator_profiles_cache.clear()
profiles = load_emulator_profiles(args.emulators_dir, skip_aliases=False)
try:
held, unread = sources.refresh_record(profiles, [sources.TREE_CACHE], fetch=True)
except ArtifactLockBusy as exc:
# The profiles are written: only the record waits for the other run.
print(
f"source paths not refreshed ({exc}): run python scripts/sitesources.py "
"--fetch once it finishes",
file=sys.stderr,
)
return
print(
f"source paths: {sum(map(len, held.values()))} in {len(held)} trees, "
f"{len(unread)} trees not cached: {sources.SOURCE_PATHS}",
file=sys.stderr,
)
+65
View File
@@ -0,0 +1,65 @@
"""--verify-version and --set-version over a selection: core_version against the code."""
from __future__ import annotations
import argparse
import json
import sys
from collections import Counter
from dataclasses import asdict
from pathlib import Path
from profiling import version_basis
from profiling.bump import write_version
from profiling.worktree import refuse_dirty_tree
from profiling.yamledit import YamlWriteError
def format_reading(name: str, reading: version_basis.Reading) -> str:
"""One reading as a line: its status, the version said, and where it was read."""
mode = f"[{reading.mode}] " if reading.mode else ""
kind = f"{reading.kind} " if reading.kind else ""
if reading.status == version_basis.DRIFT:
said = f"declared {reading.declared}, code {reading.read}"
elif reading.status == version_basis.MATCH:
said = str(reading.read)
else:
said = f"declared {reading.declared}"
return f"{name}: {mode}{reading.status} {said} ({kind}{reading.where})"
def run_versions(args: argparse.Namespace, profiles: dict, selected: dict) -> None:
"""--verify-version and --set-version over ``selected``: core_version against the code."""
if args.set_version and not args.dry_run and not args.force:
refuse_dirty_tree(args.emulators_dir, list(selected))
counts: Counter = Counter()
rows = []
failed = False
for name, profile in selected.items():
readings = version_basis.check(
profile, args.cache_dir, args.offline, profiles, recompute=args.set_version
)
for reading in readings:
counts[reading.status] += 1
rows.append({"profile": name, **asdict(reading)})
if not args.as_json and (not args.changed_only or reading.status != version_basis.MATCH):
print(format_reading(name, reading))
if args.set_version:
path = Path(args.emulators_dir) / f"{name}.yml"
if args.dry_run:
continue
try:
if write_version(path, readings):
print(f"{name}: core_version written")
except YamlWriteError as exc:
print(f"{name}: write refused: {exc}", file=sys.stderr)
failed = True
elif any(r.status in version_basis.FAILING for r in readings):
failed = True
if args.as_json:
print(json.dumps(rows, indent=2))
else:
print(", ".join(f"{count} {status}" for status, count in sorted(counts.items())),
file=sys.stderr)
if failed:
raise SystemExit(1)
+110
View File
@@ -0,0 +1,110 @@
"""The git state a profile pass reads: uncommitted profiles, the branch's profiles, the project's main line."""
from __future__ import annotations
import subprocess
import sys
from pathlib import Path
def refuse_dirty_tree(emulators_dir: str, names: list[str]) -> None:
"""Writes need a clean copy of each profile they touch to roll back to.
Only the selected profiles count: a pass writing one profile after
another used to refuse every profile after the first it wrote.
"""
try:
dirty = emulators_dir_is_dirty(emulators_dir, names)
except RuntimeError as e:
print(f"{e}. Pass --force to write anyway.", file=sys.stderr)
raise SystemExit(1) from e
if dirty:
# Another session may own the change, so the refusal names the files:
# --force writes over them.
one = len(dirty) == 1
print(
f"{', '.join(dirty)} {'has' if one else 'have'} uncommitted changes: "
f"commit {'it' if one else 'them'} or pass --force, which writes over "
f"{'it' if one else 'them'}.",
file=sys.stderr,
)
raise SystemExit(1)
def emulators_dir_is_dirty(emulators_dir: str, names: list[str] | None = None) -> list[str]:
"""The files of the profile directory that carry uncommitted changes.
Paths read as the status prints them, from the repository root.
``names`` narrows the question to those profiles. Git is asked from
inside the directory, so a profile tree that lives in another
repository answers for itself. A directory git cannot read is never
reported clean: the writes have nothing to roll back to.
"""
paths = [f"{name}.yml" for name in names] if names is not None else ["."]
result = subprocess.run(
["git", "-C", emulators_dir, "status", "--porcelain", "--", *paths],
capture_output=True,
text=True,
check=False,
)
if result.returncode:
raise RuntimeError(
f"cannot read the git status of {emulators_dir}: {result.stderr.strip()}"
)
return sorted(
line[3:].split(" -> ")[-1].strip('"')
for line in result.stdout.splitlines()
if line.strip()
)
def _git_lines(args: list[str]) -> list[str]:
from core.gitclone import GitRefused, required_git_lines
try:
return required_git_lines(*args)
except GitRefused as exc:
raise SystemExit(f"ERROR: {exc}") from exc
def branch_profiles(emulators_dir: str, base: str, added_only: bool) -> list[str]:
"""Names of the profiles the branch adds (or edits) against ``base``.
The diff starts at the merge base, so commits main gained since the
branch point are not counted, and ends at the working tree, so a
profile not yet committed is checked too. A rename reads as an addition.
"""
if subprocess.run(["git", "rev-parse", "--verify", "--quiet", base],
capture_output=True, check=False).returncode:
remote = base.split("/", 1)[0] if "/" in base else "upstream"
raise SystemExit(f"ERROR: {base} is not in this clone: run git fetch {remote} first")
merge_base = _git_lines(["merge-base", base, "HEAD"])[0]
paths = _git_lines([
"diff", "--name-only", "--no-renames",
"--diff-filter=A" if added_only else "--diff-filter=AM",
merge_base, "--", emulators_dir,
])
paths += _git_lines([
"ls-files", "--others", "--exclude-standard", "--full-name",
"--", emulators_dir,
])
top = Path(_git_lines(["rev-parse", "--show-toplevel"])[0])
root = Path(emulators_dir).resolve()
return sorted({
full.stem
for path in paths
if (full := (top / path).resolve()).suffix == ".yml" and full.parent == root
})
def project_base() -> str:
"""The project's main line, the base --changed compares with by default.
In a fork's clone origin/main is the fork: once the branch is pushed it
is HEAD, and --changed --strict found nothing and passed while the PR's
CI then failed. validate_pr.py resolves the same line the same way.
"""
from core.gitclone import compare_base
return compare_base() or "HEAD"