Debian Codegraph @

.ci/download_artifacts.py

raw
1 import requests
2 import json
3 import shutil
4 from urllib.request import urlopen
5 from pathlib import Path
6 import zipfile
7 import tempfile
8
9 organization = "scikit-hep"
10 project = "iminuit"
11
12
13 def get(url):
14 return json.loads(requests.get(url).content)
15
16
17 def get_latest_successful_build():
18 # get list of all builds
19 artifact = get(
20 f"https://api.github.com/repos/{organization}/{project}/actions/artifacts"
21 )["artifacts"][0]
22 return artifact["id"], artifact["updated_at"], artifact["archive_download_url"]
23
24
25 id, date, url = get_latest_successful_build()
26
27 # TODO needs authentication to work
28 print(f"Downloading artifacts for {id} {date}")
29
30 dist = Path("dist")
31 if dist.exists():
32 if any(dist.iterdir()):
33 raise SystemExit("Error: ./dist directory is not empty; please delete content")
34 else:
35 dist.mkdir()
36
37 print(f"Downloading {id}...")
38 with tempfile.TemporaryFile(mode="w+b") as tmp:
39 with urlopen(url) as src:
40 shutil.copyfileobj(src, tmp)
41 with zipfile.ZipFile(tmp) as zip:
42 for zip_name in zip.namelist():
43 fn = dist / Path(zip_name).name
44 print("... extracting", fn)
45 with zip.open(zip_name) as src, open(fn, "wb") as dst:
46 shutil.copyfileobj(src, dst)