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

Commit 5f3dedc

Browse files
committed
chore: add download progress bar
1 parent c28cc71 commit 5f3dedc

File tree

4 files changed

+24
-26
lines changed

4 files changed

+24
-26
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2025 Jiacai Liu <dev@liujiacai.net>
3+
Copyright (c) 2025 ZigCC contributors
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,24 @@
66

77
</div>
88

9-
# Contents
10-
11-
- [Dependencies](#dependencies)
12-
- [Install](#install)
13-
- [Contributing](#contributing)
14-
- [License](#license)
15-
169
# Dependencies
1710

1811
- `bash`, `python3`, `tar`, and [POSIX utilities](https://pubs.opengroup.org/onlinepubs/9699919799/idx/utilities.html).
1912
- asdf 0.16+
2013

2114
# Install
2215

23-
Plugin:
16+
First add asdf-zig as plugin:
2417

2518
```shell
2619
asdf plugin add zig https://github.com/zigcc/asdf-zig.git
2720
```
2821

29-
asdf-zig:
22+
Then use `asdf-zig` to install zig:
3023

3124
```shell
3225
# Show all installable versions
33-
asdf list-all zig
26+
asdf list all zig
3427

3528
# Install specific version
3629
asdf install zig latest

lib/utils.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import platform
77
import sys
88
import urllib.request
9+
from urllib.parse import urljoin
910
import json
1011
import hashlib
12+
import logging
1113

1214
INDEX_URL = os.getenv("ASDF_ZIG_INDEX_URL", "https://ziglang.org/download/index.json")
1315
HTTP_TIMEOUT = int(os.getenv("ASDF_ZIG_HTTP_TIMEOUT", "30"))
@@ -46,17 +48,21 @@ def all_versions(index=fetch_index()):
4648
return versions
4749

4850

49-
def download_and_check(url, out_file, expected_shasum):
50-
print(f"Download tarball from {url} to {out_file}...")
51-
chunk_size = 8192 # 8KB chunks
51+
def download_and_check(url, out_file, expected_shasum, total_size):
52+
logging.info(f"Begin download tarball({total_size}) from {url} to {out_file}...")
53+
chunk_size = 1024 * 1024 # 1M chunks
5254
sha256_hash = hashlib.sha256()
5355
with urllib.request.urlopen(url, timeout=HTTP_TIMEOUT) as response:
5456
if response.getcode() != 200:
5557
raise Exception(f"Fetch index.json error: {response.getcode()}")
5658

59+
read_size = 0
5760
with open(out_file, "wb") as f:
5861
while True:
5962
chunk = response.read(chunk_size)
63+
read_size += len(chunk)
64+
progress_percentage = (read_size / total_size) * 100 if total_size > 0 else 0
65+
logging.info(f'Downloaded: {read_size}/{total_size} bytes ({progress_percentage:.2f}%)')
6066
if not chunk:
6167
break # eof
6268
sha256_hash.update(chunk)
@@ -69,20 +75,21 @@ def download_and_check(url, out_file, expected_shasum):
6975
)
7076

7177

72-
def download_tarball(url, out_file, expected_shasum):
78+
def download_tarball(url, out_file, expected_shasum, total_size):
7379
filename = url.split("/")[-1]
7480
random.shuffle(MIRRORS)
75-
urls = [f"{mirror}/{filename}" for mirror in MIRRORS]
7681

77-
for url in urls:
82+
for mirror in MIRRORS:
7883
try:
79-
download_and_check(url, out_file, expected_shasum)
84+
# Ensure base_url has a trailing slash
85+
mirror = mirror if mirror.endswith('/') else mirror + '/'
86+
download_and_check(urljoin(mirror, filename), out_file, expected_shasum, total_size)
8087
return
8188
except Exception as e:
82-
print(f"Current mirror failed, try next. err:{e}")
89+
logging.error(f"Current mirror failed, try next. err:{e}")
8390

8491
# All mirrors failed, fallback to original url
85-
download_and_check(url, out_file, expected_shasum)
92+
download_and_check(url, out_file, expected_shasum, total_size)
8693

8794

8895
def download(version, out_file):
@@ -101,7 +108,8 @@ def download(version, out_file):
101108

102109
tarball_url = links[link_key]["tarball"]
103110
tarball_shasum = links[link_key]["shasum"]
104-
download_tarball(tarball_url, out_file, tarball_shasum)
111+
tarball_size = int(links[link_key]["size"])
112+
download_tarball(tarball_url, out_file, tarball_shasum, tarball_size)
105113

106114

107115
def main(args):
@@ -115,9 +123,10 @@ def main(args):
115123
elif command == "download":
116124
download(args[1], args[2])
117125
else:
118-
print(f"Unknown command: {command}")
126+
logging.error(f"Unknown command: {command}")
119127
sys.exit(1)
120128

121129

122130
if __name__ == "__main__":
131+
logging.basicConfig(level=logging.INFO, format='[%(asctime)s] %(message)s')
123132
main(sys.argv[1:])

scripts/format.bash

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)