feat: fetch ungated update-swarm artifacts into staging
This commit is contained in:
1 parent
003b2ab111
commit
dd62312f87
2 files changed
+108
No files matched your search
@@ -0,0 +1,59 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
import hashlib, os, re, urllib.request, zipfile
|
||||||
|
from update_swarm.census import _from_text, CHANGELOG_NAMES
|
||||||
|
from update_swarm.hosts import classify_url
|
||||||
|
from update_swarm.versions import compare_versions
|
||||||
|
|
||||||
|
UA = "Mozilla/5.0 (homelab personal archival)"
|
||||||
|
MF = re.compile(r"https://download\d+\.mediafire\.com/[^\"'\s<>]+")
|
||||||
|
|
||||||
|
class GatedHostError(ValueError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def resolve_mediafire(html: str) -> str | None:
|
||||||
|
m = MF.search(html or "")
|
||||||
|
return m.group(0) if m else None
|
||||||
|
|
||||||
|
def _open(url: str, opener=None):
|
||||||
|
req = urllib.request.Request(url, headers={"User-Agent": UA})
|
||||||
|
if opener:
|
||||||
|
return opener(req)
|
||||||
|
return urllib.request.urlopen(req)
|
||||||
|
|
||||||
|
def download(url: str, dest_path: str, opener=None) -> dict:
|
||||||
|
host, gate = classify_url(url)
|
||||||
|
if gate == "gated":
|
||||||
|
raise GatedHostError(f"{host} is gated: {url}")
|
||||||
|
if host == "mega":
|
||||||
|
raise NotImplementedError("mega downloads use megadl in the CLI")
|
||||||
|
fetch_url = url
|
||||||
|
if host == "mediafire" and "/file/" in url:
|
||||||
|
with _open(url, opener) as resp:
|
||||||
|
html = resp.read().decode("utf-8", "ignore")
|
||||||
|
direct = resolve_mediafire(html)
|
||||||
|
if not direct:
|
||||||
|
raise ValueError("no mediafire direct link")
|
||||||
|
fetch_url = direct
|
||||||
|
os.makedirs(os.path.dirname(dest_path) or ".", exist_ok=True)
|
||||||
|
h = hashlib.sha256()
|
||||||
|
size = 0
|
||||||
|
with _open(fetch_url, opener) as resp, open(dest_path, "wb") as out:
|
||||||
|
while True:
|
||||||
|
chunk = resp.read(1024 * 1024)
|
||||||
|
if not chunk:
|
||||||
|
break
|
||||||
|
out.write(chunk)
|
||||||
|
h.update(chunk)
|
||||||
|
size += len(chunk)
|
||||||
|
return {"path": dest_path, "sha256": h.hexdigest(), "size": size, "url": fetch_url}
|
||||||
|
|
||||||
|
def english_mismatch(archive_path: str, expected_version: str) -> bool:
|
||||||
|
if not zipfile.is_zipfile(archive_path):
|
||||||
|
return False
|
||||||
|
with zipfile.ZipFile(archive_path) as z:
|
||||||
|
for n in z.namelist():
|
||||||
|
if os.path.basename(n).lower() in CHANGELOG_NAMES:
|
||||||
|
found = _from_text(z.read(n).decode("utf-8", "ignore"))
|
||||||
|
if found and compare_versions(found, expected_version) != "eq":
|
||||||
|
return True
|
||||||
|
return False
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
import os, sys, tempfile, unittest, zipfile
|
||||||
|
from unittest.mock import patch
|
||||||
|
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
||||||
|
sys.path.insert(0, os.path.join(ROOT, "scripts"))
|
||||||
|
from update_swarm.fetch import GatedHostError, download, english_mismatch, resolve_mediafire
|
||||||
|
|
||||||
|
class TestFetch(unittest.TestCase):
|
||||||
|
def test_mediafire_regex(self):
|
||||||
|
html = '<a href="https://download123.mediafire.com/abc/file.zip">dl</a>'
|
||||||
|
self.assertEqual(resolve_mediafire(html), "https://download123.mediafire.com/abc/file.zip")
|
||||||
|
|
||||||
|
def test_refuses_gated(self):
|
||||||
|
with tempfile.TemporaryDirectory() as td:
|
||||||
|
with self.assertRaises(GatedHostError):
|
||||||
|
download("https://foo.itch.io/x", os.path.join(td, "x.zip"))
|
||||||
|
|
||||||
|
def test_download_writes_bytes(self):
|
||||||
|
class Resp:
|
||||||
|
def __init__(self):
|
||||||
|
self._done = False
|
||||||
|
def __enter__(self): return self
|
||||||
|
def __exit__(self, *a): pass
|
||||||
|
def read(self, n=-1):
|
||||||
|
if self._done:
|
||||||
|
return b""
|
||||||
|
self._done = True
|
||||||
|
return b"rombytes"
|
||||||
|
with tempfile.TemporaryDirectory() as td:
|
||||||
|
dest = os.path.join(td, "g.gba")
|
||||||
|
with patch("update_swarm.fetch.urllib.request.urlopen", return_value=Resp()):
|
||||||
|
rec = download("https://github.com/a/b/releases/download/v1/g.gba", dest)
|
||||||
|
self.assertTrue(os.path.isfile(dest))
|
||||||
|
self.assertEqual(rec["size"], 8)
|
||||||
|
|
||||||
|
def test_mega_not_implemented(self):
|
||||||
|
with tempfile.TemporaryDirectory() as td:
|
||||||
|
with self.assertRaises(NotImplementedError):
|
||||||
|
download("https://mega.nz/file/abc#key", os.path.join(td, "x"))
|
||||||
|
|
||||||
|
def test_changelog_mismatch(self):
|
||||||
|
with tempfile.TemporaryDirectory() as td:
|
||||||
|
zpath = os.path.join(td, "g.zip")
|
||||||
|
with zipfile.ZipFile(zpath, "w") as z:
|
||||||
|
z.writestr("changelog.txt", "Current Version: 2.16\n")
|
||||||
|
self.assertTrue(english_mismatch(zpath, "2.13"))
|
||||||
|
self.assertFalse(english_mismatch(zpath, "v2.16 (ES)"))
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Reference in new issue
Block a user