Skip to content

Commit 4b62769

Browse files
2bndy5shenxianpeng
andauthored
resolve #18 (#19)
* resolve #18 * Change to block char to `=` on Windows Co-authored-by: shenxianpeng <xianpeng.shen@gmail.com>
1 parent a4f8c90 commit 4b62769

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

clang_tools/util.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
A module containing utility functions.
66
"""
77
import platform
8+
import math
9+
from pathlib import Path
810
import urllib.request
911
from typing import Optional
1012
from urllib.error import HTTPError
13+
from http.client import HTTPResponse
1114

1215

1316
def check_install_os() -> str:
@@ -37,7 +40,26 @@ def download_file(url: str, file_name: str) -> Optional[str]:
3740
:returns: The path to downloaded file if successful, otherwise `None`.
3841
"""
3942
try:
40-
file, _ = urllib.request.urlretrieve(url, file_name)
43+
response: HTTPResponse = urllib.request.urlopen(url)
4144
except (ValueError, HTTPError):
4245
return None
43-
return file
46+
47+
if response.status != 200:
48+
return None
49+
length = response.length
50+
buffer = bytes()
51+
progress_bar = "=" if check_install_os() == "windows" else "█"
52+
while len(buffer) < length:
53+
block_size = int(length / 20)
54+
# show completed
55+
completed = len(buffer) / length
56+
print(" |" + progress_bar * int(completed * 20), end="")
57+
print(" " * math.ceil((1 - completed) * 20), end="|")
58+
print(f"{int(completed * 100)}% (of {length} bytes)", end="\r")
59+
remaining = length - len(buffer)
60+
buffer += response.read(block_size if remaining > block_size else remaining)
61+
response.close()
62+
print(" |" + (progress_bar * 20) + f"| 100% (of {length} bytes)")
63+
file = Path(file_name)
64+
file.write_bytes(buffer)
65+
return file.as_posix()

0 commit comments

Comments
 (0)