Skip to content

Commit

Permalink
Add download callback to ClimaArtifactsHelper
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
imreddyTeja committed Nov 19, 2024
1 parent bbef42b commit f6860fd
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion ClimaArtifactsHelper.jl/src/ClimaArtifactsHelper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit f6860fd

Please sign in to comment.