refactor: route network access through upstream

Branch heads (scrapers, FBNeo import, data directories) resolve through
core.upstream.resolve_head, with cache_dir=None to keep nothing under
.cache/upstream. Streaming downloads (large files, external pack files,
data directory archives, scraper downloads, standalone probe) go through
one core.upstream.download: scratch then rename, size cap, retries of a
cut stream, a 5xx or a rate limit. The shared rate limit wait reads
GoatCounter's X-Rate-Limit-Reset, so site insights, download.py,
indexnow and the contributor list use the same retrying client.

list_gitlinks raises TruncatedListingError on a partial tree, read
from one tree walk shared with list_tree (Forgejo paging included).
The Recalbox configs/ listing is paged with the shared page cap.
This commit is contained in:
Abdessamad Derraz committed 2026-11-21 10:30:13 +01:00
1 parent 526d7f09c7
commit 8a8fa71fbc
25 files changed
+708 -266

No files matched your search

+25 -3
View File
@@ -28,11 +28,16 @@ class _Response(io.BytesIO):
return False
def _is_head(url):
# core.upstream.resolve_head lists one commit from the branch.
return "/commits?per_page=1" in url
def _opener(failure):
def urlopen(req, timeout=0):
url = req.full_url
if "/commits/" in url:
return _Response(json.dumps({"sha": "a" * 40}).encode())
if _is_head(url):
return _Response(json.dumps([{"sha": "a" * 40}]).encode())
if "api.github.com" in url:
return _Response(json.dumps(TREE).encode())
if "pcsx2" in url and failure is not None:
@@ -71,10 +76,27 @@ class ManifestErrors(unittest.TestCase):
with mock.patch("urllib.request.urlopen", recording):
Scraper()._fetch_remote_manifests()
reads = [url for url in seen if "/commits/" not in url]
reads = [url for url in seen if not _is_head(url)]
self.assertTrue(reads)
self.assertTrue(all("a" * 40 in url for url in reads), reads)
def test_the_head_is_read_without_a_cache_write(self):
"""The branch head is asked of the forge each scrape and never
written to the profile cache under .cache/upstream."""
seen = []
opener = _opener(None)
def recording(req, timeout=0):
seen.append(req.full_url)
return opener(req, timeout)
with mock.patch("urllib.request.urlopen", recording), \
mock.patch("core.upstream.write_cache") as write:
scraper = Scraper()
self.assertEqual(scraper.revision, "a" * 40)
self.assertTrue(any(_is_head(url) for url in seen), seen)
write.assert_not_called()
def test_a_truncated_tree_stops_the_scrape(self):
TREE["truncated"] = True
self.addCleanup(TREE.pop, "truncated")