mirror of
https://github.com/katanemo/plano.git
synced 2026-04-25 00:36:34 +02:00
show download progress bar instead of curl output
This commit is contained in:
parent
abb89382f7
commit
7e8883cba2
1 changed files with 43 additions and 25 deletions
|
|
@ -2,7 +2,6 @@ import gzip
|
||||||
import os
|
import os
|
||||||
import platform
|
import platform
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
|
||||||
import sys
|
import sys
|
||||||
import tarfile
|
import tarfile
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
@ -47,16 +46,45 @@ def _get_platform_slug():
|
||||||
return slug
|
return slug
|
||||||
|
|
||||||
|
|
||||||
def _download_file(url, dest):
|
def _download_file(url, dest, label=None):
|
||||||
"""Download a file from *url* to *dest* using curl."""
|
"""Download a file from *url* to *dest* with a progress bar."""
|
||||||
|
import urllib.request
|
||||||
|
import urllib.error
|
||||||
|
|
||||||
|
if label is None:
|
||||||
|
label = os.path.basename(dest)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
subprocess.run(
|
response = urllib.request.urlopen(url)
|
||||||
["curl", "-fSL", "-o", dest, url],
|
total = int(response.headers.get("Content-Length", 0))
|
||||||
check=True,
|
downloaded = 0
|
||||||
)
|
block_size = 64 * 1024
|
||||||
except subprocess.CalledProcessError as e:
|
|
||||||
print(f"Error downloading: {e}")
|
with open(dest, "wb") as f:
|
||||||
print(f"URL: {url}")
|
while True:
|
||||||
|
chunk = response.read(block_size)
|
||||||
|
if not chunk:
|
||||||
|
break
|
||||||
|
f.write(chunk)
|
||||||
|
downloaded += len(chunk)
|
||||||
|
if total > 0:
|
||||||
|
pct = downloaded * 100 // total
|
||||||
|
bar_len = 30
|
||||||
|
filled = bar_len * downloaded // total
|
||||||
|
bar = "█" * filled + "░" * (bar_len - filled)
|
||||||
|
mb = downloaded / (1024 * 1024)
|
||||||
|
total_mb = total / (1024 * 1024)
|
||||||
|
print(
|
||||||
|
f"\r {label} {bar} {pct}% ({mb:.1f}/{total_mb:.1f} MB)",
|
||||||
|
end="",
|
||||||
|
flush=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
print() # newline after progress bar
|
||||||
|
|
||||||
|
except urllib.error.URLError as e:
|
||||||
|
print(f"\nError downloading {label}: {e}")
|
||||||
|
print(f" URL: {url}")
|
||||||
print("Please check your internet connection and try again.")
|
print("Please check your internet connection and try again.")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
@ -90,16 +118,11 @@ def ensure_envoy_binary():
|
||||||
|
|
||||||
os.makedirs(PLANO_BIN_DIR, exist_ok=True)
|
os.makedirs(PLANO_BIN_DIR, exist_ok=True)
|
||||||
|
|
||||||
print(f"Downloading Envoy {ENVOY_VERSION} for {slug}...")
|
|
||||||
print(f" URL: {url}")
|
|
||||||
|
|
||||||
with tempfile.NamedTemporaryFile(suffix=".tar.xz", delete=False) as tmp:
|
with tempfile.NamedTemporaryFile(suffix=".tar.xz", delete=False) as tmp:
|
||||||
tmp_path = tmp.name
|
tmp_path = tmp.name
|
||||||
|
|
||||||
try:
|
try:
|
||||||
_download_file(url, tmp_path)
|
_download_file(url, tmp_path, label=f"Envoy {ENVOY_VERSION}")
|
||||||
|
|
||||||
print("Extracting Envoy binary...")
|
|
||||||
with tarfile.open(tmp_path, "r:xz") as tar:
|
with tarfile.open(tmp_path, "r:xz") as tar:
|
||||||
# Find the envoy binary inside the archive
|
# Find the envoy binary inside the archive
|
||||||
envoy_member = None
|
envoy_member = None
|
||||||
|
|
@ -127,7 +150,7 @@ def ensure_envoy_binary():
|
||||||
os.chmod(envoy_path, 0o755)
|
os.chmod(envoy_path, 0o755)
|
||||||
with open(version_path, "w") as f:
|
with open(version_path, "w") as f:
|
||||||
f.write(ENVOY_VERSION)
|
f.write(ENVOY_VERSION)
|
||||||
print(f"Envoy {ENVOY_VERSION} installed at {envoy_path}")
|
log.info(f"Envoy {ENVOY_VERSION} installed at {envoy_path}")
|
||||||
return envoy_path
|
return envoy_path
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
|
|
@ -195,14 +218,11 @@ def ensure_wasm_plugins():
|
||||||
]:
|
]:
|
||||||
gz_name = f"{name}.gz"
|
gz_name = f"{name}.gz"
|
||||||
url = f"{PLANO_RELEASE_BASE_URL}/{version}/{gz_name}"
|
url = f"{PLANO_RELEASE_BASE_URL}/{version}/{gz_name}"
|
||||||
print(f"Downloading {gz_name} ({version})...")
|
|
||||||
print(f" URL: {url}")
|
|
||||||
gz_dest = dest + ".gz"
|
gz_dest = dest + ".gz"
|
||||||
_download_file(url, gz_dest)
|
_download_file(url, gz_dest, label=f"{name} ({version})")
|
||||||
with gzip.open(gz_dest, "rb") as f_in, open(dest, "wb") as f_out:
|
with gzip.open(gz_dest, "rb") as f_in, open(dest, "wb") as f_out:
|
||||||
shutil.copyfileobj(f_in, f_out)
|
shutil.copyfileobj(f_in, f_out)
|
||||||
os.unlink(gz_dest)
|
os.unlink(gz_dest)
|
||||||
print(f" Saved to {dest}")
|
|
||||||
|
|
||||||
with open(version_path, "w") as f:
|
with open(version_path, "w") as f:
|
||||||
f.write(version)
|
f.write(version)
|
||||||
|
|
@ -243,10 +263,8 @@ def ensure_brightstaff_binary():
|
||||||
|
|
||||||
os.makedirs(PLANO_BIN_DIR, exist_ok=True)
|
os.makedirs(PLANO_BIN_DIR, exist_ok=True)
|
||||||
|
|
||||||
print(f"Downloading brightstaff ({version}) for {slug}...")
|
|
||||||
print(f" URL: {url}")
|
|
||||||
gz_path = brightstaff_path + ".gz"
|
gz_path = brightstaff_path + ".gz"
|
||||||
_download_file(url, gz_path)
|
_download_file(url, gz_path, label=f"brightstaff ({version}, {slug})")
|
||||||
with gzip.open(gz_path, "rb") as f_in, open(brightstaff_path, "wb") as f_out:
|
with gzip.open(gz_path, "rb") as f_in, open(brightstaff_path, "wb") as f_out:
|
||||||
shutil.copyfileobj(f_in, f_out)
|
shutil.copyfileobj(f_in, f_out)
|
||||||
os.unlink(gz_path)
|
os.unlink(gz_path)
|
||||||
|
|
@ -254,7 +272,7 @@ def ensure_brightstaff_binary():
|
||||||
os.chmod(brightstaff_path, 0o755)
|
os.chmod(brightstaff_path, 0o755)
|
||||||
with open(version_path, "w") as f:
|
with open(version_path, "w") as f:
|
||||||
f.write(version)
|
f.write(version)
|
||||||
print(f"brightstaff {version} installed at {brightstaff_path}")
|
log.info(f"brightstaff {version} installed at {brightstaff_path}")
|
||||||
return brightstaff_path
|
return brightstaff_path
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue