Skip to content

Commit 4c4dd09

Browse files
authored
Merge pull request #745 from stan-dev/verbose-download
Add verbosity to download output and errors
2 parents b96a62a + acd3609 commit 4c4dd09

File tree

2 files changed

+88
-35
lines changed

2 files changed

+88
-35
lines changed

R/install.R

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ install_cmdstan <- function(dir = NULL,
141141
dir_cmdstan <- file.path(dir, cmdstan_ver)
142142
dest_file <- file.path(dir, tar_gz_file)
143143
} else {
144-
ver <- latest_released_version()
144+
ver <- latest_released_version(quiet = quiet)
145145
message("* Latest CmdStan release is v", ver)
146146
cmdstan_ver <- paste0("cmdstan-", ver, cmdstan_arch_suffix(ver))
147147
tar_gz_file <- paste0(cmdstan_ver, ".tar.gz")
@@ -154,15 +154,16 @@ install_cmdstan <- function(dir = NULL,
154154
if (!check_install_dir(dir_cmdstan, overwrite)) {
155155
return(invisible(NULL))
156156
}
157-
tar_downloaded <- download_with_retries(download_url, dest_file)
158-
if (!tar_downloaded) {
157+
tar_downloaded <- download_with_retries(download_url, dest_file, quiet = quiet)
158+
if (inherits(tar_downloaded, "try-error")) {
159+
error_msg <- paste("Download of CmdStan failed with error:",
160+
attr(tar_downloaded, "condition")$message)
159161
if (!is.null(version)) {
160-
stop("Download of CmdStan failed. Please check if the supplied version number is valid.", call. = FALSE)
162+
error_msg <- paste0(error_msg, "\nPlease check if the supplied version number is valid.")
163+
} else if (!is.null(release_url)) {
164+
error_msg <- paste0(error_msg, "\nPlease check if the supplied release URL is valid.")
161165
}
162-
if (!is.null(release_url)) {
163-
stop("Download of CmdStan failed. Please check if the supplied release URL is valid.", call. = FALSE)
164-
}
165-
stop("Download of CmdStan failed. Please try again.", call. = FALSE)
166+
stop(error_msg, call. = FALSE)
166167
}
167168
message("* Download complete")
168169
message("* Unpacking archive...")
@@ -360,45 +361,48 @@ github_download_url <- function(version_number) {
360361
}
361362

362363
# get version number of latest release
363-
latest_released_version <- function() {
364+
latest_released_version <- function(quiet=TRUE) {
364365
dest_file <- tempfile(pattern = "releases-", fileext = ".json")
365366
download_url <- "https://api.github.com/repos/stan-dev/cmdstan/releases/latest"
366-
release_list_downloaded <- download_with_retries(download_url, dest_file)
367-
if (!release_list_downloaded) {
368-
stop("GitHub download of release list failed.", call. = FALSE)
367+
release_list_downloaded <- download_with_retries(download_url, dest_file, quiet = quiet)
368+
if (inherits(release_list_downloaded, "try-error")) {
369+
stop("GitHub download of release list failed with error: ",
370+
attr(release_list_downloaded, "condition")$message,
371+
call. = FALSE)
369372
}
370373
release <- jsonlite::read_json(dest_file)
371374
sub("v", "", release$tag_name)
372375
}
373376

377+
try_download <- function(download_url, destination_file,
378+
quiet = TRUE) {
379+
download_status <- try(
380+
suppressWarnings(
381+
utils::download.file(url = download_url,
382+
destfile = destination_file,
383+
quiet = quiet,
384+
headers = github_auth_token())
385+
),
386+
silent = TRUE
387+
)
388+
download_status
389+
}
390+
374391
# download with retries and pauses
375392
download_with_retries <- function(download_url,
376393
destination_file,
377394
retries = 5,
378395
pause_sec = 5,
379396
quiet = TRUE) {
380-
381-
download_rc <- 1
382-
while (retries > 0 && download_rc != 0) {
383-
try(
384-
suppressWarnings(
385-
download_rc <- utils::download.file(url = download_url,
386-
destfile = destination_file,
387-
quiet = quiet,
388-
headers = github_auth_token())
389-
),
390-
silent = TRUE
391-
)
392-
if (download_rc != 0) {
393-
Sys.sleep(pause_sec)
394-
}
395-
retries <- retries - 1
396-
}
397-
if (download_rc == 0) {
398-
TRUE
399-
} else {
400-
FALSE
397+
download_rc <- try_download(download_url, destination_file,
398+
quiet = quiet)
399+
num_retries <- 0
400+
while (num_retries < retries && inherits(download_rc, "try-error")) {
401+
Sys.sleep(pause_sec)
402+
num_retries <- num_retries + 1
403+
download_rc <- try_download(download_url, destination_file, quiet = quiet)
401404
}
405+
download_rc
402406
}
403407

404408
build_cmdstan <- function(dir,

tests/testthat/test-install.R

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ test_that("install_cmdstan() errors if it times out", {
7575
test_that("install_cmdstan() errors if invalid version or URL", {
7676
expect_error(
7777
install_cmdstan(version = "2.23.2", wsl = os_is_wsl()),
78-
"Download of CmdStan failed. Please check if the supplied version number is valid."
78+
"Download of CmdStan failed with error: cannot open URL 'https://github.com/stan-dev/cmdstan/releases/download/v2.23.2/cmdstan-2.23.2.tar.gz'\nPlease check if the supplied version number is valid."
7979
)
8080
expect_error(
8181
install_cmdstan(release_url = "https://github.com/stan-dev/cmdstan/releases/download/v2.23.2/cmdstan-2.23.2.tar.gz",
8282
wsl = os_is_wsl()),
83-
"Download of CmdStan failed. Please check if the supplied release URL is valid."
83+
"Download of CmdStan failed with error: cannot open URL 'https://github.com/stan-dev/cmdstan/releases/download/v2.23.2/cmdstan-2.23.2.tar.gz'\nPlease check if the supplied release URL is valid."
8484
)
8585
expect_error(
8686
install_cmdstan(release_url = "https://github.com/stan-dev/cmdstan/releases/tag/v2.24.0", wsl = os_is_wsl()),
@@ -198,3 +198,52 @@ test_that("github_download_url constructs correct url", {
198198
)
199199
})
200200

201+
test_that("Downloads respect quiet argument", {
202+
if (getRversion() < '3.5.0') {
203+
dir <- tempdir()
204+
} else {
205+
dir <- tempdir(check = TRUE)
206+
}
207+
version <- latest_released_version()
208+
209+
ver_msg <- "trying URL 'https://api.github.com/repos/stan-dev/cmdstan/releases/latest'"
210+
download_msg <- paste0("trying URL 'https://github.com/stan-dev/cmdstan/releases/download/v",
211+
version, "/cmdstan-", version, ".tar.gz'")
212+
213+
# expect_message has trouble capturing the messages from download.file
214+
# so handle manually
215+
install_normal <- suppressWarnings(
216+
capture.output(install_cmdstan(dir = dir, overwrite = TRUE, quiet = FALSE),
217+
type = "message")
218+
)
219+
install_quiet <- suppressWarnings(
220+
capture.output(install_cmdstan(dir = dir, overwrite = TRUE, quiet = TRUE),
221+
type = "message")
222+
)
223+
224+
expect_true(any(grepl(ver_msg, install_normal, fixed = TRUE)))
225+
expect_true(any(grepl(download_msg, install_normal, fixed = TRUE)))
226+
227+
expect_false(any(grepl(ver_msg, install_quiet, fixed = TRUE)))
228+
expect_false(any(grepl(download_msg, install_quiet, fixed = TRUE)))
229+
})
230+
231+
test_that("Download failures return error message", {
232+
# GHA fails on Windows old-rel here, but cannot replicate locally
233+
skip_if(os_is_windows() && getRversion() < '4.2')
234+
235+
if (getRversion() < '3.5.0') {
236+
dir <- tempdir()
237+
} else {
238+
dir <- tempdir(check = TRUE)
239+
}
240+
241+
expect_error({
242+
# Use an invalid proxy address to force a download failure
243+
withr::with_envvar(
244+
c("http_proxy"="invalid","https_proxy"="invalid"),
245+
install_cmdstan(dir = dir, overwrite = TRUE)
246+
)},
247+
"GitHub download of release list failed with error: cannot open URL 'https://api.github.com/repos/stan-dev/cmdstan/releases/latest'")
248+
})
249+

0 commit comments

Comments
 (0)