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

+7 -8
View File
@@ -20,17 +20,16 @@ from typing import Any, Callable, Iterator, NoReturn, TypeVar
from core.github import github_token
from core.artifacts import write_text_atomic
from core.hashing import git_blob_sha
from core.upstream import (
from core.forgehttp import (
MAX_RESPONSE_SIZE,
RateLimitError,
UpstreamError,
make_repo,
open_retrying,
read_limited,
resolve_head,
)
from core.upstream import download as upstream_download
from core.upstream import forge_headers as upstream_headers
from core.upstream import make_repo, resolve_head
from core.forgehttp import download as upstream_download
from core.forgehttp import forge_headers as upstream_headers
from model.safeparse import require_yaml
from model.sources import source_yaml_file
@@ -173,7 +172,7 @@ class FetchError(ConnectionError):
def forge_headers(url: str, accept: str | None = None) -> dict[str, str]:
"""Request headers for `url`: the GitHub token goes to GitHub's API and
raw hosts only, compared whole (core.upstream.forge_headers)."""
raw hosts only, compared whole (core.forgehttp.forge_headers)."""
return upstream_headers(url, accept, SCRAPER_AGENT)
@@ -207,7 +206,7 @@ def _open(
timeout: float,
) -> _T:
"""Open `request` and hand the response to `consume`, retried as
core.upstream.open_retrying does. A header the request does not set
core.forgehttp.open_retrying does. A header the request does not set
comes from forge_headers(). A failure raises FetchError."""
url = request.full_url
for name, value in forge_headers(url).items():
@@ -264,7 +263,7 @@ def download(
"""Stream `url` into `destination`, whole or not at all, and return the
response's Content-Disposition ("" when absent or when skipped).
core.upstream.download does the work: a scratch file renamed into place,
core.forgehttp.download does the work: a scratch file renamed into place,
so a reader or an interrupted run never sees half an archive.
`skip_existing` keeps a file already there without asking the server.
A failure raises FetchError.
+1 -1
View File
@@ -6,7 +6,7 @@ import logging
import urllib.error
import urllib.parse
from core.upstream import remote_etag as get_remote_etag
from core.forgehttp import remote_etag as get_remote_etag
from core.paths import DATA_DIRS_FILE
from model.registry import data_dir_registry_at, load_versions, versions_file_for
from .base_scraper import fetch_text
+2 -1
View File
@@ -29,6 +29,7 @@ from model.sources import source_yaml_file
from core.cli import SourceUnreadable
from core import upstream
from core import upstreamtree
from .base_scraper import fetch_github_latest_version
from ._hash_merge import (
@@ -473,7 +474,7 @@ def _merge_devices(args: argparse.Namespace) -> None:
device["systems"] = filed.get(set_name, [])
report = merge_mame_devices(str(profile_path), devices, write=not args.dry_run)
shipped = upstream.list_directory(repo, commit, "hash", upstream.CACHE_DIR)
shipped = upstreamtree.list_directory(repo, commit, "hash", upstream.CACHE_DIR)
if shipped is None:
raise RuntimeError(f"hash/ of {repo.slug} at {commit} could not be listed")
present = {name[:-4] for name in shipped if name.endswith(".xml")}
+4 -4
View File
@@ -17,7 +17,7 @@ from __future__ import annotations
from collections.abc import Container
from core import upstream
from core import upstreamtree
from model.safeparse import parse_untrusted_xml
from .base_scraper import BaseScraper, BiosRequirement, fetch_bytes, systems_from_requirements
@@ -122,13 +122,13 @@ def tree_listing(directory: str, tag: str) -> list[dict]:
import json
entries: list[dict] = []
for page in range(1, upstream.MAX_TREE_PAGES + 1):
for page in range(1, upstreamtree.MAX_TREE_PAGES + 1):
url = TREE_URL.format(
path=directory, tag=tag, per_page=upstream.TREE_PAGE_SIZE, page=page
path=directory, tag=tag, per_page=upstreamtree.TREE_PAGE_SIZE, page=page
)
listing = json.loads(fetch_bytes(url))
entries += [item for item in listing if isinstance(item, dict)]
if len(listing) < upstream.TREE_PAGE_SIZE:
if len(listing) < upstreamtree.TREE_PAGE_SIZE:
return entries
raise RuntimeError(f"{directory} at {tag} outruns the page cap")
# One raw read per system.ini made about 150 requests and GitLab throttled
+4 -3
View File
@@ -26,6 +26,7 @@ if (_SCRIPTS_DIR := str(Path(__file__).resolve().parents[2])) not in sys.path:
sys.path.insert(0, _SCRIPTS_DIR)
from core import upstream
from core import upstreamtree
from ..base_scraper import PlatformDumper
@@ -85,7 +86,7 @@ class Fetcher:
repo, sha = self._repo(key)
if repo.family == "gitlab" and prefix.strip("/"):
return self._gitlab_subtree(repo, sha, prefix)
paths, truncated = upstream.list_tree(repo, sha, self.cache_dir, self.offline)
paths, truncated = upstreamtree.list_tree(repo, sha, self.cache_dir, self.offline)
if truncated:
raise SourceError(f"{repo.slug}@{sha[:8]}: the forge truncated the tree")
return sorted(p for p in paths if p.startswith(prefix))
@@ -94,7 +95,7 @@ class Fetcher:
"""Fetch a whole GitLab directory in one archive request before its
files are read one by one. A no-op on other forges."""
repo, sha = self._repo(key)
held = upstream.cache_gitlab_directory(repo, sha, directory, self.cache_dir, self.offline)
held = upstreamtree.cache_gitlab_directory(repo, sha, directory, self.cache_dir, self.offline)
if held is not None:
self._cached_dirs.setdefault(key, {})[directory.strip("/")] = held
@@ -120,7 +121,7 @@ class Fetcher:
self, repo: upstream.Repo, sha: str, directory: str, *, recursive: bool, blobs: bool
) -> list[str]:
directory = directory.strip("/")
entries, truncated = upstream.gitlab_tree_entries(
entries, truncated = upstreamtree.gitlab_tree_entries(
repo, sha, self.cache_dir, self.offline, path=directory, recursive=recursive
)
if truncated:
+1 -1
View File
@@ -12,7 +12,7 @@ import sys
from pathlib import Path
from . import Fetcher, SourceError, dump_setup, scrape_setup
from core.upstream import UpstreamError
from core.forgehttp import UpstreamError
from core.artifacts import write_if_changed
from model.romfolders import ROMFOLDERS_DIR, SOURCES_DIR, load_sources