refactor: split core.upstream by subject

HTTP to the forges moves to core.forgehttp, comparisons, trees and
clone history to core.upstreamtree. core.upstream keeps repositories,
the cache and revisions.
This commit is contained in:
Abdessamad Derraz committed 2026-11-24 06:20:52 +01:00
1 parent ce92eff0aa
commit 263a64c0e0
65 files changed
+1417 -1337

No files matched your search

+1 -1
View File
@@ -7,7 +7,7 @@ import posixpath
from collections.abc import Callable, Iterable, Sequence
from dataclasses import dataclass, replace
from core.upstream import CompareResult, find_renamed
from core.upstreamtree 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
+6 -4
View File
@@ -10,7 +10,9 @@ import re
import sys
from core import upstream
from core.upstream import CompareResult
from core import forgehttp
from core import upstreamtree
from core.upstreamtree import CompareResult
from profiling.citations import collect_tokens
from profiling.reports import CitedSource, ProfileReport
@@ -284,9 +286,9 @@ def print_extras(
return
try:
_print_extras_from(args, report)
except upstream.RateLimitError:
except forgehttp.RateLimitError:
raise
except upstream.UpstreamError as exc:
except forgehttp.UpstreamError as exc:
print(f" extras skipped: {exc}")
@@ -310,7 +312,7 @@ def _print_extras_from(args: argparse.Namespace, report: ProfileReport) -> None:
by_repo.setdefault((source.repo.host, source.repo.slug), []).append(source)
for sources in by_repo.values():
first = sources[0]
comparison = upstream.compare(
comparison = upstreamtree.compare(
first.repo, first.pin, first.head, args.cache_dir, args.offline
)
if len(by_repo) > 1:
+2 -1
View File
@@ -12,6 +12,7 @@ from pathlib import Path
import yaml
from core import upstream
from core import upstreamtree
from model.sourceref import RefPart
from profiling.anchoring import anchor_block
from profiling.attribution import owner_order
@@ -153,7 +154,7 @@ def _resolve_at(
if lines is not None:
return "found", (candidate, lines)
if "/" not in path:
tree, truncated = upstream.list_tree(repo, sha, cache_dir, offline)
tree, truncated = upstreamtree.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(
+2 -2
View File
@@ -6,7 +6,7 @@ import argparse
import sys
from pathlib import Path
from core import upstream
from core import forgehttp
from profiling.realign import realign_prose
from profiling.sourcepaths import record_write, refresh_source_paths
from profiling.yamledit import YamlWriteError
@@ -26,7 +26,7 @@ def run_realign(args: argparse.Namespace, selected: dict) -> None:
profile_path, args.cache_dir, args.offline, args.dry_run
):
print(f"{name}: {line}")
except upstream.UpstreamError as exc:
except forgehttp.UpstreamError as exc:
print(f"{name}: {exc}", file=sys.stderr)
failed.add(name)
except YamlWriteError as exc:
+11 -9
View File
@@ -7,6 +7,8 @@ from collections.abc import Iterable
from dataclasses import dataclass, replace
from core import upstream
from core import forgehttp
from core import upstreamtree
from model.profilerepos import declared_pin, declared_repositories, pin_field, serving_url
@@ -111,11 +113,11 @@ def select_views(
branch = declared if pinned_by == "source" else None
head = None
try:
unread: upstream.NotCachedError | None = None
unread: forgehttp.NotCachedError | None = None
for candidate in branch_candidates(url):
try:
head = upstream.resolve_head(repo, cache_dir, offline, candidate)
except upstream.NotCachedError as exc:
except forgehttp.NotCachedError as exc:
# Another prefix of the /tree/ tail may be the cached branch.
unread = exc
continue
@@ -130,7 +132,7 @@ def select_views(
if not pin or origin is None:
continue
head = head or upstream.resolve_head(repo, cache_dir, offline, branch)
except upstream.GoneError:
except forgehttp.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
@@ -141,13 +143,13 @@ def select_views(
# 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)
relation = upstreamtree.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")
raise forgehttp.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)
@@ -184,7 +186,7 @@ def _follow_submodule(
return views
pinned = {
sha: path
for path, sha in upstream.list_gitlinks(
for path, sha in upstreamtree.list_gitlinks(
source.repo, source.pin, cache_dir, offline
).items()
}
@@ -194,7 +196,7 @@ def _follow_submodule(
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)
upstreamtree.list_gitlinks(source.repo, source.head, cache_dir, offline).get(path)
if path else None
)
followed.append(
@@ -222,7 +224,7 @@ def _follow_fork_base(
if view.field != "upstream" or view.pin == view.head or " via " in view.origin:
followed.append(view)
continue
base = upstream.fork_base(
base = upstreamtree.fork_base(
view.repo, view.head, source.repo, source.head, cache_dir, offline
)
if base and base != view.pin:
@@ -230,7 +232,7 @@ def _follow_fork_base(
# 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)
moved = upstreamtree.ancestry(view.repo, base, view.pin, cache_dir, offline)
if moved and moved[0] > 0 and moved[1] == 0:
base = None
followed.append(
+15 -13
View File
@@ -11,7 +11,9 @@ from collections.abc import Callable, Iterable, Iterator, Sequence
from dataclasses import dataclass, field, replace
from core import upstream
from core.upstream import CompareResult
from core import forgehttp
from core import upstreamtree
from core.upstreamtree import CompareResult
from model.sourceref import RefPart, ref_role, resolve_project_word, split_source_ref
from profiling.anchoring import (
BLANK_LOOKAHEAD,
@@ -246,9 +248,9 @@ def _readable_views(
try:
views = select_views(profile, cache_dir, offline)
except upstream.RateLimitError:
except forgehttp.RateLimitError:
raise
except upstream.UpstreamError:
except forgehttp.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:
@@ -319,7 +321,7 @@ def _record_revisions(
f"{view.repo.slug}: pin {view.pin[:8]} is no ancestor of {view.head[:8]}"
for view in views
if not reads_own_revision(view, tagged)
and upstream.compare(
and upstreamtree.compare(
view.repo, view.pin, view.head, cache_dir, offline
).diverged
]
@@ -416,8 +418,8 @@ class _Resolver:
known: the parts it cannot place stay external, as before."""
for view in self.views:
try:
yield upstream.list_gitlinks(view.repo, view.pin, self.cache_dir, self.offline)
except upstream.TruncatedListingError as exc:
yield upstreamtree.list_gitlinks(view.repo, view.pin, self.cache_dir, self.offline)
except forgehttp.TruncatedListingError as exc:
print(f"{self.name}: submodule paths unknown, {exc}", file=sys.stderr)
yield ()
@@ -588,7 +590,7 @@ class _Resolver:
"""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)
tree, _ = upstreamtree.list_tree(view.repo, view.pin, self.cache_dir, self.offline)
self.pin_trees[key] = tree
return self.pin_trees[key]
@@ -614,10 +616,10 @@ class _Resolver:
# A file of a submodule is read at the commit its gitlink
# pins in this revision, so a bump moves it with the port.
try:
inner = upstream.through_gitlink(
inner = upstreamtree.through_gitlink(
repo, sha, wanted, self.cache_dir, self.offline
)
except upstream.TruncatedListingError as exc:
except forgehttp.TruncatedListingError as exc:
# The tree is listed in part: the path may sit in a
# submodule nobody can see, which is no reason to mute
# every other file of the repository.
@@ -627,9 +629,9 @@ class _Resolver:
if inner is not None:
found = upstream.fetch_file(*inner, self.cache_dir, self.offline)
return found
except (upstream.RateLimitError, upstream.NotCachedError):
except (forgehttp.RateLimitError, forgehttp.NotCachedError):
raise
except upstream.UpstreamError as exc:
except forgehttp.UpstreamError as exc:
self.mute.add(key)
print(f"{self.name}: {repo.host}/{repo.slug} muted, {exc}", file=sys.stderr)
return None
@@ -637,10 +639,10 @@ class _Resolver:
def context_for(self, view: RepoView) -> tuple[CompareResult, list[str]]:
key = view.repo.slug
if key not in self.context:
comparison = upstream.compare(
comparison = upstreamtree.compare(
view.repo, view.pin, view.head, self.cache_dir, self.offline
)
tree, truncated = upstream.list_tree(
tree, truncated = upstreamtree.list_tree(
view.repo, view.head, self.cache_dir, self.offline
)
self.context[key] = (comparison, tree)
+18 -16
View File
@@ -12,6 +12,8 @@ from pathlib import Path
import yaml
from core import upstream
from core import forgehttp
from core import upstreamtree
from profiling.bump import bump_commit, pins_changed
from profiling.detection import detect_upstream_changes, print_extras
from profiling.realign import realign_prose
@@ -76,12 +78,12 @@ def _read_stale_prose(
"""
try:
return realign_prose(path, args.cache_dir, args.offline, dry_run=True)
except upstream.RateLimitError:
except forgehttp.RateLimitError:
raise
except upstream.NotCachedError as exc:
except forgehttp.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:
except forgehttp.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)
@@ -269,9 +271,9 @@ def _attach_detection(
return
try:
detect_upstream_changes(profile, report, cache_dir, offline, files, hashes)
except upstream.RateLimitError:
except forgehttp.RateLimitError:
raise
except upstream.UpstreamError as exc:
except forgehttp.UpstreamError as exc:
report.detection_skipped = str(exc)
@@ -303,16 +305,16 @@ def _version_of(
passed = None
try:
if version and version.tag_commit and pin and version.tag_commit != pin:
relation = upstream.ancestry(
relation = upstreamtree.ancestry(
repo, version.tag_commit, pin, args.cache_dir, args.offline
)
passed = latest_passed_release(repo, version, pin, args.cache_dir, args.offline)
newer = None if passed or profile.get(frozen_by) else newer_release(
repo, version, pin, args.cache_dir, args.offline
)
except upstream.RateLimitError:
except forgehttp.RateLimitError:
raise
except upstream.UpstreamError as exc:
except forgehttp.UpstreamError as exc:
report.version_unread = f"tag ancestry unread: {exc}"
newer = None
warning = version_warning(version, pin, relation, passed) or (
@@ -340,9 +342,9 @@ def _attach_version(
for named in named_tags:
try:
version, warning = _version_of(args, profile, report, named)
except upstream.RateLimitError:
except forgehttp.RateLimitError:
raise
except upstream.UpstreamError as exc:
except forgehttp.UpstreamError as exc:
report.version_unread = f"version check skipped: {exc}"
return
if report.version is None:
@@ -368,7 +370,7 @@ def read_one_profile(
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:
except forgehttp.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.
@@ -386,7 +388,7 @@ def read_one_profile(
# The guard already left the file untouched, so keep going.
print(f"{name}: write refused: {exc}", file=sys.stderr)
failed.add(name)
except upstream.UpstreamError as exc:
except forgehttp.UpstreamError as exc:
# A prose read the writes need could not be made (offline with
# a cold cache, a forge down): the profile was put back, and one
# profile must not end the pass for the others.
@@ -450,20 +452,20 @@ def _build_full_report(
profile_path.read_text(encoding="utf-8")
if profile_path.is_file() else None,
)
except upstream.RateLimitError:
except forgehttp.RateLimitError:
raise
except upstream.NotCachedError as exc:
except forgehttp.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 = _skipped(name, SkipKind.OFFLINE, f"offline: {exc}")
except upstream.GoneError as exc:
except forgehttp.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 = _skipped(name, SkipKind.GONE, f"upstream gone: {exc}")
except upstream.UpstreamError as exc:
except forgehttp.UpstreamError as exc:
# One unreachable forge must not abandon the other profiles.
print(f"{name}: {exc}", file=sys.stderr)
report = _skipped(name, SkipKind.UPSTREAM_ERROR, f"upstream error: {exc}")
+2 -1
View File
@@ -9,6 +9,7 @@ from collections.abc import Sequence
from dataclasses import dataclass, field
from core import upstream
from core import forgehttp
from profiling.reports import ProfileReport, report_to_dict, triage_state
from profiling.versionbasis import version_repo, version_tags
from profiling.versions import VersionReport, check_version
@@ -173,7 +174,7 @@ def _score(
source.repo, source.path, source.pin, args.cache_dir, args.offline,
head=source.head,
)
except upstream.UpstreamError as exc:
except forgehttp.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"
+10 -8
View File
@@ -33,6 +33,8 @@ from collections.abc import Callable
from dataclasses import dataclass
from core import upstream
from core import forgehttp
from core import upstreamtree
from model.profilerepos import pin_field
from profiling.repos import resolve_pin, select_repo
@@ -224,15 +226,15 @@ def _ancestry(
"""The forge's compare, else the commit graph: GitHub refuses with 422
a compare whose diff is too large (aps3e's tag and pin)."""
try:
relation = upstream.ancestry(repo, base, head, cache_dir, offline)
except upstream.RateLimitError:
relation = upstreamtree.ancestry(repo, base, head, cache_dir, offline)
except forgehttp.RateLimitError:
raise
except upstream.UpstreamError:
except forgehttp.UpstreamError:
relation = None
if relation is None:
try:
relation = upstream.graph_ancestry(repo, base, head, cache_dir, offline)
except upstream.NotCachedError:
relation = upstreamtree.graph_ancestry(repo, base, head, cache_dir, offline)
except forgehttp.NotCachedError:
return None
return relation
@@ -267,7 +269,7 @@ def _read_none(req: _Request) -> Reading:
# A release tagged since the basis was written gives the
# code a version: the advance has to name it.
repo, pin = req.repo_and_pin()
described = upstream.describe(repo, pin, req.cache_dir, req.offline)
described = upstreamtree.describe(repo, pin, req.cache_dir, req.offline)
if described is not None:
tag, distance = described
return Reading(req.mode, req.kind, req.stated, describe_form(tag, distance, pin),
@@ -317,7 +319,7 @@ def _read_file(req: _Request, repo: upstream.Repo, pin: str) -> Reading:
def _read_tag(req: _Request, repo: upstream.Repo, pin: str) -> Reading:
basis = req.basis
if req.recompute:
described = upstream.describe(repo, pin, req.cache_dir, req.offline, basis.get("match"))
described = upstreamtree.describe(repo, pin, req.cache_dir, req.offline, basis.get("match"))
if described is None:
return req.unreadable(f"{repo.slug}@{pin[:7]}: no tag in its history")
tag, distance = described
@@ -375,7 +377,7 @@ def read(
on_forge = _ON_FORGE.get(kind)
if on_forge is not None:
return on_forge(req, repo, pin)
except (upstream.UpstreamError, VersionUnreadable) as exc:
except (forgehttp.UpstreamError, VersionUnreadable) as exc:
return req.unreadable(str(exc))
return req.unreadable(f"unknown kind {kind!r}")
+8 -6
View File
@@ -6,6 +6,8 @@ import re
from dataclasses import dataclass
from core import upstream
from core import forgehttp
from core import upstreamtree
from profiling.versionbasis import VersionTag, declared
@@ -68,9 +70,9 @@ def page_version(
return None, "version_source names no url or pattern"
try:
text = upstream.fetch_page(url, cache_dir, offline)
except upstream.NotCachedError:
except forgehttp.NotCachedError:
return None, "not cached offline"
except upstream.UpstreamError as exc:
except forgehttp.UpstreamError as exc:
return None, f"{url}: {exc}"
if text is None:
return None, "page absent"
@@ -206,10 +208,10 @@ def newer_release(
commit = upstream.tag_commit(repo, published, cache_dir, offline)
if not commit or commit == pin:
return None
after_tag = upstream.ancestry(repo, version.tag_commit, commit, cache_dir, offline)
after_tag = upstreamtree.ancestry(repo, version.tag_commit, commit, cache_dir, offline)
if not after_tag or after_tag[0] == 0 or after_tag[1] != 0:
return None
after_pin = upstream.ancestry(repo, pin, commit, cache_dir, offline)
after_pin = upstreamtree.ancestry(repo, pin, commit, cache_dir, offline)
if not after_pin or after_pin[0] == 0:
return None
return published
@@ -245,12 +247,12 @@ def latest_passed_release(
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, cache_dir, offline)
older = upstreamtree.ancestry(repo, commit, version.tag_commit, cache_dir, offline)
if older and older[1] == 0:
return None
if commit == pin:
return published
relation = upstream.ancestry(repo, commit, pin, cache_dir, offline)
relation = upstreamtree.ancestry(repo, commit, pin, cache_dir, offline)
if relation and relation[1] == 0:
return published
return None