copapy/tools/get_binaries.py

64 lines
1.8 KiB
Python
Raw Permalink Normal View History

from typing import Any
import os
2025-11-03 21:39:40 +00:00
import json
from urllib.request import urlopen, Request
OWNER = "Nonannet"
REPO = "copapy"
def fetch_json(url: str) -> Any:
2025-11-03 21:39:40 +00:00
req = Request(url, headers={"User-Agent": "Python"})
with urlopen(req, timeout=10) as resp:
assert resp.status == 200
return json.load(resp)
def download_file(url: str, dest_path: str) -> None:
2025-11-03 21:39:40 +00:00
req = Request(url, headers={"User-Agent": "Python"})
with urlopen(req, timeout=30) as resp, open(dest_path, "wb") as f:
f.write(resp.read())
def main() -> None:
# Get all releases (includes prereleases)
api_url = f"https://api.github.com/repos/{OWNER}/{REPO}/releases"
2025-11-03 21:39:40 +00:00
releases = fetch_json(api_url)
assert releases, "No releases found."
2025-12-08 13:01:22 +00:00
assets: list[Any] = []
for release in releases:
tag = release["tag_name"]
print(f"Found latest release: {tag}")
2025-12-08 13:01:22 +00:00
assets = release.get("assets", [])
if assets:
break
print(f"No assets found for release {tag}.")
assert assets, "No assets found."
for asset in assets:
url = asset["browser_download_url"]
name: str = asset["name"]
if name.startswith('stencils_'):
dest = 'src/copapy/obj'
elif name.startswith('musl_'):
dest = 'build/musl'
elif name == 'coparun.exe' and os.name == 'nt':
2025-11-12 23:29:48 +00:00
dest = 'build/runner'
elif name == 'coparun' and os.name == 'posix':
2025-11-12 23:29:48 +00:00
dest = 'build/runner'
elif name.startswith('coparun-'):
2025-11-12 23:29:48 +00:00
dest = 'build/runner'
else:
dest = ''
if dest:
os.makedirs(dest, exist_ok=True)
print(f"- Downloading {name} ...")
2025-11-03 21:39:40 +00:00
download_file(url, os.path.join(dest, name))
print(f"- Saved {name} to {dest}")
if __name__ == "__main__":
main()