Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.

Commit 85c001e

Browse files
committed
support mirrors
1 parent c3f9c36 commit 85c001e

File tree

5 files changed

+67
-36
lines changed

5 files changed

+67
-36
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ jobs:
1919
- name: asdf_plugin_test
2020
uses: asdf-vm/actions/plugin-test@v4
2121
with:
22-
command: <TOOL CHECK>
22+
command: zig env

bin/download

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ mkdir -p "$ASDF_DOWNLOAD_PATH"
1313
release_file="${ASDF_DOWNLOAD_PATH}/${TOOL_NAME}-${ASDF_INSTALL_VERSION}.tar.xz"
1414

1515
# Download tar.gz file to the download directory
16-
"${plugin_dir}/lib/utils.py" "${ASDF_INSTALL_VERSION}" "${release_file}"
16+
"${plugin_dir}/lib/utils.py" download "${ASDF_INSTALL_VERSION}" "${release_file}"
1717

1818
# Extract contents of tar.gz file into the download directory
1919
tar -xzf "$release_file" -C "$ASDF_DOWNLOAD_PATH" --strip-components=1 || fail "Could not extract $release_file"
2020

21-
# Remove the tar.gz file since we don't need to keep it
21+
# Remove the tar file since we don't need to keep it
2222
rm "$release_file"

bin/install

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ install_version() {
2121
mkdir -p "$install_path"
2222
cp -r "$ASDF_DOWNLOAD_PATH"/* "$install_path"
2323

24-
# TODO: Assert <YOUR TOOL> executable exists.
2524
local tool_cmd
2625
tool_cmd="$(echo "$TOOL_TEST" | cut -d' ' -f1)"
2726
test -x "$install_path/$tool_cmd" || fail "Expected $install_path/$tool_cmd to be executable."

lib/utils.py

Lines changed: 63 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,91 +2,123 @@
22
# coding: utf-8
33

44
import os
5+
import random
56
import platform
67
import sys
78
import urllib.request
89
import json
910
import hashlib
1011

11-
INDEX_URL = os.getenv('ASDF_ZIG_INDEX_URL', 'https://ziglang.org/download/index.json')
12-
HTTP_TIMEOUT = int(os.getenv('ASDF_ZIG_HTTP_TIMEOUT', '30'))
12+
INDEX_URL = os.getenv("ASDF_ZIG_INDEX_URL", "https://ziglang.org/download/index.json")
13+
HTTP_TIMEOUT = int(os.getenv("ASDF_ZIG_HTTP_TIMEOUT", "30"))
1314

15+
# https://github.com/mlugg/setup-zig/blob/main/mirrors.json
16+
# If any of these mirrors are down, please open an issue!
17+
MIRRORS = [
18+
"https://pkg.machengine.org/zig",
19+
"https://zigmirror.hryx.net/zig",
20+
"https://zig.linus.dev/zig",
21+
"https://fs.liujiacai.net/zigbuilds",
22+
]
1423
OS_MAPPING = {
15-
'darwin': 'macos',
24+
"darwin": "macos",
1625
}
1726
ARCH_MAPPING = {
18-
'i386': 'x86',
19-
'i686': 'x86',
20-
'amd64': 'x86_64',
21-
'arm64':'aarch64',
27+
"i386": "x86",
28+
"i686": "x86",
29+
"amd64": "x86_64",
30+
"arm64": "aarch64",
2231
}
2332

33+
2434
def fetch_index():
2535
with urllib.request.urlopen(INDEX_URL, timeout=HTTP_TIMEOUT) as response:
2636
if response.getcode() == 200:
27-
body = response.read().decode('utf-8')
37+
body = response.read().decode("utf-8")
2838
return json.loads(body)
2939
else:
3040
raise Exception(f"Fetch index.json error: {response.getcode()}")
3141

3242

3343
def all_versions():
3444
index = fetch_index()
35-
versions = [k for k in index.keys() if k != 'master']
36-
versions.sort(key=lambda v: tuple(map(int, v.split('.'))))
45+
versions = [k for k in index.keys() if k != "master"]
46+
versions.sort(key=lambda v: tuple(map(int, v.split("."))))
3747
return versions
3848

3949

40-
def download_tarball(url, out_file, expected_shasum):
50+
def download_and_check(url, out_file, expected_shasum):
51+
print(f"Download tarball from {url} to {out_file}...")
4152
chunk_size = 8192 # 8KB chunks
4253
sha256_hash = hashlib.sha256()
4354
with urllib.request.urlopen(url, timeout=HTTP_TIMEOUT) as response:
4455
if response.getcode() != 200:
4556
raise Exception(f"Fetch index.json error: {response.getcode()}")
4657

47-
with open(out_file, 'wb') as f:
48-
while True:
49-
chunk = response.read(chunk_size)
50-
if not chunk:
51-
break # eof
52-
sha256_hash.update(chunk)
53-
f.write(chunk)
58+
with open(out_file, "wb") as f:
59+
while True:
60+
chunk = response.read(chunk_size)
61+
if not chunk:
62+
break # eof
63+
sha256_hash.update(chunk)
64+
f.write(chunk)
5465

5566
actual = sha256_hash.hexdigest()
5667
if actual != expected_shasum:
57-
raise Exception(f"Shasum not match, expected:{expected_shasum}, actual:{actual}")
68+
raise Exception(
69+
f"Shasum not match, expected:{expected_shasum}, actual:{actual}"
70+
)
71+
72+
73+
def download_tarball(url, out_file, expected_shasum):
74+
filename = url.split("/")[-1]
75+
random.shuffle(MIRRORS)
76+
urls = [f"{mirror}/{filename}" for mirror in MIRRORS]
77+
78+
for url in urls:
79+
try:
80+
download_and_check(url, out_file, expected_shasum)
81+
return
82+
except Exception as e:
83+
print(f"Current mirror failed, try next. err:{e}")
84+
85+
# All mirror failed, fallback to original url
86+
download_and_check(url, out_file, expected_shasum)
87+
5888

5989
def download(version, out_file):
6090
index = fetch_index()
61-
if version in index is False:
62-
raise Exception(f'There is no such version: {version}')
91+
if version not in index:
92+
raise Exception(f"There is no such version: {version}")
6393

6494
links = index[version]
6595
os_name = platform.system().lower()
6696
arch = platform.machine().lower()
6797
os_name = OS_MAPPING.get(os_name, os_name)
6898
arch = ARCH_MAPPING.get(arch, arch)
69-
link_key = f'{arch}_{os_name}'
70-
if link_key in links is False:
71-
raise Exception(f'No tarball link for {link_key}')
99+
link_key = f"{arch}-{os_name}"
100+
if link_key not in links:
101+
raise Exception(f"No tarball link for {link_key}")
102+
103+
tarball_url = links[link_key]["tarball"]
104+
tarball_shasum = links[link_key]["shasum"]
105+
download_tarball(tarball_url, out_file, tarball_shasum)
72106

73-
tarball_url = link_key[link_key]['tarball']
74-
tarball_shasum = link_key[link_key]['shasum']
75-
download_tarball(url ,out_file, tarball_shasum)
76107

77108
def main(args):
78-
command = args[0] if args else 'default'
79-
if command == 'all-versions':
109+
command = args[0] if args else "default"
110+
if command == "all-versions":
80111
versions = all_versions()
81112
print(" ".join(versions))
82-
elif command == 'latest-version':
113+
elif command == "latest-version":
83114
versions = all_versions()
84115
print(versions[-1])
85-
elif command == 'download':
116+
elif command == "download":
86117
download(args[1], args[2])
87118
else:
88119
print(f"Unknown command: {command}")
89120
sys.exit(1)
90121

122+
91123
if __name__ == "__main__":
92124
main(sys.argv[1:])

scripts/lint.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
shellcheck --shell=bash --external-sources \
44
bin/* --source-path=template/lib/ \
5-
lib/* \
5+
lib/utils.bash \
66
scripts/*
77

88
shfmt --language-dialect bash --diff \

0 commit comments

Comments
 (0)