From f6860fdef20f9237c2b0dcd506840b3bc56060b8 Mon Sep 17 00:00:00 2001 From: imreddyTeja Date: Tue, 19 Nov 2024 11:28:45 -0800 Subject: [PATCH] Add download callback to ClimaArtifactsHelper The callback handles the condition when total size is unknown. Total size is printed as MB as an int if it is less than 1 GB, and GB with 2 decimal places otherwise. The same applies to dowloaded size. --- .../src/ClimaArtifactsHelper.jl | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/ClimaArtifactsHelper.jl/src/ClimaArtifactsHelper.jl b/ClimaArtifactsHelper.jl/src/ClimaArtifactsHelper.jl index 837fd70..6b46474 100644 --- a/ClimaArtifactsHelper.jl/src/ClimaArtifactsHelper.jl +++ b/ClimaArtifactsHelper.jl/src/ClimaArtifactsHelper.jl @@ -7,9 +7,10 @@ using Pkg.Artifacts import SHA: sha1 import Downloads: download -export create_artifact_guided, create_artifact_guided_one_file +export create_artifact_guided, create_artifact_guided_one_file, download_progress_callback const MB = 1024 * 1024 +const GB = 1024 * MB # Mark files larger than this as "undownloadable". This will prevent them from # being mirrored by the Julia servers @@ -214,4 +215,36 @@ function create_artifact_guided_one_file( create_artifact_guided(output_dir; artifact_name, append) end +""" + download_size_string(bytes::Integer) + +Return a human-readable string for the given number of bytes. +""" +function download_size_string(bytes::Integer) + if bytes >= GB + size = round(bytes/GB, digits=2) + unit = "GB" + else + size = Int32(div(bytes, MB)) + unit = "MB" + end + return "$size $unit" +end + +""" + download_progress_callback(total::Integer, now::Integer) + +Print the download progress to the terminal. Designed to be used as a callback +for Downloads.jl. +""" +function download_progress_callback(total::Integer, now::Integer) + if total == 0 + print("Downloaded $(download_size_string(total))\r") + else + print( + "Downloaded $(download_size_string(now)) out of $(download_size_string(total)): $(div(now * 100, total))% \r", + ) + end +end + end