diff --git a/scripts/check_freshness.py b/scripts/check_freshness.py index 4bfe22c48..40ad9aaae 100644 --- a/scripts/check_freshness.py +++ b/scripts/check_freshness.py @@ -1736,7 +1736,7 @@ def summarize_profile_sync(report: object) -> list[Finding]: # no resolvable pin) is unknown. One that is the profile's nature # (an upstream declared gone, a host no forge API serves, no # source_ref) is never checkable and does not hold the exit code. - from profile_sync import ProfileReport + from profiling.reports import ProfileReport reason = str(row["skipped"]) status = UNKNOWN if reason.startswith(ProfileReport.UNCHECKABLE) else SKIPPED diff --git a/scripts/fileless_audit.py b/scripts/fileless_audit.py index ae340ea39..61aba81b8 100644 --- a/scripts/fileless_audit.py +++ b/scripts/fileless_audit.py @@ -26,7 +26,8 @@ sys.path.insert(0, os.path.dirname(__file__)) from core import upstream from core.cli import require_directory, suggest_names -from profile_sync import collect_citations, select_views +from profiling.citations import collect_citations +from profiling.repos import select_views from model.sourceref import ref_role, split_source_ref from model.safeparse import yaml_load diff --git a/scripts/profile_sync.py b/scripts/profile_sync.py index a4a9b3303..77ffca307 100644 --- a/scripts/profile_sync.py +++ b/scripts/profile_sync.py @@ -9,26 +9,14 @@ recalable from what needs the code read again. from __future__ import annotations import argparse -import bisect -import difflib -import functools -import itertools import json import os -import posixpath -import re import subprocess import sys -import tempfile -import time from collections import Counter -from collections.abc import Callable, Iterable, Iterator, Sequence -from dataclasses import asdict, dataclass, field, replace -from datetime import date +from dataclasses import asdict from pathlib import Path -from typing import Any -import yaml sys.path.insert(0, os.path.dirname(__file__)) @@ -36,4566 +24,16 @@ from core import upstream from profiling import version_basis from model.systems import _norm_system_id, system_index from core.github import github_token -from model.registry import list_registered_platforms from model.profiles import load_emulator_profiles -from model.platforms import load_platform_config -from resolve.cores import resolve_platform_cores from core.cli import suggest_names -from core.hashing import parse_hash_list -from model.sourceref import ( - _ANNOTATION_RE, - RefPart, - _prose_runs, - _trim_prose, - is_binary_citation, - ref_prose_text, - ref_role, - resolve_project_word, - source_ref_values, - split_outside_parentheses, - split_source_ref, -) -from core.upstream import CompareResult, find_renamed +from profiling.bump import DEFAULT_CACHE, write_version +from profiling.realign import realign_prose +from profiling.reports import ProfileReport +from profiling.run import _WRITTEN, _emit, _read_one +from profiling.triage import PROGRESS_SECONDS, _Progress, _interrupted +from profiling.yamledit import YamlWriteError -DEFAULT_CACHE = upstream.CACHE_DIR ANON_QUOTA = 60 -TRIAGE_PATH_SAMPLE = 5 -# A pass that asks the forges says where it is at this interval: an online -# --all --triage ran 15 minutes without a word and read as hung. -PROGRESS_SECONDS = 30 - -STATUS_ORDER = ( - "ANCHORED", "UNCHECKED", "EXTERNAL", "BINARY", "SHIFTED", "RENAMED", "MOVED", - "AMBIGUOUS", "CHANGED", "GONE", -) -REVIEW_STATUSES = ("CHANGED", "GONE", "AMBIGUOUS") -REBASE_STATUSES = ("SHIFTED", "RENAMED", "MOVED") - -WIDEN_STEPS = (0, 3, 6, 12, 25, 50) -# A 30k-line driver diffs in about five seconds, and the biggest files the -# corpus cites sit just above that. The cap stays as a guard against -# pathological inputs, not as a limit on real source files. -# The fallback mapping is a full sequence diff, and it only runs once exact -# anchoring has failed, which is rare. openbor.c is the largest file any -# profile cites, 55k lines against 57k, and diffing the pair takes nine -# seconds. The ceiling sits above it so that one resolves, and still stops a -# pathological pair from stalling a sweep of every profile. -MAX_MATCH_LINES = 60000 - -PIN = "pin" -HEAD = "head" - -HEADER_SUFFIXES = (".h", ".hpp", ".hh", ".hxx", ".inc", ".inl") - - -@dataclass(frozen=True) -class AnchorResult: - status: str - start: int | None - end: int | None - candidates: list[int] - reason: str | None = None - - -@dataclass(frozen=True) -class PartResult: - part: RefPart - status: str - new_path: str | None - start: int | None - end: int | None - candidates: list[int] - reason: str | None = None - repo: str | None = None - head_url: str | None = None - - -@dataclass(frozen=True) -class RepoView: - """One repository a profile cites, at the two revisions under comparison.""" - - repo: upstream.Repo - pin: str - head: str - origin: str - field: str - # The build mode a keyed source names this repository under (ymir, - # xenia-edge): `standalone` or `libretro`, empty for a plain URL. - mode: str = "" - - -HASH_FIELDS = ("sha1", "md5", "crc32", "sha256", "known_hash_adler32") - - -def entry_hashes(entry: dict) -> list[str]: - """Content values an entry declares, its own and its members'. - - An archive entry carries no hash of its own: MAME and FBNeo hash the ROMs - inside the container, so `contents[]` is where its content values live. - Ignoring them leaves the archive stem as the only token, and a stem like - `esh` matches seventy lines of its own driver. - """ - tokens: list[str] = [] - contents = entry.get("contents") - members = contents if isinstance(contents, list) else [] - for source in (entry, *members): - if not isinstance(source, dict): - continue - for field_name in HASH_FIELDS: - for v in parse_hash_list(source.get(field_name)): - if v not in tokens: - tokens.append(v) - return tokens - - -def collect_tokens(entry: dict) -> list[str]: - """Values declared by one file entry: hashes, then name as fallback.""" - tokens = entry_hashes(entry) - if not tokens: - name = entry.get("name", "") - if name: - tokens.append(os.path.basename(name).lower()) - return tokens - - -_VERSION_NOTE_RE = re.compile(r"\(\s*v?(\d+(?:\.\d+)+)\s*\)") - - -def cites_another_version(part: RefPart, core_version: object) -> str | None: - """The version a part's annotation names, when it is not the documented one. - - A profile may cite two programs side by side, `main.cpp:407-411 (v0.0.11)` - beside its v0.16.0 pin. The annotation was dropped as prose and the part - compared with the pin, where it fell on unrelated code yet read ANCHORED. - """ - match = _VERSION_NOTE_RE.search(part.raw or "") - if not match: - return None - declared = core_version.values() if isinstance(core_version, dict) else [core_version] - documented = {str(v or "").strip().lstrip("vV") for v in declared} - return None if match.group(1) in documented else match.group(1) - - -def is_external_citation(path: str) -> bool: - """True when a ref names a project rather than a path in a known repo. - - `munt ROMInfo.cpp` and `Nuked-SC55-CLAP rom_io.cpp` cite where a ROM is - identified inside a dependency the profile does not declare, so no - revision of the declared repositories can confirm or deny them. Reporting - them as broken would be wrong: they are simply out of reach. - """ - if " " not in path: - return False - head = path.split(" ", 1)[0] - return "/" not in head and "." not in head - - -_VERSION_WORD_RE = re.compile(r"v?\d+(\.\d+)+[\w.-]*") - - -def strip_repo_word( - path: str, known: frozenset[str] | set[str] = frozenset() -) -> str: - """Drop a leading project word that names a declared repository. - - `BAM_FPloader FPLoader.cpp:45` follows the `munt ROMInfo.cpp` convention, - but BAM_FPloader is the profile's own source: the path is FPLoader.cpp, - to be found in that tree, not an out-of-reach citation. - """ - if " " not in path: - return path - role, rest = ref_role(path) - if role: - return rest - head, _, tail = path.partition(" ") - if head.lower() in known and tail and "/" not in head and "." not in head: - tail = tail.strip() - # `ArcadeFlashWeb v1.0.2 flash/flash.html` names the release the file - # was read from. The version is not part of the path either. - version, _, rest = tail.partition(" ") - if rest and _VERSION_WORD_RE.fullmatch(version): - return rest.strip() - return tail - return path - - -def narrow_by_cited(matches: list[str], cited_dirs: Iterable[str]) -> list[str]: - """Keep the candidates under the deepest directory the profile cites. - - A bare `version.h` matches BasiliskII/src/include/version.h and - SheepShaver/src/include/version.h in cebix/macemu. The profile's own - source_refs all live under SheepShaver/src, which is the tree the prose - meant. Candidates under no cited directory are kept only when none is. - """ - best = -1 - kept: list[str] = [] - for match in matches: - depth = max( - (d.count("/") + 1 for d in cited_dirs if match.startswith(d + "/")), - default=-1, - ) - if depth > best: - best, kept = depth, [match] - elif depth == best: - kept.append(match) - return kept if best >= 0 else matches - - -def symlink_target(path: str, lines: list[str] | None) -> str | None: - """The file a one-line blob points at, when the blob is a symbolic link. - - Git stores a symlink as a blob holding its target, and the raw endpoints - serve that text: SheepShaver/src/Unix/ether_unix.cpp is the single line - ../../../BasiliskII/src/Unix/ether_unix.cpp. Anchoring a line number in - it would report every citation as beyond the end of the file. - """ - if not lines or len(lines) != 1: - return None - target = lines[0].strip() - if not target or " " in target or "/" not in target: - return None - if posixpath.basename(target) != posixpath.basename(path): - return None - resolved = posixpath.normpath(posixpath.join(posixpath.dirname(path), target)) - return None if resolved.startswith("..") or resolved == path else resolved - - -def _anchor_tokens(entry: dict) -> list[str]: - """Values worth searching for when a cited line misses its subject. - - Both the content values and the set name are searched, because profiles - cite two different shapes. FBNeo and Hypseus refs point at the ROM table, - where the CRC32s are, while MAME refs point at the machine declaration, which - carries the set name and no hash at all. - """ - tokens = collect_tokens(entry) - name = os.path.basename(str(entry.get("name", ""))).lower() - stem = name.rsplit(".", 1)[0] - if stem and stem not in tokens and len(stem) > 2: - tokens.append(stem) - return tokens - - -@dataclass(frozen=True) -class Citation: - """One citation carried by a profile, wherever it is written. - - A structured citation lives under a source_ref key and keeps its entry - for the values it declares. A prose citation is a path:line run found - inside any other string scalar. Its spans locate the path and every - range token inside the scalar line, so a recale can move the numbers - and leave the sentence around them alone. - """ - - field: str - kind: str - ref: str - entry: dict | None = None - holder: object = None - key: object = None - label: str = "" - line: int = 0 - path_span: tuple[int, int] = (0, 0) - spans: tuple = () - parts: tuple = () - # Broken across two lines of a literal block. joined_at is where the - # second line starts in the joined run line, so each span goes back to - # the line that holds it. - wrapped: bool = False - joined_at: int = 0 - # Written in a YAML comment, which the loaded document drops. line is - # then the physical line of the file and the spans index it. - in_comment: bool = False - - -def _list_segments(items: list) -> list[object]: - """The field path segment naming each list item. - - A name keys its entry. supermodel declares Games.xml twice, once per - build mode, and both notes answered to files[Games.xml].note: the - history read one note's citations against the other's text. A repeated - name takes its build mode, or its index when the mode repeats too. - """ - keys: list[tuple[str | None, str | None]] = [] - for item in items: - name = item.get("name") if isinstance(item, dict) else None - mode = item.get("mode") if isinstance(item, dict) else None - keys.append(( - None if name is None else str(name), - mode if isinstance(mode, str) else None, - )) - names = Counter(name for name, _ in keys if name is not None) - modes = Counter(key for key in keys if key[0] is not None) - segments: list[object] = [] - for index, (name, mode) in enumerate(keys): - if name is None: - segments.append(index) - elif names[name] == 1: - segments.append(name) - elif mode and modes[(name, mode)] == 1: - segments.append(f"{name}@{mode}") - else: - segments.append(f"{name}#{index}") - return segments - - -def _walk_document( - node: object, where: str = "" -) -> Iterator[tuple[str, str, dict | list, object, Any]]: - """Every citation carrier of the document, with its holder and field path. - - Yields ("ref", field, holder, key, value) for each source_ref key and - ("str", field, holder, key, value) for every other string scalar. One - walker builds every field path, so the paths that name a scalar today - still name it when another pass looks it up at another revision. - """ - if isinstance(node, dict): - for key, value in node.items(): - spot = f"{where}.{key}" if where else str(key) - if key == "source_ref": - yield "ref", spot, node, key, value - elif isinstance(value, str): - yield "str", spot, node, key, value - else: - yield from _walk_document(value, spot) - elif isinstance(node, list): - segments = _list_segments(node) - for index, item in enumerate(node): - spot = f"{where}[{segments[index]}]" - if isinstance(item, str): - yield "str", spot, node, index, item - else: - yield from _walk_document(item, spot) - - -def _scalar_spans(node: yaml.Node, spans: list[tuple[int, int]]) -> None: - if isinstance(node, yaml.ScalarNode): - spans.append((node.start_mark.index, node.end_mark.index)) - elif isinstance(node, yaml.MappingNode): - for key, value in node.value: - _scalar_spans(key, spans) - _scalar_spans(value, spans) - elif isinstance(node, yaml.SequenceNode): - for item in node.value: - _scalar_spans(item, spans) - - -def comment_citations( - text: str, known: frozenset[str] | set[str] = frozenset() -) -> list[Citation]: - """Citation runs written in the YAML comments of a profile. - - The loader drops comments, so a pin moved without them: zesarux cites - src/start.c:4870-4874 in a comment written under a revision where those - lines now hold the --chroma81 branch. A `#` inside a scalar, quoted or in - a block, is content and not a comment. - """ - try: - root = yaml.compose(text, Loader=yaml.SafeLoader) - except yaml.YAMLError: - return [] - spans: list[tuple[int, int]] = [] - if root is not None: - _scalar_spans(root, spans) - spans.sort() - starts = [lo for lo, _ in spans] - - def inside_scalar(index: int) -> bool: - at = bisect.bisect_right(starts, index) - 1 - return at >= 0 and spans[at][0] <= index < spans[at][1] - - citations: list[Citation] = [] - offset = 0 - for number, line in enumerate(text.splitlines(keepends=True)): - for column, char in enumerate(line): - if char != "#" or (column and not line[column - 1].isspace()): - continue - if inside_scalar(offset + column): - continue - comment = line[column + 1:].rstrip("\r\n") - shift = column + 1 - for _, path_span, run_spans, parts, run, _ in _prose_runs(comment, known): - citations.append(Citation( - field=f"comment line {number + 1}", kind="prose", ref=run, - line=number, - path_span=(path_span[0] + shift, path_span[1] + shift), - spans=tuple((lo + shift, hi + shift) for lo, hi in run_spans), - parts=parts, in_comment=True, - )) - break - offset += len(line) - return citations - - -def collect_citations(document: dict, text: str | None = None) -> list[Citation]: - """Every citation the document carries, in document order. - - A source_ref key anywhere in the document is a structured citation: - files[] holds most of them, data_directories[] carries some too. Every - other string scalar is scanned for prose runs. A citation is a property - of the text, not of a field name: anything this walk misses is a - location profile_sync cannot keep honest. - """ - known = frozenset( - repo.name.lower() - for _, _, url in declared_repositories(document) - if (repo := upstream.parse_repo(url)) is not None - ) - citations: list[Citation] = [] - for kind, field_name, holder, key, value in _walk_document(document): - if kind == "ref": - for label, ref in source_ref_values(value): - citations.append(Citation( - field=field_name, kind="ref", ref=ref, - entry=holder if isinstance(holder, dict) else None, - holder=holder, key=key, label=label, - )) - if isinstance(value, str): - for line, path_span, spans, parts, run, joined_at in _prose_runs( - ref_prose_text(value), known - ): - citations.append(Citation( - field=field_name, kind="prose", ref=run, holder=holder, - key=key, line=line, path_span=path_span, spans=spans, - parts=parts, wrapped=bool(joined_at), joined_at=joined_at, - )) - continue - for line, path_span, spans, parts, run, joined_at in _prose_runs( - value, known - ): - citations.append(Citation( - field=field_name, kind="prose", ref=run, holder=holder, key=key, - line=line, path_span=path_span, spans=spans, parts=parts, - wrapped=bool(joined_at), joined_at=joined_at, - )) - if text is not None: - citations.extend(comment_citations(text, known)) - return citations - - -def worst_status(statuses: Iterable[str]) -> str: - """Severity of an entry is the worst severity among its parts.""" - worst = "ANCHORED" - for status in statuses: - if STATUS_ORDER.index(status) > STATUS_ORDER.index(worst): - worst = status - return worst - - -def _normalize(lines: list[str]) -> list[str]: - return [line.strip() for line in lines] - - -def _find_all(haystack: list[str], needle: list[str]) -> list[int]: - """Zero-based offsets where the needle sequence occurs.""" - size = len(needle) - if not size or size > len(haystack): - return [] - first = needle[0] - return [ - i for i in range(len(haystack) - size + 1) - if haystack[i] == first and haystack[i : i + size] == needle - ] - - -def _map_index( - opcodes: list[tuple[str, int, int, int, int]], index: int -) -> int | None: - """Line of HEAD that a pinned line became. - - A replace block of equal size edits each line in place (a column added to - every row of a table), so its lines map one to one like an equal block. - Collapsed onto the first line, eight refs citing eight rows all landed on - the first row. - """ - for tag, i1, i2, j1, j2 in opcodes: - if i1 <= index < i2: - in_place = tag == "equal" or (tag == "replace" and i2 - i1 == j2 - j1) - return j1 + (index - i1) if in_place else j1 - return None - - -_OPCODE_CACHE: dict[tuple[tuple[str, ...], tuple[str, ...]], list] = {} -_OPCODE_CACHE_SIZE = 8 - - -def _opcodes(pin: list[str], head: list[str]) -> list: - """Opcodes for one pair of revisions, computed once per run. - - Every ref citing the same file would otherwise redo the same diff, and a - driver of thirty thousand lines takes seconds each time. Keyed by the - lines themselves: anchor_block hands over lists it builds and frees, and - an id() key served one file's opcodes to the next pair at the same - addresses. - """ - key = (tuple(pin), tuple(head)) - cached = _OPCODE_CACHE.pop(key, None) - if cached is None: - cached = difflib.SequenceMatcher( - None, pin, head, autojunk=False - ).get_opcodes() - _OPCODE_CACHE[key] = cached - while len(_OPCODE_CACHE) > _OPCODE_CACHE_SIZE: - del _OPCODE_CACHE[next(iter(_OPCODE_CACHE))] - return cached - - -def _map_changed(pin: list[str], head: list[str], lo: int, hi: int) -> AnchorResult: - """Map a pinned range onto HEAD once exact anchoring has failed.""" - if len(pin) > MAX_MATCH_LINES or len(head) > MAX_MATCH_LINES: - return AnchorResult( - "CHANGED", None, None, [], f"file over {MAX_MATCH_LINES} lines" - ) - opcodes = _opcodes(pin, head) - # A file whose every line was edited in place keeps no equal block, yet - # its lines still map one to one: fheroes2 version.txt holds one line, - # 1.0.11 at the pin and 1.1.17 at HEAD, and read GONE. - rewritten_in_place = len(opcodes) == 1 and ( - opcodes[0][0] == "replace" - and opcodes[0][2] - opcodes[0][1] == opcodes[0][4] - opcodes[0][3] - ) - if not rewritten_in_place and not any(tag == "equal" for tag, *_ in opcodes): - return AnchorResult("GONE", None, None, []) - new_lo = _map_index(opcodes, lo) - new_hi = _map_index(opcodes, max(lo, hi - 1)) - if new_lo is None or new_hi is None: - return AnchorResult("GONE", None, None, []) - return AnchorResult("CHANGED", new_lo + 1, max(new_lo, new_hi) + 1, []) - - -def anchor_block( - pin_lines: list[str], head_lines: list[str], start: int, end: int -) -> AnchorResult: - """Locate the pinned line range inside the HEAD revision of a file.""" - pin = _normalize(pin_lines) - head = _normalize(head_lines) - lo, hi = start - 1, end - if lo < 0 or hi > len(pin): - return AnchorResult("GONE", None, None, [], "cited range ends past the end of the file") - if not any(pin[lo:hi]): - return AnchorResult("CHANGED", None, None, [], "cited range is blank") - - candidates: list[int] = [] - for pad in WIDEN_STEPS: - a = max(0, lo - pad) - b = min(len(pin), hi + pad) - hits = _find_all(head, pin[a:b]) - offset = lo - a - if len(hits) == 1: - new_start = hits[0] + offset + 1 - new_end = new_start + (hi - lo) - 1 - status = "ANCHORED" if new_start == start else "SHIFTED" - return AnchorResult(status, new_start, new_end, []) - if not hits: - break - candidates = [h + offset + 1 for h in hits] - else: - return AnchorResult("AMBIGUOUS", None, None, candidates) - - if candidates: - return AnchorResult("AMBIGUOUS", None, None, candidates) - return _map_changed(pin, head, lo, hi) - - -NUDGE_WINDOW = 60 - - -def nudge_to_declared( - pin_lines: list[str], start: int, end: int, tokens: Sequence[str] -) -> tuple[int, int] | None: - """Move a cited range onto the nearest line carrying a declared value. - - Some refs sit a few lines off their subject, and a few land on a blank - line, which anchors nothing. When the entry declares a hash or a name and - that value appears close by, the ref plainly meant that line. - """ - if not tokens: - return None - if end > start and any(line.strip() for line in pin_lines[start - 1:end]): - # A range over code cites that code (a loading function), not the - # value beside it. Moved onto the nearest name, ymir's 598-618 left - # load_rom_cartridge() and a real change read as a silent SHIFTED. - return None - lowered = [line.lower() for line in pin_lines] - hits = [ - i + 1 - for i, line in enumerate(lowered) - if any(token in line for token in tokens) - ] - if not hits: - return None - nearest = min(hits, key=lambda n: abs(n - start)) - if abs(nearest - start) > NUDGE_WINDOW or nearest == start: - return None - if sum(1 for h in hits if abs(h - nearest) <= 1) > 1: - return None - return nearest, nearest + (end - start) - - -def resolve_rename( - result: CompareResult, path: str, head_paths: Iterable[str] = () -) -> tuple[str | None, list[str]]: - """New path of a moved file, or the candidates when several match. - - The comparison is authoritative when it reports the rename. Otherwise the - HEAD tree is searched by basename, then by stem: a C++ file rewritten in C - keeps its stem and loses its basename. - """ - renamed = find_renamed(result, path) - if renamed: - return renamed, [] - pool = list(head_paths) or [change.path for change in result.files] - base = posixpath.basename(path) - matches = _narrow( - [p for p in pool if p != path and posixpath.basename(p) == base], path - ) - if len(matches) == 1: - return matches[0], [] - if not matches: - stem = base.rsplit(".", 1)[0] - matches = _narrow( - [ - p - for p in pool - if p != path and posixpath.basename(p).rsplit(".", 1)[0] == stem - ], - path, - ) - if len(matches) == 1: - return matches[0], [] - return None, matches - - -def _is_header(path: str) -> bool: - return path.lower().endswith(HEADER_SUFFIXES) - - -def _narrow(matches: list[str], path: str) -> list[str]: - """Prefer candidates of the same kind, then of the same directory. - - A stem search on `ss.cpp` matches both `ss.c` and `ss.h`, and only one of - them is the implementation the ref meant. - """ - if len(matches) < 2: - return matches - kind = [p for p in matches if _is_header(p) == _is_header(path)] - if len(kind) == 1: - return kind - pool = kind or matches - directory = posixpath.dirname(path) - same = [p for p in pool if posixpath.dirname(p) == directory] - return same if len(same) == 1 else pool - - -def locate_by_declared_value( - head_lines: list[str], anchored: AnchorResult, tokens: Sequence[str] -) -> tuple[int, int, str] | None: - """Follow a ref whose subject can still be pinpointed at HEAD. - - Two cases carry enough evidence to move a ref without judging the edit - itself: the mapped range already contains the declared value, or the value - occurs exactly once in the whole file. A value occurring several times is - left alone, since nothing says which one the ref meant. - """ - lowered = [line.lower() for line in head_lines] - if anchored.start is not None: - end = anchored.end or anchored.start - block = "\n".join(lowered[anchored.start - 1 : end]) - if any(token in block for token in tokens): - return ( - anchored.start, - end, - "content edited, declared value present in the new range", - ) - hits = [ - i + 1 - for i, line in enumerate(lowered) - if any(token in line for token in tokens) - ] - if len(hits) == 1: - return hits[0], hits[0], "declared value occurs once at HEAD" - return None - - -def _written_against_head(part: RefPart, head_lines: list[str] | None) -> bool: - """Whether the cited line exists at HEAD, which is where it was written.""" - return ( - head_lines is not None - and part.start is not None - and part.start <= len(head_lines) - ) - - -# What the rename search answers when a tree it needed came back truncated: -# a file missing from a partial listing may still exist, so absence is not -# established. eden's fs_paths.h and path_util.cpp were reported GONE. -TREE_TRUNCATED = "(HEAD tree truncated by the forge)" - - -def _unanchorable(part: RefPart) -> PartResult | None: - """The verdict on a part that cites no source path of a declared tree.""" - if is_binary_citation(part.path): - return PartResult( - part, "BINARY", None, None, None, [], - "cites a shipped binary, no source path to anchor", - ) - if is_external_citation(part.path): - return PartResult( - part, "EXTERNAL", None, None, None, [], - "names a project the profile does not declare", - ) - return None - - -def _rename_verdict( - part: RefPart, candidates: list[str], slug: str | None, url: str | None -) -> PartResult: - """What a part absent at HEAD reads as when no rename was found.""" - if candidates == [TREE_TRUNCATED]: - return PartResult( - part, "AMBIGUOUS", None, None, None, [], - "absent from a HEAD tree the forge truncated: " - "the file may still exist", - slug, url, - ) - if candidates: - return PartResult( - part, - "AMBIGUOUS", - None, - None, - None, - [], - f"{len(candidates)} candidates: {', '.join(candidates[:5])}", - slug, - url, - ) - return PartResult( - part, - "GONE", - None, - None, - None, - [], - "absent at HEAD, no rename found", - slug, - url, - ) - - -def _anchor_range( - part: RefPart, - start: int, - pin_lines: list[str], - head_lines: list[str], - tokens: Sequence[str], -) -> tuple[AnchorResult, str, str | None]: - """The cited range carried from the pin to HEAD: anchor, status, note.""" - end = part.last - anchored = anchor_block(pin_lines, head_lines, start, end) - note = anchored.reason - if anchored.status in REVIEW_STATUSES: - nudged = nudge_to_declared(pin_lines, start, end, tokens) - if nudged is not None: - retry = anchor_block(pin_lines, head_lines, *nudged) - if retry.status not in REVIEW_STATUSES: - anchored = retry - note = f"cited line was {nudged[0] - start:+d} off its subject" - status = anchored.status - if status in ("CHANGED", "GONE") and tokens: - located = locate_by_declared_value(head_lines, anchored, tokens) - if located is not None: - moved_start, moved_end, why = located - anchored = AnchorResult("MOVED", moved_start, moved_end, []) - status, note = "MOVED", why - return anchored, status, note - - -def _pin_missing( - part: RefPart, pin_lines: list[str] | None, head_lines: list[str], -) -> str | None: - """Why the pinned revision cannot carry the cited range, None when it can.""" - if pin_lines is None: - # Present at HEAD, absent at the pin, and the cited range fits the - # HEAD file: the ref was written against HEAD while source_commit - # still names an older revision. Saying "missing" sends the reader - # hunting for a move that never happened: the fix is the pin. - reason = "pin revision missing" - if _written_against_head(part, head_lines): - reason = "written against HEAD, pin names an older revision" - return reason - if part.last > len(pin_lines): - # The file is there and the line is not yet: the pinned revision is - # shorter than the one the ref was written against. nestopia cited - # the palette and database loads at 2041 and 2063, which is where - # HEAD carries them, over a pin four hundred lines shorter. A range - # that only ends past the file is no better: dolphin cited 239-323 in - # a 317-line NANDImporter.cpp, and the block found above the end - # anchored it. - return ( - "written against HEAD, pin names an older revision" - if _written_against_head(part, head_lines) - else "cited range ends past the end of the file" - ) - return None - - -def anchor_part( - part: RefPart, - fetch: Callable[[str, str, int | None, Sequence[str]], list[str] | None], - rename_getter: Callable[[str], tuple[str | None, list[str]]], - describe: Callable[..., tuple[str | None, str | None, str]] | None = None, - tokens: Sequence[str] = (), -) -> PartResult: - """Locate one reference part at HEAD, following a rename when needed. - - `describe(path)` returns (repo slug, raw URL at HEAD) for the repository - that owns the path, or (None, None) when the caller does not track it. - """ - unanchorable = _unanchorable(part) - if unanchorable is not None: - return unanchorable - slug, url, actual = ( - describe(part.path, part.start, tokens) if describe - else (None, None, part.path) - ) - head_lines = fetch(HEAD, part.path, part.start, tokens) - path = actual - renamed = actual != part.path - - if head_lines is None: - moved, candidates = rename_getter(part.path) - if moved is None: - return _rename_verdict(part, candidates, slug, url) - path = moved - head_lines = fetch(HEAD, path, part.start, tokens) - if head_lines is None: - return PartResult( - part, "GONE", None, None, None, [], None, slug, url - ) - renamed = True - if describe: - slug, url, _ = describe(path) - - if part.start is None: - status = "RENAMED" if renamed else "ANCHORED" - return PartResult( - part, status, path if renamed else None, None, None, [], None, slug, url - ) - - pin_lines = fetch(PIN, part.path, part.start, tokens) - if pin_lines is None and path != part.path: - # A ref may cite a bare filename the tree search resolved at HEAD. The - # same resolved path usually holds at the pin. A genuine rename keeps - # the old path at the pin, which is why that one is tried first. - pin_lines = fetch(PIN, path, part.start, tokens) - reason = _pin_missing(part, pin_lines, head_lines) - if reason is not None or pin_lines is None: - return PartResult(part, "GONE", None, None, None, [], reason, slug, url) - - anchored, status, note = _anchor_range(part, part.start, pin_lines, head_lines, tokens) - if renamed and status in ("ANCHORED", "SHIFTED"): - status = "RENAMED" - return PartResult( - part, - status, - path if renamed else None, - anchored.start, - anchored.end, - anchored.candidates, - note, - slug, - url, - ) - - -SHIFT_SUPPORT = 3 - -# Lines read past a blank cited line when scoring which repository owns it. -BLANK_LOOKAHEAD = 5 - - -def dominant_shifts( - parts: Iterable[PartResult], support: int = SHIFT_SUPPORT -) -> dict[str, int]: - """Most common line shift per file, taken from the unambiguous anchors. - - A file whose refs mostly moved by the same amount gives strong evidence - for the ones that stayed ambiguous: repetitive tables, as in the NeoGeo - driver, match a cited line in several places, and only one of them sits - where the rest of the file moved to. - """ - by_file: dict[str, dict[int, int]] = {} - for part in parts: - if part.status not in ("ANCHORED", "SHIFTED"): - continue - if part.start is None or part.part.start is None: - continue - counts = by_file.setdefault(part.part.path, {}) - delta = part.start - part.part.start - counts[delta] = counts.get(delta, 0) + 1 - shifts = {} - for path, counts in by_file.items(): - delta, seen = max(counts.items(), key=lambda kv: kv[1]) - if seen >= support: - shifts[path] = delta - return shifts - - -def resolve_by_shift(part: PartResult, shifts: dict[str, int]) -> PartResult: - """Settle an ambiguous part when one candidate matches the file's shift.""" - if part.status != "AMBIGUOUS" or not part.candidates: - return part - if part.part.start is None or part.part.path not in shifts: - return part - wanted = part.part.start + shifts[part.part.path] - if part.candidates.count(wanted) != 1: - return part - span = (part.part.end or part.part.start) - part.part.start - return PartResult( - part.part, - "ANCHORED" if wanted == part.part.start else "SHIFTED", - part.new_path, - wanted, - wanted + span, - [], - f"settled by the {shifts[part.part.path]:+d} line shift of its file", - part.repo, - part.head_url, - ) - - -def refuse_collapsing_moves(parts: list[PartResult]) -> list[PartResult]: - """Hold back a recale that would land on another part of the same ref. - - Two parts of one source_ref cite two places on purpose. dosbox_pure cited - the SHA-1 line of mt32emu.h and the ROMInfo line that ties it to a file - name, under two repository words: the second was self-checked alone, - found the hash once, and read MOVED onto the first line. Recaling it - would drop the ROMInfo citation without a word, so the part goes to - review instead. - """ - cited = { - (p.part.path, p.part.start, p.part.last) - for p in parts if p.part.start is not None - } - held = [] - for part in parts: - target = (part.new_path or part.part.path, part.start, part.end or part.start) - own = (part.part.path, part.part.start, part.part.last) - if ( - part.status in REBASE_STATUSES and part.start is not None - and target != own and target in cited - ): - part = replace( - part, status="AMBIGUOUS", candidates=[part.start], - reason="lands on a line another part of this ref already cites", - ) - held.append(part) - return held - - -@dataclass -class EntryReport: - name: str - source_ref: str - status: str - parts: list[PartResult] - kind: str = "ref" - field: str = "" - - -@dataclass -class ProfileReport: - name: str - repo: str | None = None - repos: list[str] = field(default_factory=list) - pinned_tag: str | None = None - # Set when pinned_tag carries a pin_frozen reason rather than a tag name. - pin_frozen: bool = False - host: str | None = None - pin: str | None = None - pin_origin: str | None = None - head: str | None = None - entries: list[EntryReport] = field(default_factory=list) - skipped: str | None = None - counts: dict[str, int] = field(default_factory=dict) - # Declared repositories whose forge the tool cannot read. They are named - # rather than dropped: a `source` on an unknown host used to fall back to - # `upstream` in silence, and a divergence between the two went unseen. - unread: list[str] = field(default_factory=list) - # The upstream view when the profile declares one: its pin and HEAD. A - # recale moves its parts onto upstream HEAD lines, so a bump advances - # upstream_commit with source_commit, or the profile describes two - # revisions at once (geolith jg.c drifted 213 -> 221 -> 229). - upstream_pin: str | None = None - upstream_head: str | None = None - upstream_origin: str | None = None - # The release tag the upstream view is pinned on, when it is one. That - # view is read at its tag, and a bump leaves upstream_commit there. - upstream_tag: str | None = None - # Repositories whose pin is no ancestor of the branch the profile - # tracks: history was rewritten, or the pin lies on another branch. - orphaned: list[str] = field(default_factory=list) - # Repositories the forge answers under another owner/name: renamed or - # transferred. The redirect is followed in silence, so the move is named - # and counted as review work, since a renamed project is re-profiled. - moved_repos: list[str] = field(default_factory=list) - # A profile off any forge names the page announcing its releases - # (version_source): the version read there, beside the one declared. - declared_version: str | None = None - page_version: str | None = None - # An announced version searched for and found only rebuilt or re-signed. - no_genuine_copy: str | None = None - # What realign_prose has to settle before a recale or a bump can run: - # prose written under an older pin. None when not looked at. Counted to - # review, or the triage read "0 to review" on a profile whose pin could - # not move (blastem, xenia-edge, lindbergh-loader). - stale_prose: list[str] | None = None - # --check-version on a forge: core_version against the latest tag and - # release, kept on the report so every output carries it. - version: VersionReport | None = None - version_warning: str | None = None - # --detect-new-files and --watch-hashes, kept on the report so --json - # carries them to check_freshness. None when not asked. A new file is - # {path, line, name, since_pin}: since_pin when the pinned revision of - # the same file does not carry the name. A new hash is {path, kind, value}. - new_files: list[dict] | None = None - new_files_elided: dict[str, int] = field(default_factory=dict) - new_hashes: list[dict] | None = None - detection_skipped: str | None = None - # config_reads refs written for a build a platform ships, each read at - # that build (pin_build) rather than at source_commit: {platform, - # revision, ref, status, detail}. A status other than BUILD_OK is review - # work. - build_refs: list[dict] = field(default_factory=list) - - # Skips that say the profile cannot be checked as written: a repository - # that no longer answers, no pin to read, a death the forge contradicts. - # --strict passed them, and the CI job meant to stop an unchecked profile - # let a mistyped repository through. - UNCHECKABLE = ( - "no declared repository answers", - "no source_commit and no resolvable profiled_date", - "upstream_gone is declared but the forge answers", - "upstream gone:", - ) - - def uncheckable(self) -> bool: - """Whether the skip is a defect of the profile rather than its nature.""" - return bool(self.skipped and self.skipped.startswith(self.UNCHECKABLE)) - - def needs_review(self) -> int: - counts = self.counts or {} - return ( - sum(counts.get(status, 0) for status in REVIEW_STATUSES) - + len(self.orphaned) - + len(self.stale_prose or ()) - + len(self.moved_repos) - + sum(1 for ref in self.build_refs if ref["status"] != "BUILD_OK") - ) - - -def triage_state(report: ProfileReport) -> str: - """One triage line: what to review, the pin to set again named first. - - A pin outside its branch's history or a repository answering under - another name is fixed before any ref is read: pcem showed as "1 to - review" for an upstream_commit off the dev branch. - """ - if report.skipped: - return report.skipped - state = f"{report.needs_review()} to review" - if report.orphaned: - state += f", {len(report.orphaned)} orphaned pin" - if report.moved_repos: - state += f", {len(report.moved_repos)} repository moved" - stale = len(report.stale_prose or ()) - if stale: - state += f", {stale} prose under an older pin" - unchecked = (report.counts or {}).get("UNCHECKED", 0) - if unchecked: - state += f", {unchecked} unchecked" - return state - - -def select_repo(profile: dict) -> upstream.Repo | None: - """Repository the profile was read from: source first, then upstream. - - A source keyed by build mode with no upstream (uzdoom) names its - repositories only in the dict, which a plain string read never parses. - """ - for field_name in ("source", "upstream"): - repo = upstream.parse_repo(str(profile.get(field_name) or "")) - if repo is not None: - return repo - for _, _, url in declared_repositories(profile): - repo = upstream.parse_repo(url) - if repo is not None: - return repo - return None - - -def resolve_pin( - profile: dict, repo: upstream.Repo, cache_dir: str, offline: bool, - field: str = "source", mode: str = "", branch: str | None = None, -) -> tuple[str, str] | tuple[None, None]: - """Commit the profile was written at, and how it was obtained. - - `field` selects which declared pin applies: `source_commit` for the port - the profile was read from, `upstream_commit` for the original project. - 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" - if pinned: - return str(pinned), origin - date = str(profile.get("profiled_date") or "") - if not date: - return None, None - sha = upstream.resolve_commit_at(repo, date, cache_dir, offline, branch) - return (sha, f"profiled_date {date}") if sha else (None, None) - - -def select_views( - profile: dict, cache_dir: str, offline: bool -) -> list[RepoView]: - """Every repository the profile cites, source before upstream. - - 241 profiles declare a `source` distinct from their `upstream`, and 168 of - those have both on a supported forge. Their refs mix paths from the two, so - a single repository cannot resolve them all. - """ - views: list[RepoView] = [] - seen: set[tuple[str, str]] = set() - gone = False - # A port living on a branch of a fork is absent from the default tip, so - # the branch it was read from is followed when the profile names one. - declared = str(profile.get("source_branch") or "") or None - for field_name, mode, url in declared_repositories(profile): - repo = upstream.parse_repo(url) - if repo is None or (repo.host, repo.slug) in seen: - continue - # A mirror carries the source's history, commits and branches alike: - # its pin is source_commit. It read `source_mirror_commit`, a field - # no profile carries, and fell back to profiled_date, which a bump - # never moves (eden judged from f6e76860 while pinned on d16735f5). - pin_field = "source" if field_name == "source_mirror" else field_name - branch = declared if pin_field == "source" else None - head = None - try: - unread: upstream.NotCachedError | None = None - for candidate in branch_candidates(url): - try: - head = upstream.resolve_head(repo, cache_dir, offline, candidate) - except upstream.NotCachedError as exc: - # Another prefix of the /tree/ tail may be the cached branch. - unread = exc - continue - if head: - branch = candidate - break - if unread and not head: - raise unread - pin, origin = resolve_pin( - profile, repo, cache_dir, offline, pin_field, mode, branch - ) - if not pin or origin is None: - continue - head = head or upstream.resolve_head(repo, cache_dir, offline, branch) - except upstream.GoneError: - # yuzu's source answers 451: the mirror declared after it is what - # keeps the profile checkable, so a gone repository is passed over. - gone = True - continue - if not head: - continue - if head != pin and not offline: - # A tip cached before the pin was written is older than the pin: - # scummvm-standalone pinned 6f7b060d while the cache still held - # 77d80d7, and a bump would have moved the pin backwards. - relation = upstream.ancestry(repo, pin, head, cache_dir, offline) - if relation and relation[0] == 0 and relation[1] > 0: - head = upstream.resolve_head(repo, cache_dir, offline, branch, fresh=True) or head - seen.add((repo.host, repo.slug)) - views.append(RepoView(repo, pin, head, origin, field_name, mode)) - if gone and not views: - raise upstream.GoneError(f"{profile.get('emulator', '?')}: every declared repository is gone") - views = _follow_fork_base(_follow_submodule(views, cache_dir, offline), cache_dir, offline) - return _hold_upstream(profile, views) - - -def _hold_upstream(profile: dict, views: list[RepoView]) -> list[RepoView]: - """An upstream tree the port vendors or synced to is judged at that - revision: aps3e carries RPCS3 1f080485, yaps2 PCSX2 342db515.""" - if not str(profile.get("upstream_frozen") or "").strip(): - return views - return [ - replace(view, head=view.pin, origin=f"{view.origin}, held") - if view.field == "upstream" else view - for view in views - ] - - -def _follow_submodule( - views: list[RepoView], cache_dir: str, offline: bool -) -> list[RepoView]: - """An upstream the port vendors as a submodule moves with the port. - - libretro/TIC-80 builds nesbox/TIC-80 through its `core` submodule, and - upstream_commit was that submodule's commit. Compared to nesbox's own - HEAD, the view reported changes the shipped core never had, and a bump - would have moved upstream_commit past what it builds. The commit the - port's HEAD points the submodule at is the upstream HEAD that matters. - """ - source = next((v for v in views if v.field == "source"), None) - behind = [v for v in views if v.field == "upstream" and v.pin != v.head] - if source is None or not behind: - return views - pinned = { - sha: path - for path, sha in upstream.list_gitlinks( - source.repo, source.pin, cache_dir, offline - ).items() - } - if not pinned: - return views - followed: list[RepoView] = [] - for view in views: - path = pinned.get(view.pin) if view.field == "upstream" else None - head = ( - upstream.list_gitlinks(source.repo, source.head, cache_dir, offline).get(path) - if path else None - ) - followed.append( - replace(view, head=head, origin=f"{view.origin} via {path}") - if head else view - ) - return followed - - -def _follow_fork_base( - views: list[RepoView], cache_dir: str, offline: bool -) -> list[RepoView]: - """An upstream the port is a fork of moves with what the port merged. - - snes9x, kronos, np2kai and six others pinned upstream_commit on the merge - base of upstream with the fork, the upstream code the port carries. Read - against upstream's own HEAD, the bump moved it past anything the port - built from. Outside one fork network the HEAD stays the reference. - """ - source = next((v for v in views if v.field == "source"), None) - if source is None: - return views - followed: list[RepoView] = [] - for view in views: - if view.field != "upstream" or view.pin == view.head or " via " in view.origin: - followed.append(view) - continue - base = upstream.fork_base( - view.repo, view.head, source.repo, source.head, cache_dir, offline - ) - if base and base != view.pin: - # A pin newer than the merge base names code the port does not - # carry: the upstream is a build of its own, the Qt melonDS or the - # EasyRPG Player a standalone + libretro profile documents, and - # its HEAD stays the reference. melonds read 825 commits back. - moved = upstream.ancestry(view.repo, base, view.pin, cache_dir, offline) - if moved and moved[0] > 0 and moved[1] == 0: - base = None - followed.append( - replace(view, head=base, origin=f"{view.origin}, fork base of {source.repo.slug}") - if base else view - ) - 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. - - A profile can cite a fork as `.../Ymir/tree/libretro`, which names both - the repository and the branch the port lives on. The tail may go on into - a directory (`.../libretro-samples/tree/master/tests/cruzes`): this is - the whole tail, and branch_candidates() its prefixes. - """ - _, sep, tail = url.partition("/tree/") - return tail.strip("/") or None if sep else None - - -def branch_candidates(url: str) -> list[str]: - """Each prefix of a /tree/ tail, shortest first, that may be the branch. - - A branch name can hold a slash, so the tail is not split blindly. The - first prefix the forge resolves is the branch, the rest a directory. - Taken whole, `master/tests/cruzes` resolved to nothing and the profile - was skipped as having no resolvable pin. - """ - tail = branch_in_url(url) - if not tail: - return [] - parts = tail.split("/") - return ["/".join(parts[:n]) for n in range(1, len(parts) + 1)] - - -SELF_CHECK_CONTEXT = 2 - - -def verify_at_pin( - part: RefPart, - pin_lines: list[str] | None, - tokens: Sequence[str], - hash_tokens: Sequence[str] = (), -) -> PartResult: - """Check a ref against its own revision instead of against HEAD. - - A profile pinned to a superseded tag documents a program HEAD no longer - contains, so comparing the two says nothing. What can still be checked is - self-consistency: does the cited range carry the value the entry declares? - """ - unanchorable = _unanchorable(part) - if unanchorable is not None: - return unanchorable - if pin_lines is None: - return PartResult( - part, "GONE", None, None, None, [], "absent at the pinned revision" - ) - if part.start is None: - return PartResult(part, "ANCHORED", None, None, None, []) - if part.last > len(pin_lines): - return PartResult( - part, "GONE", None, None, None, [], "beyond the end of the file" - ) - if not tokens: - # Nothing to look for: an entry with neither a hash nor a name (an - # analysis block, a `filename:` field) cannot be checked against its - # own revision. Calling it anchored made the check vacuously true, and - # a pin advanced without a recale hid a ref that had become false. - return PartResult( - part, "UNCHECKED", None, None, None, [], - "entry declares no value to look for", - ) - # An archive ref cites the line declaring the set. Its members follow, one - # per line, so the window reaches forward as far as the entry has members. - reach = SELF_CHECK_CONTEXT + len(tokens) - lo = max(0, part.start - 1 - SELF_CHECK_CONTEXT) - hi = min(len(pin_lines), part.last + reach) - window = "\n".join(pin_lines[lo:hi]).lower() - if any(token in window for token in tokens): - return PartResult(part, "ANCHORED", None, None, None, []) - return _value_elsewhere(part, pin_lines, hash_tokens) - - -def _value_elsewhere( - part: RefPart, pin_lines: list[str], hash_tokens: Sequence[str] -) -> PartResult: - """A part whose cited window lacks the value, judged by where it is.""" - # A ref that cites loading logic never spells the value out, so its absence - # here proves nothing. Only finding the value somewhere else in the file - # shows the ref points at the wrong place, and only a hash carries that - # weight. A filename fragment matches by coincidence: `tyrian2` hits every - # line naming `tyrian2.c`, and a name built at runtime from a format - # string appears nowhere at all. - if not hash_tokens: - return PartResult(part, "ANCHORED", None, None, None, []) - elsewhere = sorted( - { - index - for index, line in enumerate(pin_lines, 1) - for token in hash_tokens - if token.lower() in line.lower() - } - ) - if not elsewhere: - return PartResult(part, "ANCHORED", None, None, None, []) - if len(elsewhere) > 1: - return PartResult( - part, "AMBIGUOUS", None, None, None, elsewhere, - "declared value carried by several lines", - ) - # Content-level evidence, stronger than a line-shift heuristic: the value - # sits on exactly one line, so the recale target is not a guess. - return PartResult( - part, "MOVED", None, elsewhere[0], elsewhere[0], elsewhere, - "declared value occurs once at the pinned revision", - ) - - -def reconcile_self_check( - parts: list[PartResult], eligible: list[bool] | None = None -) -> list[PartResult]: - """Judge a self-checked entry whole rather than part by part. - - A ref often cites both the table carrying the value and the code that - loads the file. Once one part anchors on the value, the others in the - SAME file describe behaviour and cannot be judged by value: the checker - would only rediscover the value where the first part already points. A - part in another file carries its own value and is judged on it: geolith's - jg.c part, sixteen lines off, was settled by an anchored libretro.c part. - A structurally absent part still counts, and a part that was compared - against HEAD rather than self-checked is never rewritten. - """ - eligible = eligible if eligible is not None else [True] * len(parts) - # The settled part lies in the tree its anchor was found in: without the - # repository, attribution wrote dosbox-pure-unleashed's ROMInfo line - # under `source`, a repository that carries no mt32emu.h. - anchored_files: dict[str, str | None] = {} - for part, ok in zip(parts, eligible): - if ok and part.status == "ANCHORED": - anchored_files.setdefault(part.part.path, part.repo) - kept = ("ANCHORED", "GONE", "EXTERNAL", "BINARY") - return [ - PartResult( - part.part, "ANCHORED", None, None, None, [], - repo=anchored_files[part.part.path], - ) - if ok and part.status not in kept and part.part.path in anchored_files - else part - for part, ok in zip(parts, eligible) - ] - - -def version_tag_candidates(core_version: object, repo_name: str = "") -> list[str]: - """Tag spellings a declared core_version might use. - - Some projects prefix their own name and drop the separators, the way - mamedev/mame publishes 0.289 as `mame0289`, so that spelling is derived - from the repository name rather than listed per project. - """ - version = str(core_version or "").strip() - if not version or " " in version: - return [] - bare = version.lstrip("vV") - spellings = [version, f"v{bare}", bare] - if repo_name: - spellings.append(f"{repo_name}{bare.replace('.', '')}") - return list(dict.fromkeys(spellings)) - - -def detect_pinned_tag( - profile: dict, views: list[RepoView], cache_dir: str, offline: bool -) -> str | None: - """Tag the profile is pinned to, when the repository has moved past it. - - A profile can document a frozen release line that lives as a tag inside a - still-developed repository. HEAD is then a different program: the refs - describe the tag and must never be recaled onto modern code. `pcsx2-legacy` - is pinned to v1.6.0 of PCSX2, where a plugin-era DEV9 ref resolves onto the - modern built-in DEV9, a different code path. - - The declared `core_version` names the tag, which is looked up by name so - that a repository publishing nightly tags cannot hide an old release behind - pagination. - - Only the repository of the documented build freezes the profile. An - `upstream` a port was cut from sits on a release tag as its base - (GroovyMAME on mame0289, triforce on Dolphin 2407) while the port itself - keeps moving: that view is judged against its tag by `tag_pinned_views`, - and the port is still compared with its HEAD and still bumped. - """ - documented = [view for view in views if view.field != "upstream"] or views - tagged = tag_pinned_views(profile, documented, cache_dir, offline) - return next(iter(tagged.values()), None) - - -def tag_pinned_views( - profile: dict, views: list[RepoView], cache_dir: str, offline: bool -) -> dict[tuple[str, str], str]: - """Views whose pin is the tag `core_version` names, keyed by repository. - - A core_version keyed by build mode names the tag of the view's own mode: - uzdoom's standalone sits on 5.0.3 while its libretro branch moves. - """ - tagged: dict[tuple[str, str], str] = {} - declared = profile.get("core_version") - for view in views: - if view.pin == view.head: - continue - version = declared.get(view.mode) if isinstance(declared, dict) else declared - for tag in version_tag_candidates(version, view.repo.name): - if upstream.tag_commit(view.repo, tag, cache_dir, offline) == view.pin: - tagged[(view.repo.host, view.repo.slug)] = tag - break - return tagged - - -def resolve_through_gitlinks(path: str, links: Iterable[Iterable[str]]) -> str | None: - """Path of `project file` inside the one submodule named after the project. - - idtech4a++ cites `xash3d-fwgs filesystem/filesystem.c` and starship - `Torch src/Companion.cpp`: the declared tree holds a gitlink there, not - the files, so no blob path answers and the part stayed external. - """ - word, _, rest = path.partition(" ") - rest = rest.strip().lstrip("/") - if not rest: - return None - word = word.lower() - for paths in links: - matches = [ - link for link in paths or () - if word in link.rsplit("/", 1)[-1].lower() - ] - if len(matches) == 1: - return f"{matches[0]}/{rest}" - if matches: - return None - return None - - -def _resolve_project_words( - refs: list, tree_order: Callable[[], Iterable[list[str] | None]], - link_order: Callable[[], Iterable[Iterable[str]]] = lambda: (), -) -> list: - """Point `project file` parts at the declared tree that vendors them. - - tree_order() yields the trees to search, pin before HEAD, and - link_order() the submodule paths of the declared trees at their pins. - Each is only called for a part that needs it, since reading a tree costs - a request. Parts no tree carries stay external. - """ - resolved: dict[str, str | None] = {} - out = [] - for display, ref, tokens, hashes, parts, citation in refs: - rewritten = [] - for part in parts: - if is_external_citation(part.path): - if part.path not in resolved: - resolved[part.path] = resolve_project_word( - part.path, tree_order() - ) or resolve_through_gitlinks(part.path, link_order()) - if resolved[part.path]: - part = replace(part, path=resolved[part.path]) - rewritten.append(part) - out.append((display, ref, tokens, hashes, rewritten, citation)) - return out - - -def _path_roles(refs: list) -> dict[str, set[str]]: - """The roles each cited path is written under across the profile.""" - roles: dict[str, set[str]] = {} - for *_, parts, _citation in refs: - for part in parts: - if part.role: - roles.setdefault(part.path, set()).add(part.role) - return roles - - -def moved_repositories( - views: list[RepoView], cache_dir: str, offline: bool -) -> list[str]: - """Declared repositories the forge now answers under another name.""" - moved = [] - seen: set[tuple[str, str]] = set() - for view in views: - key = (view.repo.host, view.repo.slug) - if key in seen: - continue - seen.add(key) - current = upstream.current_slug(view.repo, cache_dir, offline) - if current and current.lower() != view.repo.slug.lower(): - moved.append( - f"{view.repo.slug} answers as {current} (renamed or transferred)" - ) - return moved - - -def shipped_build_holders(profile: dict) -> dict[int, dict]: - """The source_ref holders of config_reads entries naming platforms, by id. - - Such an entry describes the build those platforms ship, so its refs are - read at their pin_build revision, never at source_commit. - """ - found: dict[int, dict] = {} - for read in profile.get("config_reads") or []: - if not isinstance(read, dict) or not read.get("platforms"): - continue - found[id(read)] = read - for key in read.get("keys") or []: - if isinstance(key, dict): - found[id(key)] = read - return found - - -def build_revision(profile: dict, platform: str) -> str | None: - """The emulator revision a platform's pin_build entry names, None when - its value is no revision (a URL, a package name).""" - for build in profile.get("pin_build") or []: - if build.get("platform") != platform: - continue - value = str(build.get("value") or "").strip() - return value if value and re.fullmatch(r"[\w.+-]+", value) else None - return None - - -def _check_build_refs( - profile: dict, - deferred: list[tuple[Citation, dict]], - views: list[RepoView], - fetch: Callable[[upstream.Repo, str, str], list[str] | None], -) -> list[dict]: - """Each deferred config_reads ref read at the build of its platforms. - - The file must exist at that revision and every cited line lie inside - it. The entry declares no value to anchor on, so this is the check a - ref past the end of its file fails (BUILD_GONE), not a content match. - """ - results: list[dict] = [] - for citation, read in deferred: - for platform in read.get("platforms") or []: - # A closed build is named by its binary: an address ref is - # skipped below, a source path cannot exist at a sha256. - revision = (str(read.get("revision") or read.get("binary_sha256") or "") - or build_revision(profile, str(platform))) - row = {"platform": str(platform), "revision": revision or "", - "ref": citation.ref, "field": citation.field} - if revision is None: - results.append({**row, "status": "BUILD_UNREAD", - "detail": "no pin_build revision for this platform"}) - continue - problems: list[str] = [] - for part in split_source_ref(citation.ref): - role, path = ref_role(part.path) - if " " in path: - # A project word in front (`ROCKNIX patches/x.patch:4`): - # a file of the platform's tree, read with the record. - continue - lines = next( - (found for view in views - if (not role or view.field == role) - and (found := fetch(view.repo, revision, path)) is not None), - None, - ) - if lines is None: - problems.append(f"{part.path} absent at {revision[:12]}") - elif (part.end or part.start or 0) > len(lines): - problems.append(f"{part.raw} past the end ({len(lines)} lines)") - results.append({ - **row, - "status": "BUILD_GONE" if problems else "BUILD_OK", - "detail": "; ".join(problems), - }) - return results - - -def _gather_refs( - profile: dict, text: str | None -) -> tuple[list, list[tuple[Citation, dict]]]: - """The citations to anchor, and the refs read at a shipped build instead. - - One citation surface for the whole document. A prose citation carries - no declared value, so it anchors on content alone: the nudge and - relocation heuristics stay off rather than guess from a profile-wide - token pool. - """ - known = frozenset( - repo.name.lower() - for _, _, url in declared_repositories(profile) - if (repo := upstream.parse_repo(url)) is not None - ) - modes = {mode for _, mode, _ in declared_repositories(profile) if mode} - refs = [] - at_build = shipped_build_holders(profile) - deferred: list[tuple[Citation, dict]] = [] - for citation in collect_citations(profile, text): - if citation.kind == "ref" and id(citation.holder) in at_build: - # Written for the build a platform ships: read at pin_build. - deferred.append((citation, at_build[id(citation.holder)])) - continue - if citation.kind == "ref": - entry = citation.entry or {} - display = str(entry.get("name") or "") or citation.field - if citation.label: - display += f" [{citation.label}]" - # The repository a part says its lines are in (`upstream - # src/a.c:5`) stays on that part: the same path can be cited - # under both roles in one profile. - # An entry of one build mode reads that build's repository when - # the profile keys its source by mode. - mode = str(citation.label or entry.get("mode") or "") - mode = mode if mode in modes else "" - parts = [ - RefPart( - strip_repo_word(p.path, known), p.start, p.end, p.raw, - ref_role(p.path)[0] or mode, - ) - for p in split_source_ref(citation.ref) - ] - refs.append(( - display, citation.ref, _anchor_tokens(entry), - entry_hashes(entry), parts, citation, - )) - else: - refs.append(( - citation.field, citation.ref, [], [], - list(citation.parts), citation, - )) - return refs, deferred - - -def _cited_dirs(refs: list) -> set[str]: - """Directories the profile already cites, deepest first, to settle a bare - filename that several trees of one repository carry.""" - cited_dirs: set[str] = set() - for _, _, _, _, parts, _ in refs: - for part in parts: - directory = posixpath.dirname(part.path) - while directory: - cited_dirs.add(directory) - directory = posixpath.dirname(directory) - return cited_dirs - - -def _unanswered_skip(profile: dict) -> str: - """Why a profile no declared repository answers for is skipped.""" - # A declared pin whose repository or branch does not answer is not a - # profile without a pin: pyxel's kitao/pyxel answers 404, and the - # message sent the reader looking for a missing source_commit. - return ( - "no declared repository answers (moved, removed, or a branch the " - "forge does not know)" - if profile.get("source_commit") or profile.get("upstream_commit") - else "no source_commit and no resolvable profiled_date " - "on any declared repository" - ) - - -def _readable_views( - profile: dict, report: ProfileReport, cache_dir: str, offline: bool -) -> list[RepoView] | None: - """The repositories to read, None once the report says why it is skipped.""" - report.unread = [ - url - for _, _, url in declared_repositories(profile) - if upstream.parse_repo(url) is None - ] - if select_repo(profile) is None: - declared = str(profile.get("source") or profile.get("upstream") or "") - report.skipped = f"unsupported host: {declared or 'none declared'}" - return None - - stated_gone = str(profile.get("upstream_gone") or "").strip() - - try: - views = select_views(profile, cache_dir, offline) - except upstream.RateLimitError: - raise - except upstream.UpstreamError: - # citron's dead domain now resolves to an unrelated host that fails - # TLS: for a forge the profile declares gone, any refusal agrees. - if stated_gone: - # The profile says the forge is gone and the forge agrees. That is - # a recorded fact, not an open problem: the refs describe the last - # revision anyone can reach, and nothing further is possible. - report.skipped = f"upstream gone (declared): {stated_gone}" - return None - raise - if stated_gone and any(view.field != "source_mirror" for view in views): - # Declared dead, yet it answered. A profile that keeps saying so stops - # being read, so the contradiction is the finding. - report.skipped = ( - f"upstream_gone is declared but the forge answers: {stated_gone}" - ) - return None - if not views and stated_gone: - report.skipped = f"upstream gone (declared): {stated_gone}" - return None - if not views: - report.skipped = _unanswered_skip(profile) - return None - return views - - -def _on_own_revision(view: RepoView, tagged: dict[tuple[str, str], str]) -> bool: - """Whether a view is read at its own revision: on HEAD, or on its tag.""" - return view.pin == view.head or (view.repo.host, view.repo.slug) in tagged - - -def _record_revisions( - profile: dict, report: ProfileReport, views: list[RepoView], - cache_dir: str, offline: bool, -) -> dict[tuple[str, str], str]: - """Put the revisions under comparison on the report, and return the views - read at their release tag.""" - primary = views[0] - report.repo, report.host = primary.repo.slug, primary.repo.host - report.repos = [v.repo.slug for v in views] - report.pin, report.pin_origin = primary.pin, primary.origin - report.head = primary.head - for view in views[1:]: - if view.field == "upstream": - report.upstream_pin = view.pin - report.upstream_head = view.head - report.upstream_origin = view.origin - report.moved_repos = moved_repositories(views, cache_dir, offline) - # A profile can hold its pin on purpose, documenting a build that is that - # revision. Comparing it to HEAD says nothing and recaling would repoint - # its refs at code the build never had, so it is judged like a frozen tag: - # against itself. - frozen = str(profile.get("pin_frozen") or "").strip() - report.pin_frozen = bool(frozen) - report.pinned_tag = frozen or detect_pinned_tag( - profile, views, cache_dir, offline - ) - # A repository pinned on its release tag is read at that tag, whichever - # role it plays: comparing mame0174 refs with mamedev HEAD says nothing. - tagged = tag_pinned_views(profile, views, cache_dir, offline) - report.upstream_tag = next( - ( - tagged[(view.repo.host, view.repo.slug)] - for view in views - if view.field == "upstream" and (view.repo.host, view.repo.slug) in tagged - ), - None, - ) - - if not report.pinned_tag: - # A tag or a held pin is off the tracked branch by design. - report.orphaned = [ - f"{view.repo.slug}: pin {view.pin[:8]} is no ancestor of {view.head[:8]}" - for view in views - if not _on_own_revision(view, tagged) - and upstream.compare( - view.repo, view.pin, view.head, cache_dir, offline - ).diverged - ] - return tagged - - -def _line_score(lines: list[str], start: int | None, tokens: tuple) -> int: - """How well a repository's cited line matches what the ref means. - - Two repositories can hold the same path with different contents: - `mame` cites files that both mamedev/mame and its libretro fork - carry, and the fork's tree is offset, so its line 5545 is a real - but unrelated statement. A line carrying the entry's own subject - beats a merely non-empty one, which in turn beats a blank. - """ - if start is None: - return 2 - if start > len(lines): - return 0 - # A blank cited line opens the block below it: lrps2's - # VMManager.cpp:418 is blank in the fork it was written for, and - # scored 0 it handed the ref to the upstream, where 418 is - # unrelated code that then decided the rebase target. - line = next( - (ln.lower() for ln in lines[start - 1:start - 1 + BLANK_LOOKAHEAD] - if ln.strip()), - "", - ) - # A set name sits between delimiters in a MAME declaration, so - # `pgm` matches `GAME( 1997, pgm,` but not `pgm_012_025_drgw2`, - # which is what tells the two repositories apart. - if tokens and any( - re.search(rf"\b{re.escape(tok)}\b", line) for tok in tokens - ): - return 3 - if tokens and any(tok in line for tok in tokens): - return 2 - return 1 if line.strip() else 0 - - -@dataclass -class _Resolver: - """Where each cited path lives across the declared repositories, and how - a part reads there, memoised for one profile.""" - - name: str - profile: dict - cache_dir: str - offline: bool - views: list[RepoView] - report: ProfileReport - tagged: dict[tuple[str, str], str] - # A part that names no repository is looked up first where its path is - # cited elsewhere: a note citing `src/a.c:12` beside source_refs that - # all read `upstream src/a.c` means that tree. - # A path cited under both roles lends neither, and such a part is - # compared in every repository rather than in whichever comes first. - path_roles: dict[str, set[str]] - cited_dirs: set[str] - owners: dict[tuple[str, int | None, tuple, str], tuple[RepoView, str]] = field( - default_factory=dict - ) - context: dict[str, tuple[CompareResult, list[str]]] = field(default_factory=dict) - truncated_trees: set[str] = field(default_factory=set) - pin_trees: dict[str, list[str]] = field(default_factory=dict) - mute: set[tuple[str, str]] = field(default_factory=set) - lines_cache: dict[ - tuple[str, str, int | None, tuple[str, ...], str | None, str], list[str] - ] = field(default_factory=dict) - - @property - def primary(self) -> RepoView: - return self.views[0] - - def role_of(self, part: RefPart) -> str: - if part.role: - return part.role - lent = self.path_roles.get(part.path, ()) - return next(iter(lent)) if len(lent) == 1 else "" - - def on_own_revision(self, view: RepoView) -> bool: - return _on_own_revision(view, self.tagged) - - def trees_in_order(self) -> Iterable[list[str] | None]: - """Pin trees before HEAD trees, each read only when reached.""" - return itertools.chain( - map(self.pin_tree_for, self.views), - (self.context_for(v)[1] for v in self.views), - ) - - def gitlinks_in_order(self) -> Iterable[Iterable[str]]: - return ( - upstream.list_gitlinks(v.repo, v.pin, self.cache_dir, self.offline) - for v in self.views - ) - - def _add_bare_name_matches(self, path: str, candidates: list[str]) -> None: - # A bare filename, the way prose cites files. Resolved against the - # pin before HEAD: the ref was written at the pin, and a file that - # moved since would otherwise be resolved to a HEAD path that does - # not exist at the pin, reporting GONE for the one reason it never - # should. The rename search then carries the pin path to HEAD. - for view in self.views: - for tree in (self.pin_tree_for(view), self.context_for(view)[1]): - matches = [ - p for p in tree or [] - if posixpath.basename(p) == path - ] - if len(matches) > 1: - matches = narrow_by_cited(matches, self.cited_dirs) - # A few survivors are told apart by the cited line below: a - # 266-line Windows configure.ac cannot carry line 754. - if len(matches) <= 4: - candidates.extend( - m for m in matches if m not in candidates - ) - if matches: - break - - def _add_suffix_matches(self, path: str, candidates: list[str]) -> None: - # A path written from a subproject directory of a monorepo: - # `src/main/Main.cc` in emu-ex-plus-alpha is PCE.emu/src/main/Main.cc - # for one profile and C64.emu/src/main/Main.cc for another. The - # tree is searched by suffix and the cited directories decide. - suffix = "/" + path - for view in self.views: - _, tree = self.context_for(view) - matches = [p for p in tree or [] if p.endswith(suffix)] - if len(matches) > 1: - matches = narrow_by_cited(matches, self.cited_dirs) - if len(matches) <= 4: - candidates.extend(m for m in matches if m not in candidates) - - def _candidate_paths(self, path: str) -> list[str]: - """The paths a cited path may stand for, as written first.""" - candidates = [path] - head, _, tail = path.partition("/") - if tail and any(head == v.repo.name for v in self.views): - candidates.append(tail) - if "/" not in path: - self._add_bare_name_matches(path, candidates) - elif not any( - self.fetch_from(v.repo, sha, path) is not None - for v in self.views for sha in (v.pin, v.head) - ): - self._add_suffix_matches(path, candidates) - return candidates - - def _upstream_on_line( - self, candidate: str, at_pin: bool, start: int | None, tokens: tuple - ) -> RepoView | None: - """The upstream holding the entry's value on the cited line too.""" - for other in self.views: - if _view_role(other) != "upstream": - continue - sha = other.pin if at_pin else other.head - theirs = self.fetch_from(other.repo, sha, candidate) - if theirs is not None and _line_score(theirs, start, tokens) == 3: - return other - return None - - def _try_candidate( - self, - candidate: str, - ordered: list[RepoView], - start: int | None, - tokens: tuple, - wanted: str, - best: tuple[int, RepoView, str] | None, - ) -> tuple[tuple[RepoView, str] | None, tuple[int, RepoView, str] | None]: - """The owner one candidate path settles, else the best seen so far.""" - for at_pin in (True, False): - for view in ordered: - sha = view.pin if at_pin else view.head - found = self.fetch_from(view.repo, sha, candidate) - if found is None: - continue - target = symlink_target(candidate, found) - if target: - found = self.fetch_from(view.repo, sha, target) - if found is None: - continue - candidate = target - rank = _line_score(found, start, tokens) - if rank and _answers(view, wanted) and view.mode: - # An entry of one build mode is that build's code: - # xenia-edge's standalone xbox.xex refs scored higher - # in the libretro tree, whose same file is offset. - return (view, candidate), best - if rank == 3 and not wanted and _view_role(view) == "source": - # The port carries a copy of the emulator: when the - # original holds the same value on the cited line, - # the code asking for the file is the emulator's. - other = self._upstream_on_line(candidate, at_pin, start, tokens) - if other is not None: - return (other, candidate), best - if rank == 3: - return (view, candidate), best - if best is None or rank > best[0]: - best = (rank, view, candidate) - return None, best - - def locate( - self, path: str, start: int | None = None, tokens: tuple = (), wanted: str = "" - ) -> tuple[RepoView, str]: - """Repository and real path carrying a cited path, HEAD before pin. - - A ref may prefix the path with the repository directory name, as it - appears in a parent folder holding both clones: 270 parts across 21 - profiles do. That prefix is stripped only as a last resort, once the - path as written has failed against every repository and revision. - - The pin comes before HEAD because that is the revision the ref was - written against. `mame` cites files that both mamedev/mame and the - libretro fork carry today, but only the upstream pin holds them. The - fork's pin is an older tree where they lived elsewhere. - """ - candidates = self._candidate_paths(path) - # A part that names its repository is looked up there first. In a - # profile keyed by build mode, a part naming none belongs to the - # first build declared, as _realign_part decides a shared path: the - # libretro tree of xenia-edge carries emulator.cc offset, and its - # better-scored line hid a standalone ref that had moved. - if not wanted and self.primary.mode: - wanted = self.primary.mode - ordered = ( - sorted(self.views, key=lambda v: not _answers(v, wanted)) if wanted - else self.views - ) - best: tuple[int, RepoView, str] | None = None - for candidate in candidates: - found, best = self._try_candidate(candidate, ordered, start, tokens, wanted, best) - if found is not None: - return found - return (best[1], best[2]) if best else (self.primary, path) - - def resolve_path( - self, path: str, start: int | None = None, tokens: tuple = (), role: str = "" - ) -> tuple[RepoView, str]: - key = (path, start, tokens, role) - if key not in self.owners: - self.owners[key] = self.locate(path, start, tokens, role) - return self.owners[key] - - def pin_tree_for(self, view: RepoView) -> list[str]: - """Tree at the revision the refs were written against, memoised.""" - key = view.repo.slug - if key not in self.pin_trees: - tree, _ = upstream.list_tree(view.repo, view.pin, self.cache_dir, self.offline) - self.pin_trees[key] = tree - return self.pin_trees[key] - - def fetch_from(self, repo: upstream.Repo, sha: str, wanted: str) -> list[str] | None: - """Read a file, treating a repository that refuses as one that lacks it. - - A profile can name several repositories, and a mirror exists exactly - because one of them stopped answering: git.eden-emu.dev returns 403 to - anything that is not a browser. Letting the first refusal abort the - report leaves the mirror unread and the profile unverifiable, so the - repository is muted for the rest of the pass instead. A quota signal - still ends the profile, and upstream stops asking that host, because - continuing would only burn the rest of the budget on the same wall. - """ - # Keyed by host as well as slug: a mirror carries the same slug on - # another forge, and muting one must not silence the other. - key = (repo.host, repo.slug) - if key in self.mute: - return None - try: - found = upstream.fetch_file(repo, sha, wanted, self.cache_dir, self.offline) - if found is None and "/" in wanted: - # A file of a submodule is read at the commit its gitlink - # pins in this revision, so a bump moves it with the port. - inner = upstream.through_gitlink( - repo, sha, wanted, self.cache_dir, self.offline - ) - if inner is not None: - found = upstream.fetch_file(*inner, self.cache_dir, self.offline) - return found - except (upstream.RateLimitError, upstream.NotCachedError): - raise - except upstream.UpstreamError as exc: - self.mute.add(key) - print(f"{self.name}: {repo.host}/{repo.slug} muted, {exc}", file=sys.stderr) - return None - - def context_for(self, view: RepoView) -> tuple[CompareResult, list[str]]: - key = view.repo.slug - if key not in self.context: - comparison = upstream.compare( - view.repo, view.pin, view.head, self.cache_dir, self.offline - ) - tree, truncated = upstream.list_tree( - view.repo, view.head, self.cache_dir, self.offline - ) - self.context[key] = (comparison, tree) - if truncated: - self.truncated_trees.add(key) - if truncated and tree: - print( - f"{self.name}: {key} HEAD tree truncated by the forge, " - "rename search is partial", - file=sys.stderr, - ) - return self.context[key] - - def rename_getter(self, path: str, role: str = "") -> tuple[str | None, list[str]]: - """Resolved only when a cited file has vanished, then memoised. - - Every declared repository is searched, not just the one that owns the - path: a profile citing both a port and its upstream may point at a file - that only the other one carries. - """ - owner, actual = self.resolve_path(path, role=role) - ordered = [owner] + [v for v in self.views if v is not owner] - candidates: list[str] = [] - for view in ordered: - comparison, tree = self.context_for(view) - found, near = resolve_rename(comparison, actual, tree) - if found: - return found, [] - # Only the owning repository reports near misses: pooling them - # across repositories turns a resolvable path into an ambiguous one. - if view is owner: - candidates = near - if not candidates and any(v.repo.slug in self.truncated_trees for v in ordered): - return None, [TREE_TRUNCATED] - return None, candidates - - def fetch( - self, - which: str, - path: str, - start: int | None = None, - tokens: Iterable[str] = (), - forced: RepoView | None = None, - role: str = "", - ) -> list[str] | None: - """Lines for one path at one revision, read once per run. - - The same object is handed back every time so the opcode cache can key - on identity. - """ - tokens = tuple(tokens) - key = (which, path, start, tokens, forced.repo.slug if forced else None, role) - if key not in self.lines_cache: - view, actual = ( - (forced, path) if forced - else self.resolve_path(path, start, tokens, role) - ) - sha = view.pin if which == PIN else view.head - found = self.fetch_from(view.repo, sha, actual) - if found is None: - # Not cached: the miss may be a repository that has just been - # muted, and the next call resolves the path to a mirror that - # does carry it. upstream.fetch_file holds its own cache, so - # asking again costs a lookup rather than a request. - return None - self.lines_cache[key] = found - return self.lines_cache[key] - - def describe( - self, - path: str, - start: int | None = None, - tokens: Iterable[str] = (), - forced: RepoView | None = None, - role: str = "", - ) -> tuple[str | None, str, str]: - view, actual = ( - (forced, path) if forced - else self.resolve_path(path, start, tuple(tokens), role) - ) - slug = view.repo.slug if view is not self.primary else None - return slug, upstream.raw_url(view.repo, view.head, actual), actual - - def anchor_across_views(self, part: RefPart, tokens: Sequence[str]) -> PartResult: - """Judge a part against every repository that carries its path. - - A ref is only broken when it fails everywhere. `mame` cites files that - both mamedev/mame and its libretro fork hold, with the same delimited - token on the cited line, so no single attribution can be right for all - of them. Taking the best outcome across repositories settles it. - A part that names its repository is judged there alone when that - repository carries the path: hypseus-singe cites lair.cpp under - `source` for one entry and `upstream` for the next. - """ - role = self.role_of(part) - owner, _actual = self.resolve_path(part.path, part.start, tuple(tokens), role) - carrying = [ - view for view in self.views - if self.fetch_from(view.repo, view.pin, part.path) is not None - ] - # Judged in the build it belongs to: the best outcome across the - # builds of a keyed profile let the libretro tree vouch for a - # standalone ref that had moved (xenia-edge's emulator.cc). - meant = part.role or self.primary.mode - named = [view for view in carrying if _answers(view, meant)] - if meant and named: - carrying = named - live = { - id(view) for view in carrying - if self.fetch_from(view.repo, view.head, part.path) is not None - } - renames = functools.partial(self.rename_getter, role=role) - best: tuple[int, PartResult] | None = None - for view in carrying: - result = anchor_part( - part, - functools.partial(self.fetch, forced=view), - renames, - functools.partial(self.describe, forced=view), - tokens, - ) - if ( - live and id(view) not in live - and result.status in ("AMBIGUOUS", "GONE") - ): - # dosbox-pure's upstream was emptied: its rename search - # offered the .info, .sln and .vcxproj as candidates and hid - # the real change in the port that still carries the file. - continue - rank = STATUS_ORDER.index(result.status) - # On a tie the repository the citation is attributed to speaks: - # mame's "written against HEAD, pin names an older revision" was - # replaced by whatever the first repository said. - if best is None or rank < best[0] or (rank == best[0] and view is owner): - best = (rank, result) - return best[1] if best else anchor_part( - part, - functools.partial(self.fetch, role=role), - renames, - functools.partial(self.describe, role=role), - tokens, - ) - - def self_checked(self, part: RefPart, tokens: Sequence[str]) -> bool: - """Whether a part is judged on self-consistency rather than compared. - - Comparing a revision with itself anchors every ref whatever it cites, - so a part whose repository already sits on HEAD is judged on - self-consistency instead. Decided per part, by the repository that - owns it: a primary on HEAD says nothing about an upstream that moved on. - """ - if self.report.pinned_tag: - return True - if not part.role and len(self.path_roles.get(part.path, ())) > 1: - return all(self.on_own_revision(view) for view in self.views) - view, _ = self.resolve_path(part.path, part.start, tuple(tokens), self.role_of(part)) - return self.on_own_revision(view) - - def _elsewhere_at_own_revision( - self, part: RefPart, owner: RepoView, tokens: Sequence[str], hashes: Sequence[str], - ) -> PartResult | None: - """The verdict of another repository read at its own revision.""" - for view in self.views: - if view is owner or not self.on_own_revision(view): - continue - lines = self.fetch_from(view.repo, view.pin, part.path) - if lines is None: - continue - found = verify_at_pin(part, lines, tokens, hashes) - if found.status != "GONE": - return replace( - found, - repo=None if view is self.primary else view.repo.slug, - ) - return None - - def _judge_at_pin( - self, part: RefPart, tokens: Sequence[str], hashes: Sequence[str] - ) -> PartResult: - role = self.role_of(part) - result = verify_at_pin( - part, - self.fetch(PIN, part.path, part.start, tokens, role=role), - tokens, hashes, - ) - # Checked at its own revision, the part still lies in one tree: - # the report names it, as a comparison does. - owner, _actual = self.resolve_path(part.path, part.start, tuple(tokens), role) - if result.repo is None and owner is not self.primary: - result = replace(result, repo=owner.repo.slug) - if result.status == "GONE": - # The owner is chosen on the cited first line alone. hatari's - # port carries a longer src/paths.c than the upstream that a - # source_ref attributes the path to, and prose citing the - # port's 305-356 read past the end of the upstream's 345 - # lines. As in a comparison, a ref fails only everywhere. - result = self._elsewhere_at_own_revision(part, owner, tokens, hashes) or result - return result - - def judge( - self, part: RefPart, flag: bool, tokens: Sequence[str], hashes: Sequence[str] - ) -> PartResult: - other = cites_another_version(part, self.profile.get("core_version")) - if other: - # ti99sim cites v0.0.11 beside its v0.16.0 pin: no revision of - # the pinned repository holds that program, so it cannot anchor. - return PartResult( - part, "EXTERNAL", None, None, None, [], - f"annotated for version {other}, not the documented one", - ) - if flag: - result = self._judge_at_pin(part, tokens, hashes) - else: - result = self.anchor_across_views(part, tokens) - # A forge that refused the request said nothing about the file: - # git.eden-emu.dev answers 403 to scripts, and nine citations of - # eden-nightly read "absent at HEAD" for a file that exists. - owner, _actual = self.resolve_path( - part.path, part.start, tuple(tokens), self.role_of(part) - ) - if ( - result.status in ("GONE", "AMBIGUOUS") - and (owner.repo.host, owner.repo.slug) in self.mute - ): - return PartResult( - part, "UNCHECKED", None, None, None, [], - f"{owner.repo.host}/{owner.repo.slug} refused the request, not read", - ) - return result - - -def _judge_refs(resolver: _Resolver, refs: list, report: ProfileReport) -> None: - """Judge every ref and put its entry on the report.""" - staged = [] - for entry_name, ref, tokens, hashes, ref_parts, citation in refs: - flags = [resolver.self_checked(part, tokens) for part in ref_parts] - judged = [ - resolver.judge(part, flag, tokens, hashes) - for part, flag in zip(ref_parts, flags) - ] - staged.append((entry_name, ref, citation, reconcile_self_check(judged, flags))) - - shifts = dominant_shifts([p for _, _, _, parts in staged for p in parts]) - for entry_name, ref, citation, parts in staged: - parts = refuse_collapsing_moves([resolve_by_shift(p, shifts) for p in parts]) - status = worst_status([p.status for p in parts]) - report.entries.append(EntryReport( - entry_name, ref, status, parts, citation.kind, citation.field - )) - report.counts[status] = report.counts.get(status, 0) + 1 - - -def build_report( - name: str, profile: dict, cache_dir: str, offline: bool = False, - text: str | None = None, -) -> ProfileReport: - """Confront one profile with its upstream.""" - report = ProfileReport(name=name, entries=[], counts={}) - refs, deferred = _gather_refs(profile, text) - path_roles = _path_roles(refs) - cited_dirs = _cited_dirs(refs) - - views = _readable_views(profile, report, cache_dir, offline) - if views is None: - return report - tagged = _record_revisions(profile, report, views, cache_dir, offline) - - # A profile carrying no source_ref still has a pin worth writing and a - # version worth checking, so the revisions above are resolved first. - if not refs: - report.skipped = NO_SOURCE_REF - return report - - resolver = _Resolver( - name, profile, cache_dir, offline, views, report, tagged, path_roles, cited_dirs - ) - refs = _resolve_project_words( - refs, resolver.trees_in_order, resolver.gitlinks_in_order - ) - report.build_refs = _check_build_refs(profile, deferred, views, resolver.fetch_from) - _judge_refs(resolver, refs, report) - return report - - -def _render_span(path: str, start: int | None, end: int | None) -> str: - if start is None: - return path - if end and end != start: - return f"{path}:{start}-{end}" - return f"{path}:{start}" - - -def _original_part(part: PartResult) -> str: - """The part exactly as the profile writes it today.""" - return part.part.raw or _render_span( - part.part.path, part.part.start, part.part.end - ) - - -def _rendered_part(part: PartResult) -> str: - """The part rewritten onto its new location, annotation preserved.""" - span = _render_span(part.new_path or part.part.path, part.start, part.end) - raw = part.part.raw - if not raw: - return span - located = _trim_prose(_ANNOTATION_RE.sub("", raw).strip()) - # `stella src/emucore/Cart.cxx:120` names the repository the lines are - # in. The path was read without that word, and a recale wrote it back - # without it: the attribution went with the old line numbers. - word, _, rest = located.partition(" ") - if rest.startswith(f"{part.part.path}:") and word and "/" not in word: - span = f"{word} {span}" - if located and located in raw: - return raw.replace(located, span, 1) - return span - - -def _part_line(part: PartResult) -> str: - detail = part.status - if part.new_path: - detail += f" -> {part.new_path}" - if part.start is not None and part.status != "ANCHORED": - span = f"{part.start}" - if part.end and part.end != part.start: - span += f"-{part.end}" - detail += f" -> {span}" - if part.candidates: - detail += f" candidats: {', '.join(str(c) for c in part.candidates)}" - if part.reason: - detail += f" ({part.reason})" - if part.repo: - detail += f" [{part.repo}]" - return f" {_original_part(part)} {detail}" - - -def _report_header(report: ProfileReport) -> list[str]: - """Repository, revisions and version lines heading one report.""" - slugs = " + ".join(report.repos) if report.repos else (report.repo or "") - lines = [f"{report.name} {slugs}".rstrip()] - if report.pin and report.head: - lines.append( - f" {report.pin[:7]} -> {report.head[:7]} pin: {report.pin_origin}" - ) - if report.pinned_tag: - held = ( - f"pin held ({report.pinned_tag})" if report.pin_frozen - else f"pinned to tag {report.pinned_tag}" - ) - lines.append(f" {held}: checked against its own revision, not against HEAD") - elif report.pin and report.pin == report.head: - lines.append(" pin is HEAD: checked for self-consistency") - if report.upstream_tag and not report.pinned_tag: - lines.append( - f" upstream pinned to tag {report.upstream_tag}: checked against " - "its own revision, not against HEAD" - ) - lines.extend(f" not read (unsupported host): {url}" for url in report.unread or []) - lines.extend(f" orphaned pin: {line}" for line in report.orphaned) - lines.extend(f" moved repository: {line}" for line in report.moved_repos) - if report.declared_version or report.page_version: - lines.append( - f" version page: declared {report.declared_version or '-'}, " - f"announced {report.page_version or 'unreadable'}" - + (" (no genuine copy found)" - if report.page_version and report.page_version == report.no_genuine_copy - else "") - ) - if report.version: - version = report.version - lines.append( - f" version: declared {version.declared}, " - f"latest tag {version.latest_tag}, " - f"latest release {version.latest_release} ({version.release_date})" - ) - if report.version_warning: - lines.append(f" {report.version_warning}") - return lines - - -def _report_body(report: ProfileReport, changed_only: bool) -> list[str]: - """Ref, prose and build lines of one report that was read.""" - lines = [] - for entry in report.entries: - if changed_only and entry.status not in REVIEW_STATUSES: - continue - lines.append(f" {entry.name} {entry.source_ref}") - lines.extend(_part_line(part) for part in entry.parts) - summary = ", ".join( - f"{count} {status.lower()}" for status, count in sorted(report.counts.items()) - ) - lines.append(f" {len(report.entries)} refs: {summary}") - lines.extend(f" prose under an older pin: {line}" for line in report.stale_prose or ()) - for ref in report.build_refs: - if changed_only and ref["status"] == "BUILD_OK": - continue - detail = f": {ref['detail']}" if ref["detail"] else "" - lines.append( - f" {ref['status']} at {ref['platform']} build " - f"{ref['revision'][:12] or '-'} {ref['ref']}{detail}" - ) - lines.append(f" {report.needs_review()} to review") - return lines - - -def format_report(report: ProfileReport, changed_only: bool = False) -> str: - """Human-readable report for one profile.""" - lines = _report_header(report) - if report.skipped: - lines.append(f" skipped: {report.skipped}") - return "\n".join(lines) - return "\n".join(lines + _report_body(report, changed_only)) - - -def report_to_dict(report: ProfileReport) -> dict: - """Serialisable form of one report.""" - payload = asdict(report) - payload["needs_review"] = report.needs_review() - return payload - - -def format_markdown(reports: list[ProfileReport]) -> str: - """Report set as a markdown document.""" - lines = [ - "# profile-sync", - "", - "| profile | repository | refs | to review |", - "|---|---|---|---|", - ] - for report in reports: - if report.skipped: - lines.append( - f"| {report.name} | {report.repo or ''} | skipped | {report.skipped} |" - ) - continue - lines.append( - f"| {report.name} | {report.repo} | {len(report.entries)} " - f"| {report.needs_review()} |" - ) - for report in reports: - lines.extend(["", "```", format_report(report), "```"]) - return "\n".join(lines) - - -def fetch_plan(report: ProfileReport) -> list[str]: - """Raw URLs at HEAD for the refs that need the code read again.""" - if report.skipped or not report.head or not report.host: - return [] - owner, _, name = (report.repo or "").partition("/") - fallback = upstream.make_repo(report.host, owner, name) - urls = [] - for entry in report.entries: - if entry.status not in REVIEW_STATUSES: - continue - for part in entry.parts: - url = part.head_url - if url is None: - if fallback is None: - continue - path = part.new_path or part.part.path - url = upstream.raw_url(fallback, report.head, path) - if part.part.start: - url += f"#L{part.part.start}" - urls.append(url) - return urls - - -@dataclass(frozen=True) -class VersionReport: - declared: str - latest_tag: str | None - latest_release: str | None - release_date: str | None - tag_matches_declared: bool - tag_commit: str | None - - -def check_version( - profile: dict, repo: upstream.Repo, cache_dir: str, offline: bool -) -> VersionReport | None: - """Declared core_version against the latest upstream tag and release.""" - declared = str(profile.get("core_version") or "") - if not declared: - return None - release = upstream.latest_release(repo, cache_dir, offline) - # Asked by name: the first page of tags misses a release that a project - # publishing nightlies has pushed past a hundred newer tags. - commit = upstream.tag_commit(repo, declared, cache_dir, offline) - return VersionReport( - declared, - upstream.newest_tag(repo, cache_dir, offline), - release.tag if release else None, - release.date if release else None, - commit is not None, - commit, - ) - - -def page_version(profile: dict, cache_dir: str, offline: bool) -> str | None: - """The latest version a profile's version_source page announces. - - Forty-odd profiles live off any forge: profile_sync could only say - "unsupported host", and a new Altirra or mednafen release went unseen. - """ - source = profile.get("version_source") or {} - url, pattern = str(source.get("url") or ""), str(source.get("pattern") or "") - if not url or not pattern: - return None - try: - text = upstream.fetch_page(url, cache_dir, offline) - except upstream.UpstreamError as exc: - print(f"{profile.get('emulator', '?')}: {url}: {exc}", file=sys.stderr) - return None - match = re.search(pattern, text or "") - return match.group(1) if match else None - - -def version_warning( - report: VersionReport | None, - pin: str | None, - relation: tuple[int, int] | None = None, - passed_release: str | None = None, -) -> str | None: - """Flag a declared version that names a tag away from the resolved pin. - - ``relation`` is the pin's (ahead, behind) count against the tag commit. - A pin after its tag is a build between two releases. A pin before it - describes code the declared release had already replaced. - ``passed_release`` is a later release the pin already contains, which - core_version should name instead. - """ - if report is None or not pin: - return None - # Before the tag guard: parallel-launcher declared 9.0.5, a name no tag - # carries (v9.0-6), and the release its HEAD contains went unsaid. - if passed_release: - return ( - f"core_version {report.declared} is older than the code: the pin " - f"{pin[:7]} already contains release {passed_release}" - ) - if not report.tag_commit or report.tag_commit == pin: - return None - where = "" - if relation is not None: - ahead, behind = relation - if ahead and not behind: - where = f", {ahead} commits after the tag" - elif behind and not ahead: - where = f", {behind} commits before the tag: core_version is later than the code" - elif ahead and behind: - where = f", off the tag's history ({ahead} ahead, {behind} behind)" - return ( - f"core_version {report.declared} names tag commit {report.tag_commit[:7]}, " - f"the pin resolved to {pin[:7]}{where}" - ) - - -FILE_EXTENSIONS = ( - "bin", "rom", "zip", "dat", "bios", "img", "chd", "nvram", "sav", - "ips", "pal", "fnt", -) - -_FILE_RE = re.compile( - r"[\w./+-]+\.(?:" + "|".join(FILE_EXTENSIONS) + r")\b", re.IGNORECASE -) -_SHA1_RE = re.compile(r"\b[0-9a-fA-F]{40}\b") -_MD5_RE = re.compile(r"\b[0-9a-fA-F]{32}\b") -_CRC_RE = re.compile(r"\b(?:0x)?([0-9a-fA-F]{8})\b") - - -def declared_names(profile: dict) -> set[str]: - """Every filename the profile covers, aliases included.""" - names = set() - for entry in profile.get("files") or []: - if not isinstance(entry, dict): - continue - for value in [entry.get("name", "")] + list(entry.get("aliases") or []): - if value: - names.add(os.path.basename(str(value)).casefold()) - return names - - -def declared_hashes(profile: dict) -> set[str]: - """Every hash the profile declares.""" - hashes = set() - for entry in profile.get("files") or []: - if isinstance(entry, dict): - hashes.update(collect_tokens(entry)) - return hashes - - -# Sources where a filename is a string. Outside a literal or a comment, -# `EE.bios` is a config key and `psm_s.pal` a struct field (pcee2). -CODE_SUFFIXES = ( - ".c", ".cc", ".cpp", ".cxx", ".h", ".hh", ".hpp", ".hxx", ".inc", ".inl", - ".m", ".mm", ".rs", ".java", ".kt", ".cs", ".swift", -) -_PRINTF_SPEC_RE = re.compile( - r"[-+ #0]*(?:\d+|\*)?(?:\.\d+)?(?:hh|h|ll|l|z|j|t|L)?[diouxXsScpfFeEgGaA]" -) -_TITLE_WORD_RE = re.compile(r"[A-Z0-9][\w.()&'+-]*") - - -def _string_literal(line: str, index: int) -> tuple[int, int] | None: - """Bounds of the double-quoted literal holding index, None outside one.""" - start = None - position = 0 - while position < len(line): - char = line[position] - if char == "\\" and start is not None: - position += 2 - continue - if char == '"': - if start is None: - start = position + 1 - else: - if start <= index < position: - return start, position - start = None - position += 1 - return None - - -def _in_comment(line: str, index: int) -> bool: - before = line[:index] - return "//" in before or "/*" in before or line.lstrip().startswith(("*", "#")) - - -def file_literal(line: str, start: int, end: int, code: bool) -> str | None: - """The filename a match stands for, None when it names no file. - - A printf conversion glued to the name is not part of it: sdl2trs writes - `"%slevel2.rom"` with ROM_PATH in front. A name with spaces is read - whole: virtualjaguar opens `"Jaguar CD BIOS.rom"`, and the match alone - was `BIOS.rom`. Such a name is written as a title, so `"Please insert - bios.bin"` and `"Packing patches.zip"` still yield the file alone. In - a code file a match outside any string or comment is an identifier and - names nothing. - """ - name = line[start:end] - if start and line[start - 1] == "%": - spec = _PRINTF_SPEC_RE.match(name) - if spec: - name = name[spec.end():] - literal = _string_literal(line, start) - if literal is None: - if code and not _in_comment(line, start): - return None - elif literal[1] == end and line[start - 1:start] == " ": - words = line[literal[0]:start].split() + [name] - if all(_TITLE_WORD_RE.fullmatch(word) for word in words): - return line[literal[0]:end] - base = os.path.basename(name) - # `".bin.hex.iii.rom"` (sdl2trs) lists extensions, it names no file. - if base.startswith(".") or not _FILE_RE.fullmatch(base): - return None - return base - - -def detect_new_files( - head_lines: list[str], known: set[str], cap: int = 25, path: str = "" -) -> tuple[list[tuple[int, str]], int]: - """Filename literals at HEAD the profile does not declare.""" - found: list[tuple[int, str]] = [] - seen: set[str] = set() - elided = 0 - code = path.lower().endswith(CODE_SUFFIXES) - for number, line in enumerate(head_lines, start=1): - for match in _FILE_RE.finditer(line): - literal = file_literal(line, match.start(), match.end(), code) - if literal is None: - continue - name = literal.casefold() - if name in known or name in seen: - continue - seen.add(name) - if len(found) >= cap: - elided += 1 - continue - found.append((number, literal)) - return found, elided - - -def watch_hashes(added_lines: list[str], known: set[str]) -> list[tuple[str, str]]: - """Hash literals in added lines that no profile entry declares.""" - found: list[tuple[str, str]] = [] - for line in added_lines: - strong = bool(_SHA1_RE.search(line) or _MD5_RE.search(line)) - for pattern, kind in ((_SHA1_RE, "sha1"), (_MD5_RE, "md5")): - found.extend( - (kind, value.lower()) for value in pattern.findall(line) - if value.lower() not in known - ) - if strong or not _FILE_RE.search(line): - continue - found.extend( - ("crc32", value.lower()) for value in _CRC_RE.findall(line) - if value.lower() not in known - ) - return found - - -def unified_for_path( - pin_lines: list[str], head_lines: list[str], path: str, context: int -) -> str: - """Unified diff of one cited file between the two revisions.""" - diff = difflib.unified_diff( - pin_lines, - head_lines, - fromfile=f"a/{path}", - tofile=f"b/{path}", - n=context, - lineterm="", - ) - return "\n".join(diff) - - -def tree_diff(result: CompareResult, ref_dirs: set[str]) -> list[str]: - """Tree changes restricted to the directories the refs point at.""" - lines = [] - for change in result.files: - directory = posixpath.dirname(change.path) - if ref_dirs and not any( - directory == d or directory.startswith(d + "/") for d in ref_dirs - ): - continue - if change.status == "renamed" and change.previous_path: - lines.append(f"renamed {change.previous_path} -> {change.path}") - else: - lines.append(f"{change.status} {change.path}") - if result.truncated: - lines.append("comparison truncated by the forge, list is partial") - return lines - - -DRIFT_WEIGHTS = {"GONE": 40, "CHANGED": 30, "AMBIGUOUS": 15, "SHIFTED": 1} -PIN_WEIGHT = 50 - - -def drift_score( - report: ProfileReport, version: VersionReport | None, commits: int -) -> int: - """Rank profiles by how far they have drifted from their pin.""" - counts = report.counts or {} - score = sum(DRIFT_WEIGHTS.get(k, 0) * v for k, v in counts.items()) - # Prose a reader has to settle weighs as an ambiguous ref, prose - # --realign-prose moves by itself as a shifted one. - score += sum( - DRIFT_WEIGHTS["SHIFTED" if line.startswith("would recale") else "AMBIGUOUS"] - for line in report.stale_prose or () - ) - if ( - version is not None and not version.tag_matches_declared - and version.latest_release and version.latest_release != version.declared - ): - score += 10 - # A pin off its branch or a repository answering under another name is - # set again before any ref can be read, so each outranks a GONE ref: - # pcem ranked last at 0 with an orphaned upstream_commit. - score += PIN_WEIGHT * (len(report.orphaned) + len(report.moved_repos)) - # A profile no repository answers for cannot be read at all, and - # --strict fails on it: it ranked at 0 under the clean profiles. - score += PIN_WEIGHT * report.uncheckable() - score += min(commits, 20) // 4 - return score - - -class YamlWriteError(Exception): - """A profile edit did not land exactly as intended.""" - - -def _top_level_indices(lines: list[str], key: str) -> list[int]: - """Indices of lines declaring a key at the top level of the document.""" - prefix = f"{key}:" - return [i for i, line in enumerate(lines) if line.startswith(prefix)] - - -def insert_after_line(text: str, key: str, new_line: str) -> str: - """Insert a line right after a top-level key.""" - lines = text.splitlines() - indices = _top_level_indices(lines, key) - if len(indices) != 1: - raise YamlWriteError( - f"{key}: expected one top-level line, found {len(indices)}" - ) - lines.insert(indices[0] + 1, new_line) - return "\n".join(lines) + ("\n" if text.endswith("\n") else "") - - -def _field_value(line: str) -> str: - """The value of a `key: value` line as the loader reads it. - - etlegacy quotes `(FS_FOpenFileRead \\"pak0.pk3\\")` inside a double-quoted - source_ref: stripping the outer quotes keeps the backslashes, and the - line never equalled the parsed value it was searched for. - """ - raw = line.split(":", 1)[1].strip() - try: - parsed = yaml.safe_load(raw) - except yaml.YAMLError: - parsed = None - if isinstance(parsed, str): - return parsed - return raw.strip('"').strip("'") - - -def _double_quoted(value: str) -> str: - """A YAML double-quoted scalar reading back as value.""" - return '"' + value.replace("\\", "\\\\").replace('"', '\\"') + '"' - - -def find_field_line(text: str, key: str, expected: str, start: int = 0) -> int: - """Index of the first `key:` line at or after `start` holding `expected`. - - Positional counting is not safe here: `source_ref` also appears outside - `files:` in some profiles, under `data_directories:` for instance, which - shifts every ordinal past it. - """ - lines = text.splitlines() - marker = f"{key}:" - for index in range(start, len(lines)): - line = lines[index] - if line.strip().startswith(marker) and _field_value(line) == expected: - return index - raise YamlWriteError( - f"{key}: no line holding {expected!r} at or after line {start + 1}" - ) - - -def replace_field_line(text: str, key: str, expected: str, value: str, - start: int = 0) -> tuple[str, int]: - """Rewrite the first `key:` line holding `expected`. Returns text and index.""" - index = find_field_line(text, key, expected, start) - lines = text.splitlines() - line = lines[index] - indent = line[: len(line) - len(line.lstrip())] - lines[index] = f"{indent}{key}: {_double_quoted(value)}" - joined = "\n".join(lines) + ("\n" if text.endswith("\n") else "") - return joined, index - - -def apply_edit(path: Path, new_text: str, expected: dict) -> None: - """Write, then verify the parsed document matches what was intended.""" - original = path.read_text(encoding="utf-8") - path.write_text(new_text, encoding="utf-8") - try: - written = yaml.safe_load(new_text) - except yaml.YAMLError as exc: - path.write_text(original, encoding="utf-8") - raise YamlWriteError(f"{path}: parse failed after write: {exc}") from exc - if written != expected: - path.write_text(original, encoding="utf-8") - raise YamlWriteError(f"{path}: structure changed beyond the intended field") - - -# Key order of a written version_basis, as a reader expects it. -_BASIS_ORDER = ("kind", "why", "repo", "url", "commit", "path", "pattern", "format", - "match", "tag", "distance", "of", "artifact") - - -def _top_block(lines: list[str], key: str) -> tuple[int, int] | None: - """Line span of a top-level key and the indented lines under it.""" - indices = _top_level_indices(lines, key) - if not indices: - return None - if len(indices) > 1: - raise YamlWriteError(f"{key}: expected one top-level line, found {len(indices)}") - end = indices[0] + 1 - while end < len(lines) and (lines[end].startswith((" ", "\t")) or not lines[end].strip()): - end += 1 - # Blank lines closing the block belong to the document, not to it. - while end > indices[0] + 1 and not lines[end - 1].strip(): - end -= 1 - return indices[0], end - - -def _render_version(value: object) -> list[str]: - if isinstance(value, dict): - return ["core_version:"] + [ - f" {mode}: {'null' if v is None else _double_quoted(str(v))}" - for mode, v in value.items() - ] - return [f"core_version: {'null' if value is None else _double_quoted(str(value))}"] - - -def _render_basis(basis: dict) -> list[str]: - def ordered(entry: dict) -> dict: - rank = {key: i for i, key in enumerate(_BASIS_ORDER)} - return dict(sorted(entry.items(), key=lambda item: rank.get(item[0], len(rank)))) - - def scalar(value: object) -> str: - # Every string quoted: a tag 2407 or 0.10 read back as a number. A - # pattern keeps its backslashes readable in single quotes. - if isinstance(value, bool) or not isinstance(value, str): - return str(value).lower() if isinstance(value, bool) else str(value) - if "\\" in value or '"' in value: - return "'" + value.replace("'", "''") + "'" - return _double_quoted(value) - - def block(entry: dict, indent: str) -> list[str]: - # kind, why and repo are words of the schema's enums, written bare. - return [f"{indent}{key}: {value if key in ('kind', 'why', 'repo') else scalar(value)}" - for key, value in ordered(entry).items()] - - if "kind" in basis: - return ["version_basis:", *block(basis, " ")] - lines = ["version_basis:"] - for mode, entry in basis.items(): - lines += [f" {mode}:", *block(entry, " ")] - return lines - - -def set_version_fields(text: str, version: object, basis: dict | None) -> str: - """Rewrite core_version and version_basis, version_basis right after it.""" - lines = text.splitlines() - span = _top_block(lines, "core_version") - rendered = _render_version(version) - if span is None: - anchor = _top_block(lines, "upstream_commit") or _top_block(lines, "source_commit") \ - or _top_block(lines, "profiled_date") or _top_block(lines, "upstream") \ - or _top_block(lines, "source") or _top_block(lines, "alias_of") \ - or _top_block(lines, "type") - if anchor is None: - raise YamlWriteError("core_version: no line to write it after") - lines[anchor[1]:anchor[1]] = rendered - else: - lines[span[0]:span[1]] = rendered - if basis is not None: - old = _top_block(lines, "version_basis") - if old is not None: - del lines[old[0]:old[1]] - after = _top_block(lines, "core_version") - if after is None: - raise YamlWriteError("version_basis: core_version not found to write it after") - lines[after[1]:after[1]] = _render_basis(basis) - return "\n".join(lines) + ("\n" if text.endswith("\n") else "") - - -def apply_readings(document: dict, readings: list) -> tuple[object, dict | None]: - """core_version and version_basis as the readings state them. - - A reading that could not be made leaves its build mode as written. - """ - version = document.get("core_version") - basis = document.get("version_basis") - basis = dict(basis) if isinstance(basis, dict) else None - if isinstance(version, dict): - version = dict(version) - for reading in readings: - if reading.status not in (version_basis.MATCH, version_basis.DRIFT): - continue - if reading.mode: - if not isinstance(version, dict): - version = {} - version[reading.mode] = reading.read - else: - version = reading.read - if reading.updates and basis is not None: - target = basis if not reading.mode else dict(basis.get(reading.mode) or {}) - target.update(dict(reading.updates)) - if reading.mode: - basis[reading.mode] = target - return version, basis - - -def write_version(path: Path, readings: list) -> bool: - """Set core_version (and a tag basis' tag and distance) from the readings.""" - text = path.read_text(encoding="utf-8") - document = yaml.safe_load(text) - version, basis = apply_readings(document, readings) - if version == document.get("core_version") and basis == document.get("version_basis"): - return False - expected = dict(document) - expected["core_version"] = version - if basis is not None: - expected["version_basis"] = basis - apply_edit(path, set_version_fields(text, version, basis), expected) - return True - - -def backfill_commit(path: Path, sha: str) -> bool: - """Insert source_commit after profiled_date. False when already present.""" - text = path.read_text(encoding="utf-8") - document = yaml.safe_load(text) - if document.get("source_commit"): - return False - new_text = insert_after_line(text, "profiled_date", f'source_commit: "{sha}"') - expected = dict(document) - expected["source_commit"] = sha - apply_edit(path, new_text, expected) - return True - - -def _is_rewritable(ref: str) -> bool: - """Every ref whose parts can be put back with their prose intact.""" - parts = split_source_ref(ref) - if not parts: - return False - return ", ".join(p.raw for p in parts) == ", ".join( - chunk for chunk in split_outside_parentheses(ref) - ) - - -def _match_citation( - citations: list[Citation], pos: int, entry: EntryReport -) -> tuple[Citation | None, int]: - """First document citation at or after pos carrying the entry's ref. - - The pairing is by content and order, never by position alone: a - mode-keyed source_ref yields two report entries for one document line, - which is exactly the desynchronisation a positional pairing trips on. - """ - for index in range(pos, len(citations)): - citation = citations[index] - if citation.kind != entry.kind or citation.ref != entry.source_ref: - continue - if entry.kind == "prose" and entry.field and citation.field != entry.field: - continue - return citation, index + 1 - return None, pos - - -def _prose_moves( - entry: EntryReport, statuses: tuple[str, ...] -) -> dict[int, tuple[int, int, str | None]]: - """Recale targets per part of one prose run, empty when unsafe. - - A run cites one file: parts that disagree on where that file went, or a - rename that would leave untouched ranges pointing into the old file, - cannot be written without guessing. - """ - moves: dict[int, tuple[int, int, str | None]] = {} - renames = set() - for index, part in enumerate(entry.parts): - if part.status not in statuses or part.start is None: - continue - same_place = (part.start, part.end) == (part.part.start, part.part.end) - if same_place and not part.new_path: - continue - moves[index] = (part.start, part.end or part.start, part.new_path) - if part.new_path: - renames.add(part.new_path) - if len(renames) > 1: - return {} - if renames and len(moves) != len(entry.parts): - return {} - return moves - - -_RANGE_TOKEN_RE = re.compile(r"\d+(?:-\d+)?") - - -def _run_after_moves( - parts: Sequence[RefPart], - moves: dict[int, tuple[int, int, str | None]], - run: str = "", -) -> str: - """The prose run as it reads once its moved ranges are rewritten. - - The path is the one written in the run: a part can carry a project word - or the tree path the report resolved it to, neither of which the - sentence spells. - """ - new_path = next( - (move[2] for move in moves.values() if move[2]), None - ) or (run.split(":", 1)[0] if run else None) - ranges = [] - for index, part in enumerate(parts): - if index in moves: - start, end, _ = moves[index] - ranges.append(f"{start}" if end == start else f"{start}-{end}") - else: - ranges.append(part.raw) - # The separators stay as written. boom3 writes `libretro.cpp:1635-1643, - # 2384-2405` in a folded scalar, rewritten whole there, and a joined - # `3238-3246,4245-4266` no longer read as the intended document. - written = run.partition(":")[2] - tokens = list(_RANGE_TOKEN_RE.finditer(written)) - if len(tokens) == len(parts) and all( - token.group() == part.raw for token, part in zip(tokens, parts) - ): - for token, rendered in reversed(list(zip(tokens, ranges))): - written = written[:token.start()] + rendered + written[token.end():] - return f"{new_path or parts[0].path}:{written}" - return f"{new_path or parts[0].path}:{','.join(ranges)}" - - -def _unique_line( - lines: list[str], target: str, window: tuple[int, int] -) -> int | None: - """The one line of the window carrying target, None when not exactly one.""" - hits = [ - i for i in range(window[0], min(window[1], len(lines) - 1) + 1) - if target and target in lines[i] - ] - return hits[0] if len(hits) == 1 else None - - -def scalar_windows( - text: str, document: object -) -> dict[tuple[int, object], tuple[int, int]]: - """Physical lines each scalar spans, keyed by its holder and key. - - The same sentence can sit in two fields: emucorex cites - CMakeLists.txt:706 in notes and in exclusion_note, on identical lines. - Searched over the whole file neither occurrence was unique, and only - the node's own lines tell them apart. - """ - try: - root = yaml.compose(text, Loader=yaml.SafeLoader) - except yaml.YAMLError: - return {} - windows: dict[tuple[int, object], tuple[int, int]] = {} - - def walk(value: object, node: yaml.Node | None) -> None: - if isinstance(value, dict) and isinstance(node, yaml.MappingNode): - by_key = {str(k.value): v for k, v in node.value} - for key, item in value.items(): - child = by_key.get(str(key)) - if child is None: - continue - windows[(id(value), key)] = ( - child.start_mark.line, child.end_mark.line - ) - walk(item, child) - elif isinstance(value, list) and isinstance(node, yaml.SequenceNode): - for index, (item, child) in enumerate(zip(value, node.value)): - windows[(id(value), index)] = ( - child.start_mark.line, child.end_mark.line - ) - walk(item, child) - - walk(document, root) - return windows - - -def _scalar(holder: object, key: object) -> object: - """The value a citation's holder keeps under its key.""" - if isinstance(holder, list) and isinstance(key, int): - return holder[key] - if isinstance(holder, dict): - return holder[key] - raise TypeError(f"no scalar under {key!r} in a {type(holder).__name__}") - - -def _set_scalar(holder: object, key: object, value: str) -> None: - """Replace the value a citation's holder keeps under its key.""" - if isinstance(holder, dict): - holder[key] = value - return - if not isinstance(holder, list) or not isinstance(key, int): - raise TypeError(f"no scalar under {key!r} in a {type(holder).__name__}") - holder[key] = value - - -Span = tuple[int, int, str] - - -@dataclass -class _ProseEdits: - """Replacements gathered over a file and its loaded scalars, and what - was written or left.""" - - lines: list[str] - windows: dict[tuple[int, object], tuple[int, int]] - whole: tuple[int, int] - file_edits: dict[int, list[Span]] = field(default_factory=dict) - value_edits: dict[tuple[int, object], dict[int, list[Span]]] = field(default_factory=dict) - holders: dict[tuple[int, object], tuple[object, object]] = field(default_factory=dict) - applied: list[str] = field(default_factory=list) - left: list[str] = field(default_factory=list) - - -def _prose_replacements( - citation: Citation, moves: dict[int, tuple[int, int, str | None]] -) -> list[Span]: - """The tokens of one run a recale rewrites, measured on its line.""" - replacements: list[Span] = [ - (*citation.spans[index], f"{start}" if end == start else f"{start}-{end}") - for index, (start, end, _) in moves.items() - ] - new_path = next((m[2] for m in moves.values() if m[2]), None) - if new_path: - replacements.append((*citation.path_span, new_path)) - return replacements - - -def _edit_comment_run( - edits: _ProseEdits, citation: Citation, replacements: list[Span], recaled: str -) -> None: - # The comment is read again from the text being written: a run - # whose tokens no longer sit at the collected offsets is left. - lines = edits.lines - line = lines[citation.line] if citation.line < len(lines) else "" - expected = [p.raw for p in citation.parts] + [citation.ref.split(":", 1)[0]] - spans = list(citation.spans) + [citation.path_span] - if any(line[lo:hi] != token for (lo, hi), token in zip(spans, expected)): - edits.left.append(f"{citation.field}: {citation.ref} (comment changed)") - return - edits.file_edits.setdefault(citation.line, []).extend(replacements) - edits.applied.append(f"{citation.field}: {citation.ref} -> {recaled}") - - -def _split_by_line( - citation: Citation, value_lines: list[str], last: int, replacements: list[Span] -) -> dict[int, list[Span]] | None: - """Replacements by the scalar line holding them, None when a token - spans the line break.""" - # A wrapped run puts its spans back on the two scalar lines it - # joined: playdiaemu wrote `content.rs` and `:111-199` one line apart. - by_line: dict[int, list[Span]] = {} - for lo, hi, new in replacements: - if citation.wrapped and lo >= citation.joined_at: - following = value_lines[last] - indent = len(following) - len(following.lstrip()) - shift = indent - citation.joined_at - by_line.setdefault(last, []).append((lo + shift, hi + shift, new)) - elif citation.wrapped and hi > citation.joined_at: - return None - else: - by_line.setdefault(citation.line, []).append((lo, hi, new)) - return by_line - - -def _lines_apart(located: dict[int, int | None], first: int, last: int) -> bool: - """Whether the two lines of a wrapped run are not found together.""" - if None in located.values(): - return True - if len(located) != 2: - return False - low, high = located[first], located[last] - return low is None or high is None or high != low + 1 - - -def _edit_folded_run( - edits: _ProseEdits, citation: Citation, value_lines: list[str], - window: tuple[int, int], recaled: str, -) -> bool: - """Rewrite a run whose scalar is folded, False when it is left.""" - # A folded scalar has no physical line carrying its logical one. - # The run itself is a single word, which folding never breaks: - # when the whole file carries it exactly once, that occurrence - # is the citation, and the run is rewritten as one token. The - # match is delimited, or `x.c:481` would also hit inside a - # neighbouring `x.c:481-482`. - lines = edits.lines - pattern = re.compile( - rf"(? None: - value = str(_scalar(citation.holder, citation.key)) - value_lines = value.splitlines() - last = citation.line + (1 if citation.wrapped else 0) - if last >= len(value_lines): - edits.left.append(f"{citation.field}: {citation.ref} (scalar changed)") - return - by_line = _split_by_line(citation, value_lines, last, replacements) - if by_line is None: - edits.left.append(f"{citation.field}: {citation.ref} (a token spans the line break)") - return - window = edits.windows.get((id(citation.holder), citation.key), edits.whole) - located = { - number: _unique_line(edits.lines, value_lines[number], window) - for number in by_line - } - if citation.wrapped and _lines_apart(located, citation.line, last): - edits.left.append( - f"{citation.field}: {citation.ref} " - "(wrapped, and its two lines are not found together in the file)" - ) - return - placed = {number: hit for number, hit in located.items() if hit is not None} - if len(placed) == len(located): - for number, line_edits in by_line.items(): - hit = placed[number] - offset = edits.lines[hit].index(value_lines[number]) - edits.file_edits.setdefault(hit, []).extend( - (offset + lo, offset + hi, new) for lo, hi, new in line_edits - ) - elif not _edit_folded_run(edits, citation, value_lines, window, recaled): - return - slot = (id(citation.holder), citation.key) - edits.holders[slot] = (citation.holder, citation.key) - for number, line_edits in by_line.items(): - edits.value_edits.setdefault(slot, {}).setdefault(number, []).extend(line_edits) - edits.applied.append(f"{citation.field}: {citation.ref} -> {recaled}") - - -def _apply_prose_edits( - text: str, jobs: list[tuple[Citation, dict]], document: object = None -) -> tuple[str, list[str], list[str]]: - """Rewrite prose citation tokens in place, nothing else. - - Each scalar line a run touches is located as the unique physical line - carrying it. Two candidate lines, or none, means the write cannot be - proved right, so the run is left alone and reported with the reason. A - comment run is rewritten on its own physical line, once the tokens there - read as collected. All replacements of one line are applied together, - right to left, so the spans measured on the original text stay valid. - """ - lines = text.splitlines() - trailing = "\n" if text.endswith("\n") else "" - edits = _ProseEdits( - lines, - scalar_windows(text, document) if document is not None else {}, - (0, len(lines) - 1), - ) - - for citation, moves in jobs: - replacements = _prose_replacements(citation, moves) - recaled = _run_after_moves(citation.parts, moves, citation.ref) - if citation.in_comment: - _edit_comment_run(edits, citation, replacements, recaled) - else: - _edit_scalar_run(edits, citation, replacements, recaled) - - for number, spans in edits.file_edits.items(): - lines[number] = _replace_spans(lines[number], spans) - for slot, by_line in edits.value_edits.items(): - holder, key = edits.holders[slot] - value = str(_scalar(holder, key)) - value_lines = value.splitlines() - for line_number, spans in by_line.items(): - value_lines[line_number] = _replace_spans(value_lines[line_number], spans) - _set_scalar(holder, key, "\n".join(value_lines) + ( - "\n" if value.endswith("\n") else "" - )) - return "\n".join(lines) + trailing, edits.applied, edits.left - - -def _replace_spans(line: str, replacements: list[tuple[int, int, str]]) -> str: - """Apply span replacements measured on the original line.""" - for lo, hi, new in sorted(replacements, reverse=True): - line = line[:lo] + new + line[hi:] - return line - - -def _part_moves(part: PartResult, movable: tuple[str, ...]) -> bool: - """Whether a part has a new location the recale would write.""" - return ( - part.status in movable - and part.start is not None - and ((part.start, part.end) != (part.part.start, part.part.end) - or bool(part.new_path)) - ) - - -def pin_blockers(report: ProfileReport, accept_changed: bool = False) -> list[str]: - """What would keep source_commit where it is after a recale, named. - - Two kinds. A ref the writer never regenerates, annotated or under a mode - key, whose parts have moved. And a prose run whose parts disagree on - where its file went, which cannot be written without guessing. Either - one makes bump_commit refuse, so either one has to stop the recale as - well, and the caller says which. - """ - movable = REBASE_STATUSES + (("CHANGED",) if accept_changed else ()) - blockers: list[str] = [] - for entry in report.entries or []: - if entry.kind == "prose": - if not _prose_moves(entry, movable) and any( - _part_moves(part, movable) for part in entry.parts - ): - blockers.append( - f"{entry.field}: {entry.source_ref} " - "(its parts do not agree on one new file)" - ) - continue - if not any( - part.status in movable - and part.start is not None - and _rendered_part(part) != _original_part(part) - for part in entry.parts - ): - continue - if entry.name.endswith("]"): - blockers.append( - f"{entry.name}: {entry.source_ref} (under a mode key, moved by hand)" - ) - elif not _is_rewritable(entry.source_ref): - blockers.append( - f"{entry.name}: {entry.source_ref} " - "(annotated, a rewrite would drop the annotation)" - ) - return blockers - - -def _view_role(view: RepoView) -> str: - """source or upstream: the profile field a repository comes from.""" - return "upstream" if view.field == "upstream" else "source" - - -def _answers(view: RepoView, role: str) -> bool: - """Whether a part written under *role* means this repository: the field - it comes from, or the build mode a keyed source declares it under.""" - return bool(role) and role in (_view_role(view), view.mode) - - -ATTRIBUTED_STATUSES = ("ANCHORED", "SHIFTED", "RENAMED", "MOVED", "CHANGED") - - -def attribute_refs(path: Path, report: ProfileReport, profile: dict) -> list[str]: - """Open each cited path with the role of the repository its lines are in. - - A profile reading a libretro port and the original emulator cited both - trees the same way, and a reader could not tell whether the core or the - emulator asked for the file. The role is the one this comparison found - the lines in, so a word already written that disagrees is corrected. - Parts whose lines were found nowhere keep what they have. - """ - if len(report.repos) < 2: - return [] - role_of: dict[str, str] = {} - for field_name, _mode, url in declared_repositories(profile): - repo = upstream.parse_repo(url) - if repo is not None: - role_of.setdefault(repo.slug, "source" if field_name == "source_mirror" - else field_name) - if len(set(role_of.values())) < 2: - return [] - primary = role_of.get(report.repos[0], "source") - text = path.read_text(encoding="utf-8") - applied: list[str] = [] - cursor = 0 - for entry in report.entries: - if entry.kind != "ref" or entry.name.endswith("]"): - continue - if not _is_rewritable(entry.source_ref): - continue - rendered = [] - for part in entry.parts: - raw = part.part.raw or _original_part(part) - role, bare = ref_role(raw) - written_path = bare.startswith(part.part.path) - owner = role_of.get(part.repo or "", primary) if part.repo else primary - if part.status in ATTRIBUTED_STATUSES and written_path and owner != role: - rendered.append(f"{owner} {bare}") - else: - rendered.append(raw) - new_ref = ", ".join(rendered) - if new_ref == entry.source_ref: - continue - try: - text, index = replace_field_line( - text, "source_ref", entry.source_ref, new_ref, cursor - ) - except YamlWriteError: - continue - cursor = index + 1 - applied.append(f"{entry.source_ref} -> {new_ref}") - if applied: - path.write_text(text, encoding="utf-8") - return applied - - -def _moved_by_hand(entry: EntryReport, statuses: tuple[str, ...]) -> bool: - """Whether a ref the writer cannot regenerate has a part to move.""" - return any( - part.status in statuses - and part.start is not None - and _rendered_part(part) != _original_part(part) - for part in entry.parts - ) - - -def _recaled_ref(entry: EntryReport, statuses: tuple[str, ...]) -> str | None: - """The source_ref with its moved parts rendered, None when none moves.""" - rendered = [] - touched = False - for part in entry.parts: - if part.status in statuses and part.start is not None: - recaled = _rendered_part(part) - if recaled != _original_part(part): - touched = True - rendered.append(recaled) - else: - rendered.append(_original_part(part)) - return ", ".join(rendered) if touched else None - - -def _recale_statuses( - path: Path, report: ProfileReport, accept_changed: bool -) -> tuple[str, ...] | None: - """The statuses a recale moves, None when the profile must not move.""" - if report.pinned_tag: - return None - statuses = REBASE_STATUSES + (("CHANGED",) if accept_changed else ()) - blocking = [s for s in REVIEW_STATUSES if s not in statuses] - if any((report.counts or {}).get(s) for s in blocking): - return None - blockers = pin_blockers(report, accept_changed) - if blockers: - # Something here will not move: an annotated ref, one under a mode - # key, or a prose run this pass cannot rewrite without guessing. - # bump_commit will refuse while it stands, and recaling the rest - # would leave the profile describing two revisions at once. All or - # nothing means the pin too, not only the refs. - _refuse_recale(path, blockers) - return None - return statuses - - -def rebase_refs( - path: Path, report: ProfileReport, accept_changed: bool = False -) -> list[str]: - """Recale the line ranges of parts whose content is unchanged. - - All or nothing per profile. `source_commit` names the revision the refs - are written against, so moving some refs to HEAD while others still - describe the pinned revision would leave the profile self-contradictory, - and the next comparison would read the pinned revision at line numbers - that only make sense at HEAD. Prose citations move with everything else: - only their located tokens are rewritten, the sentence stays. - """ - statuses = _recale_statuses(path, report, accept_changed) - if statuses is None: - return [] - text = path.read_text(encoding="utf-8") - document = yaml.safe_load(text) - citations = collect_citations(document, text) - applied: list[str] = [] - refused: list[str] = [] - cursor = 0 - pos = 0 - prose_jobs: list[tuple[Citation, dict]] = [] - - for entry in report.entries: - citation, pos = _match_citation(citations, pos, entry) - if citation is None: - if any(_part_moves(part, statuses) for part in entry.parts): - refused.append( - f"{entry.field or entry.name}: {entry.source_ref} " - "(the profile no longer carries it as reported)" - ) - continue - if entry.kind == "prose": - moves = _prose_moves(entry, statuses) - if moves: - prose_jobs.append((citation, moves)) - continue - if citation.label or entry.name.endswith("]"): - # The ref lives under a mode key, not on a source_ref line. The - # entry name check also holds when the report and the document - # disagree: a stale report must not touch a line it never read. - if _moved_by_hand(entry, statuses): - refused.append( - f"{entry.name}: {entry.source_ref} (under a mode key, moved by hand)" - ) - continue - # Rewriting an annotated ref would drop the author's annotations. - # It, like a ref with nothing to move, still consumes its line so a - # later entry carrying the same ref cannot match it. - new_ref = _recaled_ref(entry, statuses) if _is_rewritable(entry.source_ref) else None - if new_ref is None: - cursor = find_field_line(text, "source_ref", entry.source_ref, cursor) + 1 - continue - text, index = replace_field_line( - text, "source_ref", entry.source_ref, new_ref, cursor - ) - cursor = index + 1 - _set_scalar(citation.holder, citation.key, new_ref) - applied.append(f"{entry.source_ref} -> {new_ref}") - - text, prose_applied, left = _apply_prose_edits(text, prose_jobs, document) - refused.extend(left) - if refused: - # A citation left on the pinned lines holds the pin, so writing the - # others would leave the profile describing two revisions. - _refuse_recale(path, refused) - return [] - applied.extend(prose_applied) - if applied: - apply_edit(path, text, document) - return applied - - -def _refuse_recale(path: Path, reasons: list[str]) -> None: - """Name each citation that keeps a recale from being written.""" - for reason in reasons: - print(f"{path.stem}: not recaled, cannot move {reason}", file=sys.stderr) - - -def _citation_key(ref: str) -> str: - """A citation compared without the spacing the author chose. - - A recale rewrites the located tokens and leaves the sentence alone, so - `a.c:228, 1439-1443` keeps its space while the rendered form drops it. - They are the same citation, and comparing them literally makes a run - that was written look unwritten, which holds the pin back for ever. - """ - return re.sub(r"\s+", "", ref) - - -def _pending_refs( - report: ProfileReport, movable: tuple[str, ...], document: dict | None -) -> int: - """Parts of structured refs that ought to move and have not.""" - # A ref the writer regenerates is pending only while the file still - # carries it as written: a bump run without the recale left lrps2's - # SHIFTED refs on the old lines under the new pin. It is looked up at - # its own field, never by its text alone: tr1x moved cut2 508->531 and - # cut3 531->552, and cut3 read as pending on cut2's new value. - refs_left: dict[tuple[str, str], int] = {} - if document is not None: - for citation in collect_citations(document): - if citation.kind == "ref": - for slot in ((citation.field, citation.ref), ("", citation.ref)): - refs_left[slot] = refs_left.get(slot, 0) + 1 - pending = 0 - for entry in report.entries or []: - if entry.kind == "prose": - continue - if _is_rewritable(entry.source_ref) and not entry.name.endswith("]"): - slot = (entry.field, entry.source_ref) - if not refs_left.get(slot): - continue - refs_left[slot] -= 1 - pending += sum( - 1 - for part in entry.parts - if part.status in movable - and part.start is not None - and _rendered_part(part) != _original_part(part) - ) - return pending - - -def _pending_prose( - report: ProfileReport, movable: tuple[str, ...], document: dict, text: str | None -) -> int: - """Prose runs that ought to move and that the file does not carry moved.""" - pool: dict[tuple[str, str], int] = {} - for citation in collect_citations(document, text): - if citation.kind == "prose": - slot = (citation.field, _citation_key(citation.ref)) - pool[slot] = pool.get(slot, 0) + 1 - pending = 0 - for entry in report.entries or []: - if entry.kind != "prose": - continue - moves = _prose_moves(entry, movable) - if not moves: - # Either nothing has to move, or the run cannot be written - # without guessing, and the second case still blocks the pin. - if any(_part_moves(part, movable) for part in entry.parts): - pending += 1 - continue - rendered = _run_after_moves( - [p.part for p in entry.parts], moves, entry.source_ref - ) - if _citation_key(rendered) == _citation_key(entry.source_ref): - continue - slot = (entry.field, _citation_key(rendered)) - if pool.get(slot, 0) > 0: - pool[slot] -= 1 - continue - pending += 1 - return pending - - -def pending_recale( - report: ProfileReport, accept_changed: bool = False, text: str | None = None -) -> int: - """Parts that ought to move but the writer has not moved. - - An annotated ref cannot be regenerated without losing its prose, so its - parts stay on the pinned line numbers. Advancing the pin while they do - would leave the profile describing two revisions at once. - - Prose runs are judged on the file as it stands: a run that should move - counts as pending until the document actually carries its recaled form, - so the pin cannot advance over prose that still describes the old - revision, whatever the reason it was not rewritten. - """ - movable = REBASE_STATUSES + (("CHANGED",) if accept_changed else ()) - document = yaml.safe_load(text) if text is not None else None - pending = _pending_refs(report, movable, document) - if document is None: - return pending - return pending + _pending_prose(report, movable, document, text) - - -NO_SOURCE_REF = "no source_ref" - - -def _needs_review(report: ProfileReport, accept_changed: bool) -> bool: - """Whether the report leaves a ref to read before the pin may move.""" - # A profile citing nothing has nothing to read again: anarch's pin sat - # behind a CI-only commit through every bulk advance. - if report.skipped not in (None, NO_SOURCE_REF) or not report.head or report.pinned_tag: - return True - blocking = REVIEW_STATUSES if not accept_changed else ( - s for s in REVIEW_STATUSES if s != "CHANGED" - ) - return any((report.counts or {}).get(s) for s in blocking) - - -@functools.lru_cache(maxsize=4) -def _platform_cores(emulators_dir: str, platforms_dir: str) -> tuple[tuple[str, frozenset[str]], ...]: - profiles = load_emulator_profiles(emulators_dir) - return tuple( - (platform, frozenset(resolve_platform_cores( - load_platform_config(platform, platforms_dir), profiles - ))) - for platform in list_registered_platforms(platforms_dir) - ) - - -def shipping_platforms( - name: str, emulators_dir: str, platforms_dir: str = "platforms", -) -> list[str]: - """The registered platforms whose cores resolve to this profile.""" - return [ - platform - for platform, cores in _platform_cores( - os.path.realpath(emulators_dir), platforms_dir - ) - if name in cores - ] - - -def bump_commit( - path: Path, report: ProfileReport, accept_changed: bool = False, - cache_dir: str = DEFAULT_CACHE, offline: bool = False, - platforms_dir: str = "platforms", -) -> bool: - """Advance source_commit to HEAD when nothing needs a read again. - - core_version moves with the pin, read again from version_basis at the - new revision. Four bulk advances moved 295 pins and no version: an - advance whose version cannot be read is refused. - """ - if not report.head or _needs_review(report, accept_changed): - return False - text = path.read_text(encoding="utf-8") - if pending_recale(report, accept_changed, text): - return False - document = yaml.safe_load(text) - if document.get("built_by") and not document.get("pin_follows"): - # The pin is the commit a platform builds (recalbox gsplus.mk, the - # mame0258 its package fetches), not a point on a moving branch: - # HEAD is a build nobody ships under this name. A build of the - # branch tip (RetroPie's advmame) is HEAD, and follows it. - print( - f"{path.stem}: pinned to the build {', '.join(document['built_by'])} " - "ships, advanced by hand", - file=sys.stderr, - ) - return False - if not (document.get("pin_frozen") or document.get("pin_follows")): - shipped = shipping_platforms(path.stem, str(path.parent), platforms_dir) - if len(shipped) == 1: - # One platform ships it, so the pin is the revision that platform - # builds: a bulk advance moved fheroes2, supertux and - # supermariowar past what EmuELEC compiles, and rigelengine is - # already one commit ahead of its package.mk. - print( - f"{path.stem}: only {shipped[0]} ships it. Read the revision its " - "build compiles and set pin_frozen, or pin_follows when it " - "builds the branch tip", - file=sys.stderr, - ) - return False - if any(isinstance(document.get(f), dict) for f in ("source_commit", "upstream_commit")): - # A pin per build mode names one revision per repository, and the - # report heads only the primary one: rewriting str(dict) was a - # refused write on mesence, supermodel and xemu. - print( - f"{path.stem}: pins keyed by build mode are advanced by hand", - file=sys.stderr, - ) - return False - # An upstream pinned on a release tag is the base the port was cut from: - # its HEAD is another program, so the bump leaves it on the tag. - # A profile citing nothing compared nothing upstream: writing that HEAD - # would claim gme's vendored libgme or hbmame's port is current. - upstream_moves = bool( - report.upstream_head - and report.upstream_pin != report.upstream_head - and not report.upstream_tag - and report.skipped != NO_SOURCE_REF - ) - if document.get("source_commit") == report.head and not upstream_moves: - # Rewriting the pin to the value it already holds is not a bump, and - # announcing it buries the profiles that did move. - return False - if ( - document.get("source_commit") != report.head - and not cites_primary(report) - ): - # Every ref anchored in another repository: HEAD of this one was - # never compared with anything. gam4980's pin moved to the fork's - # HEAD while its bare libretro.c refs still described upstream. - print( - f"{path.stem}: no ref resolves in {report.repo}, its pin is not advanced", - file=sys.stderr, - ) - return False - expected = dict(document) - expected["source_commit"] = report.head - new_text = _set_pin(text, document, "source_commit", report.head, "profiled_date") - if document.get("pin_follows") and report.head: - new_text, expected = _follow_config_reads(new_text, expected, document, report.head) - if upstream_moves and report.upstream_head: - expected["upstream_commit"] = report.upstream_head - new_text = _set_pin( - new_text, document, "upstream_commit", report.upstream_head, "source_commit" - ) - readings = version_basis.check(expected, cache_dir, offline, recompute=True) - refused = version_refusal(readings) - if refused: - print(f"{path.stem}: {refused}, its pin is not advanced", file=sys.stderr) - return False - version, basis = apply_readings(expected, readings) - if version != expected.get("core_version") or basis != expected.get("version_basis"): - new_text = set_version_fields(new_text, version, basis) - expected["core_version"] = version - if basis is not None: - expected["version_basis"] = basis - apply_edit(path, new_text, expected) - return True - - -def version_refusal(readings: list) -> str | None: - """Why the version at a new pin cannot be stated, or None.""" - for reading in readings: - if reading.status == version_basis.NO_BASIS: - return "no version_basis says where its version is read" - if reading.status == version_basis.UNREADABLE: - return f"core_version cannot be read at the new pin: {reading.where}" - if reading.status == version_basis.UNCHECKED: - return f"core_version is {reading.where}, not read from the code at a pin" - return None - - -def pins_changed(before: dict, after: dict) -> list[str]: - """The pin fields whose value a write changed.""" - return [ - pin for pin in ("source_commit", "upstream_commit") - if after.get(pin) and after.get(pin) != before.get(pin) - ] - - -def cites_primary(report: ProfileReport) -> bool: - """Whether a ref resolved in the repository source_commit pins. - - A part attributed to another declared repository carries its slug, and one - read in the primary carries none. A profile citing nothing proves - nothing either way and is not held back. - """ - if len(report.repos) < 2: - return True - parts = [ - part for entry in report.entries for part in entry.parts - if part.status != "EXTERNAL" - ] - return not parts or any(part.repo is None for part in parts) - - -def _follow_config_reads( - text: str, expected: dict, document: dict, head: str -) -> tuple[str, dict]: - """A config_reads entry read at the pin moves with a pin that follows the - branch tip: RetroPie installs advmame from the tip, so the build its - advmame.rc is read at is the pin itself.""" - pin = str(document.get("source_commit") or "") - reads = document.get("config_reads") or [] - if not pin or not any(isinstance(r, dict) and r.get("revision") == pin for r in reads): - return text, expected - text = re.sub( - r'^(\s*revision:\s*)(["\']?)' + re.escape(pin) + r"\2\s*$", - lambda m: f"{m.group(1)}{m.group(2)}{head}{m.group(2)}", - text, flags=re.M, - ) - expected = {**expected, "config_reads": [ - {**r, "revision": head} if isinstance(r, dict) and r.get("revision") == pin else r - for r in reads - ]} - return text, expected - - -def _set_pin(text: str, document: dict, field: str, value: str, after: str) -> str: - """Write *field* as *value*, replacing its line or adding one after *after*.""" - current = document.get(field) - if current == value: - return text - if current: - return replace_field_line(text, field, str(current), value)[0] - return insert_after_line(text, after, f'{field}: "{value}"') - - -def _git_history(path: Path) -> list[str] | None: - """Commits touching the profile, newest first, or None when git cannot say. - - An untracked profile has no history. A git that fails (no repository, - dubious ownership, git missing) has not answered, and reading that as - "nothing written under an older pin" let a bump through. - """ - def git(*argv: str) -> subprocess.CompletedProcess: - return subprocess.run( - ["git", *argv], cwd=path.parent, capture_output=True, text=True, check=False, - ) - - try: - result = git("log", "--format=%H", "--", path.name) - deleted = git("log", "--format=%H", "--diff-filter=D", "--", path.name) - if result.returncode == 0 and deleted.returncode == 0: - # A commit that deletes or renames the file away ends the history - # of the profile now at that path: what precedes it is another - # profile once filed under the same name (mupen64plus.yml became - # mupen64plus_next.yml before the standalone took the name). - history = result.stdout.split() - ends = set(deleted.stdout.split()) - cut = next((i for i, sha in enumerate(history) if sha in ends), len(history)) - return history[:cut] - # A repository with no commit yet holds nothing written earlier. - inside = git("rev-parse", "--is-inside-work-tree") - if inside.stdout.strip() == "true" and git("rev-parse", "-q", "--verify", "HEAD").returncode: - return [] - except OSError: - pass - return None - - -def _git_file_at(path: Path, sha: str) -> dict | None: - """The profile as it was at one commit, None when unreadable.""" - result = subprocess.run( - ["git", "show", f"{sha}:./{path.name}"], - cwd=path.parent, capture_output=True, text=True, check=False, - ) - if result.returncode != 0: - return None - try: - document = yaml.safe_load(result.stdout) - except yaml.YAMLError: - return None - return document if isinstance(document, dict) else None - - -def _pin_of( - document: dict, field: str, mode: str = "", - repo: upstream.Repo | None = None, -) -> str | None: - """A pin as the profile records it, read for one build mode when keyed. - - With `repo`, a pin the document declares for another repository under - the field and mode does not count. A past revision can pin another - repository under the same field: xenia-edge's source_commit named the - has207/xenia-edge sha until the field became a dict keyed by mode, and - read for the libretro mode that old string passed for a pin of the - GitLab port that moved under unchanged prose. - """ - value = document.get(field) - if isinstance(value, dict): - value = value.get(mode) if mode else None - if not (isinstance(value, str) and value): - return None - if repo is not None: - url_field = field.removesuffix("_commit") - declared = { - (mode_key, url) - for name, mode_key, url in declared_repositories(document) - if name == url_field - } - url = next( - (u for m, u in declared if m == mode), - next((u for m, u in declared if not m), None), - ) - owner = upstream.parse_repo(url) if url else None - if owner is not None and (owner.host, owner.slug.lower()) != ( - repo.host, repo.slug.lower() - ): - return None - return value - - -def _writing_pins( - revisions: list[tuple[str, dict]], intro_sha: str, field: str, mode: str = "", - repo: upstream.Repo | None = None, -) -> list[str]: - """Distinct pins committed while the scalar held its current text. - - The pin at the introducing commit stands for the scalars that predate - it (the backfill resolved missing pins from profiled_date). A pin that - moved later under the same text is either a bump that left the prose - behind or a correction putting the pin on the revision the text already - described: vitaquake2 was written under 34f0888, then re-pinned to - 59a5115 by a commit that touched nothing else. The history cannot tell - the two apart, so every pin the text lived under is returned. - """ - index = next( - (i for i, (sha, _) in enumerate(revisions) if sha == intro_sha), None - ) - if index is None: - return [] - pins: list[str] = [] - for position in range(index, -1, -1): - value = _pin_of(revisions[position][1], field, mode, repo) - if value and value not in pins: - pins.append(value) - return pins - - -def _resolve_at( - repo: upstream.Repo, sha: str, path: str, cache_dir: str, offline: bool -) -> tuple[str, tuple[str, list[str]] | str | None]: - """("found", (path, lines)) or ("unclear"|"missing", reason). - - Prose often cites a bare filename. The tree at the revision resolves it - when exactly one path carries that name. An unreadable or truncated - tree, or several paths carrying the name, proves nothing: those come - back "unclear", never "missing", because a verdict of absence taken on - a tree that could not be read would rot the citation with confidence. - """ - candidates = [path] - head, _, tail = path.partition("/") - if tail and head == repo.name: - candidates.append(tail) - for candidate in candidates: - lines = upstream.fetch_file(repo, sha, candidate, cache_dir, offline) - if lines is not None: - return "found", (candidate, lines) - if "/" not in path: - tree, truncated = upstream.list_tree(repo, sha, cache_dir, offline) - matches = [p for p in tree or [] if posixpath.basename(p) == path] - if len(matches) == 1: - lines = upstream.fetch_file( - repo, sha, matches[0], cache_dir, offline - ) - if lines is not None: - return "found", (matches[0], lines) - elif len(matches) > 1: - return "unclear", f"{len(matches)} paths carry the name" - elif truncated or not tree: - return "unclear", "tree unreadable at that revision" - return "missing", None - - -def _realign_part( - part: RefPart, - pairs: list[tuple[upstream.Repo, str, str]], - cache_dir: str, - offline: bool, - pinned: Iterable[tuple[upstream.Repo, str]] = (), -) -> tuple[str, tuple[int, int] | str] | None: - """One range, anchored from its writing revision to the current pin. - - `pinned` lists every declared (repository, current pin). A path no moved - repository carries but an unmoved one does lives where nothing moved: - it needs no move, and calling it absent made two readings of the same - history disagree on every run (nethersx2's AetherSX2 citations). - """ - if is_external_citation(part.path): - return None - # The path as written decides first, as attribution does: a prefix is - # stripped only when no declared repository carries the path itself. - # hatarib's hatari/src/tos.c lives in hatariB, which did not move, and - # stripping the prefix read it as hatari/hatari's src/tos.c instead. - # Between two repositories that both carry the path, the one declared - # first owns it, as in attribution: freej2me-plus and hex007/freej2me - # both hold src/libretro/, and the citations describe the fork. - literal = [ - (repo, written, current) for repo, written, current in pairs - if upstream.fetch_file(repo, written, part.path, cache_dir, offline) is not None - ] - moved = {repo for repo, _written, _current in pairs} - order = [repo for repo, _pin in pinned] - first_moved = min( - (order.index(repo) for repo, _w, _c in literal if repo in order), - default=len(order), - ) - if any( - upstream.fetch_file(repo, pin, part.path, cache_dir, offline) is not None - for index, (repo, pin) in enumerate(pinned) - if repo not in moved and (not literal or index < first_moved) - ): - return None - unclear: tuple[str, list[str]] | str | None = None - for repo, written, current in literal or pairs: - state, payload = _resolve_at(repo, written, part.path, cache_dir, offline) - if state == "unclear": - unclear = payload - continue - if not isinstance(payload, tuple): - continue - actual, old_lines = payload - new_lines = upstream.fetch_file( - repo, current, actual, cache_dir, offline - ) - if new_lines is None: - return "skip", f"{part.path} absent at the current pin" - anchored = anchor_block( - old_lines, new_lines, part.start, part.end or part.start - ) - if anchored.status == "ANCHORED": - return None - if anchored.status == "SHIFTED" and anchored.start is not None and anchored.end is not None: - return "ok", (anchored.start, anchored.end) - return "skip", f"{part.path}:{part.start} {anchored.status.lower()}" - if unclear: - return "skip", f"{part.path}: {unclear}" - if any( - upstream.fetch_file(repo, pin, part.path, cache_dir, offline) is not None - for repo, pin in pinned - if repo not in moved - ): - return None - return "skip", f"{part.path} absent at the writing revision" - - -def _writing_pairs( - document: dict, repos: list, revisions: list[tuple[str, dict]], intro_sha: str, - served: dict | None = None, -) -> tuple[list[list], list[str]]: - """Every reading of where the text was written, and the ambiguities. - - Each reading lists (repo, writing pin, current pin) per declared - repository. A repository whose text lived under several pins gives one - reading per pin, and the caller settles them when they all agree. - - `prose_written_at` names the pin a reader confirmed the prose against. - Among several candidate pins it is the answer: fixnes's doOpenFDSBIOS - citation lived under four pins, exact at the last, and with nothing to - record that reading the bump stayed refused for good. It never - overrides a history with a single candidate. - """ - options: list[list] = [] - ambiguous: list[str] = [] - attested = str(document.get("prose_written_at") or "") - for pin_field, mode, repo in repos: - current = _pin_of(document, f"{pin_field}_commit", mode) - if not current: - continue - written = _writing_pins( - revisions, intro_sha, f"{pin_field}_commit", mode, repo - ) - if attested in written: - written = [attested] - if len(written) > 1: - ambiguous.append(f"{pin_field}_commit {' -> '.join(written)}") - # The current pin among the candidates reads as "no move" (None) for - # that repository, which the other candidates must then agree with. - reader = (served or {}).get(repo, repo) - options.append([ - (reader, pin, current) if pin != current else None for pin in written - ]) - readings = [ - [pair for pair in combo if pair is not None] - for combo in itertools.product(*options) - ] - return readings, ambiguous - - -def _realign_citation( - citation: Citation, - readings: list[list], - ambiguous: list[str], - cache_dir: str, - offline: bool, - pinned: Iterable[tuple[upstream.Repo, str]] = (), -) -> tuple[dict[int, tuple[int, int, str | None]], list[str]]: - """The moves one prose run needs, and what blocks them. - - Several pins under one text: the history cannot say which the text - describes, but when the cited range lands in the same place from every - one of them the question does not matter. When the readings disagree, - moving from the wrong one rewrites a correct citation onto someone - else's code, with nothing to show it happened. - """ - moves: dict[int, tuple[int, int, str | None]] = {} - blocked: list[str] = [] - for index, part in enumerate(citation.parts): - outcomes = { - repr(result): result - for result in ( - _realign_part(part, pairs, cache_dir, offline, pinned) if pairs else None - for pairs in readings - ) - } - if len(outcomes) > 1: - blocked.extend( - f"pin moved under this text: {item}; the readings disagree" - for item in ambiguous - ) - continue - outcome = next(iter(outcomes.values())) - if outcome is None: - continue - _state, payload = outcome - if isinstance(payload, tuple): - moves[index] = (payload[0], payload[1], None) - else: - blocked.append(payload) - return moves, blocked - - -@dataclass(frozen=True) -class _ProseRepos: - """The repositories prose is read in, as realign_prose reads them.""" - - # (pin field, build mode, repository) for each declared repository. - repos: list[tuple[str, str, upstream.Repo]] - # A source declared gone, read through its mirror. - served: dict[upstream.Repo, upstream.Repo] - # Every declared (repository, current pin). - pinned: list[tuple[upstream.Repo, str]] - - -def _prose_repos(document: dict) -> _ProseRepos | None: - """The repositories the profile's prose is read in, None when it declares none.""" - # Every repository the profile declares, by build mode where it keys - # them so: eka2l1, mesence, supermodel and xemu pin a dict, and reading - # str(dict) found no repository at all, so their prose was never judged. - repos = [ - (field, mode, repo) - for field, mode, url in declared_repositories(document) - if field != "source_mirror" and (repo := upstream.parse_repo(url)) is not None - ] - if not repos: - return None - # A source declared gone is read through its mirror, which carries the - # same commits: teknoparrot's notes stayed blocked once - # teknogods/TeknoParrotUI answered 404, every reading of the old pin - # calling the files absent. - mirror = ( - upstream.parse_repo(str(document.get("source_mirror") or "")) - if document.get("upstream_gone") else None - ) - served = { - repo: mirror for field, _mode, repo in repos - if mirror is not None and field == "source" - } - pinned = [ - (served.get(repo, repo), pin) - for field, mode, repo in repos - if (pin := _pin_of(document, f"{field}_commit", mode)) - ] - return _ProseRepos(repos, served, pinned) - - -@dataclass -class _ProseHistory: - """Where each prose citation came into the profile's history.""" - - # The commit that introduced each citation's text, None when uncommitted. - intro: list[str | None] - revisions: list[tuple[str, dict]] = field(default_factory=list) - # Citations whose writing pin lies past a revision that cannot be read: - # where their text came in is unknown, so they are not moved. - unknown: dict[int, str] = field(default_factory=dict) - - -def _carried_by(past: dict) -> set[tuple[str, str]]: - return { - (c.field, _citation_key(c.ref)) - for c in collect_citations(past) if c.kind == "prose" - } - - -def _date_prose( - path: Path, document: dict, keys: list[tuple[str, str]], history: list[str] -) -> _ProseHistory: - """Follow each citation back through the commits that carry its text.""" - alive = [True] * len(keys) - found = _ProseHistory([None] * len(keys)) - # A revision that does not parse is passed over unless the pins differ - # on its two sides while the citation is absent across it: then the - # text may have come in there under either pin. Otherwise nothing - # it could hold changes the reading. lindbergh-loader - # committed invalid YAML once (df7346b3), fixed the same day, and every - # note older than that commit stayed blocked for good. - gap: str | None = None - for sha in history: - if not any(alive): - break - past = _git_file_at(path, sha) - if past is None: - gap = gap or sha - continue - held = _carried_by(past) - if gap: - newer = found.revisions[-1][1] if found.revisions else document - moved = any( - newer.get(pin) != past.get(pin) - for pin in ("source_commit", "upstream_commit") - ) - for index, live in enumerate(alive): - if live and moved and keys[index] not in held: - found.unknown[index] = gap - alive[index] = False - gap = None - found.revisions.append((sha, past)) - for index, live in enumerate(alive): - if not live: - continue - if keys[index] in held: - found.intro[index] = sha - else: - alive[index] = False - if gap: - # The oldest revisions do not parse: where the text came in is past - # what can be read. - found.unknown.update({index: gap for index, live in enumerate(alive) if live}) - return found - - -def _prose_jobs( - document: dict, - citations: list[Citation], - dated: _ProseHistory, - declared: _ProseRepos, - cache_dir: str, - offline: bool, -) -> tuple[list[str], list[tuple[Citation, dict]]]: - """The moves each dated citation needs, and the messages on the others.""" - messages: list[str] = [] - jobs: list[tuple[Citation, dict]] = [] - for index, citation in enumerate(citations): - if index in dated.unknown: - messages.append( - f"read again: {citation.field}: {citation.ref} " - f"(revision {dated.unknown[index][:8]} unreadable)" - ) - continue - intro_sha = dated.intro[index] - if intro_sha is None: - # The scalar is not committed yet: written now, under this pin. - continue - readings, ambiguous = _writing_pairs( - document, declared.repos, dated.revisions, intro_sha, declared.served - ) - if not any(readings): - continue - moves, blocked = _realign_citation( - citation, readings, ambiguous, cache_dir, offline, declared.pinned - ) - if blocked: - # Half a run must not move: the untouched ranges would read as - # already realigned when they were never even located. - messages.extend( - f"read again: {citation.field}: {citation.ref} ({reason})" - for reason in blocked - ) - continue - if moves: - jobs.append((citation, moves)) - return messages, jobs - - -def realign_prose( - path: Path, cache_dir: str, offline: bool = False, dry_run: bool = False -) -> list[str]: - """Recale prose runs from the pin their text was written at. - - A prose run written under an older pin describes that revision, and - anchoring it from the current pin would faithfully track the wrong - content: the cited line holds someone else's code there. The writing pin - is read from the profile's own history, at the commit that introduced - the scalar's current text, and only when no other pin was committed - under that text since. - """ - text = path.read_text(encoding="utf-8") - document = yaml.safe_load(text) - if not isinstance(document, dict): - return [] - citations = [c for c in collect_citations(document) if c.kind == "prose"] - if not citations: - return [] - declared = _prose_repos(document) - if declared is None: - return [] - history = _git_history(path) - if history is None: - return [f"read again: {path.name}: git history unreadable"] - if not history: - return [] - - # Each citation is followed on its own: a field's text changes whenever - # one of its citations is corrected by hand, and dating the whole field - # from that commit put idtech4a++'s untouched Game.java:69-72 under the - # current pin, so the stale range was never offered again. - keys = [(c.field, _citation_key(c.ref)) for c in citations] - dated = _date_prose(path, document, keys, history) - messages, jobs = _prose_jobs(document, citations, dated, declared, cache_dir, offline) - - if dry_run: - messages.extend( - f"would recale {citation.field}: {citation.ref} -> " - f"{_run_after_moves(citation.parts, moves, citation.ref)}" - for citation, moves in jobs - ) - return messages - new_text, applied, left = _apply_prose_edits(text, jobs, document) - messages.extend(applied) - messages.extend(f"left in place: {item}" for item in left) - if applied: - apply_edit(path, new_text, document) - return messages def _refuse_dirty_tree(emulators_dir: str, names: list[str]) -> None: @@ -4943,598 +381,6 @@ def _check_quota(count: int, offline: bool) -> None: raise SystemExit(1) -def _stale_prose_blocks( - name: str, path: Path, args: argparse.Namespace, - known: list[str] | None = None, -) -> bool: - """Whether prose written under an older pin has to move first. - - Anchored from the current pin, a recale or a bump follows someone else's - code (boom3 FileSystem.cpp:2125, written at b810234e, an unrelated - OpenFileRead at 132dfddb). It moves first, from the pin it was written at. - `known` is the answer the report already holds for the same text. - """ - stale = ( - known if known is not None - else realign_prose(path, args.cache_dir, args.offline, dry_run=True) - ) - if stale: - print( - f"{name}: {len(stale)} prose citation(s) written under an older pin; " - "run --realign-prose, or read them and set prose_written_at to the pin they match", - file=sys.stderr, - ) - return bool(stale) - - -def _read_stale_prose( - name: str, path: Path, args: argparse.Namespace, failed: set[str] -) -> list[str]: - """What --realign-prose would do or refuse on this profile, as a report. - - Read on every pass, not only before a write: prose that blocks the pin - is review work whether or not this run tries to move it. - """ - try: - return realign_prose(path, args.cache_dir, args.offline, dry_run=True) - except upstream.RateLimitError: - raise - except upstream.NotCachedError as exc: - # Unread offline, as a report is: not clean, and not a failure. - return [f"not read offline: {exc}"] - except upstream.UpstreamError as exc: - # Not read is not clean: the bump would refuse on the same error. - print(f"{name}: prose not read, {exc}", file=sys.stderr) - failed.add(name) - return [f"not read: {exc}"] - - -def _backfill( - args: argparse.Namespace, name: str, profile: dict, report: ProfileReport, - path: Path, -) -> None: - """Write the pin a profile without source_commit was written at.""" - if not report.pin or profile.get("source_commit"): - return - if args.dry_run: - print(f"{name}: would write source_commit {report.pin[:7]}") - elif backfill_commit(path, report.pin): - print(f"{name}: source_commit {report.pin[:7]}") - - -def _write_attributions( - args: argparse.Namespace, name: str, profile: dict, report: ProfileReport, path: Path -) -> None: - if report.skipped: - return - with tempfile.TemporaryDirectory() as scratch: - target = Path(scratch) / path.name - target.write_bytes(path.read_bytes()) - verb = "would attribute " if args.dry_run else "" - for line in attribute_refs(target, report, profile): - print(f"{name}: {verb}{line}") - if not args.dry_run: - path.write_bytes(target.read_bytes()) - - -def _promote( - args: argparse.Namespace, name: str, path: Path, target: Path, moved: bool -) -> bool: - """Write the edited copy over the profile, False when it was put back.""" - original = path.read_bytes() - path.write_bytes(target.read_bytes()) - # The guard above read the prose under the pins the profile held. - # A bump that moves upstream_commit can leave prose the report - # judged in the fork under the new upstream pin (dolphin-mmjr2, - # hatari, xenios): read it again on what was written. It reads - # the profile's own history, so it runs on the real path, and a - # read that fails (offline, rate limit) puts the profile back. - try: - left = realign_prose(path, args.cache_dir, args.offline, dry_run=True) - except BaseException: - path.write_bytes(original) - raise - if moved and left: - path.write_bytes(original) - print( - f"{name}: the bump would leave {len(left)} prose citation(s) " - "under an older pin, nothing was written. Read them first.", - file=sys.stderr, - ) - return False - _WRITTEN.add(name) - return True - - -def _apply_writes( - args: argparse.Namespace, name: str, profile: dict, report: ProfileReport -) -> None: - path = Path(args.emulators_dir) / f"{name}.yml" - if not path.is_file(): - return - if args.backfill_commits: - _backfill(args, name, profile, report, path) - if getattr(args, "attribute_refs", False): - _write_attributions(args, name, profile, report, path) - return - # A backfill has just written a pin: the report's reading predates it. - known = None if args.backfill_commits else report.stale_prose - if not (args.rebase_refs or args.bump_commit) or _stale_prose_blocks( - name, path, args, known - ): - return - if _gained_since_pin(args, name, profile, report): - return - with tempfile.TemporaryDirectory() as scratch: - # Always work on a copy. A dry run reports what it left there, a real - # run promotes it. A recale rewrites the refs for HEAD, so the pin - # has to follow or neither moves: a profile whose refs describe one - # revision and whose pin names another is exactly what the - # all-or-nothing rule exists to prevent. Recaling alone used to - # leave that state behind, and a later bump then read the recaled - # refs at the old pin and saw them shifted again. - target = Path(scratch) / path.name - target.write_bytes(path.read_bytes()) - recale, bumped = ("would recale ", "would set ") if args.dry_run else ("", "") - applied = [] - if args.rebase_refs and not report.skipped: - applied = rebase_refs(target, report, args.accept_changed) - moved = bump_commit(target, report, args.accept_changed, - args.cache_dir, args.offline) - if args.rebase_refs and applied and not moved: - keyed = any(isinstance(profile.get(f), dict) for f in ("source_commit", "upstream_commit")) - reason = ( - "its pins are keyed by build mode and are set by hand" - if keyed else "the prose or an annotated ref has to move first" - ) - print( - f"{name}: refs recaled but the pin will not follow, so nothing " - f"was written: {reason}", - file=sys.stderr, - ) - return - messages = [f"{name}: {recale}{line}" for line in applied] - if moved: - # Each pin the write changes, as written: a bump that only moved - # upstream_commit announced the source_commit it already held. - after = yaml.safe_load(target.read_text(encoding="utf-8")) or {} - before = yaml.safe_load(path.read_text(encoding="utf-8")) or {} - messages += [ - f"{name}: {bumped}{pin} -> {str(after[pin])[:7]}" - for pin in pins_changed(before, after) - ] - if ( - not args.dry_run and (applied or moved) - and not _promote(args, name, path, target, moved) - ): - return - for message in messages: - print(message) - - -def _gained_since_pin( - args: argparse.Namespace, name: str, profile: dict, report: ProfileReport -) -> bool: - """Whether the cited files gained a file name or a hash literal since the pin. - - Refs that still anchor say nothing of what the code added around them: - mame2003_plus gained two Taito G-NET BIOS sets in a zn.c whose cited - lines only shifted. A bump past that would document a file set the code - no longer has. --accept-changed says the additions were read. - """ - if args.accept_changed or not report.head or report.pin == report.head: - return False - if report.pinned_tag: - # A held pin is not advanced at all: bump_commit says so itself. - return False - if report.new_files is None and report.new_hashes is None: - probe = argparse.Namespace(**{**vars(args), "detect_new_files": True, "watch_hashes": True}) - _attach_detection(probe, profile, report) - gained = sorted({item["name"] for item in report.new_files or () if item.get("since_pin")}) - added = report.new_hashes or [] - if not (gained or added or report.detection_skipped): - return False - detail = ", ".join(gained[:5]) + (f", {len(added)} hash literal(s)" if added else "") - if report.detection_skipped: - detail = f"cited files unread ({report.detection_skipped})" - print( - f"{name}: the cited files changed beyond the refs since the pin ({detail.strip(', ')}), " - "nothing was written. Read them, then rerun with --accept-changed.", - file=sys.stderr, - ) - return True - - -def _cited_paths(report: ProfileReport) -> list[str]: - return sorted( - {part.new_path or part.part.path for e in report.entries for part in e.parts} - ) - - -def _attach_detection( - args: argparse.Namespace, profile: dict, report: ProfileReport -) -> None: - """Record undeclared names and hashes of the cited files on the report. - - Kept on the report rather than printed, so --json carries them as the - text report does. A forge that refuses is named on the report: the ref - report already muted it, and one refusal must not end a pass over every - profile. A quota signal still ends the profile. - """ - if not report.head or not (args.detect_new_files or args.watch_hashes): - return - repo = select_repo(profile) - if repo is None: - return - try: - _detect(args, profile, report, repo) - except upstream.RateLimitError: - raise - except upstream.UpstreamError as exc: - report.detection_skipped = str(exc) - - -def _detect( - args: argparse.Namespace, profile: dict, report: ProfileReport, - repo: upstream.Repo, -) -> None: - names, hashes = declared_names(profile), declared_hashes(profile) - new_files: list[dict] = [] - new_hashes: list[dict] = [] - for path in _cited_paths(report): - head_lines = upstream.fetch_file( - repo, report.head, path, args.cache_dir, args.offline - ) - if head_lines is None: - continue - pin_lines = ( - upstream.fetch_file(repo, report.pin, path, args.cache_dir, args.offline) - or [] - ) - if args.detect_new_files: - found, elided = detect_new_files(head_lines, names, path=path) - at_pin = { - name.casefold() - for _, name in detect_new_files( - pin_lines, names, cap=sys.maxsize, path=path - )[0] - } - new_files.extend( - {"path": path, "line": number, "name": name, - "since_pin": name.casefold() not in at_pin} - for number, name in found - ) - if elided: - report.new_files_elided[path] = elided - if args.watch_hashes: - added = [ - line - for line in difflib.unified_diff(pin_lines, head_lines, n=0) - if line.startswith("+") and not line.startswith("+++") - ] - new_hashes.extend( - {"path": path, "kind": kind, "value": value} - for kind, value in watch_hashes(added, hashes) - ) - if args.detect_new_files: - report.new_files = new_files - if args.watch_hashes: - report.new_hashes = new_hashes - - -def _print_detection(report: ProfileReport) -> None: - if report.detection_skipped: - print(f" extras skipped: {report.detection_skipped}") - for item in report.new_files or (): - print(f" new file: {item['path']}:{item['line']} {item['name']}") - for path, elided in report.new_files_elided.items(): - print(f" new file: {path}: {elided} further matches not shown") - for item in report.new_hashes or (): - print(f" new {item['kind']}: {item['path']} {item['value']}") - - -def _attach_version( - args: argparse.Namespace, profile: dict, report: ProfileReport -) -> None: - """Record core_version against the forge's latest tag and release. - - Kept on the report rather than printed, so --json carries it as the text - report does. A forge that refuses is named on the profile's own line. - """ - repo = select_repo(profile) if report.head else None - if repo is None: - return - try: - report.version = check_version(profile, repo, args.cache_dir, args.offline) - except upstream.RateLimitError: - raise - except upstream.UpstreamError as exc: - print(f"{report.name}: version check skipped: {exc}", file=sys.stderr) - return - relation = None - passed = None - version = report.version - try: - if version and version.tag_commit and report.pin and version.tag_commit != report.pin: - relation = upstream.ancestry( - repo, version.tag_commit, report.pin, args.cache_dir, args.offline - ) - passed = _passed_release(args, repo, version, report.pin) - newer = None if passed or profile.get("pin_frozen") else _newer_release( - args, repo, version, report.pin - ) - except upstream.RateLimitError: - raise - except upstream.UpstreamError as exc: - print(f"{report.name}: tag ancestry unread: {exc}", file=sys.stderr) - newer = None - report.version_warning = version_warning(version, report.pin, relation, passed) or ( - f"{'release' if version.latest_release else 'tag'} {newer} is newer than core_version {version.declared} and the pin: " - "profile it, or hold the pin with pin_frozen and the reason" - if newer and version else None - ) - - -# A tag that reads as a version number: 1.1, v107, v9.0-6, 2.55, 1.1d. -# Nightly, continuous and latest tags do not. -_VERSION_TAG_RE = re.compile(r"v?\d+(?:[._-]\d+)*[a-z]?", re.IGNORECASE) - - -def published_version(version: VersionReport | None) -> str | None: - """The latest version the project published: its latest release, or for - a project that publishes no release, its newest tag when that tag reads - as a version number. - - GitLab and Codeberg projects often tag without a release: eightyone sat - on v1.2 and higan on 106 while v1.3 and v107 were tagged, and nothing - compared them. - """ - if version is None: - return None - if version.latest_release: - return version.latest_release - tag = version.latest_tag - if tag and _VERSION_TAG_RE.fullmatch(tag): - return tag - return None - - -def _later_number(candidate: str, declared: str) -> bool: - """Whether both read as numbers of one scheme and the candidate's is higher. - - One scheme means the same major number: libretro forks tag RetroArch's - numbers (mame2015 0.160 beside v1.4.1) and some projects tag dates - (np2pi 0.83 beside 20120302). Trailing zeros do not count: v1 is v1.0. - """ - if not (_VERSION_TAG_RE.fullmatch(candidate) and _VERSION_TAG_RE.fullmatch(declared)): - return False - - def key(text: str) -> tuple[int, ...]: - numbers = [int(n) for n in re.findall(r"\d+", text)] - while len(numbers) > 1 and numbers[-1] == 0: - numbers.pop() - return tuple(numbers) - - high, low = key(candidate), key(declared) - if (len(high) == 1) != (len(low) == 1): - return False - if len(high) == 1: - # higan 106 and v107 are one scheme, openlara v1 and 20170424 two. - return len(str(high[0])) == len(str(low[0])) and high > low - return high[0] == low[0] and high > low - - -def _newer_release( - args: argparse.Namespace, - repo: upstream.Repo, - version: VersionReport | None, - pin: str | None, -) -> str | None: - """The latest release, when it comes after both the declared tag and the pin. - - A profile pinned on its release tag reads as clean forever: armsx1 sat on - 0.1.3, armsx2 on 2.8 and x1-box on 1.2.6 while 0.3.6, 2.9 and 1.3.2 were - out, and nothing said so. A held pin (pin_frozen) is left alone. - """ - published = published_version(version) - if not version or not pin or not version.tag_commit or not published: - return None - if published == version.declared: - return None - commit = upstream.tag_commit(repo, published, args.cache_dir, args.offline) - if not commit or commit == pin: - return None - after_tag = upstream.ancestry(repo, version.tag_commit, commit, args.cache_dir, args.offline) - if not after_tag or after_tag[0] == 0 or after_tag[1] != 0: - return None - after_pin = upstream.ancestry(repo, pin, commit, args.cache_dir, args.offline) - if not after_pin or after_pin[0] == 0: - return None - return published - - -def _passed_release( - args: argparse.Namespace, - repo: upstream.Repo, - version: VersionReport | None, - pin: str | None, -) -> str | None: - """The latest release, when the pin already contains it and core_version - names another one. - - The latest tag is no witness: nightly, continuous and Pre-release tags - sort first on many forges. The latest release is a version the project - published as such. - """ - published = published_version(version) - if not version or not pin or not published: - return None - if published == version.declared: - return None - if not version.tag_commit and not _later_number(published, version.declared): - # No tag to order them: only a release numbered past the declared - # version says it is older. "latest", "continuous" or 1.53 under a - # declared 1.61 say nothing. - return None - commit = upstream.tag_commit(repo, published, args.cache_dir, args.offline) - if not commit: - return None - if version.tag_commit and version.tag_commit != commit: - # A project can publish a release older than its newest tag: - # pcsx2x6's latest release is v0.2.20, its declared tag v0.2.22. - older = upstream.ancestry(repo, commit, version.tag_commit, args.cache_dir, args.offline) - if older and older[1] == 0: - return None - if commit == pin: - return published - relation = upstream.ancestry(repo, commit, pin, args.cache_dir, args.offline) - if relation and relation[1] == 0: - return published - return None - - -def _print_extras( - args: argparse.Namespace, profile: dict, report: ProfileReport -) -> None: - """Optional per-profile sections beyond the ref report. - - The ref report mutes a forge that refuses, but the extras read the same - forge again, so a refusal here is reported on the profile's own line - instead of ending a pass over every profile. A quota signal still - ends the profile. - """ - _print_detection(report) - if not (args.full_diff or args.tree_diff) or not report.head: - return - repo = select_repo(profile) - if repo is None: - return - try: - _print_extras_from(args, profile, report, repo) - except upstream.RateLimitError: - raise - except upstream.UpstreamError as exc: - print(f" extras skipped: {exc}") - - -def _print_extras_from( - args: argparse.Namespace, profile: dict, report: ProfileReport, - repo: upstream.Repo, -) -> None: - if args.full_diff: - for path in _cited_paths(report): - if args.ref and args.ref not in path: - continue - pin_lines = ( - upstream.fetch_file( - repo, report.pin, path, args.cache_dir, args.offline - ) - or [] - ) - head_lines = ( - upstream.fetch_file( - repo, report.head, path, args.cache_dir, args.offline - ) - or [] - ) - text = unified_for_path(pin_lines, head_lines, path, args.context) - if text: - print(text) - if args.tree_diff: - comparison = upstream.compare( - repo, report.pin, report.head, args.cache_dir, args.offline - ) - # The repository root is a directory like any other: dropping the - # empty dirname would turn the filter off and list the whole tree. - ref_dirs = {posixpath.dirname(p) for p in _cited_paths(report)} - for line in tree_diff(comparison, ref_dirs): - print(f" {line}") - - -class _Progress: - """A stderr line every PROGRESS_SECONDS while a pass asks the forges.""" - - def __init__(self, total: int, doing: str, quiet: bool) -> None: - self.total, self.doing, self.quiet = total, doing, quiet - self.last = time.monotonic() - - def step(self, done: int, name: str) -> None: - now = time.monotonic() - if not self.quiet and now - self.last >= PROGRESS_SECONDS: - print(f"{self.doing}: {done} of {self.total}, at {name}", file=sys.stderr, flush=True) - self.last = now - - -def _interrupted(done: int, total: int, what: str) -> None: - print(f"interrupted after {done} of {total} profiles: {what}", file=sys.stderr, flush=True) - - -def _print_triage( - args: argparse.Namespace, selected: dict, reports: list[ProfileReport] -) -> bool: - """Profiles ranked by drift, worst first. False when Ctrl-C cut the - scoring short: the ranking then covers the profiles scored.""" - ranked: list[tuple[int, ProfileReport, str]] = [] - sampled = 0 - progress = _Progress(len(reports), "scored", args.offline or len(reports) < 2) - complete = True - try: - for done, report in enumerate(reports): - progress.step(done, report.name) - sampled += _score(args, selected, report, ranked) - except KeyboardInterrupt: - _interrupted(len(ranked), len(reports), "the ranking covers those scored") - complete = False - if sampled: - # One line for the run: a notice per profile buried the ranking - # under 254 lines of stderr. - print( - f"commit count sampled on {TRIAGE_PATH_SAMPLE} cited paths for " - f"{sampled} profiles citing more", - file=sys.stderr, - ) - print(f"{'score':>5s} {'profile':30s} state (worst first)") - pending = 0 - for score, report, refused in sorted(ranked, key=lambda item: -item[0]): - state = triage_state(report) - if refused: - state += f", {refused}" - pending += bool(report.needs_review() or report.uncheckable()) - print(f"{score:5d} {report.name:30s} {state}") - print(f"{len(ranked)} profiles, {pending} with something to review") - return complete - - -def _score( - args: argparse.Namespace, selected: dict, report: ProfileReport, ranked: list -) -> bool: - """Append one profile's drift score to ``ranked``. True when its commit - count was read on a sample of the paths it cites.""" - profile = selected[report.name] - version = None - commits = 0 - refused = "" - sampled = False - repo = select_repo(profile) - if report.head and repo is not None: - paths = _cited_paths(report) - try: - version = check_version(profile, repo, args.cache_dir, args.offline) - for path in paths[:TRIAGE_PATH_SAMPLE]: - commits += upstream.commits_touching( - repo, path, report.pin, args.cache_dir, args.offline, - head=report.head, - ) - except upstream.UpstreamError as exc: - # One forge refusing a request (Eden's answers 403 to commit - # lookups) leaves this profile unscored, not the whole ranking. - refused = "commit history unreachable" - print(f"{report.name}: {exc}", file=sys.stderr) - sampled = len(paths) > TRIAGE_PATH_SAMPLE and not refused - ranked.append((drift_score(report, version, commits), report, refused)) - return sampled - - def _refuse_flag_combinations(args: argparse.Namespace) -> None: """Exit on a flag the chosen mode would not apply, before any request.""" if args.verify_version or args.set_version: @@ -5797,44 +643,6 @@ def _run_realign(args: argparse.Namespace, selected: dict) -> None: raise SystemExit(1) -def _read_one( - args: argparse.Namespace, - name: str, - profile: dict, - reports: list[ProfileReport], - failed: set[str], - writes: bool, -) -> None: - """Build one profile's report into ``reports``, then apply the writes.""" - profile_path = Path(args.emulators_dir) / f"{name}.yml" - try: - report = _build_full_report(args, name, profile, profile_path, failed) - except upstream.RateLimitError as exc: - # A quota is one forge's: git.libretro.com answering 429 said - # nothing about the 600 profiles on GitHub. upstream stops asking - # that host, so the rest of the pass costs it nothing. - print(f"{name}: {exc}", file=sys.stderr) - reports.append(ProfileReport( - name=name, entries=[], counts={}, - skipped=f"rate limited: {exc.host or exc}", - )) - failed.add(name) - return - reports.append(report) - if writes: - try: - _apply_writes(args, name, profile, report) - except YamlWriteError as exc: - # The guard already left the file untouched, so keep going. - print(f"{name}: write refused: {exc}", file=sys.stderr) - failed.add(name) - - -# Profiles this run wrote. Their new pins name trees the site's record of -# cited source paths does not hold yet. -_WRITTEN: set[str] = set() - - def _refresh_source_paths(args: argparse.Namespace) -> None: """Rebuild the site's record of cited source paths after a write. @@ -5871,112 +679,6 @@ def _refresh_source_paths(args: argparse.Namespace) -> None: ) -def _build_full_report( - args: argparse.Namespace, - name: str, - profile: dict, - profile_path: Path, - failed: set[str], -) -> ProfileReport: - """The report of one profile, with every section the flags ask for. - - A rate limit propagates: the caller decides what one forge's quota - means for the pass. Any other forge failure becomes a skip. - """ - try: - report = build_report( - name, profile, args.cache_dir, args.offline, - profile_path.read_text(encoding="utf-8") - if profile_path.is_file() else None, - ) - except upstream.RateLimitError: - raise - except upstream.NotCachedError as exc: - # Offline with a cache that never stored this file: the profile - # is unverified, not wrong. Reporting GONE sent readers after - # files that exist. - report = ProfileReport( - name=name, entries=[], counts={}, skipped=f"offline: {exc}" - ) - except upstream.GoneError as exc: - # The forge is not coming back: a takedown, or a host that no - # longer resolves. Saying so once in the summary beats repeating - # it on stderr every pass, and it keeps the profile out of the - # backlog, where nobody can act on it anyway. - report = ProfileReport( - name=name, entries=[], counts={}, skipped=f"upstream gone: {exc}" - ) - except upstream.UpstreamError as exc: - # One unreachable forge must not abandon the other profiles. - print(f"{name}: {exc}", file=sys.stderr) - report = ProfileReport( - name=name, entries=[], counts={}, skipped=f"upstream error: {exc}" - ) - failed.add(name) - if not report.skipped and profile_path.is_file(): - report.stale_prose = _read_stale_prose(name, profile_path, args, failed) - if args.check_version and profile.get("version_source"): - report.declared_version = str(profile.get("core_version") or "") or None - report.page_version = page_version(profile, args.cache_dir, args.offline) - report.no_genuine_copy = str( - profile["version_source"].get("no_genuine_copy") or "") or None - if args.check_version: - _attach_version(args, profile, report) - _attach_detection(args, profile, report) - return report - - -def _emit( - args: argparse.Namespace, - selected: dict, - reports: list[ProfileReport], - shown: list[ProfileReport], -) -> bool | None: - """Write the reports in the form the flags ask for. False when the - triage was cut short.""" - if args.as_json: - print(json.dumps([report_to_dict(r) for r in shown], indent=2)) - return None - if args.markdown: - target = Path(args.report_dir) / f"profile-sync-{date.today().isoformat()}.md" - target.parent.mkdir(parents=True, exist_ok=True) - target.write_text(format_markdown(shown), encoding="utf-8") - print(f"written: {target}") - return None - if args.fetch_plan: - for report in shown: - for url in fetch_plan(report): - print(url) - return None - if args.triage: - return _print_triage(args, selected, shown) - for report in shown: - print(format_report(report, args.changed_only)) - _print_extras(args, selected[report.name], report) - print() - if args.changed_only: - _print_elided(reports, len(shown), frozenset(report.name for report in shown)) - return None - - -def _print_elided( - reports: list[ProfileReport], printed: int, listed: frozenset[str] = frozenset() -) -> None: - """Account for what --changed-only left out, grouped by reason.""" - reasons: dict[str, int] = {} - for report in reports: - if report.needs_review() or report.name in listed: - continue - key = report.skipped or "nothing to review" - reasons[key] = reasons.get(key, 0) + 1 - if not reasons: - return - total = sum(reasons.values()) - print(f"{printed} profils listes, {total} non listes:") - for reason, count in sorted(reasons.items(), key=lambda item: -item[1]): - print(f" {count:4d} {reason}") - - if __name__ == "__main__": from core.cli import run_cli diff --git a/scripts/profiling/anchoring.py b/scripts/profiling/anchoring.py new file mode 100644 index 000000000..1cd81f19d --- /dev/null +++ b/scripts/profiling/anchoring.py @@ -0,0 +1,696 @@ +"""Finding a cited block again: anchoring, renames, shifts and the self-check at a pin.""" + +from __future__ import annotations + +import difflib +import posixpath +from collections.abc import Callable, Iterable, Sequence +from dataclasses import dataclass, replace + +from core.upstream import CompareResult, find_renamed +from model.sourceref import RefPart, is_binary_citation +from profiling.citations import is_external_citation +from profiling.repos import HEAD, PIN + + +STATUS_ORDER = ( + "ANCHORED", "UNCHECKED", "EXTERNAL", "BINARY", "SHIFTED", "RENAMED", "MOVED", + "AMBIGUOUS", "CHANGED", "GONE", +) +REVIEW_STATUSES = ("CHANGED", "GONE", "AMBIGUOUS") +REBASE_STATUSES = ("SHIFTED", "RENAMED", "MOVED") + +WIDEN_STEPS = (0, 3, 6, 12, 25, 50) +# A 30k-line driver diffs in about five seconds, and the biggest files the +# corpus cites sit just above that. The cap stays as a guard against +# pathological inputs, not as a limit on real source files. +# The fallback mapping is a full sequence diff, and it only runs once exact +# anchoring has failed, which is rare. openbor.c is the largest file any +# profile cites, 55k lines against 57k, and diffing the pair takes nine +# seconds. The ceiling sits above it so that one resolves, and still stops a +# pathological pair from stalling a sweep of every profile. +MAX_MATCH_LINES = 60000 + + +HEADER_SUFFIXES = (".h", ".hpp", ".hh", ".hxx", ".inc", ".inl") + + +@dataclass(frozen=True) +class AnchorResult: + status: str + start: int | None + end: int | None + candidates: list[int] + reason: str | None = None + + +@dataclass(frozen=True) +class PartResult: + part: RefPart + status: str + new_path: str | None + start: int | None + end: int | None + candidates: list[int] + reason: str | None = None + repo: str | None = None + head_url: str | None = None + + +def worst_status(statuses: Iterable[str]) -> str: + """Severity of an entry is the worst severity among its parts.""" + worst = "ANCHORED" + for status in statuses: + if STATUS_ORDER.index(status) > STATUS_ORDER.index(worst): + worst = status + return worst + + +def _normalize(lines: list[str]) -> list[str]: + return [line.strip() for line in lines] + + +def _find_all(haystack: list[str], needle: list[str]) -> list[int]: + """Zero-based offsets where the needle sequence occurs.""" + size = len(needle) + if not size or size > len(haystack): + return [] + first = needle[0] + return [ + i for i in range(len(haystack) - size + 1) + if haystack[i] == first and haystack[i : i + size] == needle + ] + + +def _map_index( + opcodes: list[tuple[str, int, int, int, int]], index: int +) -> int | None: + """Line of HEAD that a pinned line became. + + A replace block of equal size edits each line in place (a column added to + every row of a table), so its lines map one to one like an equal block. + Collapsed onto the first line, eight refs citing eight rows all landed on + the first row. + """ + for tag, i1, i2, j1, j2 in opcodes: + if i1 <= index < i2: + in_place = tag == "equal" or (tag == "replace" and i2 - i1 == j2 - j1) + return j1 + (index - i1) if in_place else j1 + return None + + +_OPCODE_CACHE: dict[tuple[tuple[str, ...], tuple[str, ...]], list] = {} +_OPCODE_CACHE_SIZE = 8 + + +def _opcodes(pin: list[str], head: list[str]) -> list: + """Opcodes for one pair of revisions, computed once per run. + + Every ref citing the same file would otherwise redo the same diff, and a + driver of thirty thousand lines takes seconds each time. Keyed by the + lines themselves: anchor_block hands over lists it builds and frees, and + an id() key served one file's opcodes to the next pair at the same + addresses. + """ + key = (tuple(pin), tuple(head)) + cached = _OPCODE_CACHE.pop(key, None) + if cached is None: + cached = difflib.SequenceMatcher( + None, pin, head, autojunk=False + ).get_opcodes() + _OPCODE_CACHE[key] = cached + while len(_OPCODE_CACHE) > _OPCODE_CACHE_SIZE: + del _OPCODE_CACHE[next(iter(_OPCODE_CACHE))] + return cached + + +def _map_changed(pin: list[str], head: list[str], lo: int, hi: int) -> AnchorResult: + """Map a pinned range onto HEAD once exact anchoring has failed.""" + if len(pin) > MAX_MATCH_LINES or len(head) > MAX_MATCH_LINES: + return AnchorResult( + "CHANGED", None, None, [], f"file over {MAX_MATCH_LINES} lines" + ) + opcodes = _opcodes(pin, head) + # A file whose every line was edited in place keeps no equal block, yet + # its lines still map one to one: fheroes2 version.txt holds one line, + # 1.0.11 at the pin and 1.1.17 at HEAD, and read GONE. + rewritten_in_place = len(opcodes) == 1 and ( + opcodes[0][0] == "replace" + and opcodes[0][2] - opcodes[0][1] == opcodes[0][4] - opcodes[0][3] + ) + if not rewritten_in_place and not any(tag == "equal" for tag, *_ in opcodes): + return AnchorResult("GONE", None, None, []) + new_lo = _map_index(opcodes, lo) + new_hi = _map_index(opcodes, max(lo, hi - 1)) + if new_lo is None or new_hi is None: + return AnchorResult("GONE", None, None, []) + return AnchorResult("CHANGED", new_lo + 1, max(new_lo, new_hi) + 1, []) + + +def anchor_block( + pin_lines: list[str], head_lines: list[str], start: int, end: int +) -> AnchorResult: + """Locate the pinned line range inside the HEAD revision of a file.""" + pin = _normalize(pin_lines) + head = _normalize(head_lines) + lo, hi = start - 1, end + if lo < 0 or hi > len(pin): + return AnchorResult("GONE", None, None, [], "cited range ends past the end of the file") + if not any(pin[lo:hi]): + return AnchorResult("CHANGED", None, None, [], "cited range is blank") + + candidates: list[int] = [] + for pad in WIDEN_STEPS: + a = max(0, lo - pad) + b = min(len(pin), hi + pad) + hits = _find_all(head, pin[a:b]) + offset = lo - a + if len(hits) == 1: + new_start = hits[0] + offset + 1 + new_end = new_start + (hi - lo) - 1 + status = "ANCHORED" if new_start == start else "SHIFTED" + return AnchorResult(status, new_start, new_end, []) + if not hits: + break + candidates = [h + offset + 1 for h in hits] + else: + return AnchorResult("AMBIGUOUS", None, None, candidates) + + if candidates: + return AnchorResult("AMBIGUOUS", None, None, candidates) + return _map_changed(pin, head, lo, hi) + + +NUDGE_WINDOW = 60 + + +def nudge_to_declared( + pin_lines: list[str], start: int, end: int, tokens: Sequence[str] +) -> tuple[int, int] | None: + """Move a cited range onto the nearest line carrying a declared value. + + Some refs sit a few lines off their subject, and a few land on a blank + line, which anchors nothing. When the entry declares a hash or a name and + that value appears close by, the ref plainly meant that line. + """ + if not tokens: + return None + if end > start and any(line.strip() for line in pin_lines[start - 1:end]): + # A range over code cites that code (a loading function), not the + # value beside it. Moved onto the nearest name, ymir's 598-618 left + # load_rom_cartridge() and a real change read as a silent SHIFTED. + return None + lowered = [line.lower() for line in pin_lines] + hits = [ + i + 1 + for i, line in enumerate(lowered) + if any(token in line for token in tokens) + ] + if not hits: + return None + nearest = min(hits, key=lambda n: abs(n - start)) + if abs(nearest - start) > NUDGE_WINDOW or nearest == start: + return None + if sum(1 for h in hits if abs(h - nearest) <= 1) > 1: + return None + return nearest, nearest + (end - start) + + +def resolve_rename( + result: CompareResult, path: str, head_paths: Iterable[str] = () +) -> tuple[str | None, list[str]]: + """New path of a moved file, or the candidates when several match. + + The comparison is authoritative when it reports the rename. Otherwise the + HEAD tree is searched by basename, then by stem: a C++ file rewritten in C + keeps its stem and loses its basename. + """ + renamed = find_renamed(result, path) + if renamed: + return renamed, [] + pool = list(head_paths) or [change.path for change in result.files] + base = posixpath.basename(path) + matches = _narrow( + [p for p in pool if p != path and posixpath.basename(p) == base], path + ) + if len(matches) == 1: + return matches[0], [] + if not matches: + stem = base.rsplit(".", 1)[0] + matches = _narrow( + [ + p + for p in pool + if p != path and posixpath.basename(p).rsplit(".", 1)[0] == stem + ], + path, + ) + if len(matches) == 1: + return matches[0], [] + return None, matches + + +def _is_header(path: str) -> bool: + return path.lower().endswith(HEADER_SUFFIXES) + + +def _narrow(matches: list[str], path: str) -> list[str]: + """Prefer candidates of the same kind, then of the same directory. + + A stem search on `ss.cpp` matches both `ss.c` and `ss.h`, and only one of + them is the implementation the ref meant. + """ + if len(matches) < 2: + return matches + kind = [p for p in matches if _is_header(p) == _is_header(path)] + if len(kind) == 1: + return kind + pool = kind or matches + directory = posixpath.dirname(path) + same = [p for p in pool if posixpath.dirname(p) == directory] + return same if len(same) == 1 else pool + + +def locate_by_declared_value( + head_lines: list[str], anchored: AnchorResult, tokens: Sequence[str] +) -> tuple[int, int, str] | None: + """Follow a ref whose subject can still be pinpointed at HEAD. + + Two cases carry enough evidence to move a ref without judging the edit + itself: the mapped range already contains the declared value, or the value + occurs exactly once in the whole file. A value occurring several times is + left alone, since nothing says which one the ref meant. + """ + lowered = [line.lower() for line in head_lines] + if anchored.start is not None: + end = anchored.end or anchored.start + block = "\n".join(lowered[anchored.start - 1 : end]) + if any(token in block for token in tokens): + return ( + anchored.start, + end, + "content edited, declared value present in the new range", + ) + hits = [ + i + 1 + for i, line in enumerate(lowered) + if any(token in line for token in tokens) + ] + if len(hits) == 1: + return hits[0], hits[0], "declared value occurs once at HEAD" + return None + + +def _written_against_head(part: RefPart, head_lines: list[str] | None) -> bool: + """Whether the cited line exists at HEAD, which is where it was written.""" + return ( + head_lines is not None + and part.start is not None + and part.start <= len(head_lines) + ) + + +# What the rename search answers when a tree it needed came back truncated: +# a file missing from a partial listing may still exist, so absence is not +# established. eden's fs_paths.h and path_util.cpp were reported GONE. +TREE_TRUNCATED = "(HEAD tree truncated by the forge)" + + +def _unanchorable(part: RefPart) -> PartResult | None: + """The verdict on a part that cites no source path of a declared tree.""" + if is_binary_citation(part.path): + return PartResult( + part, "BINARY", None, None, None, [], + "cites a shipped binary, no source path to anchor", + ) + if is_external_citation(part.path): + return PartResult( + part, "EXTERNAL", None, None, None, [], + "names a project the profile does not declare", + ) + return None + + +def _rename_verdict( + part: RefPart, candidates: list[str], slug: str | None, url: str | None +) -> PartResult: + """What a part absent at HEAD reads as when no rename was found.""" + if candidates == [TREE_TRUNCATED]: + return PartResult( + part, "AMBIGUOUS", None, None, None, [], + "absent from a HEAD tree the forge truncated: " + "the file may still exist", + slug, url, + ) + if candidates: + return PartResult( + part, + "AMBIGUOUS", + None, + None, + None, + [], + f"{len(candidates)} candidates: {', '.join(candidates[:5])}", + slug, + url, + ) + return PartResult( + part, + "GONE", + None, + None, + None, + [], + "absent at HEAD, no rename found", + slug, + url, + ) + + +def _anchor_range( + part: RefPart, + start: int, + pin_lines: list[str], + head_lines: list[str], + tokens: Sequence[str], +) -> tuple[AnchorResult, str, str | None]: + """The cited range carried from the pin to HEAD: anchor, status, note.""" + end = part.last + anchored = anchor_block(pin_lines, head_lines, start, end) + note = anchored.reason + if anchored.status in REVIEW_STATUSES: + nudged = nudge_to_declared(pin_lines, start, end, tokens) + if nudged is not None: + retry = anchor_block(pin_lines, head_lines, *nudged) + if retry.status not in REVIEW_STATUSES: + anchored = retry + note = f"cited line was {nudged[0] - start:+d} off its subject" + status = anchored.status + if status in ("CHANGED", "GONE") and tokens: + located = locate_by_declared_value(head_lines, anchored, tokens) + if located is not None: + moved_start, moved_end, why = located + anchored = AnchorResult("MOVED", moved_start, moved_end, []) + status, note = "MOVED", why + return anchored, status, note + + +def _pin_missing( + part: RefPart, pin_lines: list[str] | None, head_lines: list[str], +) -> str | None: + """Why the pinned revision cannot carry the cited range, None when it can.""" + if pin_lines is None: + # Present at HEAD, absent at the pin, and the cited range fits the + # HEAD file: the ref was written against HEAD while source_commit + # still names an older revision. Saying "missing" sends the reader + # hunting for a move that never happened: the fix is the pin. + reason = "pin revision missing" + if _written_against_head(part, head_lines): + reason = "written against HEAD, pin names an older revision" + return reason + if part.last > len(pin_lines): + # The file is there and the line is not yet: the pinned revision is + # shorter than the one the ref was written against. nestopia cited + # the palette and database loads at 2041 and 2063, which is where + # HEAD carries them, over a pin four hundred lines shorter. A range + # that only ends past the file is no better: dolphin cited 239-323 in + # a 317-line NANDImporter.cpp, and the block found above the end + # anchored it. + return ( + "written against HEAD, pin names an older revision" + if _written_against_head(part, head_lines) + else "cited range ends past the end of the file" + ) + return None + + +def anchor_part( + part: RefPart, + fetch: Callable[[str, str, int | None, Sequence[str]], list[str] | None], + rename_getter: Callable[[str], tuple[str | None, list[str]]], + describe: Callable[..., tuple[str | None, str | None, str]] | None = None, + tokens: Sequence[str] = (), +) -> PartResult: + """Locate one reference part at HEAD, following a rename when needed. + + `describe(path)` returns (repo slug, raw URL at HEAD) for the repository + that owns the path, or (None, None) when the caller does not track it. + """ + unanchorable = _unanchorable(part) + if unanchorable is not None: + return unanchorable + slug, url, actual = ( + describe(part.path, part.start, tokens) if describe + else (None, None, part.path) + ) + head_lines = fetch(HEAD, part.path, part.start, tokens) + path = actual + renamed = actual != part.path + + if head_lines is None: + moved, candidates = rename_getter(part.path) + if moved is None: + return _rename_verdict(part, candidates, slug, url) + path = moved + head_lines = fetch(HEAD, path, part.start, tokens) + if head_lines is None: + return PartResult( + part, "GONE", None, None, None, [], None, slug, url + ) + renamed = True + if describe: + slug, url, _ = describe(path) + + if part.start is None: + status = "RENAMED" if renamed else "ANCHORED" + return PartResult( + part, status, path if renamed else None, None, None, [], None, slug, url + ) + + pin_lines = fetch(PIN, part.path, part.start, tokens) + if pin_lines is None and path != part.path: + # A ref may cite a bare filename the tree search resolved at HEAD. The + # same resolved path usually holds at the pin. A genuine rename keeps + # the old path at the pin, which is why that one is tried first. + pin_lines = fetch(PIN, path, part.start, tokens) + reason = _pin_missing(part, pin_lines, head_lines) + if reason is not None or pin_lines is None: + return PartResult(part, "GONE", None, None, None, [], reason, slug, url) + + anchored, status, note = _anchor_range(part, part.start, pin_lines, head_lines, tokens) + if renamed and status in ("ANCHORED", "SHIFTED"): + status = "RENAMED" + return PartResult( + part, + status, + path if renamed else None, + anchored.start, + anchored.end, + anchored.candidates, + note, + slug, + url, + ) + + +SHIFT_SUPPORT = 3 + +# Lines read past a blank cited line when scoring which repository owns it. +BLANK_LOOKAHEAD = 5 + + +def dominant_shifts( + parts: Iterable[PartResult], support: int = SHIFT_SUPPORT +) -> dict[str, int]: + """Most common line shift per file, taken from the unambiguous anchors. + + A file whose refs mostly moved by the same amount gives strong evidence + for the ones that stayed ambiguous: repetitive tables, as in the NeoGeo + driver, match a cited line in several places, and only one of them sits + where the rest of the file moved to. + """ + by_file: dict[str, dict[int, int]] = {} + for part in parts: + if part.status not in ("ANCHORED", "SHIFTED"): + continue + if part.start is None or part.part.start is None: + continue + counts = by_file.setdefault(part.part.path, {}) + delta = part.start - part.part.start + counts[delta] = counts.get(delta, 0) + 1 + shifts = {} + for path, counts in by_file.items(): + delta, seen = max(counts.items(), key=lambda kv: kv[1]) + if seen >= support: + shifts[path] = delta + return shifts + + +def resolve_by_shift(part: PartResult, shifts: dict[str, int]) -> PartResult: + """Settle an ambiguous part when one candidate matches the file's shift.""" + if part.status != "AMBIGUOUS" or not part.candidates: + return part + if part.part.start is None or part.part.path not in shifts: + return part + wanted = part.part.start + shifts[part.part.path] + if part.candidates.count(wanted) != 1: + return part + span = (part.part.end or part.part.start) - part.part.start + return PartResult( + part.part, + "ANCHORED" if wanted == part.part.start else "SHIFTED", + part.new_path, + wanted, + wanted + span, + [], + f"settled by the {shifts[part.part.path]:+d} line shift of its file", + part.repo, + part.head_url, + ) + + +def refuse_collapsing_moves(parts: list[PartResult]) -> list[PartResult]: + """Hold back a recale that would land on another part of the same ref. + + Two parts of one source_ref cite two places on purpose. dosbox_pure cited + the SHA-1 line of mt32emu.h and the ROMInfo line that ties it to a file + name, under two repository words: the second was self-checked alone, + found the hash once, and read MOVED onto the first line. Recaling it + would drop the ROMInfo citation without a word, so the part goes to + review instead. + """ + cited = { + (p.part.path, p.part.start, p.part.last) + for p in parts if p.part.start is not None + } + held = [] + for part in parts: + target = (part.new_path or part.part.path, part.start, part.end or part.start) + own = (part.part.path, part.part.start, part.part.last) + if ( + part.status in REBASE_STATUSES and part.start is not None + and target != own and target in cited + ): + part = replace( + part, status="AMBIGUOUS", candidates=[part.start], + reason="lands on a line another part of this ref already cites", + ) + held.append(part) + return held + + +SELF_CHECK_CONTEXT = 2 + + +def verify_at_pin( + part: RefPart, + pin_lines: list[str] | None, + tokens: Sequence[str], + hash_tokens: Sequence[str] = (), +) -> PartResult: + """Check a ref against its own revision instead of against HEAD. + + A profile pinned to a superseded tag documents a program HEAD no longer + contains, so comparing the two says nothing. What can still be checked is + self-consistency: does the cited range carry the value the entry declares? + """ + unanchorable = _unanchorable(part) + if unanchorable is not None: + return unanchorable + if pin_lines is None: + return PartResult( + part, "GONE", None, None, None, [], "absent at the pinned revision" + ) + if part.start is None: + return PartResult(part, "ANCHORED", None, None, None, []) + if part.last > len(pin_lines): + return PartResult( + part, "GONE", None, None, None, [], "beyond the end of the file" + ) + if not tokens: + # Nothing to look for: an entry with neither a hash nor a name (an + # analysis block, a `filename:` field) cannot be checked against its + # own revision. Calling it anchored made the check vacuously true, and + # a pin advanced without a recale hid a ref that had become false. + return PartResult( + part, "UNCHECKED", None, None, None, [], + "entry declares no value to look for", + ) + # An archive ref cites the line declaring the set. Its members follow, one + # per line, so the window reaches forward as far as the entry has members. + reach = SELF_CHECK_CONTEXT + len(tokens) + lo = max(0, part.start - 1 - SELF_CHECK_CONTEXT) + hi = min(len(pin_lines), part.last + reach) + window = "\n".join(pin_lines[lo:hi]).lower() + if any(token in window for token in tokens): + return PartResult(part, "ANCHORED", None, None, None, []) + return _value_elsewhere(part, pin_lines, hash_tokens) + + +def _value_elsewhere( + part: RefPart, pin_lines: list[str], hash_tokens: Sequence[str] +) -> PartResult: + """A part whose cited window lacks the value, judged by where it is.""" + # A ref that cites loading logic never spells the value out, so its absence + # here proves nothing. Only finding the value somewhere else in the file + # shows the ref points at the wrong place, and only a hash carries that + # weight. A filename fragment matches by coincidence: `tyrian2` hits every + # line naming `tyrian2.c`, and a name built at runtime from a format + # string appears nowhere at all. + if not hash_tokens: + return PartResult(part, "ANCHORED", None, None, None, []) + elsewhere = sorted( + { + index + for index, line in enumerate(pin_lines, 1) + for token in hash_tokens + if token.lower() in line.lower() + } + ) + if not elsewhere: + return PartResult(part, "ANCHORED", None, None, None, []) + if len(elsewhere) > 1: + return PartResult( + part, "AMBIGUOUS", None, None, None, elsewhere, + "declared value carried by several lines", + ) + # Content-level evidence, stronger than a line-shift heuristic: the value + # sits on exactly one line, so the recale target is not a guess. + return PartResult( + part, "MOVED", None, elsewhere[0], elsewhere[0], elsewhere, + "declared value occurs once at the pinned revision", + ) + + +def reconcile_self_check( + parts: list[PartResult], eligible: list[bool] | None = None +) -> list[PartResult]: + """Judge a self-checked entry whole rather than part by part. + + A ref often cites both the table carrying the value and the code that + loads the file. Once one part anchors on the value, the others in the + SAME file describe behaviour and cannot be judged by value: the checker + would only rediscover the value where the first part already points. A + part in another file carries its own value and is judged on it: geolith's + jg.c part, sixteen lines off, was settled by an anchored libretro.c part. + A structurally absent part still counts, and a part that was compared + against HEAD rather than self-checked is never rewritten. + """ + eligible = eligible if eligible is not None else [True] * len(parts) + # The settled part lies in the tree its anchor was found in: without the + # repository, attribution wrote dosbox-pure-unleashed's ROMInfo line + # under `source`, a repository that carries no mt32emu.h. + anchored_files: dict[str, str | None] = {} + for part, ok in zip(parts, eligible): + if ok and part.status == "ANCHORED": + anchored_files.setdefault(part.part.path, part.repo) + kept = ("ANCHORED", "GONE", "EXTERNAL", "BINARY") + return [ + PartResult( + part.part, "ANCHORED", None, None, None, [], + repo=anchored_files[part.part.path], + ) + if ok and part.status not in kept and part.part.path in anchored_files + else part + for part, ok in zip(parts, eligible) + ] diff --git a/scripts/profiling/bump.py b/scripts/profiling/bump.py new file mode 100644 index 000000000..4a15ea760 --- /dev/null +++ b/scripts/profiling/bump.py @@ -0,0 +1,274 @@ +"""Advancing a profile's pin, with the version it states read again at the new pin.""" + +from __future__ import annotations + +import functools +import os +import re +import sys +from pathlib import Path + +import yaml + +from core import upstream +from model.platforms import load_platform_config +from model.profiles import load_emulator_profiles +from model.registry import list_registered_platforms +from profiling import version_basis +from profiling.anchoring import REVIEW_STATUSES +from profiling.rebase import pending_recale +from profiling.reports import ProfileReport +from profiling.resolver import NO_SOURCE_REF +from profiling.yamledit import apply_edit, insert_after_line, replace_field_line, set_version_fields +from resolve.cores import resolve_platform_cores + + +DEFAULT_CACHE = upstream.CACHE_DIR + + +def apply_readings(document: dict, readings: list) -> tuple[object, dict | None]: + """core_version and version_basis as the readings state them. + + A reading that could not be made leaves its build mode as written. + """ + version = document.get("core_version") + basis = document.get("version_basis") + basis = dict(basis) if isinstance(basis, dict) else None + if isinstance(version, dict): + version = dict(version) + for reading in readings: + if reading.status not in (version_basis.MATCH, version_basis.DRIFT): + continue + if reading.mode: + if not isinstance(version, dict): + version = {} + version[reading.mode] = reading.read + else: + version = reading.read + if reading.updates and basis is not None: + target = basis if not reading.mode else dict(basis.get(reading.mode) or {}) + target.update(dict(reading.updates)) + if reading.mode: + basis[reading.mode] = target + return version, basis + + +def write_version(path: Path, readings: list) -> bool: + """Set core_version (and a tag basis' tag and distance) from the readings.""" + text = path.read_text(encoding="utf-8") + document = yaml.safe_load(text) + version, basis = apply_readings(document, readings) + if version == document.get("core_version") and basis == document.get("version_basis"): + return False + expected = dict(document) + expected["core_version"] = version + if basis is not None: + expected["version_basis"] = basis + apply_edit(path, set_version_fields(text, version, basis), expected) + return True + + +def _needs_review(report: ProfileReport, accept_changed: bool) -> bool: + """Whether the report leaves a ref to read before the pin may move.""" + # A profile citing nothing has nothing to read again: anarch's pin sat + # behind a CI-only commit through every bulk advance. + if report.skipped not in (None, NO_SOURCE_REF) or not report.head or report.pinned_tag: + return True + blocking = REVIEW_STATUSES if not accept_changed else ( + s for s in REVIEW_STATUSES if s != "CHANGED" + ) + return any((report.counts or {}).get(s) for s in blocking) + + +@functools.lru_cache(maxsize=4) +def _platform_cores(emulators_dir: str, platforms_dir: str) -> tuple[tuple[str, frozenset[str]], ...]: + profiles = load_emulator_profiles(emulators_dir) + return tuple( + (platform, frozenset(resolve_platform_cores( + load_platform_config(platform, platforms_dir), profiles + ))) + for platform in list_registered_platforms(platforms_dir) + ) + + +def shipping_platforms( + name: str, emulators_dir: str, platforms_dir: str = "platforms", +) -> list[str]: + """The registered platforms whose cores resolve to this profile.""" + return [ + platform + for platform, cores in _platform_cores( + os.path.realpath(emulators_dir), platforms_dir + ) + if name in cores + ] + + +def bump_commit( + path: Path, report: ProfileReport, accept_changed: bool = False, + cache_dir: str = DEFAULT_CACHE, offline: bool = False, + platforms_dir: str = "platforms", +) -> bool: + """Advance source_commit to HEAD when nothing needs a read again. + + core_version moves with the pin, read again from version_basis at the + new revision. Four bulk advances moved 295 pins and no version: an + advance whose version cannot be read is refused. + """ + if not report.head or _needs_review(report, accept_changed): + return False + text = path.read_text(encoding="utf-8") + if pending_recale(report, accept_changed, text): + return False + document = yaml.safe_load(text) + if document.get("built_by") and not document.get("pin_follows"): + # The pin is the commit a platform builds (recalbox gsplus.mk, the + # mame0258 its package fetches), not a point on a moving branch: + # HEAD is a build nobody ships under this name. A build of the + # branch tip (RetroPie's advmame) is HEAD, and follows it. + print( + f"{path.stem}: pinned to the build {', '.join(document['built_by'])} " + "ships, advanced by hand", + file=sys.stderr, + ) + return False + if not (document.get("pin_frozen") or document.get("pin_follows")): + shipped = shipping_platforms(path.stem, str(path.parent), platforms_dir) + if len(shipped) == 1: + # One platform ships it, so the pin is the revision that platform + # builds: a bulk advance moved fheroes2, supertux and + # supermariowar past what EmuELEC compiles, and rigelengine is + # already one commit ahead of its package.mk. + print( + f"{path.stem}: only {shipped[0]} ships it. Read the revision its " + "build compiles and set pin_frozen, or pin_follows when it " + "builds the branch tip", + file=sys.stderr, + ) + return False + if any(isinstance(document.get(f), dict) for f in ("source_commit", "upstream_commit")): + # A pin per build mode names one revision per repository, and the + # report heads only the primary one: rewriting str(dict) was a + # refused write on mesence, supermodel and xemu. + print( + f"{path.stem}: pins keyed by build mode are advanced by hand", + file=sys.stderr, + ) + return False + # An upstream pinned on a release tag is the base the port was cut from: + # its HEAD is another program, so the bump leaves it on the tag. + # A profile citing nothing compared nothing upstream: writing that HEAD + # would claim gme's vendored libgme or hbmame's port is current. + upstream_moves = bool( + report.upstream_head + and report.upstream_pin != report.upstream_head + and not report.upstream_tag + and report.skipped != NO_SOURCE_REF + ) + if document.get("source_commit") == report.head and not upstream_moves: + # Rewriting the pin to the value it already holds is not a bump, and + # announcing it buries the profiles that did move. + return False + if ( + document.get("source_commit") != report.head + and not cites_primary(report) + ): + # Every ref anchored in another repository: HEAD of this one was + # never compared with anything. gam4980's pin moved to the fork's + # HEAD while its bare libretro.c refs still described upstream. + print( + f"{path.stem}: no ref resolves in {report.repo}, its pin is not advanced", + file=sys.stderr, + ) + return False + expected = dict(document) + expected["source_commit"] = report.head + new_text = _set_pin(text, document, "source_commit", report.head, "profiled_date") + if document.get("pin_follows") and report.head: + new_text, expected = _follow_config_reads(new_text, expected, document, report.head) + if upstream_moves and report.upstream_head: + expected["upstream_commit"] = report.upstream_head + new_text = _set_pin( + new_text, document, "upstream_commit", report.upstream_head, "source_commit" + ) + readings = version_basis.check(expected, cache_dir, offline, recompute=True) + refused = version_refusal(readings) + if refused: + print(f"{path.stem}: {refused}, its pin is not advanced", file=sys.stderr) + return False + version, basis = apply_readings(expected, readings) + if version != expected.get("core_version") or basis != expected.get("version_basis"): + new_text = set_version_fields(new_text, version, basis) + expected["core_version"] = version + if basis is not None: + expected["version_basis"] = basis + apply_edit(path, new_text, expected) + return True + + +def version_refusal(readings: list) -> str | None: + """Why the version at a new pin cannot be stated, or None.""" + for reading in readings: + if reading.status == version_basis.NO_BASIS: + return "no version_basis says where its version is read" + if reading.status == version_basis.UNREADABLE: + return f"core_version cannot be read at the new pin: {reading.where}" + if reading.status == version_basis.UNCHECKED: + return f"core_version is {reading.where}, not read from the code at a pin" + return None + + +def pins_changed(before: dict, after: dict) -> list[str]: + """The pin fields whose value a write changed.""" + return [ + pin for pin in ("source_commit", "upstream_commit") + if after.get(pin) and after.get(pin) != before.get(pin) + ] + + +def cites_primary(report: ProfileReport) -> bool: + """Whether a ref resolved in the repository source_commit pins. + + A part attributed to another declared repository carries its slug, and one + read in the primary carries none. A profile citing nothing proves + nothing either way and is not held back. + """ + if len(report.repos) < 2: + return True + parts = [ + part for entry in report.entries for part in entry.parts + if part.status != "EXTERNAL" + ] + return not parts or any(part.repo is None for part in parts) + + +def _follow_config_reads( + text: str, expected: dict, document: dict, head: str +) -> tuple[str, dict]: + """A config_reads entry read at the pin moves with a pin that follows the + branch tip: RetroPie installs advmame from the tip, so the build its + advmame.rc is read at is the pin itself.""" + pin = str(document.get("source_commit") or "") + reads = document.get("config_reads") or [] + if not pin or not any(isinstance(r, dict) and r.get("revision") == pin for r in reads): + return text, expected + text = re.sub( + r'^(\s*revision:\s*)(["\']?)' + re.escape(pin) + r"\2\s*$", + lambda m: f"{m.group(1)}{m.group(2)}{head}{m.group(2)}", + text, flags=re.M, + ) + expected = {**expected, "config_reads": [ + {**r, "revision": head} if isinstance(r, dict) and r.get("revision") == pin else r + for r in reads + ]} + return text, expected + + +def _set_pin(text: str, document: dict, field: str, value: str, after: str) -> str: + """Write *field* as *value*, replacing its line or adding one after *after*.""" + current = document.get(field) + if current == value: + return text + if current: + return replace_field_line(text, field, str(current), value)[0] + return insert_after_line(text, after, f'{field}: "{value}"') diff --git a/scripts/profiling/citations.py b/scripts/profiling/citations.py new file mode 100644 index 000000000..08d7a8911 --- /dev/null +++ b/scripts/profiling/citations.py @@ -0,0 +1,379 @@ +"""Every citation a profile makes, in source_ref fields and in prose, with the tokens that identify it.""" + +from __future__ import annotations + +import bisect +import os +import posixpath +import re +from collections import Counter +from collections.abc import Iterable, Iterator +from dataclasses import dataclass +from typing import Any + +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 + + +HASH_FIELDS = ("sha1", "md5", "crc32", "sha256", "known_hash_adler32") + + +def entry_hashes(entry: dict) -> list[str]: + """Content values an entry declares, its own and its members'. + + An archive entry carries no hash of its own: MAME and FBNeo hash the ROMs + inside the container, so `contents[]` is where its content values live. + Ignoring them leaves the archive stem as the only token, and a stem like + `esh` matches seventy lines of its own driver. + """ + tokens: list[str] = [] + contents = entry.get("contents") + members = contents if isinstance(contents, list) else [] + for source in (entry, *members): + if not isinstance(source, dict): + continue + for field_name in HASH_FIELDS: + for v in parse_hash_list(source.get(field_name)): + if v not in tokens: + tokens.append(v) + return tokens + + +def collect_tokens(entry: dict) -> list[str]: + """Values declared by one file entry: hashes, then name as fallback.""" + tokens = entry_hashes(entry) + if not tokens: + name = entry.get("name", "") + if name: + tokens.append(os.path.basename(name).lower()) + return tokens + + +_VERSION_NOTE_RE = re.compile(r"\(\s*v?(\d+(?:\.\d+)+)\s*\)") + + +def cites_another_version(part: RefPart, core_version: object) -> str | None: + """The version a part's annotation names, when it is not the documented one. + + A profile may cite two programs side by side, `main.cpp:407-411 (v0.0.11)` + beside its v0.16.0 pin. The annotation was dropped as prose and the part + compared with the pin, where it fell on unrelated code yet read ANCHORED. + """ + match = _VERSION_NOTE_RE.search(part.raw or "") + if not match: + return None + declared = core_version.values() if isinstance(core_version, dict) else [core_version] + documented = {str(v or "").strip().lstrip("vV") for v in declared} + return None if match.group(1) in documented else match.group(1) + + +def is_external_citation(path: str) -> bool: + """True when a ref names a project rather than a path in a known repo. + + `munt ROMInfo.cpp` and `Nuked-SC55-CLAP rom_io.cpp` cite where a ROM is + identified inside a dependency the profile does not declare, so no + revision of the declared repositories can confirm or deny them. Reporting + them as broken would be wrong: they are simply out of reach. + """ + if " " not in path: + return False + head = path.split(" ", 1)[0] + return "/" not in head and "." not in head + + +_VERSION_WORD_RE = re.compile(r"v?\d+(\.\d+)+[\w.-]*") + + +def strip_repo_word( + path: str, known: frozenset[str] | set[str] = frozenset() +) -> str: + """Drop a leading project word that names a declared repository. + + `BAM_FPloader FPLoader.cpp:45` follows the `munt ROMInfo.cpp` convention, + but BAM_FPloader is the profile's own source: the path is FPLoader.cpp, + to be found in that tree, not an out-of-reach citation. + """ + if " " not in path: + return path + role, rest = ref_role(path) + if role: + return rest + head, _, tail = path.partition(" ") + if head.lower() in known and tail and "/" not in head and "." not in head: + tail = tail.strip() + # `ArcadeFlashWeb v1.0.2 flash/flash.html` names the release the file + # was read from. The version is not part of the path either. + version, _, rest = tail.partition(" ") + if rest and _VERSION_WORD_RE.fullmatch(version): + return rest.strip() + return tail + return path + + +def narrow_by_cited(matches: list[str], cited_dirs: Iterable[str]) -> list[str]: + """Keep the candidates under the deepest directory the profile cites. + + A bare `version.h` matches BasiliskII/src/include/version.h and + SheepShaver/src/include/version.h in cebix/macemu. The profile's own + source_refs all live under SheepShaver/src, which is the tree the prose + meant. Candidates under no cited directory are kept only when none is. + """ + best = -1 + kept: list[str] = [] + for match in matches: + depth = max( + (d.count("/") + 1 for d in cited_dirs if match.startswith(d + "/")), + default=-1, + ) + if depth > best: + best, kept = depth, [match] + elif depth == best: + kept.append(match) + return kept if best >= 0 else matches + + +def symlink_target(path: str, lines: list[str] | None) -> str | None: + """The file a one-line blob points at, when the blob is a symbolic link. + + Git stores a symlink as a blob holding its target, and the raw endpoints + serve that text: SheepShaver/src/Unix/ether_unix.cpp is the single line + ../../../BasiliskII/src/Unix/ether_unix.cpp. Anchoring a line number in + it would report every citation as beyond the end of the file. + """ + if not lines or len(lines) != 1: + return None + target = lines[0].strip() + if not target or " " in target or "/" not in target: + return None + if posixpath.basename(target) != posixpath.basename(path): + return None + resolved = posixpath.normpath(posixpath.join(posixpath.dirname(path), target)) + return None if resolved.startswith("..") or resolved == path else resolved + + +def _anchor_tokens(entry: dict) -> list[str]: + """Values worth searching for when a cited line misses its subject. + + Both the content values and the set name are searched, because profiles + cite two different shapes. FBNeo and Hypseus refs point at the ROM table, + where the CRC32s are, while MAME refs point at the machine declaration, which + carries the set name and no hash at all. + """ + tokens = collect_tokens(entry) + name = os.path.basename(str(entry.get("name", ""))).lower() + stem = name.rsplit(".", 1)[0] + if stem and stem not in tokens and len(stem) > 2: + tokens.append(stem) + return tokens + + +@dataclass(frozen=True) +class Citation: + """One citation carried by a profile, wherever it is written. + + A structured citation lives under a source_ref key and keeps its entry + for the values it declares. A prose citation is a path:line run found + inside any other string scalar. Its spans locate the path and every + range token inside the scalar line, so a recale can move the numbers + and leave the sentence around them alone. + """ + + field: str + kind: str + ref: str + entry: dict | None = None + holder: object = None + key: object = None + label: str = "" + line: int = 0 + path_span: tuple[int, int] = (0, 0) + spans: tuple = () + parts: tuple = () + # Broken across two lines of a literal block. joined_at is where the + # second line starts in the joined run line, so each span goes back to + # the line that holds it. + wrapped: bool = False + joined_at: int = 0 + # Written in a YAML comment, which the loaded document drops. line is + # then the physical line of the file and the spans index it. + in_comment: bool = False + + +def _list_segments(items: list) -> list[object]: + """The field path segment naming each list item. + + A name keys its entry. supermodel declares Games.xml twice, once per + build mode, and both notes answered to files[Games.xml].note: the + history read one note's citations against the other's text. A repeated + name takes its build mode, or its index when the mode repeats too. + """ + keys: list[tuple[str | None, str | None]] = [] + for item in items: + name = item.get("name") if isinstance(item, dict) else None + mode = item.get("mode") if isinstance(item, dict) else None + keys.append(( + None if name is None else str(name), + mode if isinstance(mode, str) else None, + )) + names = Counter(name for name, _ in keys if name is not None) + modes = Counter(key for key in keys if key[0] is not None) + segments: list[object] = [] + for index, (name, mode) in enumerate(keys): + if name is None: + segments.append(index) + elif names[name] == 1: + segments.append(name) + elif mode and modes[(name, mode)] == 1: + segments.append(f"{name}@{mode}") + else: + segments.append(f"{name}#{index}") + return segments + + +def _walk_document( + node: object, where: str = "" +) -> Iterator[tuple[str, str, dict | list, object, Any]]: + """Every citation carrier of the document, with its holder and field path. + + Yields ("ref", field, holder, key, value) for each source_ref key and + ("str", field, holder, key, value) for every other string scalar. One + walker builds every field path, so the paths that name a scalar today + still name it when another pass looks it up at another revision. + """ + if isinstance(node, dict): + for key, value in node.items(): + spot = f"{where}.{key}" if where else str(key) + if key == "source_ref": + yield "ref", spot, node, key, value + elif isinstance(value, str): + yield "str", spot, node, key, value + else: + yield from _walk_document(value, spot) + elif isinstance(node, list): + segments = _list_segments(node) + for index, item in enumerate(node): + spot = f"{where}[{segments[index]}]" + if isinstance(item, str): + yield "str", spot, node, index, item + else: + yield from _walk_document(item, spot) + + +def _scalar_spans(node: yaml.Node, spans: list[tuple[int, int]]) -> None: + if isinstance(node, yaml.ScalarNode): + spans.append((node.start_mark.index, node.end_mark.index)) + elif isinstance(node, yaml.MappingNode): + for key, value in node.value: + _scalar_spans(key, spans) + _scalar_spans(value, spans) + elif isinstance(node, yaml.SequenceNode): + for item in node.value: + _scalar_spans(item, spans) + + +def comment_citations( + text: str, known: frozenset[str] | set[str] = frozenset() +) -> list[Citation]: + """Citation runs written in the YAML comments of a profile. + + The loader drops comments, so a pin moved without them: zesarux cites + src/start.c:4870-4874 in a comment written under a revision where those + lines now hold the --chroma81 branch. A `#` inside a scalar, quoted or in + a block, is content and not a comment. + """ + try: + root = yaml.compose(text, Loader=yaml.SafeLoader) + except yaml.YAMLError: + return [] + spans: list[tuple[int, int]] = [] + if root is not None: + _scalar_spans(root, spans) + spans.sort() + starts = [lo for lo, _ in spans] + + def inside_scalar(index: int) -> bool: + at = bisect.bisect_right(starts, index) - 1 + return at >= 0 and spans[at][0] <= index < spans[at][1] + + citations: list[Citation] = [] + offset = 0 + for number, line in enumerate(text.splitlines(keepends=True)): + for column, char in enumerate(line): + if char != "#" or (column and not line[column - 1].isspace()): + continue + if inside_scalar(offset + column): + continue + comment = line[column + 1:].rstrip("\r\n") + shift = column + 1 + for _, path_span, run_spans, parts, run, _ in _prose_runs(comment, known): + citations.append(Citation( + field=f"comment line {number + 1}", kind="prose", ref=run, + line=number, + path_span=(path_span[0] + shift, path_span[1] + shift), + spans=tuple((lo + shift, hi + shift) for lo, hi in run_spans), + parts=parts, in_comment=True, + )) + break + offset += len(line) + return citations + + +def collect_citations(document: dict, text: str | None = None) -> list[Citation]: + """Every citation the document carries, in document order. + + A source_ref key anywhere in the document is a structured citation: + files[] holds most of them, data_directories[] carries some too. Every + other string scalar is scanned for prose runs. A citation is a property + of the text, not of a field name: anything this walk misses is a + location profile_sync cannot keep honest. + """ + known = frozenset( + repo.name.lower() + for _, _, url in declared_repositories(document) + if (repo := upstream.parse_repo(url)) is not None + ) + citations: list[Citation] = [] + for kind, field_name, holder, key, value in _walk_document(document): + if kind == "ref": + for label, ref in source_ref_values(value): + citations.append(Citation( + field=field_name, kind="ref", ref=ref, + entry=holder if isinstance(holder, dict) else None, + holder=holder, key=key, label=label, + )) + if isinstance(value, str): + for line, path_span, spans, parts, run, joined_at in _prose_runs( + ref_prose_text(value), known + ): + citations.append(Citation( + field=field_name, kind="prose", ref=run, holder=holder, + key=key, line=line, path_span=path_span, spans=spans, + parts=parts, wrapped=bool(joined_at), joined_at=joined_at, + )) + continue + for line, path_span, spans, parts, run, joined_at in _prose_runs( + value, known + ): + citations.append(Citation( + field=field_name, kind="prose", ref=run, holder=holder, key=key, + line=line, path_span=path_span, spans=spans, parts=parts, + wrapped=bool(joined_at), joined_at=joined_at, + )) + if text is not None: + citations.extend(comment_citations(text, known)) + return citations + + +def _citation_key(ref: str) -> str: + """A citation compared without the spacing the author chose. + + A recale rewrites the located tokens and leaves the sentence alone, so + `a.c:228, 1439-1443` keeps its space while the rendered form drops it. + They are the same citation, and comparing them literally makes a run + that was written look unwritten, which holds the pin back for ever. + """ + return re.sub(r"\s+", "", ref) diff --git a/scripts/profiling/detection.py b/scripts/profiling/detection.py new file mode 100644 index 000000000..62113aae1 --- /dev/null +++ b/scripts/profiling/detection.py @@ -0,0 +1,317 @@ +"""Files and hashes upstream gained since the pin, and the diffs of cited paths.""" + +from __future__ import annotations + +import argparse +import difflib +import os +import posixpath +import re +import sys + +from core import upstream +from core.upstream import CompareResult +from profiling.citations import collect_tokens +from profiling.reports import ProfileReport +from profiling.repos import select_repo + + +FILE_EXTENSIONS = ( + "bin", "rom", "zip", "dat", "bios", "img", "chd", "nvram", "sav", + "ips", "pal", "fnt", +) + +_FILE_RE = re.compile( + r"[\w./+-]+\.(?:" + "|".join(FILE_EXTENSIONS) + r")\b", re.IGNORECASE +) +_SHA1_RE = re.compile(r"\b[0-9a-fA-F]{40}\b") +_MD5_RE = re.compile(r"\b[0-9a-fA-F]{32}\b") +_CRC_RE = re.compile(r"\b(?:0x)?([0-9a-fA-F]{8})\b") + + +def declared_names(profile: dict) -> set[str]: + """Every filename the profile covers, aliases included.""" + names = set() + for entry in profile.get("files") or []: + if not isinstance(entry, dict): + continue + for value in [entry.get("name", "")] + list(entry.get("aliases") or []): + if value: + names.add(os.path.basename(str(value)).casefold()) + return names + + +def declared_hashes(profile: dict) -> set[str]: + """Every hash the profile declares.""" + hashes = set() + for entry in profile.get("files") or []: + if isinstance(entry, dict): + hashes.update(collect_tokens(entry)) + return hashes + + +# Sources where a filename is a string. Outside a literal or a comment, +# `EE.bios` is a config key and `psm_s.pal` a struct field (pcee2). +CODE_SUFFIXES = ( + ".c", ".cc", ".cpp", ".cxx", ".h", ".hh", ".hpp", ".hxx", ".inc", ".inl", + ".m", ".mm", ".rs", ".java", ".kt", ".cs", ".swift", +) +_PRINTF_SPEC_RE = re.compile( + r"[-+ #0]*(?:\d+|\*)?(?:\.\d+)?(?:hh|h|ll|l|z|j|t|L)?[diouxXsScpfFeEgGaA]" +) +_TITLE_WORD_RE = re.compile(r"[A-Z0-9][\w.()&'+-]*") + + +def _string_literal(line: str, index: int) -> tuple[int, int] | None: + """Bounds of the double-quoted literal holding index, None outside one.""" + start = None + position = 0 + while position < len(line): + char = line[position] + if char == "\\" and start is not None: + position += 2 + continue + if char == '"': + if start is None: + start = position + 1 + else: + if start <= index < position: + return start, position + start = None + position += 1 + return None + + +def _in_comment(line: str, index: int) -> bool: + before = line[:index] + return "//" in before or "/*" in before or line.lstrip().startswith(("*", "#")) + + +def file_literal(line: str, start: int, end: int, code: bool) -> str | None: + """The filename a match stands for, None when it names no file. + + A printf conversion glued to the name is not part of it: sdl2trs writes + `"%slevel2.rom"` with ROM_PATH in front. A name with spaces is read + whole: virtualjaguar opens `"Jaguar CD BIOS.rom"`, and the match alone + was `BIOS.rom`. Such a name is written as a title, so `"Please insert + bios.bin"` and `"Packing patches.zip"` still yield the file alone. In + a code file a match outside any string or comment is an identifier and + names nothing. + """ + name = line[start:end] + if start and line[start - 1] == "%": + spec = _PRINTF_SPEC_RE.match(name) + if spec: + name = name[spec.end():] + literal = _string_literal(line, start) + if literal is None: + if code and not _in_comment(line, start): + return None + elif literal[1] == end and line[start - 1:start] == " ": + words = line[literal[0]:start].split() + [name] + if all(_TITLE_WORD_RE.fullmatch(word) for word in words): + return line[literal[0]:end] + base = os.path.basename(name) + # `".bin.hex.iii.rom"` (sdl2trs) lists extensions, it names no file. + if base.startswith(".") or not _FILE_RE.fullmatch(base): + return None + return base + + +def detect_new_files( + head_lines: list[str], known: set[str], cap: int = 25, path: str = "" +) -> tuple[list[tuple[int, str]], int]: + """Filename literals at HEAD the profile does not declare.""" + found: list[tuple[int, str]] = [] + seen: set[str] = set() + elided = 0 + code = path.lower().endswith(CODE_SUFFIXES) + for number, line in enumerate(head_lines, start=1): + for match in _FILE_RE.finditer(line): + literal = file_literal(line, match.start(), match.end(), code) + if literal is None: + continue + name = literal.casefold() + if name in known or name in seen: + continue + seen.add(name) + if len(found) >= cap: + elided += 1 + continue + found.append((number, literal)) + return found, elided + + +def watch_hashes(added_lines: list[str], known: set[str]) -> list[tuple[str, str]]: + """Hash literals in added lines that no profile entry declares.""" + found: list[tuple[str, str]] = [] + for line in added_lines: + strong = bool(_SHA1_RE.search(line) or _MD5_RE.search(line)) + for pattern, kind in ((_SHA1_RE, "sha1"), (_MD5_RE, "md5")): + found.extend( + (kind, value.lower()) for value in pattern.findall(line) + if value.lower() not in known + ) + if strong or not _FILE_RE.search(line): + continue + found.extend( + ("crc32", value.lower()) for value in _CRC_RE.findall(line) + if value.lower() not in known + ) + return found + + +def unified_for_path( + pin_lines: list[str], head_lines: list[str], path: str, context: int +) -> str: + """Unified diff of one cited file between the two revisions.""" + diff = difflib.unified_diff( + pin_lines, + head_lines, + fromfile=f"a/{path}", + tofile=f"b/{path}", + n=context, + lineterm="", + ) + return "\n".join(diff) + + +def tree_diff(result: CompareResult, ref_dirs: set[str]) -> list[str]: + """Tree changes restricted to the directories the refs point at.""" + lines = [] + for change in result.files: + directory = posixpath.dirname(change.path) + if ref_dirs and not any( + directory == d or directory.startswith(d + "/") for d in ref_dirs + ): + continue + if change.status == "renamed" and change.previous_path: + lines.append(f"renamed {change.previous_path} -> {change.path}") + else: + lines.append(f"{change.status} {change.path}") + if result.truncated: + lines.append("comparison truncated by the forge, list is partial") + return lines + + +def _cited_paths(report: ProfileReport) -> list[str]: + return sorted( + {part.new_path or part.part.path for e in report.entries for part in e.parts} + ) + + +def _detect( + args: argparse.Namespace, profile: dict, report: ProfileReport, + repo: upstream.Repo, +) -> None: + names, hashes = declared_names(profile), declared_hashes(profile) + new_files: list[dict] = [] + new_hashes: list[dict] = [] + for path in _cited_paths(report): + head_lines = upstream.fetch_file( + repo, report.head, path, args.cache_dir, args.offline + ) + if head_lines is None: + continue + pin_lines = ( + upstream.fetch_file(repo, report.pin, path, args.cache_dir, args.offline) + or [] + ) + if args.detect_new_files: + found, elided = detect_new_files(head_lines, names, path=path) + at_pin = { + name.casefold() + for _, name in detect_new_files( + pin_lines, names, cap=sys.maxsize, path=path + )[0] + } + new_files.extend( + {"path": path, "line": number, "name": name, + "since_pin": name.casefold() not in at_pin} + for number, name in found + ) + if elided: + report.new_files_elided[path] = elided + if args.watch_hashes: + added = [ + line + for line in difflib.unified_diff(pin_lines, head_lines, n=0) + if line.startswith("+") and not line.startswith("+++") + ] + new_hashes.extend( + {"path": path, "kind": kind, "value": value} + for kind, value in watch_hashes(added, hashes) + ) + if args.detect_new_files: + report.new_files = new_files + if args.watch_hashes: + report.new_hashes = new_hashes + + +def _print_detection(report: ProfileReport) -> None: + if report.detection_skipped: + print(f" extras skipped: {report.detection_skipped}") + for item in report.new_files or (): + print(f" new file: {item['path']}:{item['line']} {item['name']}") + for path, elided in report.new_files_elided.items(): + print(f" new file: {path}: {elided} further matches not shown") + for item in report.new_hashes or (): + print(f" new {item['kind']}: {item['path']} {item['value']}") + + +def _print_extras( + args: argparse.Namespace, profile: dict, report: ProfileReport +) -> None: + """Optional per-profile sections beyond the ref report. + + The ref report mutes a forge that refuses, but the extras read the same + forge again, so a refusal here is reported on the profile's own line + instead of ending a pass over every profile. A quota signal still + ends the profile. + """ + _print_detection(report) + if not (args.full_diff or args.tree_diff) or not report.head: + return + repo = select_repo(profile) + if repo is None: + return + try: + _print_extras_from(args, profile, report, repo) + except upstream.RateLimitError: + raise + except upstream.UpstreamError as exc: + print(f" extras skipped: {exc}") + + +def _print_extras_from( + args: argparse.Namespace, profile: dict, report: ProfileReport, + repo: upstream.Repo, +) -> None: + if args.full_diff: + for path in _cited_paths(report): + if args.ref and args.ref not in path: + continue + pin_lines = ( + upstream.fetch_file( + repo, report.pin, path, args.cache_dir, args.offline + ) + or [] + ) + head_lines = ( + upstream.fetch_file( + repo, report.head, path, args.cache_dir, args.offline + ) + or [] + ) + text = unified_for_path(pin_lines, head_lines, path, args.context) + if text: + print(text) + if args.tree_diff: + comparison = upstream.compare( + repo, report.pin, report.head, args.cache_dir, args.offline + ) + # The repository root is a directory like any other: dropping the + # empty dirname would turn the filter off and list the whole tree. + ref_dirs = {posixpath.dirname(p) for p in _cited_paths(report)} + for line in tree_diff(comparison, ref_dirs): + print(f" {line}") diff --git a/scripts/profiling/proseedit.py b/scripts/profiling/proseedit.py new file mode 100644 index 000000000..13983eeec --- /dev/null +++ b/scripts/profiling/proseedit.py @@ -0,0 +1,324 @@ +"""Moves of citation locations written inside YAML scalars and comments.""" + +from __future__ import annotations + +import re +from collections.abc import Sequence +from dataclasses import dataclass, field + +import yaml + +from model.sourceref import RefPart +from profiling.citations import Citation + + +_RANGE_TOKEN_RE = re.compile(r"\d+(?:-\d+)?") + + +def _run_after_moves( + parts: Sequence[RefPart], + moves: dict[int, tuple[int, int, str | None]], + run: str = "", +) -> str: + """The prose run as it reads once its moved ranges are rewritten. + + The path is the one written in the run: a part can carry a project word + or the tree path the report resolved it to, neither of which the + sentence spells. + """ + new_path = next( + (move[2] for move in moves.values() if move[2]), None + ) or (run.split(":", 1)[0] if run else None) + ranges = [] + for index, part in enumerate(parts): + if index in moves: + start, end, _ = moves[index] + ranges.append(f"{start}" if end == start else f"{start}-{end}") + else: + ranges.append(part.raw) + # The separators stay as written. boom3 writes `libretro.cpp:1635-1643, + # 2384-2405` in a folded scalar, rewritten whole there, and a joined + # `3238-3246,4245-4266` no longer read as the intended document. + written = run.partition(":")[2] + tokens = list(_RANGE_TOKEN_RE.finditer(written)) + if len(tokens) == len(parts) and all( + token.group() == part.raw for token, part in zip(tokens, parts) + ): + for token, rendered in reversed(list(zip(tokens, ranges))): + written = written[:token.start()] + rendered + written[token.end():] + return f"{new_path or parts[0].path}:{written}" + return f"{new_path or parts[0].path}:{','.join(ranges)}" + + +def _unique_line( + lines: list[str], target: str, window: tuple[int, int] +) -> int | None: + """The one line of the window carrying target, None when not exactly one.""" + hits = [ + i for i in range(window[0], min(window[1], len(lines) - 1) + 1) + if target and target in lines[i] + ] + return hits[0] if len(hits) == 1 else None + + +def scalar_windows( + text: str, document: object +) -> dict[tuple[int, object], tuple[int, int]]: + """Physical lines each scalar spans, keyed by its holder and key. + + The same sentence can sit in two fields: emucorex cites + CMakeLists.txt:706 in notes and in exclusion_note, on identical lines. + Searched over the whole file neither occurrence was unique, and only + the node's own lines tell them apart. + """ + try: + root = yaml.compose(text, Loader=yaml.SafeLoader) + except yaml.YAMLError: + return {} + windows: dict[tuple[int, object], tuple[int, int]] = {} + + def walk(value: object, node: yaml.Node | None) -> None: + if isinstance(value, dict) and isinstance(node, yaml.MappingNode): + by_key = {str(k.value): v for k, v in node.value} + for key, item in value.items(): + child = by_key.get(str(key)) + if child is None: + continue + windows[(id(value), key)] = ( + child.start_mark.line, child.end_mark.line + ) + walk(item, child) + elif isinstance(value, list) and isinstance(node, yaml.SequenceNode): + for index, (item, child) in enumerate(zip(value, node.value)): + windows[(id(value), index)] = ( + child.start_mark.line, child.end_mark.line + ) + walk(item, child) + + walk(document, root) + return windows + + +def _scalar(holder: object, key: object) -> object: + """The value a citation's holder keeps under its key.""" + if isinstance(holder, list) and isinstance(key, int): + return holder[key] + if isinstance(holder, dict): + return holder[key] + raise TypeError(f"no scalar under {key!r} in a {type(holder).__name__}") + + +def _set_scalar(holder: object, key: object, value: str) -> None: + """Replace the value a citation's holder keeps under its key.""" + if isinstance(holder, dict): + holder[key] = value + return + if not isinstance(holder, list) or not isinstance(key, int): + raise TypeError(f"no scalar under {key!r} in a {type(holder).__name__}") + holder[key] = value + + +Span = tuple[int, int, str] + + +@dataclass +class _ProseEdits: + """Replacements gathered over a file and its loaded scalars, and what + was written or left.""" + + lines: list[str] + windows: dict[tuple[int, object], tuple[int, int]] + whole: tuple[int, int] + file_edits: dict[int, list[Span]] = field(default_factory=dict) + value_edits: dict[tuple[int, object], dict[int, list[Span]]] = field(default_factory=dict) + holders: dict[tuple[int, object], tuple[object, object]] = field(default_factory=dict) + applied: list[str] = field(default_factory=list) + left: list[str] = field(default_factory=list) + + +def _prose_replacements( + citation: Citation, moves: dict[int, tuple[int, int, str | None]] +) -> list[Span]: + """The tokens of one run a recale rewrites, measured on its line.""" + replacements: list[Span] = [ + (*citation.spans[index], f"{start}" if end == start else f"{start}-{end}") + for index, (start, end, _) in moves.items() + ] + new_path = next((m[2] for m in moves.values() if m[2]), None) + if new_path: + replacements.append((*citation.path_span, new_path)) + return replacements + + +def _edit_comment_run( + edits: _ProseEdits, citation: Citation, replacements: list[Span], recaled: str +) -> None: + # The comment is read again from the text being written: a run + # whose tokens no longer sit at the collected offsets is left. + lines = edits.lines + line = lines[citation.line] if citation.line < len(lines) else "" + expected = [p.raw for p in citation.parts] + [citation.ref.split(":", 1)[0]] + spans = list(citation.spans) + [citation.path_span] + if any(line[lo:hi] != token for (lo, hi), token in zip(spans, expected)): + edits.left.append(f"{citation.field}: {citation.ref} (comment changed)") + return + edits.file_edits.setdefault(citation.line, []).extend(replacements) + edits.applied.append(f"{citation.field}: {citation.ref} -> {recaled}") + + +def _split_by_line( + citation: Citation, value_lines: list[str], last: int, replacements: list[Span] +) -> dict[int, list[Span]] | None: + """Replacements by the scalar line holding them, None when a token + spans the line break.""" + # A wrapped run puts its spans back on the two scalar lines it + # joined: playdiaemu wrote `content.rs` and `:111-199` one line apart. + by_line: dict[int, list[Span]] = {} + for lo, hi, new in replacements: + if citation.wrapped and lo >= citation.joined_at: + following = value_lines[last] + indent = len(following) - len(following.lstrip()) + shift = indent - citation.joined_at + by_line.setdefault(last, []).append((lo + shift, hi + shift, new)) + elif citation.wrapped and hi > citation.joined_at: + return None + else: + by_line.setdefault(citation.line, []).append((lo, hi, new)) + return by_line + + +def _lines_apart(located: dict[int, int | None], first: int, last: int) -> bool: + """Whether the two lines of a wrapped run are not found together.""" + if None in located.values(): + return True + if len(located) != 2: + return False + low, high = located[first], located[last] + return low is None or high is None or high != low + 1 + + +def _edit_folded_run( + edits: _ProseEdits, citation: Citation, value_lines: list[str], + window: tuple[int, int], recaled: str, +) -> bool: + """Rewrite a run whose scalar is folded, False when it is left.""" + # A folded scalar has no physical line carrying its logical one. + # The run itself is a single word, which folding never breaks: + # when the whole file carries it exactly once, that occurrence + # is the citation, and the run is rewritten as one token. The + # match is delimited, or `x.c:481` would also hit inside a + # neighbouring `x.c:481-482`. + lines = edits.lines + pattern = re.compile( + rf"(? None: + value = str(_scalar(citation.holder, citation.key)) + value_lines = value.splitlines() + last = citation.line + (1 if citation.wrapped else 0) + if last >= len(value_lines): + edits.left.append(f"{citation.field}: {citation.ref} (scalar changed)") + return + by_line = _split_by_line(citation, value_lines, last, replacements) + if by_line is None: + edits.left.append(f"{citation.field}: {citation.ref} (a token spans the line break)") + return + window = edits.windows.get((id(citation.holder), citation.key), edits.whole) + located = { + number: _unique_line(edits.lines, value_lines[number], window) + for number in by_line + } + if citation.wrapped and _lines_apart(located, citation.line, last): + edits.left.append( + f"{citation.field}: {citation.ref} " + "(wrapped, and its two lines are not found together in the file)" + ) + return + placed = {number: hit for number, hit in located.items() if hit is not None} + if len(placed) == len(located): + for number, line_edits in by_line.items(): + hit = placed[number] + offset = edits.lines[hit].index(value_lines[number]) + edits.file_edits.setdefault(hit, []).extend( + (offset + lo, offset + hi, new) for lo, hi, new in line_edits + ) + elif not _edit_folded_run(edits, citation, value_lines, window, recaled): + return + slot = (id(citation.holder), citation.key) + edits.holders[slot] = (citation.holder, citation.key) + for number, line_edits in by_line.items(): + edits.value_edits.setdefault(slot, {}).setdefault(number, []).extend(line_edits) + edits.applied.append(f"{citation.field}: {citation.ref} -> {recaled}") + + +def _apply_prose_edits( + text: str, jobs: list[tuple[Citation, dict]], document: object = None +) -> tuple[str, list[str], list[str]]: + """Rewrite prose citation tokens in place, nothing else. + + Each scalar line a run touches is located as the unique physical line + carrying it. Two candidate lines, or none, means the write cannot be + proved right, so the run is left alone and reported with the reason. A + comment run is rewritten on its own physical line, once the tokens there + read as collected. All replacements of one line are applied together, + right to left, so the spans measured on the original text stay valid. + """ + lines = text.splitlines() + trailing = "\n" if text.endswith("\n") else "" + edits = _ProseEdits( + lines, + scalar_windows(text, document) if document is not None else {}, + (0, len(lines) - 1), + ) + + for citation, moves in jobs: + replacements = _prose_replacements(citation, moves) + recaled = _run_after_moves(citation.parts, moves, citation.ref) + if citation.in_comment: + _edit_comment_run(edits, citation, replacements, recaled) + else: + _edit_scalar_run(edits, citation, replacements, recaled) + + for number, spans in edits.file_edits.items(): + lines[number] = _replace_spans(lines[number], spans) + for slot, by_line in edits.value_edits.items(): + holder, key = edits.holders[slot] + value = str(_scalar(holder, key)) + value_lines = value.splitlines() + for line_number, spans in by_line.items(): + value_lines[line_number] = _replace_spans(value_lines[line_number], spans) + _set_scalar(holder, key, "\n".join(value_lines) + ( + "\n" if value.endswith("\n") else "" + )) + return "\n".join(lines) + trailing, edits.applied, edits.left + + +def _replace_spans(line: str, replacements: list[tuple[int, int, str]]) -> str: + """Apply span replacements measured on the original line.""" + for lo, hi, new in sorted(replacements, reverse=True): + line = line[:lo] + new + line[hi:] + return line diff --git a/scripts/profiling/realign.py b/scripts/profiling/realign.py new file mode 100644 index 000000000..9b81f9a8e --- /dev/null +++ b/scripts/profiling/realign.py @@ -0,0 +1,529 @@ +"""Prose citations written under an older pin, moved to the pin the profile carries now.""" + +from __future__ import annotations + +import itertools +import posixpath +import subprocess +from collections.abc import Iterable +from dataclasses import dataclass, field +from pathlib import Path + +import yaml + +from core import upstream +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 profiling.yamledit import apply_edit + + +def _git_history(path: Path) -> list[str] | None: + """Commits touching the profile, newest first, or None when git cannot say. + + An untracked profile has no history. A git that fails (no repository, + dubious ownership, git missing) has not answered, and reading that as + "nothing written under an older pin" let a bump through. + """ + def git(*argv: str) -> subprocess.CompletedProcess: + return subprocess.run( + ["git", *argv], cwd=path.parent, capture_output=True, text=True, check=False, + ) + + try: + result = git("log", "--format=%H", "--", path.name) + deleted = git("log", "--format=%H", "--diff-filter=D", "--", path.name) + if result.returncode == 0 and deleted.returncode == 0: + # A commit that deletes or renames the file away ends the history + # of the profile now at that path: what precedes it is another + # profile once filed under the same name (mupen64plus.yml became + # mupen64plus_next.yml before the standalone took the name). + history = result.stdout.split() + ends = set(deleted.stdout.split()) + cut = next((i for i, sha in enumerate(history) if sha in ends), len(history)) + return history[:cut] + # A repository with no commit yet holds nothing written earlier. + inside = git("rev-parse", "--is-inside-work-tree") + if inside.stdout.strip() == "true" and git("rev-parse", "-q", "--verify", "HEAD").returncode: + return [] + except OSError: + pass + return None + + +def _git_file_at(path: Path, sha: str) -> dict | None: + """The profile as it was at one commit, None when unreadable.""" + result = subprocess.run( + ["git", "show", f"{sha}:./{path.name}"], + cwd=path.parent, capture_output=True, text=True, check=False, + ) + if result.returncode != 0: + return None + try: + document = yaml.safe_load(result.stdout) + except yaml.YAMLError: + return None + return document if isinstance(document, dict) else None + + +def _pin_of( + document: dict, field: str, mode: str = "", + repo: upstream.Repo | None = None, +) -> str | None: + """A pin as the profile records it, read for one build mode when keyed. + + With `repo`, a pin the document declares for another repository under + the field and mode does not count. A past revision can pin another + repository under the same field: xenia-edge's source_commit named the + has207/xenia-edge sha until the field became a dict keyed by mode, and + read for the libretro mode that old string passed for a pin of the + GitLab port that moved under unchanged prose. + """ + value = document.get(field) + if isinstance(value, dict): + value = value.get(mode) if mode else None + if not (isinstance(value, str) and value): + return None + if repo is not None: + url_field = field.removesuffix("_commit") + declared = { + (mode_key, url) + for name, mode_key, url in declared_repositories(document) + if name == url_field + } + url = next( + (u for m, u in declared if m == mode), + next((u for m, u in declared if not m), None), + ) + owner = upstream.parse_repo(url) if url else None + if owner is not None and (owner.host, owner.slug.lower()) != ( + repo.host, repo.slug.lower() + ): + return None + return value + + +def _writing_pins( + revisions: list[tuple[str, dict]], intro_sha: str, field: str, mode: str = "", + repo: upstream.Repo | None = None, +) -> list[str]: + """Distinct pins committed while the scalar held its current text. + + The pin at the introducing commit stands for the scalars that predate + it (the backfill resolved missing pins from profiled_date). A pin that + moved later under the same text is either a bump that left the prose + behind or a correction putting the pin on the revision the text already + described: vitaquake2 was written under 34f0888, then re-pinned to + 59a5115 by a commit that touched nothing else. The history cannot tell + the two apart, so every pin the text lived under is returned. + """ + index = next( + (i for i, (sha, _) in enumerate(revisions) if sha == intro_sha), None + ) + if index is None: + return [] + pins: list[str] = [] + for position in range(index, -1, -1): + value = _pin_of(revisions[position][1], field, mode, repo) + if value and value not in pins: + pins.append(value) + return pins + + +def _resolve_at( + repo: upstream.Repo, sha: str, path: str, cache_dir: str, offline: bool +) -> tuple[str, tuple[str, list[str]] | str | None]: + """("found", (path, lines)) or ("unclear"|"missing", reason). + + Prose often cites a bare filename. The tree at the revision resolves it + when exactly one path carries that name. An unreadable or truncated + tree, or several paths carrying the name, proves nothing: those come + back "unclear", never "missing", because a verdict of absence taken on + a tree that could not be read would rot the citation with confidence. + """ + candidates = [path] + head, _, tail = path.partition("/") + if tail and head == repo.name: + candidates.append(tail) + for candidate in candidates: + lines = upstream.fetch_file(repo, sha, candidate, cache_dir, offline) + if lines is not None: + return "found", (candidate, lines) + if "/" not in path: + tree, truncated = upstream.list_tree(repo, sha, cache_dir, offline) + matches = [p for p in tree or [] if posixpath.basename(p) == path] + if len(matches) == 1: + lines = upstream.fetch_file( + repo, sha, matches[0], cache_dir, offline + ) + if lines is not None: + return "found", (matches[0], lines) + elif len(matches) > 1: + return "unclear", f"{len(matches)} paths carry the name" + elif truncated or not tree: + return "unclear", "tree unreadable at that revision" + return "missing", None + + +def _realign_part( + part: RefPart, + pairs: list[tuple[upstream.Repo, str, str]], + cache_dir: str, + offline: bool, + pinned: Iterable[tuple[upstream.Repo, str]] = (), +) -> tuple[str, tuple[int, int] | str] | None: + """One range, anchored from its writing revision to the current pin. + + `pinned` lists every declared (repository, current pin). A path no moved + repository carries but an unmoved one does lives where nothing moved: + it needs no move, and calling it absent made two readings of the same + history disagree on every run (nethersx2's AetherSX2 citations). + """ + if is_external_citation(part.path): + return None + # The path as written decides first, as attribution does: a prefix is + # stripped only when no declared repository carries the path itself. + # hatarib's hatari/src/tos.c lives in hatariB, which did not move, and + # stripping the prefix read it as hatari/hatari's src/tos.c instead. + # Between two repositories that both carry the path, the one declared + # first owns it, as in attribution: freej2me-plus and hex007/freej2me + # both hold src/libretro/, and the citations describe the fork. + literal = [ + (repo, written, current) for repo, written, current in pairs + if upstream.fetch_file(repo, written, part.path, cache_dir, offline) is not None + ] + moved = {repo for repo, _written, _current in pairs} + order = [repo for repo, _pin in pinned] + first_moved = min( + (order.index(repo) for repo, _w, _c in literal if repo in order), + default=len(order), + ) + if any( + upstream.fetch_file(repo, pin, part.path, cache_dir, offline) is not None + for index, (repo, pin) in enumerate(pinned) + if repo not in moved and (not literal or index < first_moved) + ): + return None + unclear: tuple[str, list[str]] | str | None = None + for repo, written, current in literal or pairs: + state, payload = _resolve_at(repo, written, part.path, cache_dir, offline) + if state == "unclear": + unclear = payload + continue + if not isinstance(payload, tuple): + continue + actual, old_lines = payload + new_lines = upstream.fetch_file( + repo, current, actual, cache_dir, offline + ) + if new_lines is None: + return "skip", f"{part.path} absent at the current pin" + anchored = anchor_block( + old_lines, new_lines, part.start, part.end or part.start + ) + if anchored.status == "ANCHORED": + return None + if anchored.status == "SHIFTED" and anchored.start is not None and anchored.end is not None: + return "ok", (anchored.start, anchored.end) + return "skip", f"{part.path}:{part.start} {anchored.status.lower()}" + if unclear: + return "skip", f"{part.path}: {unclear}" + if any( + upstream.fetch_file(repo, pin, part.path, cache_dir, offline) is not None + for repo, pin in pinned + if repo not in moved + ): + return None + return "skip", f"{part.path} absent at the writing revision" + + +def _writing_pairs( + document: dict, repos: list, revisions: list[tuple[str, dict]], intro_sha: str, + served: dict | None = None, +) -> tuple[list[list], list[str]]: + """Every reading of where the text was written, and the ambiguities. + + Each reading lists (repo, writing pin, current pin) per declared + repository. A repository whose text lived under several pins gives one + reading per pin, and the caller settles them when they all agree. + + `prose_written_at` names the pin a reader confirmed the prose against. + Among several candidate pins it is the answer: fixnes's doOpenFDSBIOS + citation lived under four pins, exact at the last, and with nothing to + record that reading the bump stayed refused for good. It never + overrides a history with a single candidate. + """ + options: list[list] = [] + ambiguous: list[str] = [] + attested = str(document.get("prose_written_at") or "") + for pin_field, mode, repo in repos: + current = _pin_of(document, f"{pin_field}_commit", mode) + if not current: + continue + written = _writing_pins( + revisions, intro_sha, f"{pin_field}_commit", mode, repo + ) + if attested in written: + written = [attested] + if len(written) > 1: + ambiguous.append(f"{pin_field}_commit {' -> '.join(written)}") + # The current pin among the candidates reads as "no move" (None) for + # that repository, which the other candidates must then agree with. + reader = (served or {}).get(repo, repo) + options.append([ + (reader, pin, current) if pin != current else None for pin in written + ]) + readings = [ + [pair for pair in combo if pair is not None] + for combo in itertools.product(*options) + ] + return readings, ambiguous + + +def _realign_citation( + citation: Citation, + readings: list[list], + ambiguous: list[str], + cache_dir: str, + offline: bool, + pinned: Iterable[tuple[upstream.Repo, str]] = (), +) -> tuple[dict[int, tuple[int, int, str | None]], list[str]]: + """The moves one prose run needs, and what blocks them. + + Several pins under one text: the history cannot say which the text + describes, but when the cited range lands in the same place from every + one of them the question does not matter. When the readings disagree, + moving from the wrong one rewrites a correct citation onto someone + else's code, with nothing to show it happened. + """ + moves: dict[int, tuple[int, int, str | None]] = {} + blocked: list[str] = [] + for index, part in enumerate(citation.parts): + outcomes = { + repr(result): result + for result in ( + _realign_part(part, pairs, cache_dir, offline, pinned) if pairs else None + for pairs in readings + ) + } + if len(outcomes) > 1: + blocked.extend( + f"pin moved under this text: {item}; the readings disagree" + for item in ambiguous + ) + continue + outcome = next(iter(outcomes.values())) + if outcome is None: + continue + _state, payload = outcome + if isinstance(payload, tuple): + moves[index] = (payload[0], payload[1], None) + else: + blocked.append(payload) + return moves, blocked + + +@dataclass(frozen=True) +class _ProseRepos: + """The repositories prose is read in, as realign_prose reads them.""" + + # (pin field, build mode, repository) for each declared repository. + repos: list[tuple[str, str, upstream.Repo]] + # A source declared gone, read through its mirror. + served: dict[upstream.Repo, upstream.Repo] + # Every declared (repository, current pin). + pinned: list[tuple[upstream.Repo, str]] + + +def _prose_repos(document: dict) -> _ProseRepos | None: + """The repositories the profile's prose is read in, None when it declares none.""" + # Every repository the profile declares, by build mode where it keys + # them so: eka2l1, mesence, supermodel and xemu pin a dict, and reading + # str(dict) found no repository at all, so their prose was never judged. + repos = [ + (field, mode, repo) + for field, mode, url in declared_repositories(document) + if field != "source_mirror" and (repo := upstream.parse_repo(url)) is not None + ] + if not repos: + return None + # A source declared gone is read through its mirror, which carries the + # same commits: teknoparrot's notes stayed blocked once + # teknogods/TeknoParrotUI answered 404, every reading of the old pin + # calling the files absent. + mirror = ( + upstream.parse_repo(str(document.get("source_mirror") or "")) + if document.get("upstream_gone") else None + ) + served = { + repo: mirror for field, _mode, repo in repos + if mirror is not None and field == "source" + } + pinned = [ + (served.get(repo, repo), pin) + for field, mode, repo in repos + if (pin := _pin_of(document, f"{field}_commit", mode)) + ] + return _ProseRepos(repos, served, pinned) + + +@dataclass +class _ProseHistory: + """Where each prose citation came into the profile's history.""" + + # The commit that introduced each citation's text, None when uncommitted. + intro: list[str | None] + revisions: list[tuple[str, dict]] = field(default_factory=list) + # Citations whose writing pin lies past a revision that cannot be read: + # where their text came in is unknown, so they are not moved. + unknown: dict[int, str] = field(default_factory=dict) + + +def _carried_by(past: dict) -> set[tuple[str, str]]: + return { + (c.field, _citation_key(c.ref)) + for c in collect_citations(past) if c.kind == "prose" + } + + +def _date_prose( + path: Path, document: dict, keys: list[tuple[str, str]], history: list[str] +) -> _ProseHistory: + """Follow each citation back through the commits that carry its text.""" + alive = [True] * len(keys) + found = _ProseHistory([None] * len(keys)) + # A revision that does not parse is passed over unless the pins differ + # on its two sides while the citation is absent across it: then the + # text may have come in there under either pin. Otherwise nothing + # it could hold changes the reading. lindbergh-loader + # committed invalid YAML once (df7346b3), fixed the same day, and every + # note older than that commit stayed blocked for good. + gap: str | None = None + for sha in history: + if not any(alive): + break + past = _git_file_at(path, sha) + if past is None: + gap = gap or sha + continue + held = _carried_by(past) + if gap: + newer = found.revisions[-1][1] if found.revisions else document + moved = any( + newer.get(pin) != past.get(pin) + for pin in ("source_commit", "upstream_commit") + ) + for index, live in enumerate(alive): + if live and moved and keys[index] not in held: + found.unknown[index] = gap + alive[index] = False + gap = None + found.revisions.append((sha, past)) + for index, live in enumerate(alive): + if not live: + continue + if keys[index] in held: + found.intro[index] = sha + else: + alive[index] = False + if gap: + # The oldest revisions do not parse: where the text came in is past + # what can be read. + found.unknown.update({index: gap for index, live in enumerate(alive) if live}) + return found + + +def _prose_jobs( + document: dict, + citations: list[Citation], + dated: _ProseHistory, + declared: _ProseRepos, + cache_dir: str, + offline: bool, +) -> tuple[list[str], list[tuple[Citation, dict]]]: + """The moves each dated citation needs, and the messages on the others.""" + messages: list[str] = [] + jobs: list[tuple[Citation, dict]] = [] + for index, citation in enumerate(citations): + if index in dated.unknown: + messages.append( + f"read again: {citation.field}: {citation.ref} " + f"(revision {dated.unknown[index][:8]} unreadable)" + ) + continue + intro_sha = dated.intro[index] + if intro_sha is None: + # The scalar is not committed yet: written now, under this pin. + continue + readings, ambiguous = _writing_pairs( + document, declared.repos, dated.revisions, intro_sha, declared.served + ) + if not any(readings): + continue + moves, blocked = _realign_citation( + citation, readings, ambiguous, cache_dir, offline, declared.pinned + ) + if blocked: + # Half a run must not move: the untouched ranges would read as + # already realigned when they were never even located. + messages.extend( + f"read again: {citation.field}: {citation.ref} ({reason})" + for reason in blocked + ) + continue + if moves: + jobs.append((citation, moves)) + return messages, jobs + + +def realign_prose( + path: Path, cache_dir: str, offline: bool = False, dry_run: bool = False +) -> list[str]: + """Recale prose runs from the pin their text was written at. + + A prose run written under an older pin describes that revision, and + anchoring it from the current pin would faithfully track the wrong + content: the cited line holds someone else's code there. The writing pin + is read from the profile's own history, at the commit that introduced + the scalar's current text, and only when no other pin was committed + under that text since. + """ + text = path.read_text(encoding="utf-8") + document = yaml.safe_load(text) + if not isinstance(document, dict): + return [] + citations = [c for c in collect_citations(document) if c.kind == "prose"] + if not citations: + return [] + declared = _prose_repos(document) + if declared is None: + return [] + history = _git_history(path) + if history is None: + return [f"read again: {path.name}: git history unreadable"] + if not history: + return [] + + # Each citation is followed on its own: a field's text changes whenever + # one of its citations is corrected by hand, and dating the whole field + # from that commit put idtech4a++'s untouched Game.java:69-72 under the + # current pin, so the stale range was never offered again. + keys = [(c.field, _citation_key(c.ref)) for c in citations] + dated = _date_prose(path, document, keys, history) + messages, jobs = _prose_jobs(document, citations, dated, declared, cache_dir, offline) + + if dry_run: + messages.extend( + f"would recale {citation.field}: {citation.ref} -> " + f"{_run_after_moves(citation.parts, moves, citation.ref)}" + for citation, moves in jobs + ) + return messages + new_text, applied, left = _apply_prose_edits(text, jobs, document) + messages.extend(applied) + messages.extend(f"left in place: {item}" for item in left) + if applied: + apply_edit(path, new_text, document) + return messages diff --git a/scripts/profiling/rebase.py b/scripts/profiling/rebase.py new file mode 100644 index 000000000..a464d2b03 --- /dev/null +++ b/scripts/profiling/rebase.py @@ -0,0 +1,396 @@ +"""Recaling refs and prose citations to HEAD, and what blocks a pin from moving.""" + +from __future__ import annotations + +import sys +from pathlib import Path + +import yaml + +from core import upstream +from model.sourceref import ref_role, split_outside_parentheses, split_source_ref +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 profiling.yamledit import YamlWriteError, apply_edit, find_field_line, replace_field_line + + +def _is_rewritable(ref: str) -> bool: + """Every ref whose parts can be put back with their prose intact.""" + parts = split_source_ref(ref) + if not parts: + return False + return ", ".join(p.raw for p in parts) == ", ".join( + chunk for chunk in split_outside_parentheses(ref) + ) + + +def _match_citation( + citations: list[Citation], pos: int, entry: EntryReport +) -> tuple[Citation | None, int]: + """First document citation at or after pos carrying the entry's ref. + + The pairing is by content and order, never by position alone: a + mode-keyed source_ref yields two report entries for one document line, + which is exactly the desynchronisation a positional pairing trips on. + """ + for index in range(pos, len(citations)): + citation = citations[index] + if citation.kind != entry.kind or citation.ref != entry.source_ref: + continue + if entry.kind == "prose" and entry.field and citation.field != entry.field: + continue + return citation, index + 1 + return None, pos + + +def _prose_moves( + entry: EntryReport, statuses: tuple[str, ...] +) -> dict[int, tuple[int, int, str | None]]: + """Recale targets per part of one prose run, empty when unsafe. + + A run cites one file: parts that disagree on where that file went, or a + rename that would leave untouched ranges pointing into the old file, + cannot be written without guessing. + """ + moves: dict[int, tuple[int, int, str | None]] = {} + renames = set() + for index, part in enumerate(entry.parts): + if part.status not in statuses or part.start is None: + continue + same_place = (part.start, part.end) == (part.part.start, part.part.end) + if same_place and not part.new_path: + continue + moves[index] = (part.start, part.end or part.start, part.new_path) + if part.new_path: + renames.add(part.new_path) + if len(renames) > 1: + return {} + if renames and len(moves) != len(entry.parts): + return {} + return moves + + +def _part_moves(part: PartResult, movable: tuple[str, ...]) -> bool: + """Whether a part has a new location the recale would write.""" + return ( + part.status in movable + and part.start is not None + and ((part.start, part.end) != (part.part.start, part.part.end) + or bool(part.new_path)) + ) + + +def pin_blockers(report: ProfileReport, accept_changed: bool = False) -> list[str]: + """What would keep source_commit where it is after a recale, named. + + Two kinds. A ref the writer never regenerates, annotated or under a mode + key, whose parts have moved. And a prose run whose parts disagree on + where its file went, which cannot be written without guessing. Either + one makes bump_commit refuse, so either one has to stop the recale as + well, and the caller says which. + """ + movable = REBASE_STATUSES + (("CHANGED",) if accept_changed else ()) + blockers: list[str] = [] + for entry in report.entries or []: + if entry.kind == "prose": + if not _prose_moves(entry, movable) and any( + _part_moves(part, movable) for part in entry.parts + ): + blockers.append( + f"{entry.field}: {entry.source_ref} " + "(its parts do not agree on one new file)" + ) + continue + if not any( + part.status in movable + and part.start is not None + and _rendered_part(part) != _original_part(part) + for part in entry.parts + ): + continue + if entry.name.endswith("]"): + blockers.append( + f"{entry.name}: {entry.source_ref} (under a mode key, moved by hand)" + ) + elif not _is_rewritable(entry.source_ref): + blockers.append( + f"{entry.name}: {entry.source_ref} " + "(annotated, a rewrite would drop the annotation)" + ) + return blockers + + +ATTRIBUTED_STATUSES = ("ANCHORED", "SHIFTED", "RENAMED", "MOVED", "CHANGED") + + +def attribute_refs(path: Path, report: ProfileReport, profile: dict) -> list[str]: + """Open each cited path with the role of the repository its lines are in. + + A profile reading a libretro port and the original emulator cited both + trees the same way, and a reader could not tell whether the core or the + emulator asked for the file. The role is the one this comparison found + the lines in, so a word already written that disagrees is corrected. + Parts whose lines were found nowhere keep what they have. + """ + if len(report.repos) < 2: + return [] + role_of: dict[str, str] = {} + for field_name, _mode, url in declared_repositories(profile): + repo = upstream.parse_repo(url) + if repo is not None: + role_of.setdefault(repo.slug, "source" if field_name == "source_mirror" + else field_name) + if len(set(role_of.values())) < 2: + return [] + primary = role_of.get(report.repos[0], "source") + text = path.read_text(encoding="utf-8") + applied: list[str] = [] + cursor = 0 + for entry in report.entries: + if entry.kind != "ref" or entry.name.endswith("]"): + continue + if not _is_rewritable(entry.source_ref): + continue + rendered = [] + for part in entry.parts: + raw = part.part.raw or _original_part(part) + role, bare = ref_role(raw) + written_path = bare.startswith(part.part.path) + owner = role_of.get(part.repo or "", primary) if part.repo else primary + if part.status in ATTRIBUTED_STATUSES and written_path and owner != role: + rendered.append(f"{owner} {bare}") + else: + rendered.append(raw) + new_ref = ", ".join(rendered) + if new_ref == entry.source_ref: + continue + try: + text, index = replace_field_line( + text, "source_ref", entry.source_ref, new_ref, cursor + ) + except YamlWriteError: + continue + cursor = index + 1 + applied.append(f"{entry.source_ref} -> {new_ref}") + if applied: + path.write_text(text, encoding="utf-8") + return applied + + +def _moved_by_hand(entry: EntryReport, statuses: tuple[str, ...]) -> bool: + """Whether a ref the writer cannot regenerate has a part to move.""" + return any( + part.status in statuses + and part.start is not None + and _rendered_part(part) != _original_part(part) + for part in entry.parts + ) + + +def _recaled_ref(entry: EntryReport, statuses: tuple[str, ...]) -> str | None: + """The source_ref with its moved parts rendered, None when none moves.""" + rendered = [] + touched = False + for part in entry.parts: + if part.status in statuses and part.start is not None: + recaled = _rendered_part(part) + if recaled != _original_part(part): + touched = True + rendered.append(recaled) + else: + rendered.append(_original_part(part)) + return ", ".join(rendered) if touched else None + + +def _recale_statuses( + path: Path, report: ProfileReport, accept_changed: bool +) -> tuple[str, ...] | None: + """The statuses a recale moves, None when the profile must not move.""" + if report.pinned_tag: + return None + statuses = REBASE_STATUSES + (("CHANGED",) if accept_changed else ()) + blocking = [s for s in REVIEW_STATUSES if s not in statuses] + if any((report.counts or {}).get(s) for s in blocking): + return None + blockers = pin_blockers(report, accept_changed) + if blockers: + # Something here will not move: an annotated ref, one under a mode + # key, or a prose run this pass cannot rewrite without guessing. + # bump_commit will refuse while it stands, and recaling the rest + # would leave the profile describing two revisions at once. All or + # nothing means the pin too, not only the refs. + _refuse_recale(path, blockers) + return None + return statuses + + +def rebase_refs( + path: Path, report: ProfileReport, accept_changed: bool = False +) -> list[str]: + """Recale the line ranges of parts whose content is unchanged. + + All or nothing per profile. `source_commit` names the revision the refs + are written against, so moving some refs to HEAD while others still + describe the pinned revision would leave the profile self-contradictory, + and the next comparison would read the pinned revision at line numbers + that only make sense at HEAD. Prose citations move with everything else: + only their located tokens are rewritten, the sentence stays. + """ + statuses = _recale_statuses(path, report, accept_changed) + if statuses is None: + return [] + text = path.read_text(encoding="utf-8") + document = yaml.safe_load(text) + citations = collect_citations(document, text) + applied: list[str] = [] + refused: list[str] = [] + cursor = 0 + pos = 0 + prose_jobs: list[tuple[Citation, dict]] = [] + + for entry in report.entries: + citation, pos = _match_citation(citations, pos, entry) + if citation is None: + if any(_part_moves(part, statuses) for part in entry.parts): + refused.append( + f"{entry.field or entry.name}: {entry.source_ref} " + "(the profile no longer carries it as reported)" + ) + continue + if entry.kind == "prose": + moves = _prose_moves(entry, statuses) + if moves: + prose_jobs.append((citation, moves)) + continue + if citation.label or entry.name.endswith("]"): + # The ref lives under a mode key, not on a source_ref line. The + # entry name check also holds when the report and the document + # disagree: a stale report must not touch a line it never read. + if _moved_by_hand(entry, statuses): + refused.append( + f"{entry.name}: {entry.source_ref} (under a mode key, moved by hand)" + ) + continue + # Rewriting an annotated ref would drop the author's annotations. + # It, like a ref with nothing to move, still consumes its line so a + # later entry carrying the same ref cannot match it. + new_ref = _recaled_ref(entry, statuses) if _is_rewritable(entry.source_ref) else None + if new_ref is None: + cursor = find_field_line(text, "source_ref", entry.source_ref, cursor) + 1 + continue + text, index = replace_field_line( + text, "source_ref", entry.source_ref, new_ref, cursor + ) + cursor = index + 1 + _set_scalar(citation.holder, citation.key, new_ref) + applied.append(f"{entry.source_ref} -> {new_ref}") + + text, prose_applied, left = _apply_prose_edits(text, prose_jobs, document) + refused.extend(left) + if refused: + # A citation left on the pinned lines holds the pin, so writing the + # others would leave the profile describing two revisions. + _refuse_recale(path, refused) + return [] + applied.extend(prose_applied) + if applied: + apply_edit(path, text, document) + return applied + + +def _refuse_recale(path: Path, reasons: list[str]) -> None: + """Name each citation that keeps a recale from being written.""" + for reason in reasons: + print(f"{path.stem}: not recaled, cannot move {reason}", file=sys.stderr) + + +def _pending_refs( + report: ProfileReport, movable: tuple[str, ...], document: dict | None +) -> int: + """Parts of structured refs that ought to move and have not.""" + # A ref the writer regenerates is pending only while the file still + # carries it as written: a bump run without the recale left lrps2's + # SHIFTED refs on the old lines under the new pin. It is looked up at + # its own field, never by its text alone: tr1x moved cut2 508->531 and + # cut3 531->552, and cut3 read as pending on cut2's new value. + refs_left: dict[tuple[str, str], int] = {} + if document is not None: + for citation in collect_citations(document): + if citation.kind == "ref": + for slot in ((citation.field, citation.ref), ("", citation.ref)): + refs_left[slot] = refs_left.get(slot, 0) + 1 + pending = 0 + for entry in report.entries or []: + if entry.kind == "prose": + continue + if _is_rewritable(entry.source_ref) and not entry.name.endswith("]"): + slot = (entry.field, entry.source_ref) + if not refs_left.get(slot): + continue + refs_left[slot] -= 1 + pending += sum( + 1 + for part in entry.parts + if part.status in movable + and part.start is not None + and _rendered_part(part) != _original_part(part) + ) + return pending + + +def _pending_prose( + report: ProfileReport, movable: tuple[str, ...], document: dict, text: str | None +) -> int: + """Prose runs that ought to move and that the file does not carry moved.""" + pool: dict[tuple[str, str], int] = {} + for citation in collect_citations(document, text): + if citation.kind == "prose": + slot = (citation.field, _citation_key(citation.ref)) + pool[slot] = pool.get(slot, 0) + 1 + pending = 0 + for entry in report.entries or []: + if entry.kind != "prose": + continue + moves = _prose_moves(entry, movable) + if not moves: + # Either nothing has to move, or the run cannot be written + # without guessing, and the second case still blocks the pin. + if any(_part_moves(part, movable) for part in entry.parts): + pending += 1 + continue + rendered = _run_after_moves( + [p.part for p in entry.parts], moves, entry.source_ref + ) + if _citation_key(rendered) == _citation_key(entry.source_ref): + continue + slot = (entry.field, _citation_key(rendered)) + if pool.get(slot, 0) > 0: + pool[slot] -= 1 + continue + pending += 1 + return pending + + +def pending_recale( + report: ProfileReport, accept_changed: bool = False, text: str | None = None +) -> int: + """Parts that ought to move but the writer has not moved. + + An annotated ref cannot be regenerated without losing its prose, so its + parts stay on the pinned line numbers. Advancing the pin while they do + would leave the profile describing two revisions at once. + + Prose runs are judged on the file as it stands: a run that should move + counts as pending until the document actually carries its recaled form, + so the pin cannot advance over prose that still describes the old + revision, whatever the reason it was not rewritten. + """ + movable = REBASE_STATUSES + (("CHANGED",) if accept_changed else ()) + document = yaml.safe_load(text) if text is not None else None + pending = _pending_refs(report, movable, document) + if document is None: + return pending + return pending + _pending_prose(report, movable, document, text) diff --git a/scripts/profiling/reports.py b/scripts/profiling/reports.py new file mode 100644 index 000000000..3a3e7ba49 --- /dev/null +++ b/scripts/profiling/reports.py @@ -0,0 +1,316 @@ +"""The report of one profile, and its text, markdown and JSON forms.""" + +from __future__ import annotations + +from dataclasses import asdict, dataclass, field + +from core import upstream +from model.sourceref import _ANNOTATION_RE, _trim_prose +from profiling.anchoring import PartResult, REVIEW_STATUSES +from profiling.versions import VersionReport + + +@dataclass +class EntryReport: + name: str + source_ref: str + status: str + parts: list[PartResult] + kind: str = "ref" + field: str = "" + + +@dataclass +class ProfileReport: + name: str + repo: str | None = None + repos: list[str] = field(default_factory=list) + pinned_tag: str | None = None + # Set when pinned_tag carries a pin_frozen reason rather than a tag name. + pin_frozen: bool = False + host: str | None = None + pin: str | None = None + pin_origin: str | None = None + head: str | None = None + entries: list[EntryReport] = field(default_factory=list) + skipped: str | None = None + counts: dict[str, int] = field(default_factory=dict) + # Declared repositories whose forge the tool cannot read. They are named + # rather than dropped: a `source` on an unknown host used to fall back to + # `upstream` in silence, and a divergence between the two went unseen. + unread: list[str] = field(default_factory=list) + # The upstream view when the profile declares one: its pin and HEAD. A + # recale moves its parts onto upstream HEAD lines, so a bump advances + # upstream_commit with source_commit, or the profile describes two + # revisions at once (geolith jg.c drifted 213 -> 221 -> 229). + upstream_pin: str | None = None + upstream_head: str | None = None + upstream_origin: str | None = None + # The release tag the upstream view is pinned on, when it is one. That + # view is read at its tag, and a bump leaves upstream_commit there. + upstream_tag: str | None = None + # Repositories whose pin is no ancestor of the branch the profile + # tracks: history was rewritten, or the pin lies on another branch. + orphaned: list[str] = field(default_factory=list) + # Repositories the forge answers under another owner/name: renamed or + # transferred. The redirect is followed in silence, so the move is named + # and counted as review work, since a renamed project is re-profiled. + moved_repos: list[str] = field(default_factory=list) + # A profile off any forge names the page announcing its releases + # (version_source): the version read there, beside the one declared. + declared_version: str | None = None + page_version: str | None = None + # An announced version searched for and found only rebuilt or re-signed. + no_genuine_copy: str | None = None + # What realign_prose has to settle before a recale or a bump can run: + # prose written under an older pin. None when not looked at. Counted to + # review, or the triage read "0 to review" on a profile whose pin could + # not move (blastem, xenia-edge, lindbergh-loader). + stale_prose: list[str] | None = None + # --check-version on a forge: core_version against the latest tag and + # release, kept on the report so every output carries it. + version: VersionReport | None = None + version_warning: str | None = None + # --detect-new-files and --watch-hashes, kept on the report so --json + # carries them to check_freshness. None when not asked. A new file is + # {path, line, name, since_pin}: since_pin when the pinned revision of + # the same file does not carry the name. A new hash is {path, kind, value}. + new_files: list[dict] | None = None + new_files_elided: dict[str, int] = field(default_factory=dict) + new_hashes: list[dict] | None = None + detection_skipped: str | None = None + # config_reads refs written for a build a platform ships, each read at + # that build (pin_build) rather than at source_commit: {platform, + # revision, ref, status, detail}. A status other than BUILD_OK is review + # work. + build_refs: list[dict] = field(default_factory=list) + + # Skips that say the profile cannot be checked as written: a repository + # that no longer answers, no pin to read, a death the forge contradicts. + # --strict passed them, and the CI job meant to stop an unchecked profile + # let a mistyped repository through. + UNCHECKABLE = ( + "no declared repository answers", + "no source_commit and no resolvable profiled_date", + "upstream_gone is declared but the forge answers", + "upstream gone:", + ) + + def uncheckable(self) -> bool: + """Whether the skip is a defect of the profile rather than its nature.""" + return bool(self.skipped and self.skipped.startswith(self.UNCHECKABLE)) + + def needs_review(self) -> int: + counts = self.counts or {} + return ( + sum(counts.get(status, 0) for status in REVIEW_STATUSES) + + len(self.orphaned) + + len(self.stale_prose or ()) + + len(self.moved_repos) + + sum(1 for ref in self.build_refs if ref["status"] != "BUILD_OK") + ) + + +def triage_state(report: ProfileReport) -> str: + """One triage line: what to review, the pin to set again named first. + + A pin outside its branch's history or a repository answering under + another name is fixed before any ref is read: pcem showed as "1 to + review" for an upstream_commit off the dev branch. + """ + if report.skipped: + return report.skipped + state = f"{report.needs_review()} to review" + if report.orphaned: + state += f", {len(report.orphaned)} orphaned pin" + if report.moved_repos: + state += f", {len(report.moved_repos)} repository moved" + stale = len(report.stale_prose or ()) + if stale: + state += f", {stale} prose under an older pin" + unchecked = (report.counts or {}).get("UNCHECKED", 0) + if unchecked: + state += f", {unchecked} unchecked" + return state + + +def _render_span(path: str, start: int | None, end: int | None) -> str: + if start is None: + return path + if end and end != start: + return f"{path}:{start}-{end}" + return f"{path}:{start}" + + +def _original_part(part: PartResult) -> str: + """The part exactly as the profile writes it today.""" + return part.part.raw or _render_span( + part.part.path, part.part.start, part.part.end + ) + + +def _rendered_part(part: PartResult) -> str: + """The part rewritten onto its new location, annotation preserved.""" + span = _render_span(part.new_path or part.part.path, part.start, part.end) + raw = part.part.raw + if not raw: + return span + located = _trim_prose(_ANNOTATION_RE.sub("", raw).strip()) + # `stella src/emucore/Cart.cxx:120` names the repository the lines are + # in. The path was read without that word, and a recale wrote it back + # without it: the attribution went with the old line numbers. + word, _, rest = located.partition(" ") + if rest.startswith(f"{part.part.path}:") and word and "/" not in word: + span = f"{word} {span}" + if located and located in raw: + return raw.replace(located, span, 1) + return span + + +def _part_line(part: PartResult) -> str: + detail = part.status + if part.new_path: + detail += f" -> {part.new_path}" + if part.start is not None and part.status != "ANCHORED": + span = f"{part.start}" + if part.end and part.end != part.start: + span += f"-{part.end}" + detail += f" -> {span}" + if part.candidates: + detail += f" candidats: {', '.join(str(c) for c in part.candidates)}" + if part.reason: + detail += f" ({part.reason})" + if part.repo: + detail += f" [{part.repo}]" + return f" {_original_part(part)} {detail}" + + +def _report_header(report: ProfileReport) -> list[str]: + """Repository, revisions and version lines heading one report.""" + slugs = " + ".join(report.repos) if report.repos else (report.repo or "") + lines = [f"{report.name} {slugs}".rstrip()] + if report.pin and report.head: + lines.append( + f" {report.pin[:7]} -> {report.head[:7]} pin: {report.pin_origin}" + ) + if report.pinned_tag: + held = ( + f"pin held ({report.pinned_tag})" if report.pin_frozen + else f"pinned to tag {report.pinned_tag}" + ) + lines.append(f" {held}: checked against its own revision, not against HEAD") + elif report.pin and report.pin == report.head: + lines.append(" pin is HEAD: checked for self-consistency") + if report.upstream_tag and not report.pinned_tag: + lines.append( + f" upstream pinned to tag {report.upstream_tag}: checked against " + "its own revision, not against HEAD" + ) + lines.extend(f" not read (unsupported host): {url}" for url in report.unread or []) + lines.extend(f" orphaned pin: {line}" for line in report.orphaned) + lines.extend(f" moved repository: {line}" for line in report.moved_repos) + if report.declared_version or report.page_version: + lines.append( + f" version page: declared {report.declared_version or '-'}, " + f"announced {report.page_version or 'unreadable'}" + + (" (no genuine copy found)" + if report.page_version and report.page_version == report.no_genuine_copy + else "") + ) + if report.version: + version = report.version + lines.append( + f" version: declared {version.declared}, " + f"latest tag {version.latest_tag}, " + f"latest release {version.latest_release} ({version.release_date})" + ) + if report.version_warning: + lines.append(f" {report.version_warning}") + return lines + + +def _report_body(report: ProfileReport, changed_only: bool) -> list[str]: + """Ref, prose and build lines of one report that was read.""" + lines = [] + for entry in report.entries: + if changed_only and entry.status not in REVIEW_STATUSES: + continue + lines.append(f" {entry.name} {entry.source_ref}") + lines.extend(_part_line(part) for part in entry.parts) + summary = ", ".join( + f"{count} {status.lower()}" for status, count in sorted(report.counts.items()) + ) + lines.append(f" {len(report.entries)} refs: {summary}") + lines.extend(f" prose under an older pin: {line}" for line in report.stale_prose or ()) + for ref in report.build_refs: + if changed_only and ref["status"] == "BUILD_OK": + continue + detail = f": {ref['detail']}" if ref["detail"] else "" + lines.append( + f" {ref['status']} at {ref['platform']} build " + f"{ref['revision'][:12] or '-'} {ref['ref']}{detail}" + ) + lines.append(f" {report.needs_review()} to review") + return lines + + +def format_report(report: ProfileReport, changed_only: bool = False) -> str: + """Human-readable report for one profile.""" + lines = _report_header(report) + if report.skipped: + lines.append(f" skipped: {report.skipped}") + return "\n".join(lines) + return "\n".join(lines + _report_body(report, changed_only)) + + +def report_to_dict(report: ProfileReport) -> dict: + """Serialisable form of one report.""" + payload = asdict(report) + payload["needs_review"] = report.needs_review() + return payload + + +def format_markdown(reports: list[ProfileReport]) -> str: + """Report set as a markdown document.""" + lines = [ + "# profile-sync", + "", + "| profile | repository | refs | to review |", + "|---|---|---|---|", + ] + for report in reports: + if report.skipped: + lines.append( + f"| {report.name} | {report.repo or ''} | skipped | {report.skipped} |" + ) + continue + lines.append( + f"| {report.name} | {report.repo} | {len(report.entries)} " + f"| {report.needs_review()} |" + ) + for report in reports: + lines.extend(["", "```", format_report(report), "```"]) + return "\n".join(lines) + + +def fetch_plan(report: ProfileReport) -> list[str]: + """Raw URLs at HEAD for the refs that need the code read again.""" + if report.skipped or not report.head or not report.host: + return [] + owner, _, name = (report.repo or "").partition("/") + fallback = upstream.make_repo(report.host, owner, name) + urls = [] + for entry in report.entries: + if entry.status not in REVIEW_STATUSES: + continue + for part in entry.parts: + url = part.head_url + if url is None: + if fallback is None: + continue + path = part.new_path or part.part.path + url = upstream.raw_url(fallback, report.head, path) + if part.part.start: + url += f"#L{part.part.start}" + urls.append(url) + return urls diff --git a/scripts/profiling/repos.py b/scripts/profiling/repos.py new file mode 100644 index 000000000..d2d990ec5 --- /dev/null +++ b/scripts/profiling/repos.py @@ -0,0 +1,412 @@ +"""The repositories a profile declares and the revision each one is read at.""" + +from __future__ import annotations + +import re +from collections.abc import Iterable +from dataclasses import dataclass, replace + +from core import upstream + + +PIN = "pin" +HEAD = "head" + + +@dataclass(frozen=True) +class RepoView: + """One repository a profile cites, at the two revisions under comparison.""" + + repo: upstream.Repo + pin: str + head: str + origin: str + field: str + # The build mode a keyed source names this repository under (ymir, + # xenia-edge): `standalone` or `libretro`, empty for a plain URL. + mode: str = "" + + +def select_repo(profile: dict) -> upstream.Repo | None: + """Repository the profile was read from: source first, then upstream. + + A source keyed by build mode with no upstream (uzdoom) names its + repositories only in the dict, which a plain string read never parses. + """ + for field_name in ("source", "upstream"): + repo = upstream.parse_repo(str(profile.get(field_name) or "")) + if repo is not None: + return repo + for _, _, url in declared_repositories(profile): + repo = upstream.parse_repo(url) + if repo is not None: + return repo + return None + + +def resolve_pin( + profile: dict, repo: upstream.Repo, cache_dir: str, offline: bool, + field: str = "source", mode: str = "", branch: str | None = None, +) -> tuple[str, str] | tuple[None, None]: + """Commit the profile was written at, and how it was obtained. + + `field` selects which declared pin applies: `source_commit` for the port + the profile was read from, `upstream_commit` for the original project. + 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" + if pinned: + return str(pinned), origin + date = str(profile.get("profiled_date") or "") + if not date: + return None, None + sha = upstream.resolve_commit_at(repo, date, cache_dir, offline, branch) + return (sha, f"profiled_date {date}") if sha else (None, None) + + +def select_views( + profile: dict, cache_dir: str, offline: bool +) -> list[RepoView]: + """Every repository the profile cites, source before upstream. + + 241 profiles declare a `source` distinct from their `upstream`, and 168 of + those have both on a supported forge. Their refs mix paths from the two, so + a single repository cannot resolve them all. + """ + views: list[RepoView] = [] + seen: set[tuple[str, str]] = set() + gone = False + # A port living on a branch of a fork is absent from the default tip, so + # the branch it was read from is followed when the profile names one. + declared = str(profile.get("source_branch") or "") or None + for field_name, mode, url in declared_repositories(profile): + repo = upstream.parse_repo(url) + if repo is None or (repo.host, repo.slug) in seen: + continue + # A mirror carries the source's history, commits and branches alike: + # its pin is source_commit. It read `source_mirror_commit`, a field + # no profile carries, and fell back to profiled_date, which a bump + # never moves (eden judged from f6e76860 while pinned on d16735f5). + pin_field = "source" if field_name == "source_mirror" else field_name + branch = declared if pin_field == "source" else None + head = None + try: + unread: upstream.NotCachedError | None = None + for candidate in branch_candidates(url): + try: + head = upstream.resolve_head(repo, cache_dir, offline, candidate) + except upstream.NotCachedError as exc: + # Another prefix of the /tree/ tail may be the cached branch. + unread = exc + continue + if head: + branch = candidate + break + if unread and not head: + raise unread + pin, origin = resolve_pin( + profile, repo, cache_dir, offline, pin_field, mode, branch + ) + if not pin or origin is None: + continue + head = head or upstream.resolve_head(repo, cache_dir, offline, branch) + except upstream.GoneError: + # yuzu's source answers 451: the mirror declared after it is what + # keeps the profile checkable, so a gone repository is passed over. + gone = True + continue + if not head: + continue + if head != pin and not offline: + # A tip cached before the pin was written is older than the pin: + # scummvm-standalone pinned 6f7b060d while the cache still held + # 77d80d7, and a bump would have moved the pin backwards. + relation = upstream.ancestry(repo, pin, head, cache_dir, offline) + if relation and relation[0] == 0 and relation[1] > 0: + head = upstream.resolve_head(repo, cache_dir, offline, branch, fresh=True) or head + seen.add((repo.host, repo.slug)) + views.append(RepoView(repo, pin, head, origin, field_name, mode)) + if gone and not views: + raise upstream.GoneError(f"{profile.get('emulator', '?')}: every declared repository is gone") + views = _follow_fork_base(_follow_submodule(views, cache_dir, offline), cache_dir, offline) + return _hold_upstream(profile, views) + + +def _hold_upstream(profile: dict, views: list[RepoView]) -> list[RepoView]: + """An upstream tree the port vendors or synced to is judged at that + revision: aps3e carries RPCS3 1f080485, yaps2 PCSX2 342db515.""" + if not str(profile.get("upstream_frozen") or "").strip(): + return views + return [ + replace(view, head=view.pin, origin=f"{view.origin}, held") + if view.field == "upstream" else view + for view in views + ] + + +def _follow_submodule( + views: list[RepoView], cache_dir: str, offline: bool +) -> list[RepoView]: + """An upstream the port vendors as a submodule moves with the port. + + libretro/TIC-80 builds nesbox/TIC-80 through its `core` submodule, and + upstream_commit was that submodule's commit. Compared to nesbox's own + HEAD, the view reported changes the shipped core never had, and a bump + would have moved upstream_commit past what it builds. The commit the + port's HEAD points the submodule at is the upstream HEAD that matters. + """ + source = next((v for v in views if v.field == "source"), None) + behind = [v for v in views if v.field == "upstream" and v.pin != v.head] + if source is None or not behind: + return views + pinned = { + sha: path + for path, sha in upstream.list_gitlinks( + source.repo, source.pin, cache_dir, offline + ).items() + } + if not pinned: + return views + followed: list[RepoView] = [] + for view in views: + path = pinned.get(view.pin) if view.field == "upstream" else None + head = ( + upstream.list_gitlinks(source.repo, source.head, cache_dir, offline).get(path) + if path else None + ) + followed.append( + replace(view, head=head, origin=f"{view.origin} via {path}") + if head else view + ) + return followed + + +def _follow_fork_base( + views: list[RepoView], cache_dir: str, offline: bool +) -> list[RepoView]: + """An upstream the port is a fork of moves with what the port merged. + + snes9x, kronos, np2kai and six others pinned upstream_commit on the merge + base of upstream with the fork, the upstream code the port carries. Read + against upstream's own HEAD, the bump moved it past anything the port + built from. Outside one fork network the HEAD stays the reference. + """ + source = next((v for v in views if v.field == "source"), None) + if source is None: + return views + followed: list[RepoView] = [] + for view in views: + if view.field != "upstream" or view.pin == view.head or " via " in view.origin: + followed.append(view) + continue + base = upstream.fork_base( + view.repo, view.head, source.repo, source.head, cache_dir, offline + ) + if base and base != view.pin: + # A pin newer than the merge base names code the port does not + # carry: the upstream is a build of its own, the Qt melonDS or the + # EasyRPG Player a standalone + libretro profile documents, and + # its HEAD stays the reference. melonds read 825 commits back. + moved = upstream.ancestry(view.repo, base, view.pin, cache_dir, offline) + if moved and moved[0] > 0 and moved[1] == 0: + base = None + followed.append( + replace(view, head=base, origin=f"{view.origin}, fork base of {source.repo.slug}") + if base else view + ) + 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. + + A profile can cite a fork as `.../Ymir/tree/libretro`, which names both + the repository and the branch the port lives on. The tail may go on into + a directory (`.../libretro-samples/tree/master/tests/cruzes`): this is + the whole tail, and branch_candidates() its prefixes. + """ + _, sep, tail = url.partition("/tree/") + return tail.strip("/") or None if sep else None + + +def branch_candidates(url: str) -> list[str]: + """Each prefix of a /tree/ tail, shortest first, that may be the branch. + + A branch name can hold a slash, so the tail is not split blindly. The + first prefix the forge resolves is the branch, the rest a directory. + Taken whole, `master/tests/cruzes` resolved to nothing and the profile + was skipped as having no resolvable pin. + """ + tail = branch_in_url(url) + if not tail: + return [] + parts = tail.split("/") + return ["/".join(parts[:n]) for n in range(1, len(parts) + 1)] + + +def version_tag_candidates(core_version: object, repo_name: str = "") -> list[str]: + """Tag spellings a declared core_version might use. + + Some projects prefix their own name and drop the separators, the way + mamedev/mame publishes 0.289 as `mame0289`, so that spelling is derived + from the repository name rather than listed per project. + """ + version = str(core_version or "").strip() + if not version or " " in version: + return [] + bare = version.lstrip("vV") + spellings = [version, f"v{bare}", bare] + if repo_name: + spellings.append(f"{repo_name}{bare.replace('.', '')}") + return list(dict.fromkeys(spellings)) + + +def detect_pinned_tag( + profile: dict, views: list[RepoView], cache_dir: str, offline: bool +) -> str | None: + """Tag the profile is pinned to, when the repository has moved past it. + + A profile can document a frozen release line that lives as a tag inside a + still-developed repository. HEAD is then a different program: the refs + describe the tag and must never be recaled onto modern code. `pcsx2-legacy` + is pinned to v1.6.0 of PCSX2, where a plugin-era DEV9 ref resolves onto the + modern built-in DEV9, a different code path. + + The declared `core_version` names the tag, which is looked up by name so + that a repository publishing nightly tags cannot hide an old release behind + pagination. + + Only the repository of the documented build freezes the profile. An + `upstream` a port was cut from sits on a release tag as its base + (GroovyMAME on mame0289, triforce on Dolphin 2407) while the port itself + keeps moving: that view is judged against its tag by `tag_pinned_views`, + and the port is still compared with its HEAD and still bumped. + """ + documented = [view for view in views if view.field != "upstream"] or views + tagged = tag_pinned_views(profile, documented, cache_dir, offline) + return next(iter(tagged.values()), None) + + +def tag_pinned_views( + profile: dict, views: list[RepoView], cache_dir: str, offline: bool +) -> dict[tuple[str, str], str]: + """Views whose pin is the tag `core_version` names, keyed by repository. + + A core_version keyed by build mode names the tag of the view's own mode: + uzdoom's standalone sits on 5.0.3 while its libretro branch moves. + """ + tagged: dict[tuple[str, str], str] = {} + declared = profile.get("core_version") + for view in views: + if view.pin == view.head: + continue + version = declared.get(view.mode) if isinstance(declared, dict) else declared + for tag in version_tag_candidates(version, view.repo.name): + if upstream.tag_commit(view.repo, tag, cache_dir, offline) == view.pin: + tagged[(view.repo.host, view.repo.slug)] = tag + break + return tagged + + +def resolve_through_gitlinks(path: str, links: Iterable[Iterable[str]]) -> str | None: + """Path of `project file` inside the one submodule named after the project. + + idtech4a++ cites `xash3d-fwgs filesystem/filesystem.c` and starship + `Torch src/Companion.cpp`: the declared tree holds a gitlink there, not + the files, so no blob path answers and the part stayed external. + """ + word, _, rest = path.partition(" ") + rest = rest.strip().lstrip("/") + if not rest: + return None + word = word.lower() + for paths in links: + matches = [ + link for link in paths or () + if word in link.rsplit("/", 1)[-1].lower() + ] + if len(matches) == 1: + return f"{matches[0]}/{rest}" + if matches: + return None + return None + + +def moved_repositories( + views: list[RepoView], cache_dir: str, offline: bool +) -> list[str]: + """Declared repositories the forge now answers under another name.""" + moved = [] + seen: set[tuple[str, str]] = set() + for view in views: + key = (view.repo.host, view.repo.slug) + if key in seen: + continue + seen.add(key) + current = upstream.current_slug(view.repo, cache_dir, offline) + if current and current.lower() != view.repo.slug.lower(): + moved.append( + f"{view.repo.slug} answers as {current} (renamed or transferred)" + ) + return moved + + +def shipped_build_holders(profile: dict) -> dict[int, dict]: + """The source_ref holders of config_reads entries naming platforms, by id. + + Such an entry describes the build those platforms ship, so its refs are + read at their pin_build revision, never at source_commit. + """ + found: dict[int, dict] = {} + for read in profile.get("config_reads") or []: + if not isinstance(read, dict) or not read.get("platforms"): + continue + found[id(read)] = read + for key in read.get("keys") or []: + if isinstance(key, dict): + found[id(key)] = read + return found + + +def build_revision(profile: dict, platform: str) -> str | None: + """The emulator revision a platform's pin_build entry names, None when + its value is no revision (a URL, a package name).""" + for build in profile.get("pin_build") or []: + if build.get("platform") != platform: + continue + value = str(build.get("value") or "").strip() + return value if value and re.fullmatch(r"[\w.+-]+", value) else None + return None + + +def _on_own_revision(view: RepoView, tagged: dict[tuple[str, str], str]) -> bool: + """Whether a view is read at its own revision: on HEAD, or on its tag.""" + return view.pin == view.head or (view.repo.host, view.repo.slug) in tagged diff --git a/scripts/profiling/resolver.py b/scripts/profiling/resolver.py new file mode 100644 index 000000000..2a6ba422c --- /dev/null +++ b/scripts/profiling/resolver.py @@ -0,0 +1,907 @@ +"""Builds a profile's report: each citation resolved against its repositories at the pin and at HEAD.""" + +from __future__ import annotations + +import functools +import itertools +import posixpath +import re +import sys +from collections.abc import Callable, Iterable, Sequence +from dataclasses import dataclass, field, replace + +from core import upstream +from core.upstream import CompareResult +from model.sourceref import RefPart, ref_role, resolve_project_word, split_source_ref +from profiling.anchoring import ( + BLANK_LOOKAHEAD, + PartResult, + STATUS_ORDER, + TREE_TRUNCATED, + anchor_part, + dominant_shifts, + reconcile_self_check, + refuse_collapsing_moves, + resolve_by_shift, + resolve_rename, + verify_at_pin, + worst_status, +) +from profiling.citations import ( + Citation, + _anchor_tokens, + cites_another_version, + collect_citations, + entry_hashes, + is_external_citation, + narrow_by_cited, + strip_repo_word, + symlink_target, +) +from profiling.reports import EntryReport, ProfileReport +from profiling.repos import ( + PIN, + RepoView, + _on_own_revision, + build_revision, + declared_repositories, + detect_pinned_tag, + moved_repositories, + resolve_through_gitlinks, + select_repo, + select_views, + shipped_build_holders, + tag_pinned_views, +) + + +def _resolve_project_words( + refs: list, tree_order: Callable[[], Iterable[list[str] | None]], + link_order: Callable[[], Iterable[Iterable[str]]] = lambda: (), +) -> list: + """Point `project file` parts at the declared tree that vendors them. + + tree_order() yields the trees to search, pin before HEAD, and + link_order() the submodule paths of the declared trees at their pins. + Each is only called for a part that needs it, since reading a tree costs + a request. Parts no tree carries stay external. + """ + resolved: dict[str, str | None] = {} + out = [] + for display, ref, tokens, hashes, parts, citation in refs: + rewritten = [] + for part in parts: + if is_external_citation(part.path): + if part.path not in resolved: + resolved[part.path] = resolve_project_word( + part.path, tree_order() + ) or resolve_through_gitlinks(part.path, link_order()) + if resolved[part.path]: + part = replace(part, path=resolved[part.path]) + rewritten.append(part) + out.append((display, ref, tokens, hashes, rewritten, citation)) + return out + + +def _path_roles(refs: list) -> dict[str, set[str]]: + """The roles each cited path is written under across the profile.""" + roles: dict[str, set[str]] = {} + for *_, parts, _citation in refs: + for part in parts: + if part.role: + roles.setdefault(part.path, set()).add(part.role) + return roles + + +def _check_build_refs( + profile: dict, + deferred: list[tuple[Citation, dict]], + views: list[RepoView], + fetch: Callable[[upstream.Repo, str, str], list[str] | None], +) -> list[dict]: + """Each deferred config_reads ref read at the build of its platforms. + + The file must exist at that revision and every cited line lie inside + it. The entry declares no value to anchor on, so this is the check a + ref past the end of its file fails (BUILD_GONE), not a content match. + """ + results: list[dict] = [] + for citation, read in deferred: + for platform in read.get("platforms") or []: + # A closed build is named by its binary: an address ref is + # skipped below, a source path cannot exist at a sha256. + revision = (str(read.get("revision") or read.get("binary_sha256") or "") + or build_revision(profile, str(platform))) + row = {"platform": str(platform), "revision": revision or "", + "ref": citation.ref, "field": citation.field} + if revision is None: + results.append({**row, "status": "BUILD_UNREAD", + "detail": "no pin_build revision for this platform"}) + continue + problems: list[str] = [] + for part in split_source_ref(citation.ref): + role, path = ref_role(part.path) + if " " in path: + # A project word in front (`ROCKNIX patches/x.patch:4`): + # a file of the platform's tree, read with the record. + continue + lines = next( + (found for view in views + if (not role or view.field == role) + and (found := fetch(view.repo, revision, path)) is not None), + None, + ) + if lines is None: + problems.append(f"{part.path} absent at {revision[:12]}") + elif (part.end or part.start or 0) > len(lines): + problems.append(f"{part.raw} past the end ({len(lines)} lines)") + results.append({ + **row, + "status": "BUILD_GONE" if problems else "BUILD_OK", + "detail": "; ".join(problems), + }) + return results + + +def _gather_refs( + profile: dict, text: str | None +) -> tuple[list, list[tuple[Citation, dict]]]: + """The citations to anchor, and the refs read at a shipped build instead. + + One citation surface for the whole document. A prose citation carries + no declared value, so it anchors on content alone: the nudge and + relocation heuristics stay off rather than guess from a profile-wide + token pool. + """ + known = frozenset( + repo.name.lower() + for _, _, url in declared_repositories(profile) + if (repo := upstream.parse_repo(url)) is not None + ) + modes = {mode for _, mode, _ in declared_repositories(profile) if mode} + refs = [] + at_build = shipped_build_holders(profile) + deferred: list[tuple[Citation, dict]] = [] + for citation in collect_citations(profile, text): + if citation.kind == "ref" and id(citation.holder) in at_build: + # Written for the build a platform ships: read at pin_build. + deferred.append((citation, at_build[id(citation.holder)])) + continue + if citation.kind == "ref": + entry = citation.entry or {} + display = str(entry.get("name") or "") or citation.field + if citation.label: + display += f" [{citation.label}]" + # The repository a part says its lines are in (`upstream + # src/a.c:5`) stays on that part: the same path can be cited + # under both roles in one profile. + # An entry of one build mode reads that build's repository when + # the profile keys its source by mode. + mode = str(citation.label or entry.get("mode") or "") + mode = mode if mode in modes else "" + parts = [ + RefPart( + strip_repo_word(p.path, known), p.start, p.end, p.raw, + ref_role(p.path)[0] or mode, + ) + for p in split_source_ref(citation.ref) + ] + refs.append(( + display, citation.ref, _anchor_tokens(entry), + entry_hashes(entry), parts, citation, + )) + else: + refs.append(( + citation.field, citation.ref, [], [], + list(citation.parts), citation, + )) + return refs, deferred + + +def _cited_dirs(refs: list) -> set[str]: + """Directories the profile already cites, deepest first, to settle a bare + filename that several trees of one repository carry.""" + cited_dirs: set[str] = set() + for _, _, _, _, parts, _ in refs: + for part in parts: + directory = posixpath.dirname(part.path) + while directory: + cited_dirs.add(directory) + directory = posixpath.dirname(directory) + return cited_dirs + + +def _unanswered_skip(profile: dict) -> str: + """Why a profile no declared repository answers for is skipped.""" + # A declared pin whose repository or branch does not answer is not a + # profile without a pin: pyxel's kitao/pyxel answers 404, and the + # message sent the reader looking for a missing source_commit. + return ( + "no declared repository answers (moved, removed, or a branch the " + "forge does not know)" + if profile.get("source_commit") or profile.get("upstream_commit") + else "no source_commit and no resolvable profiled_date " + "on any declared repository" + ) + + +def _readable_views( + profile: dict, report: ProfileReport, cache_dir: str, offline: bool +) -> list[RepoView] | None: + """The repositories to read, None once the report says why it is skipped.""" + report.unread = [ + url + for _, _, url in declared_repositories(profile) + if upstream.parse_repo(url) is None + ] + if select_repo(profile) is None: + declared = str(profile.get("source") or profile.get("upstream") or "") + report.skipped = f"unsupported host: {declared or 'none declared'}" + return None + + stated_gone = str(profile.get("upstream_gone") or "").strip() + + try: + views = select_views(profile, cache_dir, offline) + except upstream.RateLimitError: + raise + except upstream.UpstreamError: + # citron's dead domain now resolves to an unrelated host that fails + # TLS: for a forge the profile declares gone, any refusal agrees. + if stated_gone: + # The profile says the forge is gone and the forge agrees. That is + # a recorded fact, not an open problem: the refs describe the last + # revision anyone can reach, and nothing further is possible. + report.skipped = f"upstream gone (declared): {stated_gone}" + return None + raise + if stated_gone and any(view.field != "source_mirror" for view in views): + # Declared dead, yet it answered. A profile that keeps saying so stops + # being read, so the contradiction is the finding. + report.skipped = ( + f"upstream_gone is declared but the forge answers: {stated_gone}" + ) + return None + if not views and stated_gone: + report.skipped = f"upstream gone (declared): {stated_gone}" + return None + if not views: + report.skipped = _unanswered_skip(profile) + return None + return views + + +def _record_revisions( + profile: dict, report: ProfileReport, views: list[RepoView], + cache_dir: str, offline: bool, +) -> dict[tuple[str, str], str]: + """Put the revisions under comparison on the report, and return the views + read at their release tag.""" + primary = views[0] + report.repo, report.host = primary.repo.slug, primary.repo.host + report.repos = [v.repo.slug for v in views] + report.pin, report.pin_origin = primary.pin, primary.origin + report.head = primary.head + for view in views[1:]: + if view.field == "upstream": + report.upstream_pin = view.pin + report.upstream_head = view.head + report.upstream_origin = view.origin + report.moved_repos = moved_repositories(views, cache_dir, offline) + # A profile can hold its pin on purpose, documenting a build that is that + # revision. Comparing it to HEAD says nothing and recaling would repoint + # its refs at code the build never had, so it is judged like a frozen tag: + # against itself. + frozen = str(profile.get("pin_frozen") or "").strip() + report.pin_frozen = bool(frozen) + report.pinned_tag = frozen or detect_pinned_tag( + profile, views, cache_dir, offline + ) + # A repository pinned on its release tag is read at that tag, whichever + # role it plays: comparing mame0174 refs with mamedev HEAD says nothing. + tagged = tag_pinned_views(profile, views, cache_dir, offline) + report.upstream_tag = next( + ( + tagged[(view.repo.host, view.repo.slug)] + for view in views + if view.field == "upstream" and (view.repo.host, view.repo.slug) in tagged + ), + None, + ) + + if not report.pinned_tag: + # A tag or a held pin is off the tracked branch by design. + report.orphaned = [ + f"{view.repo.slug}: pin {view.pin[:8]} is no ancestor of {view.head[:8]}" + for view in views + if not _on_own_revision(view, tagged) + and upstream.compare( + view.repo, view.pin, view.head, cache_dir, offline + ).diverged + ] + return tagged + + +def _line_score(lines: list[str], start: int | None, tokens: tuple) -> int: + """How well a repository's cited line matches what the ref means. + + Two repositories can hold the same path with different contents: + `mame` cites files that both mamedev/mame and its libretro fork + carry, and the fork's tree is offset, so its line 5545 is a real + but unrelated statement. A line carrying the entry's own subject + beats a merely non-empty one, which in turn beats a blank. + """ + if start is None: + return 2 + if start > len(lines): + return 0 + # A blank cited line opens the block below it: lrps2's + # VMManager.cpp:418 is blank in the fork it was written for, and + # scored 0 it handed the ref to the upstream, where 418 is + # unrelated code that then decided the rebase target. + line = next( + (ln.lower() for ln in lines[start - 1:start - 1 + BLANK_LOOKAHEAD] + if ln.strip()), + "", + ) + # A set name sits between delimiters in a MAME declaration, so + # `pgm` matches `GAME( 1997, pgm,` but not `pgm_012_025_drgw2`, + # which is what tells the two repositories apart. + if tokens and any( + re.search(rf"\b{re.escape(tok)}\b", line) for tok in tokens + ): + return 3 + if tokens and any(tok in line for tok in tokens): + return 2 + return 1 if line.strip() else 0 + + +@dataclass +class _Resolver: + """Where each cited path lives across the declared repositories, and how + a part reads there, memoised for one profile.""" + + name: str + profile: dict + cache_dir: str + offline: bool + views: list[RepoView] + report: ProfileReport + tagged: dict[tuple[str, str], str] + # A part that names no repository is looked up first where its path is + # cited elsewhere: a note citing `src/a.c:12` beside source_refs that + # all read `upstream src/a.c` means that tree. + # A path cited under both roles lends neither, and such a part is + # compared in every repository rather than in whichever comes first. + path_roles: dict[str, set[str]] + cited_dirs: set[str] + owners: dict[tuple[str, int | None, tuple, str], tuple[RepoView, str]] = field( + default_factory=dict + ) + context: dict[str, tuple[CompareResult, list[str]]] = field(default_factory=dict) + truncated_trees: set[str] = field(default_factory=set) + pin_trees: dict[str, list[str]] = field(default_factory=dict) + mute: set[tuple[str, str]] = field(default_factory=set) + lines_cache: dict[ + tuple[str, str, int | None, tuple[str, ...], str | None, str], list[str] + ] = field(default_factory=dict) + + @property + def primary(self) -> RepoView: + return self.views[0] + + def role_of(self, part: RefPart) -> str: + if part.role: + return part.role + lent = self.path_roles.get(part.path, ()) + return next(iter(lent)) if len(lent) == 1 else "" + + def on_own_revision(self, view: RepoView) -> bool: + return _on_own_revision(view, self.tagged) + + def trees_in_order(self) -> Iterable[list[str] | None]: + """Pin trees before HEAD trees, each read only when reached.""" + return itertools.chain( + map(self.pin_tree_for, self.views), + (self.context_for(v)[1] for v in self.views), + ) + + def gitlinks_in_order(self) -> Iterable[Iterable[str]]: + return ( + upstream.list_gitlinks(v.repo, v.pin, self.cache_dir, self.offline) + for v in self.views + ) + + def _add_bare_name_matches(self, path: str, candidates: list[str]) -> None: + # A bare filename, the way prose cites files. Resolved against the + # pin before HEAD: the ref was written at the pin, and a file that + # moved since would otherwise be resolved to a HEAD path that does + # not exist at the pin, reporting GONE for the one reason it never + # should. The rename search then carries the pin path to HEAD. + for view in self.views: + for tree in (self.pin_tree_for(view), self.context_for(view)[1]): + matches = [ + p for p in tree or [] + if posixpath.basename(p) == path + ] + if len(matches) > 1: + matches = narrow_by_cited(matches, self.cited_dirs) + # A few survivors are told apart by the cited line below: a + # 266-line Windows configure.ac cannot carry line 754. + if len(matches) <= 4: + candidates.extend( + m for m in matches if m not in candidates + ) + if matches: + break + + def _add_suffix_matches(self, path: str, candidates: list[str]) -> None: + # A path written from a subproject directory of a monorepo: + # `src/main/Main.cc` in emu-ex-plus-alpha is PCE.emu/src/main/Main.cc + # for one profile and C64.emu/src/main/Main.cc for another. The + # tree is searched by suffix and the cited directories decide. + suffix = "/" + path + for view in self.views: + _, tree = self.context_for(view) + matches = [p for p in tree or [] if p.endswith(suffix)] + if len(matches) > 1: + matches = narrow_by_cited(matches, self.cited_dirs) + if len(matches) <= 4: + candidates.extend(m for m in matches if m not in candidates) + + def _candidate_paths(self, path: str) -> list[str]: + """The paths a cited path may stand for, as written first.""" + candidates = [path] + head, _, tail = path.partition("/") + if tail and any(head == v.repo.name for v in self.views): + candidates.append(tail) + if "/" not in path: + self._add_bare_name_matches(path, candidates) + elif not any( + self.fetch_from(v.repo, sha, path) is not None + for v in self.views for sha in (v.pin, v.head) + ): + self._add_suffix_matches(path, candidates) + return candidates + + def _upstream_on_line( + self, candidate: str, at_pin: bool, start: int | None, tokens: tuple + ) -> RepoView | None: + """The upstream holding the entry's value on the cited line too.""" + for other in self.views: + if _view_role(other) != "upstream": + continue + sha = other.pin if at_pin else other.head + theirs = self.fetch_from(other.repo, sha, candidate) + if theirs is not None and _line_score(theirs, start, tokens) == 3: + return other + return None + + def _try_candidate( + self, + candidate: str, + ordered: list[RepoView], + start: int | None, + tokens: tuple, + wanted: str, + best: tuple[int, RepoView, str] | None, + ) -> tuple[tuple[RepoView, str] | None, tuple[int, RepoView, str] | None]: + """The owner one candidate path settles, else the best seen so far.""" + for at_pin in (True, False): + for view in ordered: + sha = view.pin if at_pin else view.head + found = self.fetch_from(view.repo, sha, candidate) + if found is None: + continue + target = symlink_target(candidate, found) + if target: + found = self.fetch_from(view.repo, sha, target) + if found is None: + continue + candidate = target + rank = _line_score(found, start, tokens) + if rank and _answers(view, wanted) and view.mode: + # An entry of one build mode is that build's code: + # xenia-edge's standalone xbox.xex refs scored higher + # in the libretro tree, whose same file is offset. + return (view, candidate), best + if rank == 3 and not wanted and _view_role(view) == "source": + # The port carries a copy of the emulator: when the + # original holds the same value on the cited line, + # the code asking for the file is the emulator's. + other = self._upstream_on_line(candidate, at_pin, start, tokens) + if other is not None: + return (other, candidate), best + if rank == 3: + return (view, candidate), best + if best is None or rank > best[0]: + best = (rank, view, candidate) + return None, best + + def locate( + self, path: str, start: int | None = None, tokens: tuple = (), wanted: str = "" + ) -> tuple[RepoView, str]: + """Repository and real path carrying a cited path, HEAD before pin. + + A ref may prefix the path with the repository directory name, as it + appears in a parent folder holding both clones: 270 parts across 21 + profiles do. That prefix is stripped only as a last resort, once the + path as written has failed against every repository and revision. + + The pin comes before HEAD because that is the revision the ref was + written against. `mame` cites files that both mamedev/mame and the + libretro fork carry today, but only the upstream pin holds them. The + fork's pin is an older tree where they lived elsewhere. + """ + candidates = self._candidate_paths(path) + # A part that names its repository is looked up there first. In a + # profile keyed by build mode, a part naming none belongs to the + # first build declared, as _realign_part decides a shared path: the + # libretro tree of xenia-edge carries emulator.cc offset, and its + # better-scored line hid a standalone ref that had moved. + if not wanted and self.primary.mode: + wanted = self.primary.mode + ordered = ( + sorted(self.views, key=lambda v: not _answers(v, wanted)) if wanted + else self.views + ) + best: tuple[int, RepoView, str] | None = None + for candidate in candidates: + found, best = self._try_candidate(candidate, ordered, start, tokens, wanted, best) + if found is not None: + return found + return (best[1], best[2]) if best else (self.primary, path) + + def resolve_path( + self, path: str, start: int | None = None, tokens: tuple = (), role: str = "" + ) -> tuple[RepoView, str]: + key = (path, start, tokens, role) + if key not in self.owners: + self.owners[key] = self.locate(path, start, tokens, role) + return self.owners[key] + + def pin_tree_for(self, view: RepoView) -> list[str]: + """Tree at the revision the refs were written against, memoised.""" + key = view.repo.slug + if key not in self.pin_trees: + tree, _ = upstream.list_tree(view.repo, view.pin, self.cache_dir, self.offline) + self.pin_trees[key] = tree + return self.pin_trees[key] + + def fetch_from(self, repo: upstream.Repo, sha: str, wanted: str) -> list[str] | None: + """Read a file, treating a repository that refuses as one that lacks it. + + A profile can name several repositories, and a mirror exists exactly + because one of them stopped answering: git.eden-emu.dev returns 403 to + anything that is not a browser. Letting the first refusal abort the + report leaves the mirror unread and the profile unverifiable, so the + repository is muted for the rest of the pass instead. A quota signal + still ends the profile, and upstream stops asking that host, because + continuing would only burn the rest of the budget on the same wall. + """ + # Keyed by host as well as slug: a mirror carries the same slug on + # another forge, and muting one must not silence the other. + key = (repo.host, repo.slug) + if key in self.mute: + return None + try: + found = upstream.fetch_file(repo, sha, wanted, self.cache_dir, self.offline) + if found is None and "/" in wanted: + # A file of a submodule is read at the commit its gitlink + # pins in this revision, so a bump moves it with the port. + inner = upstream.through_gitlink( + repo, sha, wanted, self.cache_dir, self.offline + ) + if inner is not None: + found = upstream.fetch_file(*inner, self.cache_dir, self.offline) + return found + except (upstream.RateLimitError, upstream.NotCachedError): + raise + except upstream.UpstreamError as exc: + self.mute.add(key) + print(f"{self.name}: {repo.host}/{repo.slug} muted, {exc}", file=sys.stderr) + return None + + def context_for(self, view: RepoView) -> tuple[CompareResult, list[str]]: + key = view.repo.slug + if key not in self.context: + comparison = upstream.compare( + view.repo, view.pin, view.head, self.cache_dir, self.offline + ) + tree, truncated = upstream.list_tree( + view.repo, view.head, self.cache_dir, self.offline + ) + self.context[key] = (comparison, tree) + if truncated: + self.truncated_trees.add(key) + if truncated and tree: + print( + f"{self.name}: {key} HEAD tree truncated by the forge, " + "rename search is partial", + file=sys.stderr, + ) + return self.context[key] + + def rename_getter(self, path: str, role: str = "") -> tuple[str | None, list[str]]: + """Resolved only when a cited file has vanished, then memoised. + + Every declared repository is searched, not just the one that owns the + path: a profile citing both a port and its upstream may point at a file + that only the other one carries. + """ + owner, actual = self.resolve_path(path, role=role) + ordered = [owner] + [v for v in self.views if v is not owner] + candidates: list[str] = [] + for view in ordered: + comparison, tree = self.context_for(view) + found, near = resolve_rename(comparison, actual, tree) + if found: + return found, [] + # Only the owning repository reports near misses: pooling them + # across repositories turns a resolvable path into an ambiguous one. + if view is owner: + candidates = near + if not candidates and any(v.repo.slug in self.truncated_trees for v in ordered): + return None, [TREE_TRUNCATED] + return None, candidates + + def fetch( + self, + which: str, + path: str, + start: int | None = None, + tokens: Iterable[str] = (), + forced: RepoView | None = None, + role: str = "", + ) -> list[str] | None: + """Lines for one path at one revision, read once per run. + + The same object is handed back every time so the opcode cache can key + on identity. + """ + tokens = tuple(tokens) + key = (which, path, start, tokens, forced.repo.slug if forced else None, role) + if key not in self.lines_cache: + view, actual = ( + (forced, path) if forced + else self.resolve_path(path, start, tokens, role) + ) + sha = view.pin if which == PIN else view.head + found = self.fetch_from(view.repo, sha, actual) + if found is None: + # Not cached: the miss may be a repository that has just been + # muted, and the next call resolves the path to a mirror that + # does carry it. upstream.fetch_file holds its own cache, so + # asking again costs a lookup rather than a request. + return None + self.lines_cache[key] = found + return self.lines_cache[key] + + def describe( + self, + path: str, + start: int | None = None, + tokens: Iterable[str] = (), + forced: RepoView | None = None, + role: str = "", + ) -> tuple[str | None, str, str]: + view, actual = ( + (forced, path) if forced + else self.resolve_path(path, start, tuple(tokens), role) + ) + slug = view.repo.slug if view is not self.primary else None + return slug, upstream.raw_url(view.repo, view.head, actual), actual + + def anchor_across_views(self, part: RefPart, tokens: Sequence[str]) -> PartResult: + """Judge a part against every repository that carries its path. + + A ref is only broken when it fails everywhere. `mame` cites files that + both mamedev/mame and its libretro fork hold, with the same delimited + token on the cited line, so no single attribution can be right for all + of them. Taking the best outcome across repositories settles it. + A part that names its repository is judged there alone when that + repository carries the path: hypseus-singe cites lair.cpp under + `source` for one entry and `upstream` for the next. + """ + role = self.role_of(part) + owner, _actual = self.resolve_path(part.path, part.start, tuple(tokens), role) + carrying = [ + view for view in self.views + if self.fetch_from(view.repo, view.pin, part.path) is not None + ] + # Judged in the build it belongs to: the best outcome across the + # builds of a keyed profile let the libretro tree vouch for a + # standalone ref that had moved (xenia-edge's emulator.cc). + meant = part.role or self.primary.mode + named = [view for view in carrying if _answers(view, meant)] + if meant and named: + carrying = named + live = { + id(view) for view in carrying + if self.fetch_from(view.repo, view.head, part.path) is not None + } + renames = functools.partial(self.rename_getter, role=role) + best: tuple[int, PartResult] | None = None + for view in carrying: + result = anchor_part( + part, + functools.partial(self.fetch, forced=view), + renames, + functools.partial(self.describe, forced=view), + tokens, + ) + if ( + live and id(view) not in live + and result.status in ("AMBIGUOUS", "GONE") + ): + # dosbox-pure's upstream was emptied: its rename search + # offered the .info, .sln and .vcxproj as candidates and hid + # the real change in the port that still carries the file. + continue + rank = STATUS_ORDER.index(result.status) + # On a tie the repository the citation is attributed to speaks: + # mame's "written against HEAD, pin names an older revision" was + # replaced by whatever the first repository said. + if best is None or rank < best[0] or (rank == best[0] and view is owner): + best = (rank, result) + return best[1] if best else anchor_part( + part, + functools.partial(self.fetch, role=role), + renames, + functools.partial(self.describe, role=role), + tokens, + ) + + def self_checked(self, part: RefPart, tokens: Sequence[str]) -> bool: + """Whether a part is judged on self-consistency rather than compared. + + Comparing a revision with itself anchors every ref whatever it cites, + so a part whose repository already sits on HEAD is judged on + self-consistency instead. Decided per part, by the repository that + owns it: a primary on HEAD says nothing about an upstream that moved on. + """ + if self.report.pinned_tag: + return True + if not part.role and len(self.path_roles.get(part.path, ())) > 1: + return all(self.on_own_revision(view) for view in self.views) + view, _ = self.resolve_path(part.path, part.start, tuple(tokens), self.role_of(part)) + return self.on_own_revision(view) + + def _elsewhere_at_own_revision( + self, part: RefPart, owner: RepoView, tokens: Sequence[str], hashes: Sequence[str], + ) -> PartResult | None: + """The verdict of another repository read at its own revision.""" + for view in self.views: + if view is owner or not self.on_own_revision(view): + continue + lines = self.fetch_from(view.repo, view.pin, part.path) + if lines is None: + continue + found = verify_at_pin(part, lines, tokens, hashes) + if found.status != "GONE": + return replace( + found, + repo=None if view is self.primary else view.repo.slug, + ) + return None + + def _judge_at_pin( + self, part: RefPart, tokens: Sequence[str], hashes: Sequence[str] + ) -> PartResult: + role = self.role_of(part) + result = verify_at_pin( + part, + self.fetch(PIN, part.path, part.start, tokens, role=role), + tokens, hashes, + ) + # Checked at its own revision, the part still lies in one tree: + # the report names it, as a comparison does. + owner, _actual = self.resolve_path(part.path, part.start, tuple(tokens), role) + if result.repo is None and owner is not self.primary: + result = replace(result, repo=owner.repo.slug) + if result.status == "GONE": + # The owner is chosen on the cited first line alone. hatari's + # port carries a longer src/paths.c than the upstream that a + # source_ref attributes the path to, and prose citing the + # port's 305-356 read past the end of the upstream's 345 + # lines. As in a comparison, a ref fails only everywhere. + result = self._elsewhere_at_own_revision(part, owner, tokens, hashes) or result + return result + + def judge( + self, part: RefPart, flag: bool, tokens: Sequence[str], hashes: Sequence[str] + ) -> PartResult: + other = cites_another_version(part, self.profile.get("core_version")) + if other: + # ti99sim cites v0.0.11 beside its v0.16.0 pin: no revision of + # the pinned repository holds that program, so it cannot anchor. + return PartResult( + part, "EXTERNAL", None, None, None, [], + f"annotated for version {other}, not the documented one", + ) + if flag: + result = self._judge_at_pin(part, tokens, hashes) + else: + result = self.anchor_across_views(part, tokens) + # A forge that refused the request said nothing about the file: + # git.eden-emu.dev answers 403 to scripts, and nine citations of + # eden-nightly read "absent at HEAD" for a file that exists. + owner, _actual = self.resolve_path( + part.path, part.start, tuple(tokens), self.role_of(part) + ) + if ( + result.status in ("GONE", "AMBIGUOUS") + and (owner.repo.host, owner.repo.slug) in self.mute + ): + return PartResult( + part, "UNCHECKED", None, None, None, [], + f"{owner.repo.host}/{owner.repo.slug} refused the request, not read", + ) + return result + + +def _judge_refs(resolver: _Resolver, refs: list, report: ProfileReport) -> None: + """Judge every ref and put its entry on the report.""" + staged = [] + for entry_name, ref, tokens, hashes, ref_parts, citation in refs: + flags = [resolver.self_checked(part, tokens) for part in ref_parts] + judged = [ + resolver.judge(part, flag, tokens, hashes) + for part, flag in zip(ref_parts, flags) + ] + staged.append((entry_name, ref, citation, reconcile_self_check(judged, flags))) + + shifts = dominant_shifts([p for _, _, _, parts in staged for p in parts]) + for entry_name, ref, citation, parts in staged: + parts = refuse_collapsing_moves([resolve_by_shift(p, shifts) for p in parts]) + status = worst_status([p.status for p in parts]) + report.entries.append(EntryReport( + entry_name, ref, status, parts, citation.kind, citation.field + )) + report.counts[status] = report.counts.get(status, 0) + 1 + + +def build_report( + name: str, profile: dict, cache_dir: str, offline: bool = False, + text: str | None = None, +) -> ProfileReport: + """Confront one profile with its upstream.""" + report = ProfileReport(name=name, entries=[], counts={}) + refs, deferred = _gather_refs(profile, text) + path_roles = _path_roles(refs) + cited_dirs = _cited_dirs(refs) + + views = _readable_views(profile, report, cache_dir, offline) + if views is None: + return report + tagged = _record_revisions(profile, report, views, cache_dir, offline) + + # A profile carrying no source_ref still has a pin worth writing and a + # version worth checking, so the revisions above are resolved first. + if not refs: + report.skipped = NO_SOURCE_REF + return report + + resolver = _Resolver( + name, profile, cache_dir, offline, views, report, tagged, path_roles, cited_dirs + ) + refs = _resolve_project_words( + refs, resolver.trees_in_order, resolver.gitlinks_in_order + ) + report.build_refs = _check_build_refs(profile, deferred, views, resolver.fetch_from) + _judge_refs(resolver, refs, report) + return report + + +def _view_role(view: RepoView) -> str: + """source or upstream: the profile field a repository comes from.""" + return "upstream" if view.field == "upstream" else "source" + + +def _answers(view: RepoView, role: str) -> bool: + """Whether a part written under *role* means this repository: the field + it comes from, or the build mode a keyed source declares it under.""" + return bool(role) and role in (_view_role(view), view.mode) + + +NO_SOURCE_REF = "no source_ref" diff --git a/scripts/profiling/run.py b/scripts/profiling/run.py new file mode 100644 index 000000000..1f65e62a7 --- /dev/null +++ b/scripts/profiling/run.py @@ -0,0 +1,447 @@ +"""One profile's pass: the report the flags ask for, the writes it allows, and the output.""" + +from __future__ import annotations + +import argparse +import json +import sys +import tempfile +from datetime import date +from pathlib import Path + +import yaml + +from core import upstream +from profiling.bump import bump_commit, pins_changed +from profiling.detection import _detect, _print_extras +from profiling.realign import realign_prose +from profiling.rebase import attribute_refs, rebase_refs +from profiling.reports import ( + ProfileReport, + fetch_plan, + format_markdown, + format_report, + report_to_dict, +) +from profiling.repos import select_repo +from profiling.resolver import build_report +from profiling.triage import _print_triage +from profiling.versions import ( + _newer_release, + _passed_release, + check_version, + page_version, + version_warning, +) +from profiling.yamledit import YamlWriteError, backfill_commit + + +def _stale_prose_blocks( + name: str, path: Path, args: argparse.Namespace, + known: list[str] | None = None, +) -> bool: + """Whether prose written under an older pin has to move first. + + Anchored from the current pin, a recale or a bump follows someone else's + code (boom3 FileSystem.cpp:2125, written at b810234e, an unrelated + OpenFileRead at 132dfddb). It moves first, from the pin it was written at. + `known` is the answer the report already holds for the same text. + """ + stale = ( + known if known is not None + else realign_prose(path, args.cache_dir, args.offline, dry_run=True) + ) + if stale: + print( + f"{name}: {len(stale)} prose citation(s) written under an older pin; " + "run --realign-prose, or read them and set prose_written_at to the pin they match", + file=sys.stderr, + ) + return bool(stale) + + +def _read_stale_prose( + name: str, path: Path, args: argparse.Namespace, failed: set[str] +) -> list[str]: + """What --realign-prose would do or refuse on this profile, as a report. + + Read on every pass, not only before a write: prose that blocks the pin + is review work whether or not this run tries to move it. + """ + try: + return realign_prose(path, args.cache_dir, args.offline, dry_run=True) + except upstream.RateLimitError: + raise + except upstream.NotCachedError as exc: + # Unread offline, as a report is: not clean, and not a failure. + return [f"not read offline: {exc}"] + except upstream.UpstreamError as exc: + # Not read is not clean: the bump would refuse on the same error. + print(f"{name}: prose not read, {exc}", file=sys.stderr) + failed.add(name) + return [f"not read: {exc}"] + + +def _backfill( + args: argparse.Namespace, name: str, profile: dict, report: ProfileReport, + path: Path, +) -> None: + """Write the pin a profile without source_commit was written at.""" + if not report.pin or profile.get("source_commit"): + return + if args.dry_run: + print(f"{name}: would write source_commit {report.pin[:7]}") + elif backfill_commit(path, report.pin): + print(f"{name}: source_commit {report.pin[:7]}") + + +def _write_attributions( + args: argparse.Namespace, name: str, profile: dict, report: ProfileReport, path: Path +) -> None: + if report.skipped: + return + with tempfile.TemporaryDirectory() as scratch: + target = Path(scratch) / path.name + target.write_bytes(path.read_bytes()) + verb = "would attribute " if args.dry_run else "" + for line in attribute_refs(target, report, profile): + print(f"{name}: {verb}{line}") + if not args.dry_run: + path.write_bytes(target.read_bytes()) + + +def _promote( + args: argparse.Namespace, name: str, path: Path, target: Path, moved: bool +) -> bool: + """Write the edited copy over the profile, False when it was put back.""" + original = path.read_bytes() + path.write_bytes(target.read_bytes()) + # The guard above read the prose under the pins the profile held. + # A bump that moves upstream_commit can leave prose the report + # judged in the fork under the new upstream pin (dolphin-mmjr2, + # hatari, xenios): read it again on what was written. It reads + # the profile's own history, so it runs on the real path, and a + # read that fails (offline, rate limit) puts the profile back. + try: + left = realign_prose(path, args.cache_dir, args.offline, dry_run=True) + except BaseException: + path.write_bytes(original) + raise + if moved and left: + path.write_bytes(original) + print( + f"{name}: the bump would leave {len(left)} prose citation(s) " + "under an older pin, nothing was written. Read them first.", + file=sys.stderr, + ) + return False + _WRITTEN.add(name) + return True + + +def _apply_writes( + args: argparse.Namespace, name: str, profile: dict, report: ProfileReport +) -> None: + path = Path(args.emulators_dir) / f"{name}.yml" + if not path.is_file(): + return + if args.backfill_commits: + _backfill(args, name, profile, report, path) + if getattr(args, "attribute_refs", False): + _write_attributions(args, name, profile, report, path) + return + # A backfill has just written a pin: the report's reading predates it. + known = None if args.backfill_commits else report.stale_prose + if not (args.rebase_refs or args.bump_commit) or _stale_prose_blocks( + name, path, args, known + ): + return + if _gained_since_pin(args, name, profile, report): + return + with tempfile.TemporaryDirectory() as scratch: + # Always work on a copy. A dry run reports what it left there, a real + # run promotes it. A recale rewrites the refs for HEAD, so the pin + # has to follow or neither moves: a profile whose refs describe one + # revision and whose pin names another is exactly what the + # all-or-nothing rule exists to prevent. Recaling alone used to + # leave that state behind, and a later bump then read the recaled + # refs at the old pin and saw them shifted again. + target = Path(scratch) / path.name + target.write_bytes(path.read_bytes()) + recale, bumped = ("would recale ", "would set ") if args.dry_run else ("", "") + applied = [] + if args.rebase_refs and not report.skipped: + applied = rebase_refs(target, report, args.accept_changed) + moved = bump_commit(target, report, args.accept_changed, + args.cache_dir, args.offline) + if args.rebase_refs and applied and not moved: + keyed = any(isinstance(profile.get(f), dict) for f in ("source_commit", "upstream_commit")) + reason = ( + "its pins are keyed by build mode and are set by hand" + if keyed else "the prose or an annotated ref has to move first" + ) + print( + f"{name}: refs recaled but the pin will not follow, so nothing " + f"was written: {reason}", + file=sys.stderr, + ) + return + messages = [f"{name}: {recale}{line}" for line in applied] + if moved: + # Each pin the write changes, as written: a bump that only moved + # upstream_commit announced the source_commit it already held. + after = yaml.safe_load(target.read_text(encoding="utf-8")) or {} + before = yaml.safe_load(path.read_text(encoding="utf-8")) or {} + messages += [ + f"{name}: {bumped}{pin} -> {str(after[pin])[:7]}" + for pin in pins_changed(before, after) + ] + if ( + not args.dry_run and (applied or moved) + and not _promote(args, name, path, target, moved) + ): + return + for message in messages: + print(message) + + +def _gained_since_pin( + args: argparse.Namespace, name: str, profile: dict, report: ProfileReport +) -> bool: + """Whether the cited files gained a file name or a hash literal since the pin. + + Refs that still anchor say nothing of what the code added around them: + mame2003_plus gained two Taito G-NET BIOS sets in a zn.c whose cited + lines only shifted. A bump past that would document a file set the code + no longer has. --accept-changed says the additions were read. + """ + if args.accept_changed or not report.head or report.pin == report.head: + return False + if report.pinned_tag: + # A held pin is not advanced at all: bump_commit says so itself. + return False + if report.new_files is None and report.new_hashes is None: + probe = argparse.Namespace(**{**vars(args), "detect_new_files": True, "watch_hashes": True}) + _attach_detection(probe, profile, report) + gained = sorted({item["name"] for item in report.new_files or () if item.get("since_pin")}) + added = report.new_hashes or [] + if not (gained or added or report.detection_skipped): + return False + detail = ", ".join(gained[:5]) + (f", {len(added)} hash literal(s)" if added else "") + if report.detection_skipped: + detail = f"cited files unread ({report.detection_skipped})" + print( + f"{name}: the cited files changed beyond the refs since the pin ({detail.strip(', ')}), " + "nothing was written. Read them, then rerun with --accept-changed.", + file=sys.stderr, + ) + return True + + +def _attach_detection( + args: argparse.Namespace, profile: dict, report: ProfileReport +) -> None: + """Record undeclared names and hashes of the cited files on the report. + + Kept on the report rather than printed, so --json carries them as the + text report does. A forge that refuses is named on the report: the ref + report already muted it, and one refusal must not end a pass over every + profile. A quota signal still ends the profile. + """ + if not report.head or not (args.detect_new_files or args.watch_hashes): + return + repo = select_repo(profile) + if repo is None: + return + try: + _detect(args, profile, report, repo) + except upstream.RateLimitError: + raise + except upstream.UpstreamError as exc: + report.detection_skipped = str(exc) + + +def _attach_version( + args: argparse.Namespace, profile: dict, report: ProfileReport +) -> None: + """Record core_version against the forge's latest tag and release. + + Kept on the report rather than printed, so --json carries it as the text + report does. A forge that refuses is named on the profile's own line. + """ + repo = select_repo(profile) if report.head else None + if repo is None: + return + try: + report.version = check_version(profile, repo, args.cache_dir, args.offline) + except upstream.RateLimitError: + raise + except upstream.UpstreamError as exc: + print(f"{report.name}: version check skipped: {exc}", file=sys.stderr) + return + relation = None + passed = None + version = report.version + try: + if version and version.tag_commit and report.pin and version.tag_commit != report.pin: + relation = upstream.ancestry( + repo, version.tag_commit, report.pin, args.cache_dir, args.offline + ) + passed = _passed_release(args, repo, version, report.pin) + newer = None if passed or profile.get("pin_frozen") else _newer_release( + args, repo, version, report.pin + ) + except upstream.RateLimitError: + raise + except upstream.UpstreamError as exc: + print(f"{report.name}: tag ancestry unread: {exc}", file=sys.stderr) + newer = None + report.version_warning = version_warning(version, report.pin, relation, passed) or ( + f"{'release' if version.latest_release else 'tag'} {newer} is newer than core_version {version.declared} and the pin: " + "profile it, or hold the pin with pin_frozen and the reason" + if newer and version else None + ) + + +def _read_one( + args: argparse.Namespace, + name: str, + profile: dict, + reports: list[ProfileReport], + failed: set[str], + writes: bool, +) -> None: + """Build one profile's report into ``reports``, then apply the writes.""" + profile_path = Path(args.emulators_dir) / f"{name}.yml" + try: + report = _build_full_report(args, name, profile, profile_path, failed) + except upstream.RateLimitError as exc: + # A quota is one forge's: git.libretro.com answering 429 said + # nothing about the 600 profiles on GitHub. upstream stops asking + # that host, so the rest of the pass costs it nothing. + print(f"{name}: {exc}", file=sys.stderr) + reports.append(ProfileReport( + name=name, entries=[], counts={}, + skipped=f"rate limited: {exc.host or exc}", + )) + failed.add(name) + return + reports.append(report) + if writes: + try: + _apply_writes(args, name, profile, report) + except YamlWriteError as exc: + # The guard already left the file untouched, so keep going. + print(f"{name}: write refused: {exc}", file=sys.stderr) + failed.add(name) + + +# Profiles this run wrote. Their new pins name trees the site's record of +# cited source paths does not hold yet. +_WRITTEN: set[str] = set() + + +def _build_full_report( + args: argparse.Namespace, + name: str, + profile: dict, + profile_path: Path, + failed: set[str], +) -> ProfileReport: + """The report of one profile, with every section the flags ask for. + + A rate limit propagates: the caller decides what one forge's quota + means for the pass. Any other forge failure becomes a skip. + """ + try: + report = build_report( + name, profile, args.cache_dir, args.offline, + profile_path.read_text(encoding="utf-8") + if profile_path.is_file() else None, + ) + except upstream.RateLimitError: + raise + except upstream.NotCachedError as exc: + # Offline with a cache that never stored this file: the profile + # is unverified, not wrong. Reporting GONE sent readers after + # files that exist. + report = ProfileReport( + name=name, entries=[], counts={}, skipped=f"offline: {exc}" + ) + except upstream.GoneError as exc: + # The forge is not coming back: a takedown, or a host that no + # longer resolves. Saying so once in the summary beats repeating + # it on stderr every pass, and it keeps the profile out of the + # backlog, where nobody can act on it anyway. + report = ProfileReport( + name=name, entries=[], counts={}, skipped=f"upstream gone: {exc}" + ) + except upstream.UpstreamError as exc: + # One unreachable forge must not abandon the other profiles. + print(f"{name}: {exc}", file=sys.stderr) + report = ProfileReport( + name=name, entries=[], counts={}, skipped=f"upstream error: {exc}" + ) + failed.add(name) + if not report.skipped and profile_path.is_file(): + report.stale_prose = _read_stale_prose(name, profile_path, args, failed) + if args.check_version and profile.get("version_source"): + report.declared_version = str(profile.get("core_version") or "") or None + report.page_version = page_version(profile, args.cache_dir, args.offline) + report.no_genuine_copy = str( + profile["version_source"].get("no_genuine_copy") or "") or None + if args.check_version: + _attach_version(args, profile, report) + _attach_detection(args, profile, report) + return report + + +def _emit( + args: argparse.Namespace, + selected: dict, + reports: list[ProfileReport], + shown: list[ProfileReport], +) -> bool | None: + """Write the reports in the form the flags ask for. False when the + triage was cut short.""" + if args.as_json: + print(json.dumps([report_to_dict(r) for r in shown], indent=2)) + return None + if args.markdown: + target = Path(args.report_dir) / f"profile-sync-{date.today().isoformat()}.md" + target.parent.mkdir(parents=True, exist_ok=True) + target.write_text(format_markdown(shown), encoding="utf-8") + print(f"written: {target}") + return None + if args.fetch_plan: + for report in shown: + for url in fetch_plan(report): + print(url) + return None + if args.triage: + return _print_triage(args, selected, shown) + for report in shown: + print(format_report(report, args.changed_only)) + _print_extras(args, selected[report.name], report) + print() + if args.changed_only: + _print_elided(reports, len(shown), frozenset(report.name for report in shown)) + return None + + +def _print_elided( + reports: list[ProfileReport], printed: int, listed: frozenset[str] = frozenset() +) -> None: + """Account for what --changed-only left out, grouped by reason.""" + reasons: dict[str, int] = {} + for report in reports: + if report.needs_review() or report.name in listed: + continue + key = report.skipped or "nothing to review" + reasons[key] = reasons.get(key, 0) + 1 + if not reasons: + return + total = sum(reasons.values()) + print(f"{printed} profils listes, {total} non listes:") + for reason, count in sorted(reasons.items(), key=lambda item: -item[1]): + print(f" {count:4d} {reason}") diff --git a/scripts/profiling/triage.py b/scripts/profiling/triage.py new file mode 100644 index 000000000..45a48e712 --- /dev/null +++ b/scripts/profiling/triage.py @@ -0,0 +1,135 @@ +"""Which profiles to read first: drift scores and the triage listing.""" + +from __future__ import annotations + +import argparse +import sys +import time + +from core import upstream +from profiling.detection import _cited_paths +from profiling.reports import ProfileReport, triage_state +from profiling.repos import select_repo +from profiling.versions import VersionReport, check_version + + +TRIAGE_PATH_SAMPLE = 5 +# A pass that asks the forges says where it is at this interval: an online +# --all --triage ran 15 minutes without a word and read as hung. +PROGRESS_SECONDS = 30 + + +DRIFT_WEIGHTS = {"GONE": 40, "CHANGED": 30, "AMBIGUOUS": 15, "SHIFTED": 1} +PIN_WEIGHT = 50 + + +def drift_score( + report: ProfileReport, version: VersionReport | None, commits: int +) -> int: + """Rank profiles by how far they have drifted from their pin.""" + counts = report.counts or {} + score = sum(DRIFT_WEIGHTS.get(k, 0) * v for k, v in counts.items()) + # Prose a reader has to settle weighs as an ambiguous ref, prose + # --realign-prose moves by itself as a shifted one. + score += sum( + DRIFT_WEIGHTS["SHIFTED" if line.startswith("would recale") else "AMBIGUOUS"] + for line in report.stale_prose or () + ) + if ( + version is not None and not version.tag_matches_declared + and version.latest_release and version.latest_release != version.declared + ): + score += 10 + # A pin off its branch or a repository answering under another name is + # set again before any ref can be read, so each outranks a GONE ref: + # pcem ranked last at 0 with an orphaned upstream_commit. + score += PIN_WEIGHT * (len(report.orphaned) + len(report.moved_repos)) + # A profile no repository answers for cannot be read at all, and + # --strict fails on it: it ranked at 0 under the clean profiles. + score += PIN_WEIGHT * report.uncheckable() + score += min(commits, 20) // 4 + return score + + +class _Progress: + """A stderr line every PROGRESS_SECONDS while a pass asks the forges.""" + + def __init__(self, total: int, doing: str, quiet: bool) -> None: + self.total, self.doing, self.quiet = total, doing, quiet + self.last = time.monotonic() + + def step(self, done: int, name: str) -> None: + now = time.monotonic() + if not self.quiet and now - self.last >= PROGRESS_SECONDS: + print(f"{self.doing}: {done} of {self.total}, at {name}", file=sys.stderr, flush=True) + self.last = now + + +def _interrupted(done: int, total: int, what: str) -> None: + print(f"interrupted after {done} of {total} profiles: {what}", file=sys.stderr, flush=True) + + +def _print_triage( + args: argparse.Namespace, selected: dict, reports: list[ProfileReport] +) -> bool: + """Profiles ranked by drift, worst first. False when Ctrl-C cut the + scoring short: the ranking then covers the profiles scored.""" + ranked: list[tuple[int, ProfileReport, str]] = [] + sampled = 0 + progress = _Progress(len(reports), "scored", args.offline or len(reports) < 2) + complete = True + try: + for done, report in enumerate(reports): + progress.step(done, report.name) + sampled += _score(args, selected, report, ranked) + except KeyboardInterrupt: + _interrupted(len(ranked), len(reports), "the ranking covers those scored") + complete = False + if sampled: + # One line for the run: a notice per profile buried the ranking + # under 254 lines of stderr. + print( + f"commit count sampled on {TRIAGE_PATH_SAMPLE} cited paths for " + f"{sampled} profiles citing more", + file=sys.stderr, + ) + print(f"{'score':>5s} {'profile':30s} state (worst first)") + pending = 0 + for score, report, refused in sorted(ranked, key=lambda item: -item[0]): + state = triage_state(report) + if refused: + state += f", {refused}" + pending += bool(report.needs_review() or report.uncheckable()) + print(f"{score:5d} {report.name:30s} {state}") + print(f"{len(ranked)} profiles, {pending} with something to review") + return complete + + +def _score( + args: argparse.Namespace, selected: dict, report: ProfileReport, ranked: list +) -> bool: + """Append one profile's drift score to ``ranked``. True when its commit + count was read on a sample of the paths it cites.""" + profile = selected[report.name] + version = None + commits = 0 + refused = "" + sampled = False + repo = select_repo(profile) + if report.head and repo is not None: + paths = _cited_paths(report) + try: + version = check_version(profile, repo, args.cache_dir, args.offline) + for path in paths[:TRIAGE_PATH_SAMPLE]: + commits += upstream.commits_touching( + repo, path, report.pin, args.cache_dir, args.offline, + head=report.head, + ) + except upstream.UpstreamError as exc: + # One forge refusing a request (Eden's answers 403 to commit + # lookups) leaves this profile unscored, not the whole ranking. + refused = "commit history unreachable" + print(f"{report.name}: {exc}", file=sys.stderr) + sampled = len(paths) > TRIAGE_PATH_SAMPLE and not refused + ranked.append((drift_score(report, version, commits), report, refused)) + return sampled diff --git a/scripts/profiling/versions.py b/scripts/profiling/versions.py new file mode 100644 index 000000000..310b82032 --- /dev/null +++ b/scripts/profiling/versions.py @@ -0,0 +1,218 @@ +"""The version a profile declares against the version upstream publishes.""" + +from __future__ import annotations + +import argparse +import re +import sys +from dataclasses import dataclass + +from core import upstream + + +@dataclass(frozen=True) +class VersionReport: + declared: str + latest_tag: str | None + latest_release: str | None + release_date: str | None + tag_matches_declared: bool + tag_commit: str | None + + +def check_version( + profile: dict, repo: upstream.Repo, cache_dir: str, offline: bool +) -> VersionReport | None: + """Declared core_version against the latest upstream tag and release.""" + declared = str(profile.get("core_version") or "") + if not declared: + return None + release = upstream.latest_release(repo, cache_dir, offline) + # Asked by name: the first page of tags misses a release that a project + # publishing nightlies has pushed past a hundred newer tags. + commit = upstream.tag_commit(repo, declared, cache_dir, offline) + return VersionReport( + declared, + upstream.newest_tag(repo, cache_dir, offline), + release.tag if release else None, + release.date if release else None, + commit is not None, + commit, + ) + + +def page_version(profile: dict, cache_dir: str, offline: bool) -> str | None: + """The latest version a profile's version_source page announces. + + Forty-odd profiles live off any forge: profile_sync could only say + "unsupported host", and a new Altirra or mednafen release went unseen. + """ + source = profile.get("version_source") or {} + url, pattern = str(source.get("url") or ""), str(source.get("pattern") or "") + if not url or not pattern: + return None + try: + text = upstream.fetch_page(url, cache_dir, offline) + except upstream.UpstreamError as exc: + print(f"{profile.get('emulator', '?')}: {url}: {exc}", file=sys.stderr) + return None + match = re.search(pattern, text or "") + return match.group(1) if match else None + + +def version_warning( + report: VersionReport | None, + pin: str | None, + relation: tuple[int, int] | None = None, + passed_release: str | None = None, +) -> str | None: + """Flag a declared version that names a tag away from the resolved pin. + + ``relation`` is the pin's (ahead, behind) count against the tag commit. + A pin after its tag is a build between two releases. A pin before it + describes code the declared release had already replaced. + ``passed_release`` is a later release the pin already contains, which + core_version should name instead. + """ + if report is None or not pin: + return None + # Before the tag guard: parallel-launcher declared 9.0.5, a name no tag + # carries (v9.0-6), and the release its HEAD contains went unsaid. + if passed_release: + return ( + f"core_version {report.declared} is older than the code: the pin " + f"{pin[:7]} already contains release {passed_release}" + ) + if not report.tag_commit or report.tag_commit == pin: + return None + where = "" + if relation is not None: + ahead, behind = relation + if ahead and not behind: + where = f", {ahead} commits after the tag" + elif behind and not ahead: + where = f", {behind} commits before the tag: core_version is later than the code" + elif ahead and behind: + where = f", off the tag's history ({ahead} ahead, {behind} behind)" + return ( + f"core_version {report.declared} names tag commit {report.tag_commit[:7]}, " + f"the pin resolved to {pin[:7]}{where}" + ) + + +# A tag that reads as a version number: 1.1, v107, v9.0-6, 2.55, 1.1d. +# Nightly, continuous and latest tags do not. +_VERSION_TAG_RE = re.compile(r"v?\d+(?:[._-]\d+)*[a-z]?", re.IGNORECASE) + + +def published_version(version: VersionReport | None) -> str | None: + """The latest version the project published: its latest release, or for + a project that publishes no release, its newest tag when that tag reads + as a version number. + + GitLab and Codeberg projects often tag without a release: eightyone sat + on v1.2 and higan on 106 while v1.3 and v107 were tagged, and nothing + compared them. + """ + if version is None: + return None + if version.latest_release: + return version.latest_release + tag = version.latest_tag + if tag and _VERSION_TAG_RE.fullmatch(tag): + return tag + return None + + +def _later_number(candidate: str, declared: str) -> bool: + """Whether both read as numbers of one scheme and the candidate's is higher. + + One scheme means the same major number: libretro forks tag RetroArch's + numbers (mame2015 0.160 beside v1.4.1) and some projects tag dates + (np2pi 0.83 beside 20120302). Trailing zeros do not count: v1 is v1.0. + """ + if not (_VERSION_TAG_RE.fullmatch(candidate) and _VERSION_TAG_RE.fullmatch(declared)): + return False + + def key(text: str) -> tuple[int, ...]: + numbers = [int(n) for n in re.findall(r"\d+", text)] + while len(numbers) > 1 and numbers[-1] == 0: + numbers.pop() + return tuple(numbers) + + high, low = key(candidate), key(declared) + if (len(high) == 1) != (len(low) == 1): + return False + if len(high) == 1: + # higan 106 and v107 are one scheme, openlara v1 and 20170424 two. + return len(str(high[0])) == len(str(low[0])) and high > low + return high[0] == low[0] and high > low + + +def _newer_release( + args: argparse.Namespace, + repo: upstream.Repo, + version: VersionReport | None, + pin: str | None, +) -> str | None: + """The latest release, when it comes after both the declared tag and the pin. + + A profile pinned on its release tag reads as clean forever: armsx1 sat on + 0.1.3, armsx2 on 2.8 and x1-box on 1.2.6 while 0.3.6, 2.9 and 1.3.2 were + out, and nothing said so. A held pin (pin_frozen) is left alone. + """ + published = published_version(version) + if not version or not pin or not version.tag_commit or not published: + return None + if published == version.declared: + return None + commit = upstream.tag_commit(repo, published, args.cache_dir, args.offline) + if not commit or commit == pin: + return None + after_tag = upstream.ancestry(repo, version.tag_commit, commit, args.cache_dir, args.offline) + if not after_tag or after_tag[0] == 0 or after_tag[1] != 0: + return None + after_pin = upstream.ancestry(repo, pin, commit, args.cache_dir, args.offline) + if not after_pin or after_pin[0] == 0: + return None + return published + + +def _passed_release( + args: argparse.Namespace, + repo: upstream.Repo, + version: VersionReport | None, + pin: str | None, +) -> str | None: + """The latest release, when the pin already contains it and core_version + names another one. + + The latest tag is no witness: nightly, continuous and Pre-release tags + sort first on many forges. The latest release is a version the project + published as such. + """ + published = published_version(version) + if not version or not pin or not published: + return None + if published == version.declared: + return None + if not version.tag_commit and not _later_number(published, version.declared): + # No tag to order them: only a release numbered past the declared + # version says it is older. "latest", "continuous" or 1.53 under a + # declared 1.61 say nothing. + return None + commit = upstream.tag_commit(repo, published, args.cache_dir, args.offline) + if not commit: + return None + if version.tag_commit and version.tag_commit != commit: + # A project can publish a release older than its newest tag: + # pcsx2x6's latest release is v0.2.20, its declared tag v0.2.22. + older = upstream.ancestry(repo, commit, version.tag_commit, args.cache_dir, args.offline) + if older and older[1] == 0: + return None + if commit == pin: + return published + relation = upstream.ancestry(repo, commit, pin, args.cache_dir, args.offline) + if relation and relation[1] == 0: + return published + return None diff --git a/scripts/profiling/yamledit.py b/scripts/profiling/yamledit.py new file mode 100644 index 000000000..890908e18 --- /dev/null +++ b/scripts/profiling/yamledit.py @@ -0,0 +1,192 @@ +"""Line edits of a profile YAML that keep its layout and comments, checked against the parsed result.""" + +from __future__ import annotations + +from pathlib import Path + +import yaml + + + +class YamlWriteError(Exception): + """A profile edit did not land exactly as intended.""" + + +def _top_level_indices(lines: list[str], key: str) -> list[int]: + """Indices of lines declaring a key at the top level of the document.""" + prefix = f"{key}:" + return [i for i, line in enumerate(lines) if line.startswith(prefix)] + + +def insert_after_line(text: str, key: str, new_line: str) -> str: + """Insert a line right after a top-level key.""" + lines = text.splitlines() + indices = _top_level_indices(lines, key) + if len(indices) != 1: + raise YamlWriteError( + f"{key}: expected one top-level line, found {len(indices)}" + ) + lines.insert(indices[0] + 1, new_line) + return "\n".join(lines) + ("\n" if text.endswith("\n") else "") + + +def _field_value(line: str) -> str: + """The value of a `key: value` line as the loader reads it. + + etlegacy quotes `(FS_FOpenFileRead \\"pak0.pk3\\")` inside a double-quoted + source_ref: stripping the outer quotes keeps the backslashes, and the + line never equalled the parsed value it was searched for. + """ + raw = line.split(":", 1)[1].strip() + try: + parsed = yaml.safe_load(raw) + except yaml.YAMLError: + parsed = None + if isinstance(parsed, str): + return parsed + return raw.strip('"').strip("'") + + +def _double_quoted(value: str) -> str: + """A YAML double-quoted scalar reading back as value.""" + return '"' + value.replace("\\", "\\\\").replace('"', '\\"') + '"' + + +def find_field_line(text: str, key: str, expected: str, start: int = 0) -> int: + """Index of the first `key:` line at or after `start` holding `expected`. + + Positional counting is not safe here: `source_ref` also appears outside + `files:` in some profiles, under `data_directories:` for instance, which + shifts every ordinal past it. + """ + lines = text.splitlines() + marker = f"{key}:" + for index in range(start, len(lines)): + line = lines[index] + if line.strip().startswith(marker) and _field_value(line) == expected: + return index + raise YamlWriteError( + f"{key}: no line holding {expected!r} at or after line {start + 1}" + ) + + +def replace_field_line(text: str, key: str, expected: str, value: str, + start: int = 0) -> tuple[str, int]: + """Rewrite the first `key:` line holding `expected`. Returns text and index.""" + index = find_field_line(text, key, expected, start) + lines = text.splitlines() + line = lines[index] + indent = line[: len(line) - len(line.lstrip())] + lines[index] = f"{indent}{key}: {_double_quoted(value)}" + joined = "\n".join(lines) + ("\n" if text.endswith("\n") else "") + return joined, index + + +def apply_edit(path: Path, new_text: str, expected: dict) -> None: + """Write, then verify the parsed document matches what was intended.""" + original = path.read_text(encoding="utf-8") + path.write_text(new_text, encoding="utf-8") + try: + written = yaml.safe_load(new_text) + except yaml.YAMLError as exc: + path.write_text(original, encoding="utf-8") + raise YamlWriteError(f"{path}: parse failed after write: {exc}") from exc + if written != expected: + path.write_text(original, encoding="utf-8") + raise YamlWriteError(f"{path}: structure changed beyond the intended field") + + +# Key order of a written version_basis, as a reader expects it. +_BASIS_ORDER = ("kind", "why", "repo", "url", "commit", "path", "pattern", "format", + "match", "tag", "distance", "of", "artifact") + + +def _top_block(lines: list[str], key: str) -> tuple[int, int] | None: + """Line span of a top-level key and the indented lines under it.""" + indices = _top_level_indices(lines, key) + if not indices: + return None + if len(indices) > 1: + raise YamlWriteError(f"{key}: expected one top-level line, found {len(indices)}") + end = indices[0] + 1 + while end < len(lines) and (lines[end].startswith((" ", "\t")) or not lines[end].strip()): + end += 1 + # Blank lines closing the block belong to the document, not to it. + while end > indices[0] + 1 and not lines[end - 1].strip(): + end -= 1 + return indices[0], end + + +def _render_version(value: object) -> list[str]: + if isinstance(value, dict): + return ["core_version:"] + [ + f" {mode}: {'null' if v is None else _double_quoted(str(v))}" + for mode, v in value.items() + ] + return [f"core_version: {'null' if value is None else _double_quoted(str(value))}"] + + +def _render_basis(basis: dict) -> list[str]: + def ordered(entry: dict) -> dict: + rank = {key: i for i, key in enumerate(_BASIS_ORDER)} + return dict(sorted(entry.items(), key=lambda item: rank.get(item[0], len(rank)))) + + def scalar(value: object) -> str: + # Every string quoted: a tag 2407 or 0.10 read back as a number. A + # pattern keeps its backslashes readable in single quotes. + if isinstance(value, bool) or not isinstance(value, str): + return str(value).lower() if isinstance(value, bool) else str(value) + if "\\" in value or '"' in value: + return "'" + value.replace("'", "''") + "'" + return _double_quoted(value) + + def block(entry: dict, indent: str) -> list[str]: + # kind, why and repo are words of the schema's enums, written bare. + return [f"{indent}{key}: {value if key in ('kind', 'why', 'repo') else scalar(value)}" + for key, value in ordered(entry).items()] + + if "kind" in basis: + return ["version_basis:", *block(basis, " ")] + lines = ["version_basis:"] + for mode, entry in basis.items(): + lines += [f" {mode}:", *block(entry, " ")] + return lines + + +def set_version_fields(text: str, version: object, basis: dict | None) -> str: + """Rewrite core_version and version_basis, version_basis right after it.""" + lines = text.splitlines() + span = _top_block(lines, "core_version") + rendered = _render_version(version) + if span is None: + anchor = _top_block(lines, "upstream_commit") or _top_block(lines, "source_commit") \ + or _top_block(lines, "profiled_date") or _top_block(lines, "upstream") \ + or _top_block(lines, "source") or _top_block(lines, "alias_of") \ + or _top_block(lines, "type") + if anchor is None: + raise YamlWriteError("core_version: no line to write it after") + lines[anchor[1]:anchor[1]] = rendered + else: + lines[span[0]:span[1]] = rendered + if basis is not None: + old = _top_block(lines, "version_basis") + if old is not None: + del lines[old[0]:old[1]] + after = _top_block(lines, "core_version") + if after is None: + raise YamlWriteError("version_basis: core_version not found to write it after") + lines[after[1]:after[1]] = _render_basis(basis) + return "\n".join(lines) + ("\n" if text.endswith("\n") else "") + + +def backfill_commit(path: Path, sha: str) -> bool: + """Insert source_commit after profiled_date. False when already present.""" + text = path.read_text(encoding="utf-8") + document = yaml.safe_load(text) + if document.get("source_commit"): + return False + new_text = insert_after_line(text, "profiled_date", f'source_commit: "{sha}"') + expected = dict(document) + expected["source_commit"] = sha + apply_edit(path, new_text, expected) + return True diff --git a/tests/test_audit_regressions.py b/tests/test_audit_regressions.py index 570cf2f5d..fae73d48b 100644 --- a/tests/test_audit_regressions.py +++ b/tests/test_audit_regressions.py @@ -4166,7 +4166,7 @@ class ACitationInsideASourceRefIsTracked(unittest.TestCase): pin, and pce-emu joined two citations with ` + `: neither was anchored.""" def citations(self, ref: str) -> list[str]: - from scripts.profile_sync import collect_citations + from scripts.profiling.citations import collect_citations document = {"files": [{"name": "x.rom", "source_ref": ref}]} return [(c.kind, c.ref) for c in collect_citations(document)] @@ -4204,11 +4204,12 @@ class TriageSurvivesAForgeRefusal(unittest.TestCase): def test_the_ranking_is_printed_with_the_profile_marked(self): from types import SimpleNamespace - from scripts import profile_sync + from scripts.profiling import reports as profiling_reports + from scripts.profiling import triage reports = [ - profile_sync.ProfileReport(name="eden", pin="a" * 40, head="b" * 40), - profile_sync.ProfileReport(name="vice", pin="c" * 40, head="d" * 40), + profiling_reports.ProfileReport(name="eden", pin="a" * 40, head="b" * 40), + profiling_reports.ProfileReport(name="vice", pin="c" * 40, head="d" * 40), ] selected = { "eden": {"source": "https://git.eden-emu.dev/eden-emu/eden"}, @@ -4217,16 +4218,16 @@ class TriageSurvivesAForgeRefusal(unittest.TestCase): def check_version(profile, *_args): if "eden" in profile["source"]: - raise profile_sync.upstream.UpstreamError("HTTP 403") + raise triage.upstream.UpstreamError("HTTP 403") return args = SimpleNamespace(cache_dir="", offline=True) out = io.StringIO() - with mock.patch.object(profile_sync, "check_version", check_version), \ - mock.patch.object(profile_sync, "select_repo", lambda p: object()), \ + with mock.patch.object(triage, "check_version", check_version), \ + mock.patch.object(triage, "select_repo", lambda p: object()), \ contextlib.redirect_stdout(out), \ contextlib.redirect_stderr(io.StringIO()): - profile_sync._print_triage(args, selected, reports) + triage._print_triage(args, selected, reports) # A header row and a closing count frame the ranking. lines = out.getvalue().splitlines()[1:-1] self.assertEqual(len(lines), 2) @@ -5943,7 +5944,7 @@ class AHashListIsReadOneWay(unittest.TestCase): def test_every_reader_splits_the_list(self): from scripts.cross_reference import _declared_content from scripts.core.hashing import parse_hash_list - from scripts.profile_sync import entry_hashes + from scripts.profiling.citations import entry_hashes a, b = "a" * 64, "B" * 64 entry = {"name": "BS-X.bin", "sha256": f"{a}, {b}", "crc32": "0xDEADBEEF"} diff --git a/tests/test_launcher_evidence.py b/tests/test_launcher_evidence.py index 0ea963bd2..a2c4c4b88 100644 --- a/tests/test_launcher_evidence.py +++ b/tests/test_launcher_evidence.py @@ -23,7 +23,8 @@ from resolve.launchers import ( launcher_places, launcher_unproven, ) -from profile_sync import _check_build_refs, shipped_build_holders +from profiling.resolver import _check_build_refs +from profiling.repos import shipped_build_holders from scraper.retrobat_launchers import _choices_apart from scraper.shell_launchers import LauncherPatternMissing, Script, config_placement from resolve.launcherrefs import config_ref, is_feature_ref diff --git a/tests/test_layers.py b/tests/test_layers.py index 39a060be7..4a3a0fca1 100644 --- a/tests/test_layers.py +++ b/tests/test_layers.py @@ -23,6 +23,12 @@ PACKAGE_LAYERS = {"core": 0, "model": 1, "resolve": 2, "scraper": 3, "exporter": LIBRARY_LAYER = 3 COMMAND_LAYER = 4 +# The other domain packages a domain package may import. A package listed +# here imports no sibling outside its set. +SIBLING_IMPORTS: dict[str, frozenset[str]] = { + "profiling": frozenset(), +} + # Top-level modules other modules of scripts/ import: library code that a # later wave moves into a package. NOT_YET_PACKAGED = frozenset({ @@ -31,7 +37,7 @@ NOT_YET_PACKAGED = frozenset({ "cross_reference", "dedup", "derivations", "export_native", "generate_event_schema", "generate_pack", "generate_readme", "generate_site", "manifestselect", "measurement_records", "packextras", "packpaths", "packreadme", "packresolve", "packverify", "pin_installer", - "pr_checks", "profile_sync", "provenance_report", "refresh_data_dirs", "region_audit", + "pr_checks", "provenance_report", "refresh_data_dirs", "region_audit", "release_record", "romfolders", "romset_recipes", "siteagents", "sitebootstrap", "sitecollection", "sitediagrams", "siteentries", "siteexports", "siteguides", "sitehandheld", "siteinputs", "sitemarkup", "sitemeasurements", "siterender", "sitesources", "sitestarlight", @@ -181,6 +187,14 @@ class ImportLayers(unittest.TestCase): edges.setdefault(a, set()).add(b) self.assertIsNone(find_cycle(set(PACKAGE_LAYERS), edges)) + def test_domain_packages_import_declared_siblings_only(self): + found = sorted( + f"{src} imports {dst}" for src, dst in self.graph.edges() + if (a := src.split(".")[0]) in SIBLING_IMPORTS and (b := dst.split(".")[0]) != a + and PACKAGE_LAYERS.get(b) == LIBRARY_LAYER and b not in SIBLING_IMPORTS[a] + ) + self.assertEqual(found, []) + def test_foundation_modules_form_no_cycle(self): """Inside core, model and resolve, no two modules import each other, not even through a function-level import.""" diff --git a/tests/test_profile_repositories.py b/tests/test_profile_repositories.py index e29fb4fb8..ef476313a 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 profile_sync import declared_repositories +from profiling.repos import declared_repositories REPO_ROOT = Path(__file__).resolve().parent.parent diff --git a/tests/test_profile_sync.py b/tests/test_profile_sync.py index 09c7efba1..aa2891265 100644 --- a/tests/test_profile_sync.py +++ b/tests/test_profile_sync.py @@ -19,44 +19,53 @@ from unittest import mock import yaml import profile_sync +from profiling import anchoring +from profiling import bump +from profiling import citations as profiling_citations +from profiling import detection +from profiling import proseedit +from profiling import realign +from profiling import rebase +from profiling import reports as profiling_reports +from profiling import repos as profiling_repos +from profiling import resolver +from profiling import run as profiling_run +from profiling import triage +from profiling import versions from model import sourceref -from profile_sync import ( +from profiling.reports import ( EntryReport, - PartResult, ProfileReport, - VersionReport, + fetch_plan, + format_markdown, + format_report, + report_to_dict, +) +from profiling.anchoring import PartResult, anchor_block, anchor_part, resolve_rename, worst_status +from profiling.versions import VersionReport, check_version, version_warning +from profiling.yamledit import ( YamlWriteError, - anchor_block, - anchor_part, apply_edit, backfill_commit, - build_parser, - build_report, - bump_commit, - check_version, - collect_citations, - collect_tokens, + find_field_line, + insert_after_line, + replace_field_line, +) +from profile_sync import build_parser, select_profiles +from profiling.resolver import build_report +from profiling.bump import bump_commit +from profiling.citations import collect_citations, collect_tokens +from profiling.detection import ( declared_hashes, declared_names, detect_new_files, - drift_score, - fetch_plan, - find_field_line, - format_markdown, - format_report, - insert_after_line, - rebase_refs, - replace_field_line, - report_to_dict, - resolve_rename, - select_profiles, - select_repo, tree_diff, unified_for_path, - version_warning, watch_hashes, - worst_status, ) +from profiling.triage import drift_score +from profiling.rebase import rebase_refs +from profiling.repos import select_repo from model.sourceref import RefPart, parse_source_ref, split_source_ref from core import upstream from profiling import version_basis @@ -254,7 +263,7 @@ class TestSourceMirror(unittest.TestCase): "source_mirror": "https://codeberg.org/o/mirror", } self.assertEqual( - [url for _, _, url in profile_sync.declared_repositories(profile)], + [url for _, _, url in profiling_repos.declared_repositories(profile)], [ "https://github.com/o/port", "https://github.com/o/up", @@ -265,7 +274,7 @@ class TestSourceMirror(unittest.TestCase): def test_mirror_alone_is_enough(self): profile = {"source_mirror": "https://codeberg.org/o/mirror"} self.assertEqual( - profile_sync.declared_repositories(profile), + profiling_repos.declared_repositories(profile), [("source_mirror", "", "https://codeberg.org/o/mirror")], ) @@ -288,9 +297,9 @@ class TestSourceMirror(unittest.TestCase): def at(*args, **kwargs): raise AssertionError("a declared pin needs no date lookup") - with mock.patch.object(profile_sync.upstream, "resolve_head", head), \ - mock.patch.object(profile_sync.upstream, "resolve_commit_at", at): - views = profile_sync.select_views(profile, "unused", True) + with mock.patch.object(upstream, "resolve_head", head), \ + mock.patch.object(upstream, "resolve_commit_at", at): + views = profiling_repos.select_views(profile, "unused", True) self.assertEqual([(v.repo.host, v.pin) for v in views], [("codeberg.org", "d16735f5")]) self.assertIn(("head", "codeberg.org", "dev"), calls) @@ -308,13 +317,13 @@ class TestSourceMirror(unittest.TestCase): "source_commit": "87b176a", "upstream_commit": "07021f0", } - with tempfile.TemporaryDirectory() as cache, self.assertRaises(profile_sync.upstream.NotCachedError): - profile_sync.select_views(profile, cache, True) + with tempfile.TemporaryDirectory() as cache, self.assertRaises(upstream.NotCachedError): + profiling_repos.select_views(profile, cache, True) def test_a_profile_without_a_mirror_is_unchanged(self): profile = {"source": "https://github.com/o/port"} self.assertEqual( - profile_sync.declared_repositories(profile), + profiling_repos.declared_repositories(profile), [("source", "", "https://github.com/o/port")], ) @@ -343,7 +352,7 @@ class TestGoneUpstreamIsNotAnError(unittest.TestCase): skipped="upstream error: h: HTTP 403") buffer = io.StringIO() with contextlib.redirect_stdout(buffer): - profile_sync._print_elided([gone, failing], 0) + profiling_run._print_elided([gone, failing], 0) out = buffer.getvalue() self.assertIn("upstream gone", out) self.assertIn("upstream error", out) @@ -357,7 +366,7 @@ class TestGoneUpstreamIsNotAnError(unittest.TestCase): moved_repos=["a/b answers as c/b (renamed or transferred)"], ) self.assertEqual( - profile_sync.triage_state(report), + profiling_reports.triage_state(report), "2 to review, 1 orphaned pin, 1 repository moved, 2 unchecked", ) @@ -455,12 +464,12 @@ class TestAnchorBlock(unittest.TestCase): self.assertEqual(result.status, "CHANGED") def test_a_file_past_the_cap_skips_mapping_with_reason(self): - size = profile_sync.MAX_MATCH_LINES + 1 + size = anchoring.MAX_MATCH_LINES + 1 pin = [f"line {i}" for i in range(size)] head = [f"other {i}" for i in range(size)] result = anchor_block(pin, head, 10, 10) self.assertEqual(result.status, "CHANGED") - self.assertIn(str(profile_sync.MAX_MATCH_LINES), result.reason) + self.assertIn(str(anchoring.MAX_MATCH_LINES), result.reason) self.assertIsNone(result.start) def test_a_thirty_thousand_line_file_is_still_mapped(self): @@ -490,32 +499,32 @@ class TestVerifyAtPin(unittest.TestCase): """mame cited casloopy.cpp:2105-2462 in a 2105-line file.""" part = RefPart("a.c", 3, 9, "a.c:3-9") self.assertEqual( - profile_sync.verify_at_pin(part, self.LINES, ["deadbeef"]).status, "GONE" + anchoring.verify_at_pin(part, self.LINES, ["deadbeef"]).status, "GONE" ) def test_declared_value_present(self): part = RefPart("a.c", 3, 3, "a.c:3") self.assertEqual( - profile_sync.verify_at_pin(part, self.LINES, ["deadbeef"]).status, + anchoring.verify_at_pin(part, self.LINES, ["deadbeef"]).status, "ANCHORED", ) def test_value_found_within_the_context_window(self): part = RefPart("a.c", 1, 1, "a.c:1") self.assertEqual( - profile_sync.verify_at_pin(part, self.LINES, ["deadbeef"]).status, + anchoring.verify_at_pin(part, self.LINES, ["deadbeef"]).status, "ANCHORED", ) def test_declared_value_absent_from_the_whole_file(self): part = RefPart("a.c", 1, 1, "a.c:1") - result = profile_sync.verify_at_pin(part, self.LINES, ["cafebabe"]) + result = anchoring.verify_at_pin(part, self.LINES, ["cafebabe"]) self.assertEqual(result.status, "ANCHORED") def test_declared_value_carried_by_another_line(self): lines = ["load()", "", "", "", "", "", 'rom("cafebabe")'] part = RefPart("a.c", 1, 1, "a.c:1") - result = profile_sync.verify_at_pin( + result = anchoring.verify_at_pin( part, lines, ["cafebabe"], ["cafebabe"] ) self.assertEqual(result.status, "MOVED") @@ -525,9 +534,9 @@ class TestVerifyAtPin(unittest.TestCase): """ti99sim cites v0.0.11 beside its v0.16.0 pin.""" other = RefPart("src/sdl/main.cpp", 407, 411, "src/sdl/main.cpp:407-411 (v0.0.11)") same = RefPart("src/core/ti994a.cpp", 122, 131, "src/core/ti994a.cpp:122-131 (v0.16.0)") - self.assertEqual(profile_sync.cites_another_version(other, "0.16.0"), "0.0.11") - self.assertIsNone(profile_sync.cites_another_version(same, "0.16.0")) - self.assertIsNone(profile_sync.cites_another_version(same, {"libretro": "v0.16.0"})) + self.assertEqual(profiling_citations.cites_another_version(other, "0.16.0"), "0.0.11") + self.assertIsNone(profiling_citations.cites_another_version(same, "0.16.0")) + self.assertIsNone(profiling_citations.cites_another_version(same, {"libretro": "v0.16.0"})) def test_a_profile_with_a_repository_names_a_revision(self): """redream declared libretro/redream, a repository that never @@ -561,7 +570,7 @@ class TestVerifyAtPin(unittest.TestCase): refs = " ".join( str(entry.get("source_ref") or "") for entry in profile.get("files") or [] ) - if not profile_sync._VERSION_NOTE_RE.search(refs): + if not profiling_citations._VERSION_NOTE_RE.search(refs): continue declared = profile.get("core_version") values = declared.values() if isinstance(declared, dict) else [declared] @@ -574,7 +583,7 @@ class TestVerifyAtPin(unittest.TestCase): lines = ['option(ENABLE_ROM_CLONE_PRAVETS "" OFF)', "", "", "", "", "", "", "", "", "", "", "", "res/roms/pravets82.rom"] part = RefPart("CMakeLists.txt", 1, 1, "CMakeLists.txt:1") - result = profile_sync.verify_at_pin( + result = anchoring.verify_at_pin( part, lines, ["0123456789abcdef0123456789abcdef", "pravets82"], ["0123456789abcdef0123456789abcdef"], ) @@ -583,61 +592,61 @@ class TestVerifyAtPin(unittest.TestCase): def test_declared_value_on_several_lines_is_ambiguous(self): lines = ["load()", "", "", "", 'a("cafebabe")', "", 'b("cafebabe")'] part = RefPart("a.c", 1, 1, "a.c:1") - result = profile_sync.verify_at_pin( + result = anchoring.verify_at_pin( part, lines, ["cafebabe"], ["cafebabe"] ) self.assertEqual(result.status, "AMBIGUOUS") def test_one_anchored_part_settles_the_others(self): parts = [ - profile_sync.PartResult( + anchoring.PartResult( RefPart("a.c", 1, 1, "a.c:1"), "ANCHORED", None, None, None, [] ), - profile_sync.PartResult( + anchoring.PartResult( RefPart("a.c", 9, 9, "a.c:9"), "AMBIGUOUS", None, None, None, [1, 2] ), ] - settled = profile_sync.reconcile_self_check(parts) + settled = anchoring.reconcile_self_check(parts) self.assertEqual([p.status for p in settled], ["ANCHORED", "ANCHORED"]) def test_an_absent_part_survives_reconciliation(self): parts = [ - profile_sync.PartResult( + anchoring.PartResult( RefPart("a.c", 1, 1, "a.c:1"), "ANCHORED", None, None, None, [] ), - profile_sync.PartResult( + anchoring.PartResult( RefPart("b.c", 9, 9, "b.c:9"), "GONE", None, None, None, [] ), ] - settled = profile_sync.reconcile_self_check(parts) + settled = anchoring.reconcile_self_check(parts) self.assertEqual([p.status for p in settled], ["ANCHORED", "GONE"]) def test_nothing_anchored_leaves_the_verdicts_alone(self): parts = [ - profile_sync.PartResult( + anchoring.PartResult( RefPart("a.c", 1, 1, "a.c:1"), "MOVED", None, 5, 5, [5] ), ] self.assertEqual( - [p.status for p in profile_sync.reconcile_self_check(parts)], ["MOVED"] + [p.status for p in anchoring.reconcile_self_check(parts)], ["MOVED"] ) def test_external_citation_is_out_of_reach_at_the_pin_too(self): part = RefPart("munt ROMInfo.cpp", 1, 1, "munt ROMInfo.cpp:1") - result = profile_sync.verify_at_pin(part, None, ["cafebabe"]) + result = anchoring.verify_at_pin(part, None, ["cafebabe"]) self.assertEqual(result.status, "EXTERNAL") def test_a_name_fragment_elsewhere_is_not_evidence(self): # Only a hash is unique enough to prove a ref points elsewhere. lines = ["load()", "", "", "", "", "", "tyrian2.c line"] part = RefPart("a.c", 1, 1, "a.c:1") - result = profile_sync.verify_at_pin(part, lines, ["tyrian2"], []) + result = anchoring.verify_at_pin(part, lines, ["tyrian2"], []) self.assertEqual(result.status, "ANCHORED") def test_missing_file(self): part = RefPart("a.c", 1, 1, "a.c:1") self.assertEqual( - profile_sync.verify_at_pin(part, None, ["deadbeef"]).status, "GONE" + anchoring.verify_at_pin(part, None, ["deadbeef"]).status, "GONE" ) def test_an_entry_with_nothing_to_look_for_is_unchecked_not_anchored(self): @@ -645,22 +654,22 @@ class TestVerifyAtPin(unittest.TestCase): pin equal to HEAD its ref was reported anchored while the cited block had been deleted upstream. No token means no verdict.""" part = RefPart("a.c", 3, 3, "a.c:3") - result = profile_sync.verify_at_pin(part, self.LINES, []) + result = anchoring.verify_at_pin(part, self.LINES, []) self.assertEqual(result.status, "UNCHECKED") - self.assertNotIn("UNCHECKED", profile_sync.REVIEW_STATUSES) - self.assertEqual(profile_sync.worst_status(["ANCHORED", "UNCHECKED"]), "UNCHECKED") - self.assertEqual(profile_sync.worst_status(["UNCHECKED", "SHIFTED"]), "SHIFTED") + self.assertNotIn("UNCHECKED", anchoring.REVIEW_STATUSES) + self.assertEqual(anchoring.worst_status(["ANCHORED", "UNCHECKED"]), "UNCHECKED") + self.assertEqual(anchoring.worst_status(["UNCHECKED", "SHIFTED"]), "SHIFTED") def test_line_beyond_the_file(self): part = RefPart("a.c", 99, 99, "a.c:99") self.assertEqual( - profile_sync.verify_at_pin(part, self.LINES, ["deadbeef"]).status, "GONE" + anchoring.verify_at_pin(part, self.LINES, ["deadbeef"]).status, "GONE" ) def test_a_ref_without_a_line_is_accepted(self): part = RefPart("a.c", None, None, "a.c") self.assertEqual( - profile_sync.verify_at_pin(part, self.LINES, ["deadbeef"]).status, + anchoring.verify_at_pin(part, self.LINES, ["deadbeef"]).status, "ANCHORED", ) @@ -668,19 +677,19 @@ class TestVerifyAtPin(unittest.TestCase): # Nothing to re-read, so no review is demanded, but the verdict says # that nothing was checked rather than claiming the ref anchors. part = RefPart("a.c", 1, 1, "a.c:1") - result = profile_sync.verify_at_pin(part, self.LINES, []) + result = anchoring.verify_at_pin(part, self.LINES, []) self.assertEqual(result.status, "UNCHECKED") - self.assertNotIn(result.status, profile_sync.REVIEW_STATUSES) + self.assertNotIn(result.status, anchoring.REVIEW_STATUSES) class TestExternalCitation(unittest.TestCase): def test_project_name_then_file(self): - self.assertTrue(profile_sync.is_external_citation("munt ROMInfo.cpp")) + self.assertTrue(profiling_citations.is_external_citation("munt ROMInfo.cpp")) self.assertTrue( - profile_sync.is_external_citation("Nuked-SC55-CLAP rom_io.cpp") + profiling_citations.is_external_citation("Nuked-SC55-CLAP rom_io.cpp") ) self.assertTrue( - profile_sync.is_external_citation("EmuDeck emuDeckares.sh") + profiling_citations.is_external_citation("EmuDeck emuDeckares.sh") ) def test_a_file_without_an_extension_is_still_a_citation(self): @@ -705,15 +714,15 @@ class TestExternalCitation(unittest.TestCase): def test_plain_paths_are_not_citations(self): for path in ("src/midi/mt32.cpp", "libretro.c", "a/b/c.h"): - self.assertFalse(profile_sync.is_external_citation(path), path) + self.assertFalse(profiling_citations.is_external_citation(path), path) def test_a_path_with_a_directory_prefix_is_not_a_citation(self): self.assertFalse( - profile_sync.is_external_citation("src/dir file.cpp") + profiling_citations.is_external_citation("src/dir file.cpp") ) def test_a_filename_prefix_is_not_a_citation(self): - self.assertFalse(profile_sync.is_external_citation("main.cpp note")) + self.assertFalse(profiling_citations.is_external_citation("main.cpp note")) def test_external_part_is_reported_not_reviewed(self): result = anchor_part( @@ -722,7 +731,7 @@ class TestExternalCitation(unittest.TestCase): renamer(CompareResult([], False)), ) self.assertEqual(result.status, "EXTERNAL") - self.assertNotIn("EXTERNAL", profile_sync.REVIEW_STATUSES) + self.assertNotIn("EXTERNAL", anchoring.REVIEW_STATUSES) class TestBinaryCitation(unittest.TestCase): @@ -755,11 +764,11 @@ class TestBinaryCitation(unittest.TestCase): renamer(CompareResult([], False)), ) self.assertEqual(result.status, "BINARY") - self.assertNotIn("BINARY", profile_sync.REVIEW_STATUSES) + self.assertNotIn("BINARY", anchoring.REVIEW_STATUSES) def test_binary_part_at_the_pin(self): part = RefPart("ModelImporter.exe import directory", None, None, "") - self.assertEqual(profile_sync.verify_at_pin(part, None, []).status, "BINARY") + self.assertEqual(anchoring.verify_at_pin(part, None, []).status, "BINARY") class TestRepoWord(unittest.TestCase): @@ -767,13 +776,13 @@ class TestRepoWord(unittest.TestCase): def test_declared_repository_word_is_stripped(self): self.assertEqual( - profile_sync.strip_repo_word("BAM_FPloader FPLoader.cpp", {"bam_fploader"}), + profiling_citations.strip_repo_word("BAM_FPloader FPLoader.cpp", {"bam_fploader"}), "FPLoader.cpp", ) def test_release_version_after_the_repository_word_is_dropped(self): self.assertEqual( - profile_sync.strip_repo_word( + profiling_citations.strip_repo_word( "ArcadeFlashWeb v1.0.2 flash/flash.html", {"arcadeflashweb"} ), "flash/flash.html", @@ -781,13 +790,13 @@ class TestRepoWord(unittest.TestCase): def test_undeclared_project_word_stays(self): self.assertEqual( - profile_sync.strip_repo_word("munt ROMInfo.cpp", {"bam_fploader"}), + profiling_citations.strip_repo_word("munt ROMInfo.cpp", {"bam_fploader"}), "munt ROMInfo.cpp", ) def test_a_directory_prefix_is_not_a_project_word(self): self.assertEqual( - profile_sync.strip_repo_word("src/dir file.cpp", {"src/dir"}), + profiling_citations.strip_repo_word("src/dir file.cpp", {"src/dir"}), "src/dir file.cpp", ) @@ -802,7 +811,7 @@ class TestNarrowByCited(unittest.TestCase): def test_deepest_cited_directory_wins(self): self.assertEqual( - profile_sync.narrow_by_cited(self.MATCHES, {"SheepShaver", "SheepShaver/src"}), + profiling_citations.narrow_by_cited(self.MATCHES, {"SheepShaver", "SheepShaver/src"}), ["SheepShaver/src/include/version.h"], ) @@ -812,11 +821,11 @@ class TestNarrowByCited(unittest.TestCase): "SheepShaver/src/include/cpu_emulation.h", ] self.assertEqual( - profile_sync.narrow_by_cited(matches, {"SheepShaver/src"}), matches + profiling_citations.narrow_by_cited(matches, {"SheepShaver/src"}), matches ) def test_nothing_cited_keeps_all(self): - self.assertEqual(profile_sync.narrow_by_cited(self.MATCHES, set()), self.MATCHES) + self.assertEqual(profiling_citations.narrow_by_cited(self.MATCHES, set()), self.MATCHES) def test_depth_counts_components_not_characters(self): matches = [ @@ -824,7 +833,7 @@ class TestNarrowByCited(unittest.TestCase): "SheepShaver/src/Windows/configure.ac", ] cited = {"SheepShaver", "SheepShaver/src", "SheepShaver/src/Unix", "SheepShaver/src/Windows"} - self.assertEqual(profile_sync.narrow_by_cited(matches, cited), matches) + self.assertEqual(profiling_citations.narrow_by_cited(matches, cited), matches) class TestSymlinkTarget(unittest.TestCase): @@ -832,7 +841,7 @@ class TestSymlinkTarget(unittest.TestCase): def test_relative_link_resolves_from_the_link_directory(self): self.assertEqual( - profile_sync.symlink_target( + profiling_citations.symlink_target( "SheepShaver/src/Unix/ether_unix.cpp", ["../../../BasiliskII/src/Unix/ether_unix.cpp"], ), @@ -840,17 +849,17 @@ class TestSymlinkTarget(unittest.TestCase): ) def test_a_real_one_line_file_is_not_a_link(self): - self.assertIsNone(profile_sync.symlink_target("a/VERSION", ["1.2.3"])) - self.assertIsNone(profile_sync.symlink_target("a/x.c", ["int main() { return 0; }"])) - self.assertIsNone(profile_sync.symlink_target("a/x.c", ["../other.c"])) + self.assertIsNone(profiling_citations.symlink_target("a/VERSION", ["1.2.3"])) + self.assertIsNone(profiling_citations.symlink_target("a/x.c", ["int main() { return 0; }"])) + self.assertIsNone(profiling_citations.symlink_target("a/x.c", ["../other.c"])) def test_a_link_leaving_the_tree_is_ignored(self): - self.assertIsNone(profile_sync.symlink_target("x.c", ["../../x.c"])) + self.assertIsNone(profiling_citations.symlink_target("x.c", ["../../x.c"])) class TestAnchorTokens(unittest.TestCase): def test_archive_members_supply_the_content_values(self): - tokens = profile_sync._anchor_tokens( + tokens = profiling_citations._anchor_tokens( {"name": "esh.zip", "contents": [{"crc32": "c14f36b3"}]} ) # The set name stays: a MAME ref cites the machine declaration, which @@ -858,24 +867,24 @@ class TestAnchorTokens(unittest.TestCase): self.assertEqual(tokens, ["c14f36b3", "esh"]) def test_a_hashless_archive_still_falls_back_to_its_stem(self): - tokens = profile_sync._anchor_tokens( + tokens = profiling_citations._anchor_tokens( {"name": "esh.zip", "contents": [{"name": "rom.u1"}]} ) self.assertIn("esh", tokens) def test_archive_stem_is_searched_too(self): - tokens = profile_sync._anchor_tokens({"name": "stvbios.zip"}) + tokens = profiling_citations._anchor_tokens({"name": "stvbios.zip"}) self.assertIn("stvbios.zip", tokens) self.assertIn("stvbios", tokens) def test_hashes_still_win(self): - tokens = profile_sync._anchor_tokens( + tokens = profiling_citations._anchor_tokens( {"name": "a.zip", "sha1": "AABB" * 10} ) self.assertIn("aabb" * 10, tokens) def test_very_short_stem_is_skipped(self): - self.assertNotIn("ab", profile_sync._anchor_tokens({"name": "ab.zip"})) + self.assertNotIn("ab", profiling_citations._anchor_tokens({"name": "ab.zip"})) class TestNudgeToDeclared(unittest.TestCase): @@ -883,14 +892,14 @@ class TestNudgeToDeclared(unittest.TestCase): def test_moves_onto_the_declared_value(self): self.assertEqual( - profile_sync.nudge_to_declared(self.LINES, 19, 19, ["stvbios"]), + anchoring.nudge_to_declared(self.LINES, 19, 19, ["stvbios"]), (21, 21), ) def test_range_length_is_kept(self): lines = [""] * 20 + ["ROM_START( stvbios )"] + ["pad"] * 20 self.assertEqual( - profile_sync.nudge_to_declared(lines, 17, 19, ["stvbios"]), + anchoring.nudge_to_declared(lines, 17, 19, ["stvbios"]), (21, 23), ) @@ -898,78 +907,78 @@ class TestNudgeToDeclared(unittest.TestCase): """ymir's 598-618 cited load_rom_cartridge(). Nudged onto the file table beside it, a rewritten function read SHIFTED, not CHANGED.""" lines = ["void load_rom_cartridge() {"] + [" code();"] * 19 + ["}", "kKOF95Files"] - self.assertIsNone(profile_sync.nudge_to_declared(lines, 1, 21, ["kkof95files"])) + self.assertIsNone(anchoring.nudge_to_declared(lines, 1, 21, ["kkof95files"])) def test_nothing_when_already_on_target(self): self.assertIsNone( - profile_sync.nudge_to_declared(self.LINES, 21, 21, ["stvbios"]) + anchoring.nudge_to_declared(self.LINES, 21, 21, ["stvbios"]) ) def test_nothing_beyond_the_window(self): lines = ["pad"] * 500 + ["ROM_START( stvbios )"] - self.assertIsNone(profile_sync.nudge_to_declared(lines, 1, 1, ["stvbios"])) + self.assertIsNone(anchoring.nudge_to_declared(lines, 1, 1, ["stvbios"])) def test_nothing_without_tokens(self): - self.assertIsNone(profile_sync.nudge_to_declared(self.LINES, 19, 19, [])) + self.assertIsNone(anchoring.nudge_to_declared(self.LINES, 19, 19, [])) def test_nothing_when_the_value_is_absent(self): self.assertIsNone( - profile_sync.nudge_to_declared(self.LINES, 19, 19, ["absent"]) + anchoring.nudge_to_declared(self.LINES, 19, 19, ["absent"]) ) def test_adjacent_duplicates_are_refused(self): lines = ["pad"] * 20 + ["stvbios a", "stvbios b"] + ["pad"] * 20 self.assertIsNone( - profile_sync.nudge_to_declared(lines, 19, 19, ["stvbios"]) + anchoring.nudge_to_declared(lines, 19, 19, ["stvbios"]) ) class TestDominantShift(unittest.TestCase): def test_shift_needs_enough_support(self): parts = [_part("a.c", 10, 30, "SHIFTED"), _part("a.c", 20, 40, "SHIFTED")] - self.assertEqual(profile_sync.dominant_shifts(parts), {}) + self.assertEqual(anchoring.dominant_shifts(parts), {}) def test_shift_is_taken_from_resolved_anchors(self): parts = [_part("a.c", i, i + 20, "SHIFTED") for i in (10, 20, 30)] - self.assertEqual(profile_sync.dominant_shifts(parts), {"a.c": 20}) + self.assertEqual(anchoring.dominant_shifts(parts), {"a.c": 20}) def test_ambiguous_parts_do_not_vote(self): parts = [_part("a.c", i, i + 20, "SHIFTED") for i in (10, 20, 30)] parts.append(_ambiguous("a.c", 40, [99, 500])) - self.assertEqual(profile_sync.dominant_shifts(parts), {"a.c": 20}) + self.assertEqual(anchoring.dominant_shifts(parts), {"a.c": 20}) def test_candidate_matching_the_shift_is_selected(self): part = _ambiguous("a.c", 1624, [1644, 1790, 1834]) - settled = profile_sync.resolve_by_shift(part, {"a.c": 20}) + settled = anchoring.resolve_by_shift(part, {"a.c": 20}) self.assertEqual(settled.status, "SHIFTED") self.assertEqual(settled.start, 1644) self.assertIn("+20", settled.reason) def test_no_candidate_matches_the_shift(self): part = _ambiguous("a.c", 1624, [1790, 1834]) - self.assertEqual(profile_sync.resolve_by_shift(part, {"a.c": 20}).status, + self.assertEqual(anchoring.resolve_by_shift(part, {"a.c": 20}).status, "AMBIGUOUS") def test_two_candidates_on_the_shift_stay_ambiguous(self): part = _ambiguous("a.c", 1624, [1644, 1644]) - self.assertEqual(profile_sync.resolve_by_shift(part, {"a.c": 20}).status, + self.assertEqual(anchoring.resolve_by_shift(part, {"a.c": 20}).status, "AMBIGUOUS") def test_range_length_is_preserved(self): part = PartResult( RefPart("a.c", 100, 104), "AMBIGUOUS", None, None, None, [120] ) - settled = profile_sync.resolve_by_shift(part, {"a.c": 20}) + settled = anchoring.resolve_by_shift(part, {"a.c": 20}) self.assertEqual((settled.start, settled.end), (120, 124)) def test_file_without_a_known_shift_is_untouched(self): part = _ambiguous("b.c", 10, [30]) - self.assertEqual(profile_sync.resolve_by_shift(part, {"a.c": 20}).status, + self.assertEqual(anchoring.resolve_by_shift(part, {"a.c": 20}).status, "AMBIGUOUS") -PIN = profile_sync.PIN -HEAD = profile_sync.HEAD +PIN = profiling_repos.PIN +HEAD = profiling_repos.HEAD def make_fetch(files: dict[tuple[str, str], list[str]]): @@ -1220,8 +1229,8 @@ class TestMovedStatus(unittest.TestCase): self.assertEqual(result.status, "GONE") def test_moved_is_mechanically_recalable(self): - self.assertIn("MOVED", profile_sync.REBASE_STATUSES) - self.assertNotIn("MOVED", profile_sync.REVIEW_STATUSES) + self.assertIn("MOVED", anchoring.REBASE_STATUSES) + self.assertNotIn("MOVED", anchoring.REVIEW_STATUSES) def test_moved_ranks_below_ambiguous(self): self.assertEqual(worst_status(["MOVED", "AMBIGUOUS"]), "AMBIGUOUS") @@ -1268,37 +1277,37 @@ class TestBuildReport(unittest.TestCase): self.dir = self.tmp.name self.files: dict[tuple[str, str], list[str]] = {} self._orig = ( - profile_sync.upstream.fetch_file, - profile_sync.upstream.resolve_head, - profile_sync.upstream.resolve_commit_at, - profile_sync.upstream.compare, - profile_sync.upstream.list_tree, - profile_sync.upstream.list_tags, - profile_sync.upstream.resolve_tag_commit, - profile_sync.upstream.tag_commit, - profile_sync.upstream._http_json, - profile_sync.upstream._http_text, - profile_sync.upstream.list_gitlinks, - profile_sync.upstream.current_slug, - profile_sync.upstream.fork_base, - profile_sync.upstream.ancestry, + upstream.fetch_file, + upstream.resolve_head, + upstream.resolve_commit_at, + upstream.compare, + upstream.list_tree, + upstream.list_tags, + upstream.resolve_tag_commit, + upstream.tag_commit, + upstream._http_json, + upstream._http_text, + upstream.list_gitlinks, + upstream.current_slug, + upstream.fork_base, + upstream.ancestry, ) # Pin and tip are unrelated shas in these fixtures: no ancestry. - profile_sync.upstream.ancestry = lambda *a, **k: None + upstream.ancestry = lambda *a, **k: None self.fork_bases: dict[str, str] = {} - profile_sync.upstream.fork_base = ( + upstream.fork_base = ( lambda repo, sha, fork, fork_sha, cache_dir, offline=False: self.fork_bases.get(fork_sha) ) self.current_slugs: dict[str, str] = {} - profile_sync.upstream.current_slug = ( + upstream.current_slug = ( lambda repo, cache_dir, offline=False: self.current_slugs.get( repo.slug, repo.slug ) ) self.tags: list[str] = [] self.gitlinks: dict[str, dict[str, str]] = {} - profile_sync.upstream.list_gitlinks = ( + upstream.list_gitlinks = ( lambda repo, sha, cache_dir, offline=False: self.gitlinks.get(sha, {}) ) self.tag_commits: dict[str, str] = {} @@ -1306,53 +1315,53 @@ class TestBuildReport(unittest.TestCase): def _no_network(url): raise AssertionError(f"test reached the network: {url}") - profile_sync.upstream._http_json = _no_network - profile_sync.upstream._http_text = _no_network - profile_sync.upstream.list_tags = ( + upstream._http_json = _no_network + upstream._http_text = _no_network + upstream.list_tags = ( lambda repo, cache, offline=False: self.tags ) - profile_sync.upstream.resolve_tag_commit = ( + upstream.resolve_tag_commit = ( lambda repo, tag, cache, offline=False: self.tag_commits.get(tag) ) - profile_sync.upstream.tag_commit = ( + upstream.tag_commit = ( lambda repo, tag, cache, offline=False: self.tag_commits.get(tag) ) - profile_sync.upstream.list_tree = ( + upstream.list_tree = ( lambda repo, sha, cache_dir, offline=False: ( sorted({path for _, path in self.files}), False ) ) - profile_sync.upstream.fetch_file = ( + upstream.fetch_file = ( lambda repo, sha, path, cache_dir, offline=False: self.files.get( (sha, path) ) ) - profile_sync.upstream.resolve_head = ( + upstream.resolve_head = ( lambda repo, cache_dir, offline=False, branch=None, fresh=False: "headsha" ) - profile_sync.upstream.resolve_commit_at = ( + upstream.resolve_commit_at = ( lambda repo, date, cache_dir, offline=False, branch=None: "pinsha" ) - profile_sync.upstream.compare = ( + upstream.compare = ( lambda repo, base, head, cache_dir, offline=False: CompareResult([], False) ) def tearDown(self): ( - profile_sync.upstream.fetch_file, - profile_sync.upstream.resolve_head, - profile_sync.upstream.resolve_commit_at, - profile_sync.upstream.compare, - profile_sync.upstream.list_tree, - profile_sync.upstream.list_tags, - profile_sync.upstream.resolve_tag_commit, - profile_sync.upstream.tag_commit, - profile_sync.upstream._http_json, - profile_sync.upstream._http_text, - profile_sync.upstream.list_gitlinks, - profile_sync.upstream.current_slug, - profile_sync.upstream.fork_base, - profile_sync.upstream.ancestry, + upstream.fetch_file, + upstream.resolve_head, + upstream.resolve_commit_at, + upstream.compare, + upstream.list_tree, + upstream.list_tags, + upstream.resolve_tag_commit, + upstream.tag_commit, + upstream._http_json, + upstream._http_text, + upstream.list_gitlinks, + upstream.current_slug, + upstream.fork_base, + upstream.ancestry, ) = self._orig self.tmp.cleanup() @@ -1377,7 +1386,7 @@ class TestBuildReport(unittest.TestCase): self.assertEqual(len(report.moved_repos), 1) self.assertIn("o/renamed", report.moved_repos[0]) self.assertGreaterEqual(report.needs_review(), 1) - self.assertIn("moved repository", profile_sync.format_report(report)) + self.assertIn("moved repository", profiling_reports.format_report(report)) def test_a_repository_under_its_own_name_is_not_moved(self): self.files[("pinsha", "a.c")] = ["x", "y"] @@ -1390,9 +1399,9 @@ class TestBuildReport(unittest.TestCase): """git.eden-emu.dev answers 403 to scripts: the muted repository's citations read "absent at HEAD" for files that exist.""" def refuse(repo, sha, path, cache_dir, offline=False): - raise profile_sync.upstream.UpstreamError(f"{path}: HTTP 403") + raise upstream.UpstreamError(f"{path}: HTTP 403") - profile_sync.upstream.fetch_file = refuse + upstream.fetch_file = refuse with contextlib.redirect_stderr(io.StringIO()): report = build_report("test", self._profile(["a.c:2"]), self.dir) statuses = [p.status for e in report.entries for p in e.parts] @@ -1403,7 +1412,7 @@ class TestBuildReport(unittest.TestCase): """git.eden-emu.dev truncates the HEAD tree: fs_paths.h, cited bare, was reported GONE though it lives under src/common/fs/.""" self.files[("pinsha", "fs_paths.h")] = ["x", "hit"] - profile_sync.upstream.list_tree = ( + upstream.list_tree = ( lambda repo, sha, cache_dir, offline=False: (["other.c"], True) ) with contextlib.redirect_stderr(io.StringIO()): @@ -1624,7 +1633,7 @@ class TestBuildReport(unittest.TestCase): self.files[("uppin", "src/a.cpp")] = ["x", "discord_init();", "pine();", "z"] self.files[("uphead", "src/a.cpp")] = ["a", "b", "x", "discord_init();", "pine();", "z"] heads = {"libretro/ps2": "porthead", "PCSX2/pcsx2": "uphead"} - profile_sync.upstream.resolve_head = ( + upstream.resolve_head = ( lambda repo, cache, offline=False, branch=None: heads[repo.slug] ) profile = { @@ -1635,7 +1644,7 @@ class TestBuildReport(unittest.TestCase): "upstream_commit": "uppin", "files": [{"name": "cheats_ni.zip", "source_ref": "src/a.cpp:2-3"}], } - report = profile_sync.report_to_dict(build_report("lrps2", profile, self.dir)) + report = profiling_reports.report_to_dict(build_report("lrps2", profile, self.dir)) part = report["entries"][0]["parts"][0] # Moved by -1 in the fork, the block's real target. The upstream's # +2 shift of unrelated code is not it. @@ -1649,7 +1658,7 @@ class TestBuildReport(unittest.TestCase): seen[repo.slug] = branch return "headsha" - profile_sync.upstream.resolve_head = _head + upstream.resolve_head = _head profile = self._profile(["a.c:1"]) profile["upstream"] = "https://github.com/o/up" profile["source_branch"] = "libretro" @@ -1685,7 +1694,7 @@ class TestBuildReport(unittest.TestCase): self.files[("uphead", "src/a.cpp")] = ["x", "GAME(1997, pgm, 0, pgm,", "y"] heads = {"o/port": "porthead", "o/up": "uphead"} - profile_sync.upstream.resolve_head = ( + upstream.resolve_head = ( lambda repo, cache, offline=False, branch=None: heads[repo.slug] ) profile = { @@ -1707,7 +1716,7 @@ class TestBuildReport(unittest.TestCase): self.files[("porthead", "src/p.c")] = [f"port {i}" for i in range(8)] self.files[("uphead", "src/p.c")] = ["a", "load x.bin", "c", "d", "e"] heads = {"o/port": "porthead", "o/up": "uphead"} - profile_sync.upstream.resolve_head = ( + upstream.resolve_head = ( lambda repo, cache, offline=False, branch=None: heads[repo.slug] ) profile = { @@ -1736,10 +1745,10 @@ class TestBuildReport(unittest.TestCase): for name in ("core.info", "core.sln", "core.vcxproj"): self.files[("porthead", name)] = ["x"] heads = {"o/port": "porthead", "o/up": "uphead"} - profile_sync.upstream.resolve_head = ( + upstream.resolve_head = ( lambda repo, cache, offline=False, branch=None: heads[repo.slug] ) - profile_sync.upstream.list_tree = ( + upstream.list_tree = ( lambda repo, sha, cache_dir, offline=False: ( sorted(path for revision, path in self.files if revision == sha), False ) @@ -1765,7 +1774,7 @@ class TestBuildReport(unittest.TestCase): self.files[("uppin", "src/stv.cpp")] = ["x"] self.files[("uphead", "src/stv.cpp")] = ["x", "y", "GAME( 1996, stvbios,"] heads = {"o/port": "porthead", "o/up": "uphead"} - profile_sync.upstream.resolve_head = ( + upstream.resolve_head = ( lambda repo, cache, offline=False, branch=None: heads[repo.slug] ) profile = { @@ -1786,19 +1795,19 @@ class TestBuildReport(unittest.TestCase): # the forge's orphaned objects, and every ref read ANCHORED. self.files[("pinsha", "a.c")] = ["x", "hit"] self.files[("headsha", "a.c")] = ["x", "hit"] - profile_sync.upstream.compare = ( + upstream.compare = ( lambda repo, base, head, cache_dir, offline=False: CompareResult([], False, True) ) report = build_report("test", self._profile(["a.c:2"]), self.dir) self.assertEqual(len(report.orphaned), 1) self.assertGreater(report.needs_review(), 0) - self.assertIn("orphaned pin", profile_sync.format_report(report)) + self.assertIn("orphaned pin", profiling_reports.format_report(report)) def test_a_frozen_tag_is_off_the_branch_on_purpose(self): self.files[("pinsha", "a.c")] = ["x", "hit"] self.files[("headsha", "a.c")] = ["x", "hit"] self.tag_commits = {"v1.6.0": "pinsha"} - profile_sync.upstream.compare = ( + upstream.compare = ( lambda repo, base, head, cache_dir, offline=False: CompareResult([], False, True) ) report = build_report("test", self._versioned("1.6.0"), self.dir) @@ -1828,11 +1837,11 @@ class TestBuildReport(unittest.TestCase): self.files[("uppin", "src/u.c")] = ["x", "ROM_LOAD(u.bin)", "y"] self.files[("uphead", "src/u.c")] = ["x", "", "", "", "y"] heads = {"o/port": "porthead", "o/up": "uphead"} - profile_sync.upstream.resolve_head = ( + upstream.resolve_head = ( lambda repo, cache, offline=False, branch=None: heads[repo.slug] ) # A tag off the tracked branch is not an orphaned pin. - profile_sync.upstream.compare = ( + upstream.compare = ( lambda repo, base, head, cache_dir, offline=False: CompareResult( [], False, repo.slug == "o/up" ) @@ -1858,7 +1867,7 @@ class TestBuildReport(unittest.TestCase): [entry.status for entry in report.entries], ["ANCHORED", "ANCHORED"] ) self.assertIn( - "upstream pinned to tag 0.289", profile_sync.format_report(report) + "upstream pinned to tag 0.289", profiling_reports.format_report(report) ) def test_a_tag_pointing_elsewhere_is_not_flagged(self): @@ -1869,8 +1878,8 @@ class TestBuildReport(unittest.TestCase): self.assertIsNone(report.pinned_tag) def test_prose_version_is_not_looked_up(self): - self.assertEqual(profile_sync.version_tag_candidates("SVN (2015 snapshot)"), []) - self.assertEqual(profile_sync.version_tag_candidates(""), []) + self.assertEqual(profiling_repos.version_tag_candidates("SVN (2015 snapshot)"), []) + self.assertEqual(profiling_repos.version_tag_candidates(""), []) def test_pin_equal_to_head_is_never_flagged(self): self.files[("pinsha", "a.c")] = ["x", "hit"] @@ -1928,9 +1937,9 @@ class TestDeclaredDeadUpstream(TestBuildReport): def test_a_declared_death_is_reported_as_declared(self): def boom(*a, **k): - raise profile_sync.upstream.GoneError("https://h/x: HTTP 451") + raise upstream.GoneError("https://h/x: HTTP 451") - profile_sync.upstream.resolve_head = boom + upstream.resolve_head = boom report = build_report( "test", self._profile_with("withdrawn, HTTP 451"), self.dir ) @@ -1942,9 +1951,9 @@ class TestDeclaredDeadUpstream(TestBuildReport): def test_any_refusal_of_a_declared_dead_forge_agrees(self): # citron's taken domain now resolves and fails TLS. def tls(*a, **k): - raise profile_sync.upstream.UpstreamError("TLSV1_UNRECOGNIZED_NAME") + raise upstream.UpstreamError("TLSV1_UNRECOGNIZED_NAME") - profile_sync.upstream.resolve_head = tls + upstream.resolve_head = tls report = build_report("test", self._profile_with("domain taken"), self.dir) self.assertIn("declared", report.skipped) @@ -1960,10 +1969,10 @@ class TestDeclaredDeadUpstream(TestBuildReport): def test_an_undeclared_death_is_unchanged(self): def boom(*a, **k): - raise profile_sync.upstream.GoneError("https://h/x: HTTP 451") + raise upstream.GoneError("https://h/x: HTTP 451") - profile_sync.upstream.resolve_head = boom - with self.assertRaises(profile_sync.upstream.GoneError): + upstream.resolve_head = boom + with self.assertRaises(upstream.GoneError): build_report("test", self._profile(["a.c:1"]), self.dir) @@ -2029,7 +2038,7 @@ class TestBareNameResolvedAtThePin(TestBuildReport): def list_tree(repo, sha, cache_dir, offline=False): return sorted({path for rev, path in self.files if rev == sha}), False - profile_sync.upstream.list_tree = list_tree + upstream.list_tree = list_tree def test_a_bare_name_follows_the_file_that_moved_after_the_pin(self): self._tree_per_revision() @@ -2106,19 +2115,19 @@ class TestRunResilience(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory() - self._orig = profile_sync.upstream.resolve_commit_at + self._orig = upstream.resolve_commit_at self.seen: list[str] = [] def flaky(repo, date, cache_dir, offline=False, branch=None): self.seen.append(repo.slug) if repo.slug == "o/bad": - raise profile_sync.upstream.UpstreamError("HTTP 401") + raise upstream.UpstreamError("HTTP 401") return "pinsha" - profile_sync.upstream.resolve_commit_at = flaky + upstream.resolve_commit_at = flaky def tearDown(self): - profile_sync.upstream.resolve_commit_at = self._orig + upstream.resolve_commit_at = self._orig self.tmp.cleanup() def test_upstream_error_becomes_a_skip(self): @@ -2128,21 +2137,21 @@ class TestRunResilience(unittest.TestCase): "profiled_date": "2026-03-29", "files": [{"name": "a.bin", "source_ref": "a.c:1"}], } - with self.assertRaises(profile_sync.upstream.UpstreamError): + with self.assertRaises(upstream.UpstreamError): build_report("bad", profile, self.tmp.name) def test_rate_limit_still_propagates(self): def limited(repo, date, cache_dir, offline=False, branch=None): - raise profile_sync.upstream.RateLimitError("HTTP 403") + raise upstream.RateLimitError("HTTP 403") - profile_sync.upstream.resolve_commit_at = limited + upstream.resolve_commit_at = limited profile = { "emulator": "T", "source": "https://github.com/o/n", "profiled_date": "2026-03-29", "files": [{"name": "a.bin", "source_ref": "a.c:1"}], } - with self.assertRaises(profile_sync.upstream.RateLimitError): + with self.assertRaises(upstream.RateLimitError): build_report("x", profile, self.tmp.name) @@ -2156,7 +2165,7 @@ class TestElidedSummary(unittest.TestCase): ] buffer = io.StringIO() with contextlib.redirect_stdout(buffer): - profile_sync._print_elided(reports, printed=1) + profiling_run._print_elided(reports, printed=1) text = buffer.getvalue() self.assertIn("3 non listes", text) self.assertIn("2 no source_ref", text) @@ -2165,7 +2174,7 @@ class TestElidedSummary(unittest.TestCase): def test_silent_when_everything_was_listed(self): buffer = io.StringIO() with contextlib.redirect_stdout(buffer): - profile_sync._print_elided( + profiling_run._print_elided( [ProfileReport(name="a", entries=[], counts={"GONE": 1})], printed=1 ) self.assertEqual(buffer.getvalue(), "") @@ -2194,10 +2203,10 @@ class TestExtrasSurviveARefusingForge(unittest.TestCase): upstream.fetch_file = refuse try: report = _sample_report() - profile_sync._attach_detection(self._args(), self._profile(), report) + profiling_run._attach_detection(self._args(), self._profile(), report) buffer = io.StringIO() with contextlib.redirect_stdout(buffer): - profile_sync._print_extras(self._args(), self._profile(), report) + detection._print_extras(self._args(), self._profile(), report) finally: upstream.fetch_file = original self.assertEqual(report.detection_skipped, "https://x/y: HTTP 403") @@ -2216,7 +2225,7 @@ class TestExtrasSurviveARefusingForge(unittest.TestCase): args.watch_hashes = True report = _sample_report() try: - profile_sync._attach_detection(args, self._profile(), report) + profiling_run._attach_detection(args, self._profile(), report) finally: upstream.fetch_file = original by_name = {(i["path"], i["name"]): i["since_pin"] for i in report.new_files} @@ -2238,7 +2247,7 @@ class TestExtrasSurviveARefusingForge(unittest.TestCase): upstream.fetch_file = quota try: with self.assertRaises(upstream.RateLimitError): - profile_sync._attach_detection( + profiling_run._attach_detection( self._args(), self._profile(), _sample_report() ) finally: @@ -2589,8 +2598,8 @@ class TestRebaseRefs(unittest.TestCase): self.assertIn('source_ref: "a.c:20-22 (loads the kernel)"', text) def test_a_chunk_that_is_only_prose_blocks_the_rewrite(self): - self.assertFalse(profile_sync._is_rewritable("a.c:1, (just a note)")) - self.assertTrue(profile_sync._is_rewritable("a.c:1 (note), b.c:2")) + self.assertFalse(rebase._is_rewritable("a.c:1, (just a note)")) + self.assertTrue(rebase._is_rewritable("a.c:1 (note), b.c:2")) def test_mode_keyed_ref_is_never_rewritten(self): part = PartResult(RefPart("a.c", 10, 12), "SHIFTED", None, 20, 22, []) @@ -2642,7 +2651,7 @@ class TestWriteDryRun(unittest.TestCase): report.new_files, report.new_hashes = [], [] self._detection = mock.patch.object( - profile_sync, "_attach_detection", side_effect=nothing_gained + profiling_run, "_attach_detection", side_effect=nothing_gained ) self._detection.start() @@ -2660,12 +2669,12 @@ class TestWriteDryRun(unittest.TestCase): report.new_hashes = [] before = self.path.read_text() - with mock.patch.object(profile_sync, "_attach_detection", side_effect=gained), \ + with mock.patch.object(profiling_run, "_attach_detection", side_effect=gained), \ contextlib.redirect_stderr(io.StringIO()) as err: self._run(self._args(rebase_refs=True, dry_run=False), self._shifted()) self.assertEqual(self.path.read_text(), before) self.assertIn("coh3002t.zip", err.getvalue()) - with mock.patch.object(profile_sync, "_attach_detection", side_effect=gained): + with mock.patch.object(profiling_run, "_attach_detection", side_effect=gained): self._run(self._args(rebase_refs=True, dry_run=False, accept_changed=True), self._shifted()) self.assertIn('source_commit: "newhead"', self.path.read_text()) @@ -2681,7 +2690,7 @@ class TestWriteDryRun(unittest.TestCase): def _run(self, args, report, sample=SAMPLE): buffer = io.StringIO() with contextlib.redirect_stdout(buffer): - profile_sync._apply_writes( + profiling_run._apply_writes( args, "p", yaml.safe_load(sample), report ) return buffer.getvalue() @@ -2703,9 +2712,9 @@ class TestWriteDryRun(unittest.TestCase): from unittest import mock before = self.path.read_bytes() - cold = profile_sync.upstream.NotCachedError("x.c not cached") - with mock.patch.object(profile_sync, "realign_prose", side_effect=[[], cold]), \ - self.assertRaises(profile_sync.upstream.NotCachedError): + cold = upstream.NotCachedError("x.c not cached") + with mock.patch.object(profiling_run, "realign_prose", side_effect=[[], cold]), \ + self.assertRaises(upstream.NotCachedError): self._run(self._args(rebase_refs=True, dry_run=False), self._shifted()) self.assertEqual(self.path.read_bytes(), before) @@ -2746,7 +2755,7 @@ class TestWriteDryRun(unittest.TestCase): ("c1", {"source_commit": "pin1", "notes": "x"}), ] repo = object() - readings, ambiguous = profile_sync._writing_pairs( + readings, ambiguous = realign._writing_pairs( {"source_commit": "pin2"}, [("source", "", repo)], revisions, "c1" ) self.assertEqual(readings, [[(repo, "pin1", "pin2")], []]) @@ -2761,12 +2770,12 @@ class TestWriteDryRun(unittest.TestCase): ("c1", {"source_commit": "pin1", "notes": "x"}), ] repo = object() - readings, _ambiguous = profile_sync._writing_pairs( + readings, _ambiguous = realign._writing_pairs( {"source_commit": "pin2", "prose_written_at": "pin2"}, [("source", "", repo)], revisions, "c1", ) self.assertEqual(readings, [[]]) - later, _ = profile_sync._writing_pairs( + later, _ = realign._writing_pairs( {"source_commit": "pin3", "prose_written_at": "pin2"}, [("source", "", repo)], revisions, "c1", ) @@ -2778,7 +2787,7 @@ class TestWriteDryRun(unittest.TestCase): ("c1", {"source_commit": "pin1", "notes": "x"}), ] repo = object() - readings, ambiguous = profile_sync._writing_pairs( + readings, ambiguous = realign._writing_pairs( {"source_commit": "pin2", "prose_written_at": "elsewhere"}, [("source", "", repo)], revisions, "c1", ) @@ -2793,7 +2802,7 @@ class TestWriteDryRun(unittest.TestCase): ("c1", {"source_commit": {"libretro": "old", "standalone": "s"}}), ] repo = object() - readings, _ambiguous = profile_sync._writing_pairs( + readings, _ambiguous = realign._writing_pairs( {"source_commit": {"libretro": "new", "standalone": "s"}}, [("source", "libretro", repo)], revisions, "c1", ) @@ -2813,9 +2822,9 @@ class TestWriteDryRun(unittest.TestCase): ("c1", {"source": edge, "notes": "x", "source_commit": "e1"}), ] document = dict(revisions[0][1]) - repos = [("source", mode, profile_sync.upstream.parse_repo(url)) + repos = [("source", mode, upstream.parse_repo(url)) for mode, url in modes.items()] - readings, ambiguous = profile_sync._writing_pairs( + readings, ambiguous = realign._writing_pairs( document, repos, revisions, "c1" ) self.assertEqual(readings, [[]]) @@ -2834,7 +2843,7 @@ class TestWriteDryRun(unittest.TestCase): ) stale = ["read again: notes: FileSystem.cpp:2125 (pin moved under this text)"] errors = io.StringIO() - with mock.patch.object(profile_sync, "realign_prose", return_value=stale), \ + with mock.patch.object(profiling_run, "realign_prose", return_value=stale), \ contextlib.redirect_stderr(errors): output = self._run(self._args(bump_commit=True, dry_run=False), report) self.assertNotIn("source_commit ->", output) @@ -2923,7 +2932,7 @@ class TestWriteDryRun(unittest.TestCase): before = self.path.read_text() answers = iter([[], ["notes: a.c:10 (a.c:10 changed)"]]) with mock.patch.object( - profile_sync, "realign_prose", side_effect=lambda *a, **k: next(answers) + profiling_run, "realign_prose", side_effect=lambda *a, **k: next(answers) ), contextlib.redirect_stderr(io.StringIO()) as err: self._run(self._args(rebase_refs=True, dry_run=False), self._shifted()) self.assertEqual(self.path.read_text(), before) @@ -2946,7 +2955,7 @@ class TestWriteDryRun(unittest.TestCase): entries=[entry], counts={"SHIFTED": 1}, ) self.assertEqual( - profile_sync.pending_recale(report, False, before), 1, + rebase.pending_recale(report, False, before), 1, "the prose still describes the pinned revision", ) output = self._run( @@ -2982,11 +2991,11 @@ class AKeyedProfileJudgesEachBuildInItsOwnTree(TestBuildReport): ("o/port", "lpin", "a.c"): ["y", "f.bin cited"], } heads = {"o/edge": "shead", "o/port": "lpin"} - profile_sync.upstream.fetch_file = ( + upstream.fetch_file = ( lambda repo, sha, path, cache_dir, offline=False: trees.get((repo.slug, sha, path)) ) - profile_sync.upstream.resolve_head = ( + upstream.resolve_head = ( lambda repo, cache_dir, offline=False, branch=None, fresh=False: heads[repo.slug] ) @@ -3123,20 +3132,20 @@ class TestProseSpacingIsNotADifference(unittest.TestCase): def test_a_written_run_counts_as_written_whatever_the_spacing(self): self.assertEqual( - profile_sync._citation_key("src/Harddisk.cpp:228, 1439-1443"), - profile_sync._citation_key("src/Harddisk.cpp:228,1439-1443"), + profiling_citations._citation_key("src/Harddisk.cpp:228, 1439-1443"), + profiling_citations._citation_key("src/Harddisk.cpp:228,1439-1443"), ) def test_different_citations_still_differ(self): self.assertNotEqual( - profile_sync._citation_key("a.c:1, 2"), - profile_sync._citation_key("a.c:1, 3"), + profiling_citations._citation_key("a.c:1, 2"), + profiling_citations._citation_key("a.c:1, 3"), ) def test_a_path_change_still_differs(self): self.assertNotEqual( - profile_sync._citation_key("a.c:1"), - profile_sync._citation_key("src/a.c:1"), + profiling_citations._citation_key("a.c:1"), + profiling_citations._citation_key("src/a.c:1"), ) @@ -3149,21 +3158,21 @@ class TestLargeFileCeiling(unittest.TestCase): """ def test_a_pair_over_the_ceiling_is_not_diffed(self): - big = ["x"] * (profile_sync.MAX_MATCH_LINES + 1) - result = profile_sync._map_changed(big, big, 1, 2) + big = ["x"] * (anchoring.MAX_MATCH_LINES + 1) + result = anchoring._map_changed(big, big, 1, 2) self.assertEqual(result.status, "CHANGED") self.assertIn("over", result.reason) def test_a_pair_under_the_ceiling_is_mapped(self): pin = ["a", "subject", "b"] head = ["pad", "pad", "a", "subject", "b"] - result = profile_sync._map_changed(pin, head, 2, 2) + result = anchoring._map_changed(pin, head, 2, 2) self.assertIsNotNone(result.start, "a small pair has to be diffed") self.assertNotIn("over", result.reason or "") def test_the_ceiling_clears_the_largest_file_any_profile_cites(self): self.assertGreater( - profile_sync.MAX_MATCH_LINES, 57000, + anchoring.MAX_MATCH_LINES, 57000, "openbor.c is 57k lines at HEAD and its ref has to map", ) @@ -3204,7 +3213,7 @@ class TestRebaseWaitsForThePin(unittest.TestCase): ], {"SHIFTED": 2}, ) - self.assertTrue(profile_sync.pending_recale(report)) + self.assertTrue(rebase.pending_recale(report)) self.assertEqual(rebase_refs(self.path, report), []) self.assertIn('source_ref: "a.c:10-12"', self.path.read_text()) @@ -3224,7 +3233,7 @@ class TestRebaseWaitsForThePin(unittest.TestCase): ) out, err = io.StringIO(), io.StringIO() with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): - profile_sync._apply_writes(args, "p", yaml.safe_load(SAMPLE), report) + profiling_run._apply_writes(args, "p", yaml.safe_load(SAMPLE), report) self.assertEqual(self.path.read_text(), before) self.assertIn("nothing was written", err.getvalue()) self.assertNotIn("a.c:30-32", out.getvalue()) @@ -3235,7 +3244,7 @@ class TestRebaseWaitsForThePin(unittest.TestCase): [EntryReport("a.bin", "a.c:10-12", "SHIFTED", [movable])], {"SHIFTED": 1}, ) - self.assertFalse(profile_sync.pending_recale(report)) + self.assertFalse(rebase.pending_recale(report)) self.assertEqual(rebase_refs(self.path, report), ["a.c:10-12 -> a.c:30-32"]) def test_chained_shifts_are_judged_at_their_own_entry(self): @@ -3263,7 +3272,7 @@ class TestRebaseWaitsForThePin(unittest.TestCase): written = self.path.read_text() self.assertIn('source_ref: "g.json5:575"', written) self.assertEqual( - profile_sync.pending_recale(report, text=written), 0 + rebase.pending_recale(report, text=written), 0 ) @@ -3287,7 +3296,7 @@ class TestBumpCommit(unittest.TestCase): ], counts={"SHIFTED": 1}, ) - self.assertEqual(profile_sync.pending_recale(report), 1) + self.assertEqual(rebase.pending_recale(report), 1) self.assertFalse(bump_commit(self.path, report)) def test_a_profile_citing_nothing_advances(self): @@ -3295,7 +3304,7 @@ class TestBumpCommit(unittest.TestCase): # report skips a profile with no source_ref, and the skip blocked it. report = ProfileReport( name="p", repo="o/n", pin="pin", head="newhead", entries=[], counts={}, - skipped=profile_sync.NO_SOURCE_REF, + skipped=resolver.NO_SOURCE_REF, ) report.upstream_head, report.upstream_pin = "uphead", "uppin" self.assertTrue(bump_commit(self.path, report)) @@ -3318,11 +3327,11 @@ class TestBumpCommit(unittest.TestCase): ) report = ProfileReport( name="advanced_drastic", repo="o/n", pin="pin", head="newhead", entries=[], - counts={}, skipped=profile_sync.NO_SOURCE_REF, + counts={}, skipped=resolver.NO_SOURCE_REF, ) platforms = str(root / "platforms") self.assertEqual( - profile_sync.shipping_platforms("advanced_drastic", self.tmp.name, platforms), + bump.shipping_platforms("advanced_drastic", self.tmp.name, platforms), ["knulli"], ) with contextlib.redirect_stderr(io.StringIO()) as err: @@ -3353,7 +3362,7 @@ class TestBumpCommit(unittest.TestCase): ], counts={"ANCHORED": 1}, ) - self.assertEqual(profile_sync.pending_recale(report), 0) + self.assertEqual(rebase.pending_recale(report), 0) self.assertTrue(bump_commit(self.path, report)) def test_refused_on_a_build_a_platform_compiles(self): @@ -3544,34 +3553,34 @@ class TestCheckVersion(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory() self.dir = self.tmp.name - self.repo = profile_sync.upstream.parse_repo("https://github.com/o/n") + self.repo = upstream.parse_repo("https://github.com/o/n") self._orig = ( - profile_sync.upstream.latest_release, - profile_sync.upstream.list_tags, - profile_sync.upstream.resolve_tag_commit, - profile_sync.upstream.tag_commit, + upstream.latest_release, + upstream.list_tags, + upstream.resolve_tag_commit, + upstream.tag_commit, ) - profile_sync.upstream.latest_release = ( - lambda repo, cache, offline=False: profile_sync.upstream.Release( + upstream.latest_release = ( + lambda repo, cache, offline=False: upstream.Release( "v3.1.2", "2026-06-14", False ) ) - profile_sync.upstream.list_tags = ( + upstream.list_tags = ( lambda repo, cache, offline=False: ["v3.1.2", "v3.0.0"] ) - profile_sync.upstream.resolve_tag_commit = ( + upstream.resolve_tag_commit = ( lambda repo, tag, cache, offline=False: ( "tagsha" if tag == "v3.0.0" else None ) ) - profile_sync.upstream.tag_commit = profile_sync.upstream.resolve_tag_commit + upstream.tag_commit = upstream.resolve_tag_commit def tearDown(self): ( - profile_sync.upstream.latest_release, - profile_sync.upstream.list_tags, - profile_sync.upstream.resolve_tag_commit, - profile_sync.upstream.tag_commit, + upstream.latest_release, + upstream.list_tags, + upstream.resolve_tag_commit, + upstream.tag_commit, ) = self._orig self.tmp.cleanup() @@ -3624,13 +3633,13 @@ class APassedReleaseIsNewerThanTheTag(unittest.TestCase): def test_a_release_older_than_the_declared_tag_is_not_passed(self): from unittest import mock - version = profile_sync.VersionReport( + version = versions.VersionReport( "v0.2.22", "v0.2.22", "v0.2.20", "2026-07-25", True, "tag22" ) args = argparse.Namespace(cache_dir="c", offline=True) - with mock.patch.object(profile_sync.upstream, "tag_commit", return_value="rel20"), \ - mock.patch.object(profile_sync.upstream, "ancestry", return_value=(5, 0)): - self.assertIsNone(profile_sync._passed_release(args, "repo", version, "pin")) + with mock.patch.object(upstream, "tag_commit", return_value="rel20"), \ + mock.patch.object(upstream, "ancestry", return_value=(5, 0)): + self.assertIsNone(versions._passed_release(args, "repo", version, "pin")) class ANewerReleaseThanTheTag(unittest.TestCase): @@ -3639,12 +3648,12 @@ class ANewerReleaseThanTheTag(unittest.TestCase): def _newer(self, after_tag, after_pin): from unittest import mock - version = profile_sync.VersionReport("2.8", "2.9", "2.9", "2026-10-01", True, "tag28") + version = versions.VersionReport("2.8", "2.9", "2.9", "2026-10-01", True, "tag28") args = argparse.Namespace(cache_dir="c", offline=True) answers = iter([after_tag, after_pin]) - with mock.patch.object(profile_sync.upstream, "tag_commit", return_value="rel29"), \ - mock.patch.object(profile_sync.upstream, "ancestry", side_effect=lambda *a, **k: next(answers)): - return profile_sync._newer_release(args, "repo", version, "tag28") + with mock.patch.object(upstream, "tag_commit", return_value="rel29"), \ + mock.patch.object(upstream, "ancestry", side_effect=lambda *a, **k: next(answers)): + return versions._newer_release(args, "repo", version, "tag28") def test_a_later_release_is_named(self): self.assertEqual(self._newer((12, 0), (12, 0)), "2.9") @@ -3659,29 +3668,29 @@ class AProjectThatOnlyTags(unittest.TestCase): with no release published, nothing compared them.""" def test_a_version_tag_stands_in_for_the_missing_release(self): - version = profile_sync.VersionReport("v1.2", "v1.3", None, None, True, "tag12") - self.assertEqual(profile_sync.published_version(version), "v1.3") + version = versions.VersionReport("v1.2", "v1.3", None, None, True, "tag12") + self.assertEqual(versions.published_version(version), "v1.3") for tag in ("v107", "1.1d", "v9.0-6", "2.55"): - version = profile_sync.VersionReport("x", tag, None, None, False, None) - self.assertEqual(profile_sync.published_version(version), tag) + version = versions.VersionReport("x", tag, None, None, False, None) + self.assertEqual(versions.published_version(version), tag) def test_a_nightly_tag_does_not(self): for tag in ("latest-nightly", "continuous", "nightly-20261001"): - version = profile_sync.VersionReport("v1.2", tag, None, None, True, "t") - self.assertIsNone(profile_sync.published_version(version)) + version = versions.VersionReport("v1.2", tag, None, None, True, "t") + self.assertIsNone(versions.published_version(version)) def test_a_release_wins_over_a_newer_tag(self): - version = profile_sync.VersionReport("1.0", "1.2", "1.1", "2026-01-01", True, "t") - self.assertEqual(profile_sync.published_version(version), "1.1") + version = versions.VersionReport("1.0", "1.2", "1.1", "2026-01-01", True, "t") + self.assertEqual(versions.published_version(version), "1.1") def test_a_newer_tag_is_named(self): from unittest import mock - version = profile_sync.VersionReport("v1.2", "v1.3", None, None, True, "tag12") + version = versions.VersionReport("v1.2", "v1.3", None, None, True, "tag12") args = argparse.Namespace(cache_dir="c", offline=True) - with mock.patch.object(profile_sync.upstream, "tag_commit", return_value="tag13"), \ - mock.patch.object(profile_sync.upstream, "ancestry", return_value=(7, 0)): - self.assertEqual(profile_sync._newer_release(args, "repo", version, "tag12"), "v1.3") + with mock.patch.object(upstream, "tag_commit", return_value="tag13"), \ + mock.patch.object(upstream, "ancestry", return_value=(7, 0)): + self.assertEqual(versions._newer_release(args, "repo", version, "tag12"), "v1.3") class APassedReleaseUnderAnUntaggedVersion(unittest.TestCase): @@ -3689,7 +3698,7 @@ class APassedReleaseUnderAnUntaggedVersion(unittest.TestCase): already held release v9.0-6: the missing tag silenced the warning.""" def test_the_passed_release_is_named_without_a_declared_tag(self): - version = profile_sync.VersionReport("9.0.5", "v9.0-6", "v9.0-6", "2026-08-08", False, None) + version = versions.VersionReport("9.0.5", "v9.0-6", "v9.0-6", "2026-08-08", False, None) warning = version_warning(version, "head", None, "v9.0-6") self.assertIn("already contains release v9.0-6", warning) @@ -3702,24 +3711,24 @@ class APassedReleaseUnderAnUntaggedVersion(unittest.TestCase): ("v1", "v1.0"): None, ("0.83", "20120302"): None, ("0.160", "v1.4.1"): None, ("0.0.35", "v0.0.43"): "v0.0.43", ("106", "v107"): "v107", ("v1", "20170424"): None} for (declared, release), expected in cases.items(): - version = profile_sync.VersionReport(declared, release, release, "d", False, None) + version = versions.VersionReport(declared, release, release, "d", False, None) with self.subTest(declared=declared, release=release), \ - mock.patch.object(profile_sync.upstream, "tag_commit", return_value="rel"), \ - mock.patch.object(profile_sync.upstream, "ancestry", return_value=(3, 0)): - self.assertEqual(profile_sync._passed_release(args, "repo", version, "pin"), expected) + mock.patch.object(upstream, "tag_commit", return_value="rel"), \ + mock.patch.object(upstream, "ancestry", return_value=(3, 0)): + self.assertEqual(versions._passed_release(args, "repo", version, "pin"), expected) def test_no_tag_and_nothing_passed_stays_quiet(self): - version = profile_sync.VersionReport("9.0.5", None, None, None, False, None) + version = versions.VersionReport("9.0.5", None, None, None, False, None) self.assertIsNone(version_warning(version, "head")) class TestAncestry(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory() - self.saved = profile_sync.upstream._http_json + self.saved = upstream._http_json def tearDown(self): - profile_sync.upstream._http_json = self.saved + upstream._http_json = self.saved self.tmp.cleanup() def test_github_answers_both_directions_at_once(self): @@ -3729,10 +3738,10 @@ class TestAncestry(unittest.TestCase): calls.append(url) return {"ahead_by": 3, "behind_by": 0} - profile_sync.upstream._http_json = answer - repo = profile_sync.upstream.parse_repo("https://github.com/o/n") + upstream._http_json = answer + repo = upstream.parse_repo("https://github.com/o/n") self.assertEqual( - profile_sync.upstream.ancestry(repo, "tag", "pin", self.tmp.name), (3, 0) + upstream.ancestry(repo, "tag", "pin", self.tmp.name), (3, 0) ) self.assertEqual(len(calls), 1) @@ -3741,10 +3750,10 @@ class TestAncestry(unittest.TestCase): forward = "from=tag&to=pin" in url return {"commits": [{}] * (0 if forward else 5)} - profile_sync.upstream._http_json = answer - repo = profile_sync.upstream.parse_repo("https://gitlab.com/o/n") + upstream._http_json = answer + repo = upstream.parse_repo("https://gitlab.com/o/n") self.assertEqual( - profile_sync.upstream.ancestry(repo, "tag", "pin", self.tmp.name), (0, 5) + upstream.ancestry(repo, "tag", "pin", self.tmp.name), (0, 5) ) @@ -4092,7 +4101,7 @@ class TestCollectCitations(unittest.TestCase): ["mt32_model.cpp:36-97", "ROMInfo.cpp:206-213"], ) self.assertEqual(citations[1].parts[0].path, "munt ROMInfo.cpp") - self.assertTrue(profile_sync.is_external_citation("munt ROMInfo.cpp")) + self.assertTrue(profiling_citations.is_external_citation("munt ROMInfo.cpp")) def test_project_word_ending_the_line_before_marks_the_path_external(self): # genesis_plus_gx_expanded wrapped `(Batocera` and @@ -4106,7 +4115,7 @@ class TestCollectCitations(unittest.TestCase): self.assertEqual( citation.parts[0].path, "Batocera package/batocera/emulators/gpgx.mk" ) - self.assertTrue(profile_sync.is_external_citation(citation.parts[0].path)) + self.assertTrue(profiling_citations.is_external_citation(citation.parts[0].path)) sentence = {"notes": "It is read by the file\nsrc/gpgx.c:7 at boot.\n"} self.assertEqual(collect_citations(sentence)[0].parts[0].path, "src/gpgx.c") @@ -4133,7 +4142,7 @@ class TestCollectCitations(unittest.TestCase): tree holds only the gitlink, so the parts stayed external and moved unchecked with every bump.""" links = ["Q3E/src/main/jni/xash3d/xash3d-fwgs", "tools/Torch", "libs/zlib"] - resolve = profile_sync.resolve_through_gitlinks + resolve = profiling_repos.resolve_through_gitlinks self.assertEqual( resolve("xash3d-fwgs filesystem/filesystem.c", [links]), "Q3E/src/main/jni/xash3d/xash3d-fwgs/filesystem/filesystem.c", @@ -4175,7 +4184,7 @@ class TestCollectCitations(unittest.TestCase): def test_run_is_rendered_with_its_written_path(self): parts = [RefPart("mupen64plus-video-rice/upstream/src/Config.cpp", 38, 38, "38")] self.assertEqual( - profile_sync._run_after_moves(parts, {0: (40, 40, None)}, "Config.cpp:38"), + proseedit._run_after_moves(parts, {0: (40, 40, None)}, "Config.cpp:38"), "Config.cpp:40", ) @@ -4260,7 +4269,11 @@ class TestCitationSurfaceGuard(unittest.TestCase): citations outside its list rot while the pin advances. The walker matches the key once. Everything else goes through the collector. """ - source = Path(profile_sync.__file__).read_text(encoding="utf-8") + cli = Path(profile_sync.__file__) + source = "".join( + path.read_text(encoding="utf-8") + for path in [cli, *sorted((cli.parent / "profiling").glob("*.py"))] + ) self.assertNotIn('.get("source_ref")', source) self.assertEqual(source.count('== "source_ref"'), 1) @@ -4382,7 +4395,7 @@ class TestRebaseProse(unittest.TestCase): ) written = self.path.read_text() self.assertEqual(written, text.replace("(a.c:10)", "(a.c:14)")) - self.assertEqual(profile_sync.pending_recale(report, text=written), 0) + self.assertEqual(rebase.pending_recale(report, text=written), 0) self.assertTrue(bump_commit(self.path, report)) def test_a_refused_recale_names_the_citation_and_writes_nothing(self): @@ -4491,7 +4504,7 @@ class TestPendingProse(unittest.TestCase): part = PartResult(RefPart("a.c", 10, 10, "10"), "SHIFTED", None, 14, 14, []) report = self._report([part]) text = self.path.read_text() - self.assertEqual(profile_sync.pending_recale(report, text=text), 1) + self.assertEqual(rebase.pending_recale(report, text=text), 1) self.assertFalse(bump_commit(self.path, report)) self.assertEqual( yaml.safe_load(self.path.read_text())["source_commit"], "pin" @@ -4510,7 +4523,7 @@ class TestPendingProse(unittest.TestCase): part = PartResult(RefPart("a.c", 10, 10, "10"), "ANCHORED", None, None, None, []) report = self._report([part]) self.assertEqual( - profile_sync.pending_recale(report, text=self.path.read_text()), 0 + rebase.pending_recale(report, text=self.path.read_text()), 0 ) self.assertTrue(bump_commit(self.path, report)) @@ -4547,7 +4560,7 @@ class TestWrappedProse(unittest.TestCase): def test_both_wraps_are_read_whole(self): runs = { c.ref: c.wrapped - for c in profile_sync.collect_citations(yaml.safe_load(self.TEXT)) + for c in profiling_citations.collect_citations(yaml.safe_load(self.TEXT)) if c.field == "adam" } self.assertEqual( @@ -4564,7 +4577,7 @@ class TestWrappedProse(unittest.TestCase): ) runs = { c.ref: c.wrapped - for c in profile_sync.collect_citations(yaml.safe_load(text)) + for c in profiling_citations.collect_citations(yaml.safe_load(text)) if c.field == "cfg" } self.assertEqual(runs, {"src/config.rs:76-78,95-99": True}) @@ -4625,7 +4638,7 @@ class TestOpcodeCache(unittest.TestCase): expected = difflib.SequenceMatcher( None, pin, head, autojunk=False ).get_opcodes() - self.assertEqual(profile_sync._opcodes(list(pin), list(head)), expected) + self.assertEqual(anchoring._opcodes(list(pin), list(head)), expected) class TestCommentCitations(unittest.TestCase): @@ -4649,20 +4662,20 @@ class TestCommentCitations(unittest.TestCase): self.tmp.cleanup() def test_comments_only(self): - found = [c.ref for c in profile_sync.comment_citations(self.TEXT)] + found = [c.ref for c in profiling_citations.comment_citations(self.TEXT)] self.assertEqual(found, ["src/cpu.c:3212-3451", "z.c:9"]) document = yaml.safe_load(self.TEXT) self.assertNotIn( "src/cpu.c:3212-3451", - [c.ref for c in profile_sync.collect_citations(document)], + [c.ref for c in profiling_citations.collect_citations(document)], ) self.assertIn( "src/cpu.c:3212-3451", - [c.ref for c in profile_sync.collect_citations(document, self.TEXT)], + [c.ref for c in profiling_citations.collect_citations(document, self.TEXT)], ) def _report(self, index, part): - field = profile_sync.comment_citations(self.TEXT)[index].field + field = profiling_citations.comment_citations(self.TEXT)[index].field return ProfileReport( name="p", repo="o/n", pin="pin", head="newhead", entries=[EntryReport("comment", f"{part.part.path}:{part.part.raw}", @@ -4707,15 +4720,15 @@ class TestRealignProse(unittest.TestCase): self.path = self.dir / "p.yml" self.files: dict[tuple[str, str], list[str]] = {} self._orig = ( - profile_sync.upstream.fetch_file, - profile_sync.upstream.list_tree, + upstream.fetch_file, + upstream.list_tree, ) - profile_sync.upstream.fetch_file = ( + upstream.fetch_file = ( lambda repo, sha, path, cache_dir, offline=False: self.files.get( (sha, path) ) ) - profile_sync.upstream.list_tree = ( + upstream.list_tree = ( lambda repo, sha, cache_dir, offline=False: ( sorted({p for _, p in self.files}), False ) @@ -4723,8 +4736,8 @@ class TestRealignProse(unittest.TestCase): def tearDown(self): ( - profile_sync.upstream.fetch_file, - profile_sync.upstream.list_tree, + upstream.fetch_file, + upstream.list_tree, ) = self._orig self.tmp.cleanup() @@ -4757,7 +4770,7 @@ class TestRealignProse(unittest.TestCase): self.files[("pin", "a.c")] = ["x"] * 13 + ["subject"] self.files[("oldpin", "b.c")] = ["p", "two", "q", "r", "s", "t", "u", "eight", "nine"] self.files[("pin", "b.c")] = ["p", "two", "q", "r", "s", "t", "u", "eight", "nine"] - messages = profile_sync.realign_prose(self.path, self.tmp.name) + messages = realign.realign_prose(self.path, self.tmp.name) self.assertIn("notes: a.c:10 -> a.c:14", messages) text = self.path.read_text() self.assertIn("(a.c:14)", text) @@ -4794,7 +4807,7 @@ class TestRealignProse(unittest.TestCase): self.files[("oldpin", "a.c")] = ["x"] * 9 + ["subject"] self.files[("pin", "a.c")] = ["x"] * 13 + ["subject"] self.files[("pin", "b.c")] = ["p"] * 4 + ["read"] - messages = profile_sync.realign_prose(self.path, self.tmp.name, dry_run=True) + messages = realign.realign_prose(self.path, self.tmp.name, dry_run=True) self.assertEqual( messages, ["would recale files[Games.xml@standalone].note: a.c:10 -> a.c:14"], @@ -4814,7 +4827,7 @@ class TestRealignProse(unittest.TestCase): self.files[("oldpin", "b.c")] = list("pqrstuvwx") self.files[("pin", "b.c")] = list("pqrstuvwx") before = self.path.read_text() - messages = profile_sync.realign_prose(self.path, self.tmp.name) + messages = realign.realign_prose(self.path, self.tmp.name) self.assertTrue( any("pin moved under this text" in m for m in messages), messages ) @@ -4836,7 +4849,7 @@ class TestRealignProse(unittest.TestCase): self.files[("pin", "a.c")] = ["x"] * 13 + ["subject"] self.files[("oldpin", "b.c")] = list("pqrstuvwx") self.files[("pin", "b.c")] = list("pqrstuvwx") - messages = profile_sync.realign_prose(self.path, self.tmp.name, dry_run=True) + messages = realign.realign_prose(self.path, self.tmp.name, dry_run=True) self.assertTrue( any("pin moved under this text" in m for m in messages), messages ) @@ -4854,7 +4867,7 @@ class TestRealignProse(unittest.TestCase): self.files[("oldpin", "a.c")] = ["x"] * 9 + ["subject"] self.files[("pin", "a.c")] = ["x"] * 13 + ["subject"] self.files[("pin", "b.c")] = list("pqrstuvwxyz") - messages = profile_sync.realign_prose(self.path, self.tmp.name, dry_run=True) + messages = realign.realign_prose(self.path, self.tmp.name, dry_run=True) flagged = [m for m in messages if "a.c:10" in m] self.assertTrue(flagged, messages) self.assertFalse([m for m in messages if "b.c:" in m], messages) @@ -4876,14 +4889,14 @@ class TestRealignProse(unittest.TestCase): self.files[("pin", "a.c")] = ["x"] * 13 + ["subject"] self.files[("oldpin", "b.c")] = list("pqrstuvwx") self.files[("pin", "b.c")] = list("pqrstuvwx") - served = profile_sync.upstream.fetch_file - profile_sync.upstream.fetch_file = ( + served = upstream.fetch_file + upstream.fetch_file = ( lambda repo, sha, path, cache_dir, offline=False: ( served(repo, sha, path, cache_dir, offline) if repo.slug == "m/n" else None ) ) - messages = profile_sync.realign_prose(self.path, self.tmp.name, dry_run=True) + messages = realign.realign_prose(self.path, self.tmp.name, dry_run=True) # b.c did not move between the two pins: nothing to read again. self.assertFalse([m for m in messages if "b.c:" in m], messages) @@ -4908,7 +4921,7 @@ class TestRealignProse(unittest.TestCase): self._commit(sample.replace('"oldpin"', '"pin"'), "advance the port") self.files[("uppin", "up.c")] = ["a", "b", "c", "d", "check bios"] before = self.path.read_text() - messages = profile_sync.realign_prose(self.path, self.tmp.name, dry_run=True) + messages = realign.realign_prose(self.path, self.tmp.name, dry_run=True) self.assertFalse( any("readings disagree" in m for m in messages), messages ) @@ -4920,7 +4933,7 @@ class TestRealignProse(unittest.TestCase): self.files[("pin", "a.c")] = ["x"] * 13 + ["subject"] self.files[("oldpin", "b.c")] = list("pqrstuvwx") self.files[("pin", "b.c")] = list("pqrstuvwx") - messages = profile_sync.realign_prose( + messages = realign.realign_prose( self.path, self.tmp.name, dry_run=True ) self.assertTrue(any("would recale" in m for m in messages)) @@ -4930,15 +4943,15 @@ class TestRealignProse(unittest.TestCase): """git failing (no repository, dubious ownership) read as "nothing written under an older pin" and let a bump through.""" self.path.write_text(PROSE_SAMPLE, encoding="utf-8") - messages = profile_sync.realign_prose(self.path, self.tmp.name, dry_run=True) + messages = realign.realign_prose(self.path, self.tmp.name, dry_run=True) self.assertEqual(messages, ["read again: p.yml: git history unreadable"]) def test_an_unreadable_revision_stops_the_move(self): self._repo_with_advanced_pin() self.files[("oldpin", "a.c")] = ["x"] * 9 + ["subject"] self.files[("pin", "a.c")] = ["x"] * 13 + ["subject"] - with mock.patch.object(profile_sync, "_git_file_at", return_value=None): - messages = profile_sync.realign_prose(self.path, self.tmp.name) + with mock.patch.object(realign, "_git_file_at", return_value=None): + messages = realign.realign_prose(self.path, self.tmp.name) self.assertTrue(messages) self.assertTrue(all(m.startswith("read again: notes:") for m in messages)) self.assertIn("(a.c:10)", self.path.read_text()) @@ -4963,7 +4976,7 @@ class TestRealignProse(unittest.TestCase): day under the same pin. Every older note was reported unreadable and the recale stayed refused for good.""" self._repo_with_a_broken_revision("oldpin") - messages = profile_sync.realign_prose(self.path, self.tmp.name, dry_run=True) + messages = realign.realign_prose(self.path, self.tmp.name, dry_run=True) self.assertFalse(any("unreadable" in m for m in messages), messages) self.assertTrue(any("would recale" in m for m in messages), messages) @@ -4980,7 +4993,7 @@ class TestRealignProse(unittest.TestCase): self._commit(PROSE_SAMPLE + "files: [\n", "invalid yaml") self._commit(PROSE_SAMPLE.replace('"pin"', '"oldpin"'), "repair") self.path.write_text(PROSE_SAMPLE, encoding="utf-8") - messages = profile_sync.realign_prose(self.path, self.tmp.name, dry_run=True) + messages = realign.realign_prose(self.path, self.tmp.name, dry_run=True) self.assertTrue(any("unreadable" in m for m in messages), messages) def test_a_deleted_namesake_is_not_the_profiles_history(self): @@ -5000,7 +5013,7 @@ class TestRealignProse(unittest.TestCase): self._commit(PROSE_SAMPLE, "new profile") self.files[("pin", "a.c")] = ["x"] * 9 + ["subject"] self.assertEqual( - profile_sync.realign_prose(self.path, self.tmp.name, dry_run=True), [] + realign.realign_prose(self.path, self.tmp.name, dry_run=True), [] ) def test_scalar_written_at_the_current_pin_is_left_alone(self): @@ -5010,7 +5023,7 @@ class TestRealignProse(unittest.TestCase): self._commit(PROSE_SAMPLE, "profile") self.files[("pin", "a.c")] = ["x"] * 9 + ["subject"] self.assertEqual( - profile_sync.realign_prose(self.path, self.tmp.name), [] + realign.realign_prose(self.path, self.tmp.name), [] ) def test_range_lost_between_the_pins_is_reported_not_moved(self): @@ -5019,7 +5032,7 @@ class TestRealignProse(unittest.TestCase): self.files[("pin", "a.c")] = ["y"] * 20 self.files[("oldpin", "b.c")] = list("pqrstuvwx") self.files[("pin", "b.c")] = list("pqrstuvwx") - messages = profile_sync.realign_prose(self.path, self.tmp.name) + messages = realign.realign_prose(self.path, self.tmp.name) self.assertTrue(any("read again" in m for m in messages)) self.assertIn("(a.c:10)", self.path.read_text()) @@ -5030,12 +5043,12 @@ class TestRealignProse(unittest.TestCase): base = PROSE_SAMPLE.replace("(a.c:10)", "(deep.c:10)") self._commit(base.replace('"pin"', '"oldpin"'), "profile") self.path.write_text(base, encoding="utf-8") - profile_sync.upstream.list_tree = ( + upstream.list_tree = ( lambda repo, sha, cache_dir, offline=False: ([], True) ) self.files[("oldpin", "b.c")] = list("pqrstuvwx") self.files[("pin", "b.c")] = list("pqrstuvwx") - messages = profile_sync.realign_prose(self.path, self.tmp.name) + messages = realign.realign_prose(self.path, self.tmp.name) self.assertTrue( any("tree unreadable" in m for m in messages), messages ) @@ -5052,7 +5065,7 @@ class TestRealignProse(unittest.TestCase): self.files[("oldpin", "contrib/deep.c")] = ["y"] self.files[("oldpin", "b.c")] = list("pqrstuvwx") self.files[("pin", "b.c")] = list("pqrstuvwx") - messages = profile_sync.realign_prose(self.path, self.tmp.name) + messages = realign.realign_prose(self.path, self.tmp.name) self.assertTrue( any("2 paths carry the name" in m for m in messages), messages ) @@ -5068,7 +5081,7 @@ class TestRealignProse(unittest.TestCase): self.files[("pin", "src/deep.c")] = ["x"] * 13 + ["subject"] self.files[("oldpin", "b.c")] = list("pqrstuvwx") self.files[("pin", "b.c")] = list("pqrstuvwx") - messages = profile_sync.realign_prose(self.path, self.tmp.name) + messages = realign.realign_prose(self.path, self.tmp.name) self.assertIn("notes: deep.c:10 -> deep.c:14", messages) self.assertIn("(deep.c:14)", self.path.read_text()) @@ -5082,7 +5095,7 @@ class TestRateLimitEndsOneProfile(unittest.TestCase): def build(name, *_args, **_kwargs): if name == "cemu": - raise profile_sync.upstream.RateLimitError( + raise upstream.RateLimitError( "https://git.libretro.com/x: HTTP 429", "git.libretro.com" ) return ProfileReport(name=name, counts={"CHANGED": 1}) @@ -5095,7 +5108,7 @@ class TestRateLimitEndsOneProfile(unittest.TestCase): mock.patch.object(profile_sync, "select_profiles", return_value={"cemu": {}, "after": {}}), \ mock.patch.object(profile_sync, "_check_quota"), \ - mock.patch.object(profile_sync, "build_report", side_effect=build), \ + mock.patch.object(profiling_run, "build_report", side_effect=build), \ contextlib.redirect_stdout(out), \ contextlib.redirect_stderr(err), \ self.assertRaises(SystemExit) as caught: @@ -5126,15 +5139,15 @@ class AWriteRefreshesTheSourcePaths(unittest.TestCase): mock.patch.object(profile_sync, "load_emulator_profiles", return_value={}), \ mock.patch("core.artifacts.write_if_changed") as written, \ contextlib.redirect_stderr(io.StringIO()): - profile_sync._WRITTEN.clear() + profiling_run._WRITTEN.clear() profile_sync._refresh_source_paths(self._args()) self.assertFalse(built.called) - profile_sync._WRITTEN.add("x") + profiling_run._WRITTEN.add("x") profile_sync._refresh_source_paths(self._args(dry_run=True)) self.assertFalse(built.called) profile_sync._refresh_source_paths(self._args()) self.assertTrue(built.called and written.called) - profile_sync._WRITTEN.clear() + profiling_run._WRITTEN.clear() def test_the_record_reads_the_pins_just_written(self): @@ -5158,8 +5171,8 @@ class AWriteRefreshesTheSourcePaths(unittest.TestCase): mock.patch("core.artifacts.write_if_changed"), \ contextlib.redirect_stderr(io.StringIO()), \ contextlib.redirect_stdout(io.StringIO()): - profile_sync._WRITTEN.add("p") - self.addCleanup(profile_sync._WRITTEN.clear) + profiling_run._WRITTEN.add("p") + self.addCleanup(profiling_run._WRITTEN.clear) profile_sync._refresh_source_paths(self._args(emulators_dir=tmp)) self.assertEqual(built.call_args[0][0]["p"]["source_commit"], "new") @@ -5206,7 +5219,7 @@ class TestOutputModesApplyTheirFlags(unittest.TestCase): mock.patch.object(profile_sync, "select_profiles", return_value={"clean": {}, "drift": {}}), \ mock.patch.object(profile_sync, "_check_quota"), \ - mock.patch.object(profile_sync, "build_report", + mock.patch.object(profiling_run, "build_report", side_effect=lambda name, *a, **k: reports[name]), \ contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): try: @@ -5245,7 +5258,7 @@ class TestOutputModesApplyTheirFlags(unittest.TestCase): (Path(tmp) / f"{name}.yml").write_text("emulator: x\n") stale = {"clean": ["read again: notes: a.c:5 (pin moved)"], "drift": []} with mock.patch.object( - profile_sync, "realign_prose", + profiling_run, "realign_prose", side_effect=lambda path, *a, **k: stale[path.stem], ): code, out, _ = self._main( @@ -5276,7 +5289,7 @@ class TestOutputModesApplyTheirFlags(unittest.TestCase): ] report.new_hashes = [{"path": "a.c", "kind": "md5", "value": "0" * 32}] - with mock.patch.object(profile_sync, "_attach_detection", side_effect=detect): + with mock.patch.object(profiling_run, "_attach_detection", side_effect=detect): code, out, err = self._main("--json", "--detect-new-files", "--watch-hashes") self.assertEqual(code, 0, err) rows = json.loads(out) @@ -5303,7 +5316,7 @@ class TestOutputModesApplyTheirFlags(unittest.TestCase): def build(name, *args, **kwargs): if name == "drift": - raise profile_sync.upstream.UpstreamError("https://x: HTTP 502") + raise upstream.UpstreamError("https://x: HTTP 502") return ProfileReport(name=name) out = io.StringIO() @@ -5314,7 +5327,7 @@ class TestOutputModesApplyTheirFlags(unittest.TestCase): mock.patch.object(profile_sync, "select_profiles", return_value={"clean": {}, "drift": {}}), \ mock.patch.object(profile_sync, "_check_quota"), \ - mock.patch.object(profile_sync, "build_report", side_effect=build), \ + mock.patch.object(profiling_run, "build_report", side_effect=build), \ contextlib.redirect_stdout(out), \ contextlib.redirect_stderr(io.StringIO()), \ self.assertRaises(SystemExit) as caught: @@ -5332,7 +5345,7 @@ class TestReconcilePerFile(unittest.TestCase): anchored = PartResult(RefPart("libretro.c", 5, 5, "5"), "ANCHORED", None, None, None, []) same = PartResult(RefPart("libretro.c", 9, 9, "9"), "AMBIGUOUS", None, None, None, []) other = PartResult(RefPart("jg.c", 229, 230, "229-230"), "AMBIGUOUS", None, None, None, []) - result = profile_sync.reconcile_self_check([anchored, same, other]) + result = anchoring.reconcile_self_check([anchored, same, other]) self.assertEqual([p.status for p in result], ["ANCHORED", "ANCHORED", "AMBIGUOUS"]) def test_a_settled_part_keeps_the_repository_of_its_anchor(self): @@ -5340,14 +5353,14 @@ class TestReconcilePerFile(unittest.TestCase): None, None, None, [], repo="schelling/dosbox-pure") same = PartResult(RefPart("mt32emu.h", 5129, 5129, "5129"), "MOVED", None, 5112, 5112, [5112]) - result = profile_sync.reconcile_self_check([anchored, same]) + result = anchoring.reconcile_self_check([anchored, same]) self.assertEqual(result[1].status, "ANCHORED") self.assertEqual(result[1].repo, "schelling/dosbox-pure") def test_a_compared_part_is_never_rewritten(self): anchored = PartResult(RefPart("a.c", 5, 5, "5"), "ANCHORED", None, None, None, []) shifted = PartResult(RefPart("a.c", 9, 9, "9"), "SHIFTED", None, 12, 12, []) - result = profile_sync.reconcile_self_check([anchored, shifted], [True, False]) + result = anchoring.reconcile_self_check([anchored, shifted], [True, False]) self.assertEqual(result[1].status, "SHIFTED") @@ -5359,13 +5372,13 @@ class TestCollapsingMoves(unittest.TestCase): "ANCHORED", None, None, None, []) info = PartResult(RefPart("mt32emu.h", 5129, 5129, "source mt32emu.h:5129", "source"), "MOVED", None, 5112, 5112, [5112]) - result = profile_sync.refuse_collapsing_moves([sha, info]) + result = anchoring.refuse_collapsing_moves([sha, info]) self.assertEqual([p.status for p in result], ["ANCHORED", "AMBIGUOUS"]) def test_a_move_to_a_free_line_is_kept(self): first = PartResult(RefPart("a.c", 5, 5, "a.c:5"), "ANCHORED", None, None, None, []) moved = PartResult(RefPart("a.c", 9, 9, "a.c:9"), "MOVED", None, 12, 12, [12]) - result = profile_sync.refuse_collapsing_moves([first, moved]) + result = anchoring.refuse_collapsing_moves([first, moved]) self.assertEqual(result[1].status, "MOVED") @@ -5504,14 +5517,14 @@ class UpstreamVendoredAsASubmodule(unittest.TestCase): repo_s, repo_u = object(), object() views = [ - profile_sync.RepoView(repo_s, "src-pin", "src-head", "source_commit", "source"), - profile_sync.RepoView(repo_u, "sub-old", "nesbox-tip", "upstream_commit", "upstream"), + profiling_repos.RepoView(repo_s, "src-pin", "src-head", "source_commit", "source"), + profiling_repos.RepoView(repo_u, "sub-old", "nesbox-tip", "upstream_commit", "upstream"), ] with mock.patch.object( - profile_sync.upstream, "list_gitlinks", + upstream, "list_gitlinks", side_effect=lambda repo, sha, *a, **k: links.get(sha, {}), ): - return profile_sync._follow_submodule(views, "cache", True) + return profiling_repos._follow_submodule(views, "cache", True) def test_the_upstream_head_is_what_the_port_head_builds(self): views = self._views({"src-pin": {"core": "sub-old"}, "src-head": {"core": "sub-new"}}) @@ -5530,12 +5543,12 @@ class AMirrorOutlivesAGoneSource(unittest.TestCase): def test_the_mirror_is_read_when_the_source_is_gone(self): from unittest import mock - gone_repo = profile_sync.upstream.make_repo("github.com", "yuzu-emu-mirror", "yuzu") - mirror = profile_sync.upstream.make_repo("github.com", "fork", "yuzu") + gone_repo = upstream.make_repo("github.com", "yuzu-emu-mirror", "yuzu") + mirror = upstream.make_repo("github.com", "fork", "yuzu") def head(repo, cache_dir, offline=False, branch=None): if repo == gone_repo: - raise profile_sync.upstream.GoneError("HTTP 451") + raise upstream.GoneError("HTTP 451") return "mirror-head" profile = { @@ -5543,9 +5556,9 @@ class AMirrorOutlivesAGoneSource(unittest.TestCase): "source_mirror": "https://github.com/fork/yuzu", "source_commit": "pin", } - with mock.patch.object(profile_sync.upstream, "resolve_head", side_effect=head), \ - mock.patch.object(profile_sync, "resolve_pin", return_value=("pin", "source_commit")): - views = profile_sync.select_views(profile, "cache", True) + with mock.patch.object(upstream, "resolve_head", side_effect=head), \ + mock.patch.object(profiling_repos, "resolve_pin", return_value=("pin", "source_commit")): + views = profiling_repos.select_views(profile, "cache", True) self.assertEqual([(v.repo, v.field) for v in views], [(mirror, "source_mirror")]) @@ -5555,12 +5568,12 @@ class AHeldUpstreamStaysOnItsPin(unittest.TestCase): def test_the_upstream_view_is_read_at_its_pin(self): views = [ - profile_sync.RepoView("fork", "p", "h", "source_commit", "source"), - profile_sync.RepoView("up", "vendored", "tip", "upstream_commit", "upstream"), + profiling_repos.RepoView("fork", "p", "h", "source_commit", "source"), + profiling_repos.RepoView("up", "vendored", "tip", "upstream_commit", "upstream"), ] - held = profile_sync._hold_upstream({"upstream_frozen": "vendored tree"}, views) + held = profiling_repos._hold_upstream({"upstream_frozen": "vendored tree"}, views) self.assertEqual((held[0].head, held[1].head), ("h", "vendored")) - self.assertEqual(profile_sync._hold_upstream({}, views), views) + self.assertEqual(profiling_repos._hold_upstream({}, views), views) class RealignReadsThePathAsWritten(unittest.TestCase): @@ -5571,8 +5584,8 @@ class RealignReadsThePathAsWritten(unittest.TestCase): def test_an_unmoved_repository_carrying_the_path_keeps_it(self): from unittest import mock - fork = profile_sync.upstream.make_repo("github.com", "b", "hatariB") - up = profile_sync.upstream.make_repo("github.com", "hatari", "hatari") + fork = upstream.make_repo("github.com", "b", "hatariB") + up = upstream.make_repo("github.com", "hatari", "hatari") trees = {(fork, "fork-pin"): {"hatari/src/tos.c"}, (up, "old"): {"src/tos.c"}, (up, "new"): {"src/tos.c"}} @@ -5580,10 +5593,10 @@ class RealignReadsThePathAsWritten(unittest.TestCase): return ["line"] * 40 if path in trees.get((repo, sha), set()) else None part = sourceref.RefPart("hatari/src/tos.c", 10, 10, "10") - with mock.patch.object(profile_sync.upstream, "fetch_file", side_effect=fetch), \ - mock.patch.object(profile_sync, "_resolve_at", + with mock.patch.object(upstream, "fetch_file", side_effect=fetch), \ + mock.patch.object(realign, "_resolve_at", return_value=("found", ("src/tos.c", ["x"] * 40))): - result = profile_sync._realign_part( + result = realign._realign_part( part, [(up, "old", "new")], "cache", True, pinned=[(fork, "fork-pin"), (up, "new")], ) @@ -5597,15 +5610,15 @@ class RealignGivesASharedPathToTheFirstRepository(unittest.TestCase): def test_the_source_declared_first_keeps_it(self): from unittest import mock - fork = profile_sync.upstream.make_repo("github.com", "t", "freej2me-plus") - up = profile_sync.upstream.make_repo("github.com", "hex007", "freej2me") + fork = upstream.make_repo("github.com", "t", "freej2me-plus") + up = upstream.make_repo("github.com", "hex007", "freej2me") part = sourceref.RefPart("src/libretro/freej2me_libretro.c", 39, 46, "39-46") with mock.patch.object( - profile_sync.upstream, "fetch_file", return_value=["line"] * 99 + upstream, "fetch_file", return_value=["line"] * 99 ), mock.patch.object( - profile_sync, "_resolve_at", return_value=("found", (part.path, ["x"] * 99)) + realign, "_resolve_at", return_value=("found", (part.path, ["x"] * 99)) ): - result = profile_sync._realign_part( + result = realign._realign_part( part, [(up, "eb4ac87", "8b9bc8a")], "cache", True, pinned=[(fork, "3933e75"), (up, "8b9bc8a")], ) @@ -5620,19 +5633,19 @@ class UpstreamForkedByThePort(unittest.TestCase): def _views(self, base, ancestry=None): from unittest import mock - fork = profile_sync.upstream.make_repo("github.com", "f", "port") - self.up = profile_sync.upstream.make_repo("github.com", "o", "up") + fork = upstream.make_repo("github.com", "f", "port") + self.up = upstream.make_repo("github.com", "o", "up") self.fork = fork views = [ - profile_sync.RepoView(fork, "src-pin", "src-head", "source_commit", "source"), - profile_sync.RepoView(self.up, "old-base", "up-tip", "upstream_commit", "upstream"), + profiling_repos.RepoView(fork, "src-pin", "src-head", "source_commit", "source"), + profiling_repos.RepoView(self.up, "old-base", "up-tip", "upstream_commit", "upstream"), ] with mock.patch.object( - profile_sync.upstream, "fork_base", return_value=base + upstream, "fork_base", return_value=base ) as asked, mock.patch.object( - profile_sync.upstream, "ancestry", return_value=ancestry + upstream, "ancestry", return_value=ancestry ): - followed = profile_sync._follow_fork_base(views, "cache", True) + followed = profiling_repos._follow_fork_base(views, "cache", True) return followed, asked def test_the_upstream_head_is_the_fork_base(self): @@ -5663,8 +5676,8 @@ class ACachedTipOlderThanThePin(TestBuildReport): 77d80d7, its ancestor: a bump would have moved the pin backwards.""" def test_the_tip_is_asked_again(self): - profile_sync.upstream.ancestry = lambda *a, **k: (0, 3) - profile_sync.upstream.resolve_head = ( + upstream.ancestry = lambda *a, **k: (0, 3) + upstream.resolve_head = ( lambda repo, cache_dir, offline=False, branch=None, fresh=False: "newtip" if fresh else "staletip" ) @@ -5681,10 +5694,10 @@ class OfflineCacheMissIsNotGone(TestBuildReport): def test_an_uncached_file_is_not_reported_gone(self): def cold(repo, sha, path, cache_dir, offline=False): - raise profile_sync.upstream.NotCachedError(f"{path} not cached") + raise upstream.NotCachedError(f"{path} not cached") - profile_sync.upstream.fetch_file = cold - with self.assertRaises(profile_sync.upstream.NotCachedError): + upstream.fetch_file = cold + with self.assertRaises(upstream.NotCachedError): build_report("test", self._profile(["a.c:2"]), self.dir, offline=True) @@ -5748,12 +5761,12 @@ class TestTreeUrlIntoADirectory(unittest.TestCase): def test_candidates_are_prefixes_shortest_first(self): self.assertEqual( - profile_sync.branch_candidates( + profiling_repos.branch_candidates( "https://github.com/libretro/libretro-samples/tree/master/tests/cruzes" ), ["master", "master/tests", "master/tests/cruzes"], ) - self.assertEqual(profile_sync.branch_candidates("https://github.com/o/r"), []) + self.assertEqual(profiling_repos.branch_candidates("https://github.com/o/r"), []) def test_the_first_resolving_prefix_is_the_branch(self): from unittest import mock @@ -5766,8 +5779,8 @@ class TestTreeUrlIntoADirectory(unittest.TestCase): profile = {"source": "https://github.com/libretro/libretro-samples/tree/master/tests/cruzes", "source_commit": "pinsha"} - with mock.patch.object(profile_sync.upstream, "resolve_head", side_effect=head): - views = profile_sync.select_views(profile, ".cache", True) + with mock.patch.object(upstream, "resolve_head", side_effect=head): + views = profiling_repos.select_views(profile, ".cache", True) self.assertEqual([(v.pin, v.head) for v in views], [("pinsha", "tipsha")]) self.assertEqual(asked[0], "master") @@ -5781,8 +5794,8 @@ class TestAnUnansweringRepository(unittest.TestCase): def _report(self, profile): from unittest import mock - with mock.patch.object(profile_sync, "select_views", return_value=[]): - return profile_sync.build_report("p", profile, ".cache", True) + with mock.patch.object(resolver, "select_views", return_value=[]): + return resolver.build_report("p", profile, ".cache", True) def test_a_declared_pin_names_the_repository(self): report = self._report({"source": "https://github.com/o/r", "source_commit": "abc", @@ -5821,7 +5834,7 @@ class StrictRefusesAnUncheckableProfile(unittest.TestCase): was skipped with empty counts and --strict passed it in CI.""" def test_defects_are_told_from_the_profiles_nature(self): - from profile_sync import ProfileReport + from profiling.reports import ProfileReport def report(reason): return ProfileReport(name="p", entries=[], counts={}, skipped=reason) @@ -5845,9 +5858,9 @@ class AnOffForgeProfileWatchesItsReleasePage(unittest.TestCase): profile = {"emulator": "Altirra", "core_version": "4.40", "version_source": { "url": "https://example.test/altirra", "pattern": r"Altirra (\d+\.\d+) released", }} - with mock.patch.object(profile_sync.upstream, "fetch_page", + with mock.patch.object(upstream, "fetch_page", return_value="

Altirra 4.50 released

"): - self.assertEqual(profile_sync.page_version(profile, "cache", False), "4.50") + self.assertEqual(versions.page_version(profile, "cache", False), "4.50") class ARecaleKeepsTheRepositoryWord(unittest.TestCase): @@ -5865,9 +5878,9 @@ class ARecaleKeepsTheRepositoryWord(unittest.TestCase): with self.subTest(raw=raw): p = split_source_ref(raw)[0] part = sourceref.RefPart( - profile_sync.strip_repo_word(p.path, {"stella"}), p.start, p.end, p.raw) - result = profile_sync.PartResult(part, "SHIFTED", None, 140, 150, [], "") - self.assertEqual(profile_sync._rendered_part(result), expected) + profiling_citations.strip_repo_word(p.path, {"stella"}), p.start, p.end, p.raw) + result = anchoring.PartResult(part, "SHIFTED", None, 140, 150, [], "") + self.assertEqual(profiling_reports._rendered_part(result), expected) class EachCitationNamesTheCodeThatAsks(unittest.TestCase): @@ -5895,14 +5908,14 @@ class EachCitationNamesTheCodeThatAsks(unittest.TestCase): "libretro.c": [f"load {self.MD5}"]}, "alekmaul/a5200": {"emu/memory.c": ["y", f"rom {self.MD5}"]}, } - profile_sync.upstream.fetch_file = ( + upstream.fetch_file = ( lambda repo, sha, path, cache_dir, offline=False: trees.get(repo.slug, {}).get(path) ) - profile_sync.upstream.list_tree = ( + upstream.list_tree = ( lambda repo, sha, cache_dir, offline=False: (sorted(trees[repo.slug]), False) ) - profile_sync.upstream.resolve_head = ( + upstream.resolve_head = ( lambda repo, cache_dir, offline=False, branch=None: "pins" if repo.owner == "libretro" else "pinu" ) @@ -5911,7 +5924,7 @@ class EachCitationNamesTheCodeThatAsks(unittest.TestCase): profile = yaml.safe_load(path.read_text(encoding="utf-8")) with contextlib.redirect_stderr(io.StringIO()): report = build_report("t", profile, self.dir, text=path.read_text()) - profile_sync.attribute_refs(path, report, profile) + rebase.attribute_refs(path, report, profile) return yaml.safe_load(path.read_text(encoding="utf-8"))["files"][0]["source_ref"] def test_emulator_code_the_port_carries_is_the_emulators(self): @@ -5942,19 +5955,19 @@ class ARoleIsReadPerPart(unittest.TestCase): ("alek/x", "pinu"): {"a.c": [f"rom {self.MD5A}", "x", f"rom {self.MD5B}"]}, } - profile_sync.upstream.fetch_file = ( + upstream.fetch_file = ( lambda repo, sha, path, cache_dir, offline=False: trees.get((repo.slug, sha), {}).get(path) ) - profile_sync.upstream.list_tree = ( + upstream.list_tree = ( lambda repo, sha, cache_dir, offline=False: (sorted(trees.get((repo.slug, sha), {})), False) ) - profile_sync.upstream.resolve_head = ( + upstream.resolve_head = ( lambda repo, cache_dir, offline=False, branch=None: "heads" if repo.owner == "libretro" else "pinu" ) - profile_sync.upstream.compare = ( + upstream.compare = ( lambda repo, base, head, cache_dir, offline=False: upstream.CompareResult([], False) ) @@ -6074,7 +6087,7 @@ class AnOnlinePassSaysWhereItIs(unittest.TestCase): saved = sys.argv try: sys.argv = ["profile_sync.py", "--all", "--triage", *argv] - with mock.patch.object(profile_sync, "_build_full_report", side_effect=build), \ + with mock.patch.object(profiling_run, "_build_full_report", side_effect=build), \ mock.patch.object(profile_sync, "_check_quota"), \ contextlib.redirect_stdout(out), contextlib.redirect_stderr(err), \ self.assertRaises(SystemExit) as caught: @@ -6096,11 +6109,11 @@ class AnOnlinePassSaysWhereItIs(unittest.TestCase): self.assertIn("--offline answers from the cache alone", err) def test_a_progress_line_at_each_interval(self): - clock = iter([0.0, 10.0, profile_sync.PROGRESS_SECONDS + 1, 40.0]) + clock = iter([0.0, 10.0, triage.PROGRESS_SECONDS + 1, 40.0]) err = io.StringIO() - with mock.patch.object(profile_sync.time, "monotonic", lambda: next(clock)), \ + with mock.patch.object(triage.time, "monotonic", lambda: next(clock)), \ contextlib.redirect_stderr(err): - progress = profile_sync._Progress(9, "read", quiet=False) + progress = triage._Progress(9, "read", quiet=False) for done, name in enumerate(("a", "b", "c")): progress.step(done, name) self.assertEqual(err.getvalue(), "read: 1 of 9, at b\n") @@ -6139,11 +6152,11 @@ class TheTriageCountsWhatBlocksThePin(unittest.TestCase): selected = {r.name: {"source": "https://github.com/o/n"} for r in reports} args = argparse.Namespace(cache_dir="unused", offline=True) out, err = io.StringIO(), io.StringIO() - with mock.patch.object(profile_sync, "check_version", return_value=None), \ - mock.patch.object(profile_sync.upstream, "commits_touching", + with mock.patch.object(triage, "check_version", return_value=None), \ + mock.patch.object(upstream, "commits_touching", return_value=0), \ contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): - profile_sync._print_triage(args, selected, reports) + triage._print_triage(args, selected, reports) lines = out.getvalue().splitlines() self.assertIn("profile", lines[0]) self.assertIn("stuck", lines[1]) @@ -6162,11 +6175,11 @@ class TheTriageCountsWhatBlocksThePin(unittest.TestCase): selected = {r.name: {"source": "https://github.com/o/n"} for r in reports} args = argparse.Namespace(cache_dir="unused", offline=True) out = io.StringIO() - with mock.patch.object(profile_sync, "check_version", return_value=None), \ - mock.patch.object(profile_sync.upstream, "commits_touching", + with mock.patch.object(triage, "check_version", return_value=None), \ + mock.patch.object(upstream, "commits_touching", return_value=0), \ contextlib.redirect_stdout(out), contextlib.redirect_stderr(io.StringIO()): - profile_sync._print_triage(args, selected, reports) + triage._print_triage(args, selected, reports) lines = out.getvalue().splitlines() self.assertIn("gone", lines[1]) self.assertEqual(lines[-1], "2 profiles, 1 with something to review") diff --git a/tests/test_version_basis.py b/tests/test_version_basis.py index d1e729a9e..7e1861db0 100644 --- a/tests/test_version_basis.py +++ b/tests/test_version_basis.py @@ -24,10 +24,13 @@ from unittest import mock import yaml import profile_sync +from profiling import resolver +from profiling import yamledit from core import upstream from profiling import version_basis from model.profiles import load_emulator_profiles -from profile_sync import ProfileReport, bump_commit +from profiling.reports import ProfileReport +from profiling.bump import bump_commit ROOT = Path(__file__).resolve().parent.parent @@ -237,7 +240,7 @@ class AnAdvanceReadsTheVersionAgain(unittest.TestCase): self.path.write_text(SAMPLE, encoding="utf-8") self.report = ProfileReport( name="p", repo="o/n", pin="aaaaaaa", head="bbbbbbb", entries=[], counts={}, - skipped=profile_sync.NO_SOURCE_REF, + skipped=resolver.NO_SOURCE_REF, ) def tearDown(self): @@ -286,7 +289,7 @@ class TheWriterKeepsTheDocument(unittest.TestCase): ' libretro: "2"\ndisplay_name: X\n') basis = {"standalone": {"kind": "tag", "tag": "2407", "distance": 0}, "libretro": {"kind": "none"}} - written = profile_sync.set_version_fields( + written = yamledit.set_version_fields( text, {"standalone": "2407", "libretro": None}, basis) document = yaml.safe_load(written) self.assertEqual(document["core_version"], {"standalone": "2407", "libretro": None}) @@ -296,7 +299,7 @@ class TheWriterKeepsTheDocument(unittest.TestCase): def test_a_missing_version_is_written_after_the_pins(self): text = 'emulator: X\nsource_commit: "abc"\nfiles: []\n' - written = profile_sync.set_version_fields(text, None, {"kind": "none"}) + written = yamledit.set_version_fields(text, None, {"kind": "none"}) self.assertEqual(written.splitlines()[2:4], ["core_version: null", "version_basis:"]) diff --git a/tests/test_workflow_paths.py b/tests/test_workflow_paths.py index b2c338e49..6c7908d37 100644 --- a/tests/test_workflow_paths.py +++ b/tests/test_workflow_paths.py @@ -103,8 +103,10 @@ class DeploySiteTriggers(unittest.TestCase): def test_the_site_reads_the_ref_grammar_not_the_checker(self): """sitesources imported two parsing functions from profile_sync, so every change to the comparison tool rebuilt an unchanged site.""" - self.assertNotIn("scripts/profile_sync.py", closure(["generate_site.py"])) - self.assertIn("scripts/model/sourceref.py", closure(["generate_site.py"])) + read = closure(["generate_site.py"]) + self.assertNotIn("scripts/profile_sync.py", read) + self.assertEqual(sorted(p for p in read if p.startswith("scripts/profiling/")), []) + self.assertIn("scripts/model/sourceref.py", read) class ThePullRequestBuildsTheSite(unittest.TestCase):