Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show status when downloading assets #41

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [UNRELEASED]

* Download status is now displayed while downloading Shinylive assets.

## [0.6.0] - 2024-09-03

Expand Down
29 changes: 28 additions & 1 deletion shinylive/_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re
import shutil
import sys
import time
import urllib.request
from pathlib import Path
from typing import Optional
Expand All @@ -16,6 +17,7 @@ def download_shinylive(
destdir: str | Path | None = None,
version: str = SHINYLIVE_ASSETS_VERSION,
url: Optional[str] = None,
status: bool = True,
) -> None:
if destdir is None:
# Note that this is the cache directory, which is the parent of the assets
Expand All @@ -30,7 +32,32 @@ def download_shinylive(

try:
print(f"Downloading {url}...", file=sys.stderr)
tmp_name, _ = urllib.request.urlretrieve(url)

start_time = time.time()
last_update_time = start_time

def reporthook(count: int, block_size: int, total_size: int):
if not status:
return

nonlocal last_update_time
current_time = time.time()

if current_time - last_update_time < 1:
return

duration = current_time - start_time
progress_size = int(count * block_size)
speed = int(progress_size / (1024 * duration))
percent = min(int(count * block_size * 100 / total_size), 100)
sys.stderr.write(
f"\r{percent}%, {speed} KB/s, {progress_size / (1024 * 1024):.1f}/{total_size / (1024 * 1024):.1f} MB"
)
sys.stderr.flush()
last_update_time = current_time

tmp_name, _ = urllib.request.urlretrieve(url, reporthook=reporthook)
sys.stderr.write("\n")

print(f"Unzipping to {destdir}/", file=sys.stderr)
tar_safe_extractall(tmp_name, destdir)
Expand Down
11 changes: 10 additions & 1 deletion shinylive/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,23 @@ def assets_info(
default=None,
help="URL to download from. If used, this will override --version.",
)
@click.option(
"--status/--no-status",
is_flag=True,
default=True,
help="Enable/disable status output during download.",
)
def download(
version: str,
dir: Optional[str | Path],
url: Optional[str],
status: bool,
) -> None:
if version is None: # pyright: ignore[reportUnnecessaryComparison]
version = SHINYLIVE_ASSETS_VERSION
_assets.download_shinylive(destdir=upgrade_dir(dir), version=version, url=url)
_assets.download_shinylive(
destdir=upgrade_dir(dir), version=version, url=url, status=status
)


cleanup_help = f"Remove all versions of local assets except the currently-used version, {SHINYLIVE_ASSETS_VERSION}."
Expand Down
Loading