-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathfetch_release.py
166 lines (127 loc) · 4.8 KB
/
fetch_release.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import os
import shutil
import requests
import rich
from pathlib import Path
import hashlib
import subprocess
import sys
from packaging.version import Version
cache = {}
known_subdirs = {
"linux-64",
"linux-ppc64le",
"linux-aarch64",
"osx-64",
"osx-arm64",
"win-64",
}
def get_all_tags_github():
url = "https://api.github.com/repos/mamba-org/micromamba-releases/tags"
r = requests.get(url, timeout=10)
r.raise_for_status()
tags = [t["name"] for t in r.json()]
return set(tags)
def extract_with_micromamba(archive, outdir):
subprocess.check_call(
["micromamba", "package", "extract", str(archive), str(outdir)]
)
def set_output(name, value):
if os.environ.get("GITHUB_OUTPUT"):
with open(os.environ.get("GITHUB_OUTPUT"), "a") as fh:
print(f"{name}={value}\n", file=fh)
def get_micromamba(version, use_default_version):
url = f"https://api.anaconda.org/release/conda-forge/micromamba/{version}"
existing_tags = get_all_tags_github()
print("Getting Anaconda.org API")
r = requests.get(url, timeout=10)
r.raise_for_status()
rj = r.json()
rich.print(rj)
all_subdirs = set([d["attrs"]["subdir"] for d in rj["distributions"]])
if not known_subdirs.issubset(all_subdirs):
raise ValueError(f"Missing subdirs: {known_subdirs - all_subdirs}")
all_versions = set([d["version"] for d in rj["distributions"]])
assert len(all_versions) == 1
version = all_versions.pop()
v = Version(version)
is_stable = not (v.is_devrelease or v.is_prerelease)
if use_default_version and not is_stable:
print(f"Skipping dev and pre releases version '{version}'")
set_output("MICROMAMBA_NEW_VERSION", "false")
return
all_build = set([d["attrs"]["build_number"] for d in rj["distributions"]])
build = max(all_build)
print(f"Existing versions: {existing_tags}")
if f"{version}-{build}" in existing_tags:
print("Tag already exists, skipping")
set_output("MICROMAMBA_NEW_VERSION", "false")
return
for d in rj["distributions"]:
build_number = d["attrs"]["build_number"]
if build_number != build:
continue
buildplat = d["attrs"]["subdir"]
url = d["download_url"]
dsum = d["sha256"]
dplat = d["attrs"]["subdir"]
# fetch file and extract it
r = requests.get(f"https:{url}", timeout=10)
r.raise_for_status()
path = Path(d["basename"])
if d["basename"].endswith(".tar.bz2"):
ext = ".tar.bz2"
elif d["basename"].endswith(".conda"):
ext = ".conda"
dlloc = Path(f"micromamba-{version}-{build_number}-{dplat}{ext}")
with open(dlloc, "wb") as f:
f.write(r.content)
# verify the sha256
sha256 = hashlib.sha256()
with open(dlloc, "rb") as f:
sha256.update(f.read())
dlsum = sha256.hexdigest()
if dlsum != dsum:
raise Exception(f"checksum mismatch, expected {dsum} but was {dlsum}")
# extract the file
extract_dir = Path(f"micromamba-{version}-{build_number}-{dplat}")
extract_with_micromamba(dlloc, extract_dir)
# move the file to the right place
if dplat != "win-64":
binary = extract_dir / "bin" / "micromamba"
else:
binary = extract_dir / "Library" / "bin" / "micromamba.exe"
outdir = Path("releases")
outdir.mkdir(exist_ok=True)
outfile = outdir / f"micromamba-{dplat}"
shutil.copyfile(binary, outfile)
# On Windows, we need to add the .exe extension explicitly
# so that the file is recognized as an executable.
if dplat == "win-64":
shutil.copyfile(binary, outdir / f"micromamba-{dplat}.exe")
shutil.copyfile(dlloc, outdir / f"micromamba-{dplat}{ext}")
# compute the sha256
sha256 = hashlib.sha256()
with open(outdir / f"micromamba-{dplat}", "rb") as f:
sha256.update(f.read())
with open(outdir / f"micromamba-{dplat}{ext}.sha256", "w") as f:
f.write(sha256.hexdigest())
print(sha256.hexdigest())
shafile = outfile.with_suffix(".sha256")
with open(shafile, "w") as f:
f.write(sha256.hexdigest())
if is_stable:
set_output("MICROMAMBA_NEW_PRERELEASE", "false")
else:
set_output("MICROMAMBA_NEW_PRERELEASE", "true")
if is_stable and v.major == 2:
set_output("MICROMAMBA_LATEST", "true")
else:
set_output("MICROMAMBA_LATEST", "false")
set_output("MICROMAMBA_NEW_VERSION", "true")
set_output("MICROMAMBA_VERSION", f"{version}-{build_number}")
if __name__ == "__main__":
if len(sys.argv) > 1:
get_micromamba(sys.argv[1], False)
else:
get_micromamba("latest", True)