mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-12-03 06:48:52 -06:00
fix: let core.upstream refuse requests offline
RETROBIOS_OFFLINE was read by six callers and never by the network gateway: a caller that did not pass offline=True sent the request, untokened. core.offline holds the rule, open_retrying refuses with OfflineError and the cached reads answer as an offline run. The callers read the same predicate, and the tests that fake the network clear the variable for themselves (tests.ONLINE).
This commit is contained in:
1 parent
409cc843f9
commit
83e810d1b3
23 files changed
+124
-17
No files matched your search
@@ -5,6 +5,8 @@ from __future__ import annotations
|
||||
import functools
|
||||
import os
|
||||
|
||||
from core.offline import run_is_offline
|
||||
|
||||
|
||||
@functools.lru_cache(maxsize=1)
|
||||
def _gh_cli_token() -> str:
|
||||
@@ -41,7 +43,7 @@ def github_token() -> str:
|
||||
call and never ask gh.
|
||||
"""
|
||||
token = os.environ.get("GITHUB_TOKEN") or os.environ.get("GH_TOKEN") or ""
|
||||
if token or os.environ.get("RETROBIOS_OFFLINE"):
|
||||
if token or run_is_offline():
|
||||
return token
|
||||
token = _gh_cli_token()
|
||||
if token:
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
"""Whether a run fetches nothing: the one rule core.upstream and its callers read."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
|
||||
# Set, this run fetches nothing whatever its callers pass: the test runners
|
||||
# and the PR checks set it.
|
||||
OFFLINE_ENV = "RETROBIOS_OFFLINE"
|
||||
|
||||
|
||||
def run_is_offline(offline: bool | None = False) -> bool:
|
||||
"""Whether this run fetches nothing: the caller's flag, or RETROBIOS_OFFLINE."""
|
||||
return bool(offline) or bool(os.environ.get(OFFLINE_ENV))
|
||||
@@ -27,6 +27,7 @@ from pathlib import Path
|
||||
from typing import Callable, Mapping, Protocol, TypeVar
|
||||
|
||||
from core.artifacts import atomic_scratch, write_text_atomic
|
||||
from core.offline import OFFLINE_ENV, run_is_offline
|
||||
|
||||
USER_AGENT = "retrobios-profile-sync/1.0"
|
||||
# Where fetched upstream files live, shared by every reader of this cache.
|
||||
@@ -123,6 +124,10 @@ class NotCachedError(UpstreamError):
|
||||
"""
|
||||
|
||||
|
||||
class OfflineError(NotCachedError):
|
||||
"""A request a run that fetches nothing (RETROBIOS_OFFLINE) would have sent."""
|
||||
|
||||
|
||||
class TruncatedListingError(UpstreamError):
|
||||
"""A listing the forge served in part: a page that could not be read,
|
||||
the page cap, or a tree GitHub cut short. What it holds is real, but an
|
||||
@@ -379,9 +384,12 @@ def open_retrying(
|
||||
limit is waited out, and a host that outlasts the waits is not asked
|
||||
again in this process. What ends the attempts is raised as urllib gave
|
||||
it, except a host that does not resolve (GoneError) and a host muted
|
||||
earlier (RateLimitError).
|
||||
earlier (RateLimitError). A run that fetches nothing (run_is_offline)
|
||||
raises OfflineError before anything goes out.
|
||||
"""
|
||||
url = request.full_url
|
||||
if run_is_offline():
|
||||
raise OfflineError(f"{url}: {OFFLINE_ENV} is set, nothing is fetched")
|
||||
host = urllib.parse.urlsplit(url).hostname or ""
|
||||
if host in _exhausted_hosts:
|
||||
raise RateLimitError(f"{url}: {host} rate limited earlier in this run", host)
|
||||
@@ -554,6 +562,7 @@ def fetch_file(
|
||||
Offline, a path the cache never stored raises NotCachedError: nothing
|
||||
is known about it, which is not the same as knowing it is absent.
|
||||
"""
|
||||
offline = run_is_offline(offline)
|
||||
target = cache_path(cache_dir, repo, sha, path)
|
||||
if target.is_file():
|
||||
text = target.read_text(encoding="utf-8")
|
||||
@@ -585,7 +594,7 @@ def _require_cached(url: str, cache_dir: str, offline: bool, what: str) -> None:
|
||||
cached_api answers None both for a resource the forge does not have and for
|
||||
one never asked, and only the first is a fact.
|
||||
"""
|
||||
if offline and not _api_target(url, cache_dir).is_file():
|
||||
if run_is_offline(offline) and not _api_target(url, cache_dir).is_file():
|
||||
raise NotCachedError(f"{what} is not in the offline cache")
|
||||
|
||||
|
||||
@@ -600,6 +609,7 @@ def cached_api(
|
||||
``absent_max_age`` bounds a cached absence alone, for a resource that
|
||||
can appear but, once there, does not change.
|
||||
"""
|
||||
offline = run_is_offline(offline)
|
||||
target = _api_target(url, cache_dir)
|
||||
if target.is_file():
|
||||
raw = target.read_text(encoding="utf-8")
|
||||
@@ -626,6 +636,7 @@ def fetch_page(url: str, cache_dir: str, offline: bool) -> str | None:
|
||||
None is a page the site answers absent. Offline, a page the cache never
|
||||
stored raises NotCachedError, as every other read of this module does.
|
||||
"""
|
||||
offline = run_is_offline(offline)
|
||||
key = hashlib.sha256(url.encode()).hexdigest()
|
||||
target = Path(cache_dir) / "_pages" / f"{key}.txt"
|
||||
if target.is_file():
|
||||
@@ -819,6 +830,7 @@ def _github_graphql(
|
||||
"""
|
||||
from core.github import github_token
|
||||
|
||||
offline = run_is_offline(offline)
|
||||
body = json.dumps({"query": query, "variables": variables}, sort_keys=True)
|
||||
target = Path(cache_dir) / "_api" / f"{hashlib.sha256(body.encode()).hexdigest()}.json"
|
||||
if target.is_file():
|
||||
@@ -1162,7 +1174,7 @@ def cache_gitlab_directory(
|
||||
index = cache_path(cache_dir, repo, sha, f".archives/{directory}.json")
|
||||
if index.is_file():
|
||||
return set(json.loads(index.read_text(encoding="utf-8")))
|
||||
if offline or repo.family != "gitlab":
|
||||
if run_is_offline(offline) or repo.family != "gitlab":
|
||||
return None
|
||||
try:
|
||||
files = _gitlab_archive_files(repo, sha, directory)
|
||||
@@ -1434,6 +1446,7 @@ def _has_commit(clone: Path, sha: str) -> bool:
|
||||
|
||||
def _graph(repo: Repo, sha: str, cache_dir: str, offline: bool) -> Path:
|
||||
"""A clone holding sha and every tag, fetched again once tags may have moved."""
|
||||
offline = run_is_offline(offline)
|
||||
clone = clone_dir(cache_dir, repo)
|
||||
url = f"https://{repo.host}/{repo.slug}.git"
|
||||
if not (clone / "HEAD").is_file():
|
||||
|
||||
@@ -246,7 +246,7 @@ def _fetch_asset(
|
||||
# shared cache filled the runner's disk and changed what later tests
|
||||
# resolved. Decided once, so the release check of a cached copy obeys it
|
||||
# as the download does.
|
||||
offline = offline or bool(os.environ.get("RETROBIOS_OFFLINE"))
|
||||
offline = upstream.run_is_offline(offline)
|
||||
cached = os.path.join(dest_dir, name)
|
||||
# Between the existence test and the hash, a concurrent run can drop the
|
||||
# same stale entry: the file is gone by the time this one reads it, and
|
||||
|
||||
@@ -5,13 +5,14 @@ from __future__ import annotations
|
||||
import sys
|
||||
|
||||
from core.github import github_token
|
||||
from core.offline import run_is_offline
|
||||
|
||||
ANON_QUOTA = 60
|
||||
|
||||
|
||||
def check_quota(count: int, offline: bool) -> None:
|
||||
"""Refuse a run the anonymous quota cannot carry to completion."""
|
||||
if offline or github_token():
|
||||
if run_is_offline(offline) or github_token():
|
||||
return
|
||||
if count * 5 > ANON_QUOTA:
|
||||
print(
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import os
|
||||
import sys
|
||||
from collections.abc import Iterator
|
||||
from contextlib import contextmanager
|
||||
from pathlib import Path
|
||||
|
||||
from core.offline import run_is_offline
|
||||
from core.paths import EMULATORS_DIR, REPO_ROOT
|
||||
from model.profiles import load_emulator_profiles
|
||||
|
||||
@@ -39,7 +39,7 @@ def refresh_source_paths(args: argparse.Namespace, written: set[str]) -> None:
|
||||
another tree prints the command for it instead of reducing the record
|
||||
to the trees that tree cites.
|
||||
"""
|
||||
if not written or args.dry_run or args.offline or os.environ.get("RETROBIOS_OFFLINE"):
|
||||
if not written or args.dry_run or run_is_offline(args.offline):
|
||||
return
|
||||
if Path(args.emulators_dir).resolve() != (REPO_ROOT / EMULATORS_DIR).resolve():
|
||||
print(
|
||||
|
||||
@@ -150,10 +150,7 @@ def _fetched_asset(path: str, sha1: str, file_entry: dict, offline: bool) -> str
|
||||
|
||||
def run_is_offline(offline: bool | None = None) -> bool:
|
||||
"""Whether this run fetches nothing: --offline, or RETROBIOS_OFFLINE."""
|
||||
return bool(
|
||||
(OFFLINE if offline is None else offline)
|
||||
or os.environ.get("RETROBIOS_OFFLINE")
|
||||
)
|
||||
return upstream.run_is_offline(OFFLINE if offline is None else offline)
|
||||
|
||||
|
||||
def release_asset_paths(
|
||||
|
||||
@@ -8,6 +8,7 @@ from collections.abc import Callable
|
||||
from pathlib import Path
|
||||
|
||||
from core.cli import InputMissing
|
||||
from core.offline import run_is_offline
|
||||
from core.paths import DB_FILE, EMULATORS_DIR, REPO_ROOT
|
||||
from model import siteinputs
|
||||
from model.largefiles import LARGE_FILES_CACHE
|
||||
@@ -130,7 +131,7 @@ def _coverage_inputs(db_path: Path, platforms_dir: str, emulators_dir: str) -> s
|
||||
for path in (REPO_ROOT / ".gitignore", REPO_ROOT / "_mame_clones.json",
|
||||
REPO_ROOT / VERSIONS_FILE):
|
||||
add_file(path)
|
||||
digest.update(b"offline\0" + (b"1" if os.environ.get("RETROBIOS_OFFLINE") else b"0") + b"\n")
|
||||
digest.update(b"offline\0" + (b"1" if run_is_offline() else b"0") + b"\n")
|
||||
large = Path(LARGE_FILES_CACHE).resolve()
|
||||
digest.update(str(large).encode() + b"\0")
|
||||
for path in sorted(large.glob("*")) if large.is_dir() else ():
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
from collections.abc import Callable
|
||||
@@ -155,7 +154,7 @@ def fetch_contributors() -> list[dict]:
|
||||
"""Contributors from the GitHub API, bots and the owner left out. Empty
|
||||
when the API cannot be read, or under RETROBIOS_OFFLINE: the caller then
|
||||
keeps the published list."""
|
||||
if os.environ.get("RETROBIOS_OFFLINE"):
|
||||
if upstream.run_is_offline():
|
||||
return []
|
||||
url = f"https://api.github.com/repos/{REPO}/contributors"
|
||||
try:
|
||||
|
||||
Reference in new issue
Block a user