Files
ItxiaohuiContentGuard/scripts/fetch-servers.py
T

118 lines
5.7 KiB
Python

"""Download pinned-by-evidence upstream builds for isolated compatibility runs."""
import argparse
import hashlib
import json
import os
import subprocess
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from urllib.parse import urlsplit
ROOT = Path(__file__).resolve().parents[1]
AGENT = os.environ.get("ICG_DOWNLOAD_USER_AGENT", "ItxiaohuiContentGuard-tests/0.1 (https://www.minecraft.net/)")
VERSIONS = ("1.21.1", "1.21.4", "1.21.8", "1.21.11")
CURL = "curl.exe" if os.name == "nt" else "curl"
def fetch(url, destination=None, accept=None):
command = [CURL, "-fsSL", "--retry", "2",
"--connect-timeout", "8", "--max-time", "150", "-A", AGENT, url]
if accept:
command.extend(["-H", f"Accept: {accept}"])
if destination:
subprocess.run(command + ["-o", str(destination)], check=True, capture_output=True)
else:
return json.loads(subprocess.check_output(command, stderr=subprocess.PIPE))
def leaves_release_asset(url, version):
headers = subprocess.check_output(
[CURL, "-fsS", "--connect-timeout", "8", "--max-time", "15", "-A", AGENT,
"-D", "-", "-o", os.devnull, url], stderr=subprocess.PIPE, text=True)
redirect = next(line.split(":", 1)[1].strip() for line in headers.splitlines()
if line.lower().startswith("location:"))
release = urlsplit(redirect)
parts = release.path.split("/")
if release.hostname != "github.com" or "/".join(parts[1:5]).lower() != "leavesmc/leaves/releases/download":
raise ValueError("Leaves download did not redirect to its upstream GitHub releases")
info = fetch(f"https://api.github.com/repos/LeavesMC/Leaves/releases/tags/{parts[5]}")
return next(item for item in info["assets"] if item["name"] == f"leaves-{version}.jar")
def build_info(core, version, allow_prerelease):
if core in ("paper", "folia"):
api = f"https://fill.papermc.io/v3/projects/{core}/versions/{version}/builds"
builds = fetch(api)
stable = [entry for entry in builds if entry["channel"] == "STABLE"]
eligible = stable or (builds if allow_prerelease else [])
if not eligible:
raise ValueError("No stable build in upstream channel")
build = max(eligible, key=lambda entry: entry["id"])
asset = build["downloads"]["server:default"]
return build["id"], build["channel"], asset["url"], asset["checksums"]["sha256"]
if core == "purpur":
api = f"https://api.purpurmc.org/v2/purpur/{version}"
build = fetch(api)["builds"]["latest"]
details = fetch(f"{api}/{build}")
return build, details.get("result", "unknown"), f"{api}/{build}/download", None
api = f"https://api.leavesmc.org/v2/projects/leaves/versions/{version}/builds"
builds = fetch(api)["builds"]
stable = [entry for entry in builds if entry["channel"] == "default"]
eligible = stable or (builds if allow_prerelease else [])
if not eligible:
raise ValueError("No default-channel build in upstream channel")
build = max(eligible, key=lambda entry: entry["build"])
asset = build["downloads"]["application"]
return (build["build"], build["channel"], f'{api}/{build["build"]}/downloads/{asset["name"]}', asset["sha256"])
def acquire(core, version, should_download, allow_prerelease):
destination = ROOT / ".test-servers" / core / version
try:
build, channel, url, checksum = build_info(core, version, allow_prerelease)
status = {"core": core, "version": version, "build": build, "channel": channel,
"url": url, "upstream_sha256": checksum}
if should_download:
destination.mkdir(parents=True, exist_ok=True)
partial = destination / "server.download"
try:
fetch(url, partial)
except subprocess.CalledProcessError:
if core != "leaves":
raise
asset = leaves_release_asset(url, version)
fetch(asset["url"] + "?download=1", partial, "application/octet-stream")
status["url"] = asset["browser_download_url"]
with partial.open("rb") as stream:
actual = hashlib.file_digest(stream, "sha256").hexdigest()
if checksum and actual != checksum:
raise ValueError(f"SHA-256 mismatch: {actual}")
partial.replace(destination / "server.jar")
status["sha256"] = actual
(destination / "source.json").write_text(json.dumps(status, indent=2), encoding="utf-8")
return status
except subprocess.CalledProcessError as error:
message = error.stderr if isinstance(error.stderr, str) else error.stderr.decode()
return {"core": core, "version": version, "error": message.strip()}
except (OSError, KeyError, IndexError, ValueError, StopIteration) as error:
return {"core": core, "version": version, "error": str(error)}
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--core", nargs="+", default=["paper", "purpur", "folia", "leaves"],
choices=["paper", "purpur", "folia", "leaves"])
parser.add_argument("--version", nargs="+", default=VERSIONS, choices=VERSIONS)
parser.add_argument("--download", action="store_true")
parser.add_argument("--allow-prerelease", action="store_true")
args = parser.parse_args()
with ThreadPoolExecutor(max_workers=8) as pool:
results = [pool.submit(acquire, core, version, args.download, args.allow_prerelease)
for core in args.core for version in args.version]
print(json.dumps([result.result() for result in results], ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()