Skip to content

Commit

Permalink
added local get_ss3_exe function to resolve gh::gh SAML token issues …
Browse files Browse the repository at this point in the history
…downloading ss3 from nfms-ost (#109, #110)
  • Loading branch information
efletcherPIFSC committed Nov 15, 2024
1 parent 2120d76 commit cb35a2a
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 1 deletion.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Imports:
r4ss (== 1.49.3),
randtests,
reshape2,
gh,
rlang
Suggests:
knitr,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ export(SSretroComps)
export(SSrmse)
export(SSrunstest)
export(SSsettingsBratioF)
export(get_ss3_exe)
export(ssruns_sig3)
import(gh)
importFrom(gplots,ci2d)
importFrom(grDevices,adjustcolor)
importFrom(grDevices,dev.new)
Expand Down
217 changes: 217 additions & 0 deletions R/get_ss3_exe.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
#' Download the Stock Synthesis (SS3) executable
#'
#' Downloads the SS3 executable according to specified version and the user
#' operating system.
#'
#' @param dir The directory that you would like the executable downloaded to.
#' @param version A character string of the executable version tag to download
#' (e.g.'v3.30.20' or 'v3.30.18'). A list of tags is available at
#' https://github.com/nmfs-ost/ss3-source-code/tags
#' @return A string of the file path to the downloaded executable
#' @author Elizabeth F. Gugliotti
#' @export
#' @import gh
#' @examples
#' \dontrun{
#' get_ss3_exe()
#' get_ss3_exe(version = "v3.30.18")
#' }
#' @description The `get_ss3_exe()` function uses the `gh` package to get either
#' the latest release (if version = NULL) or the specified version of the Stock
#' Synthesis executable for the appropriate operating system to the directory `dir`
#' (if dir = NULL, then the executable is downloaded to the working directory).
#' To view the version tags available go to
#' https://github.com/nmfs-ost/ss3-source-code/tags

get_ss3_exe <- function(dir = NULL, version = NULL) {
# Get latest release if version not specified
if (is.null(version)) {
latest_release <- gh::gh("GET /repos/nmfs-ost/ss3-source-code/releases/latest", page = 1, .token = NA_character_)
tag <- latest_release[["tag_name"]]
} else {
# Otherwise get specified version
all_tags <- gh::gh("GET /repos/nmfs-ost/ss3-source-code/tags", .token = NA_character_)
df_tags <- as.data.frame(do.call(rbind, all_tags))
tags <- unlist(df_tags[["name"]])

if (!version %in% tags) {
warning("The version tag that you entered is invalid or not in the right format,
please go to https://github.com/nmfs-ost/ss3-source-code/tags
to get a correct version tag or version tag format")
} else {
tag <- version
}
}

if (is.null(dir)) {
dir <- getwd()
message("No directory provided, the executable will be downloaded to the working directory")
}

if (!dir.exists(dir)) {
stop("Directory doesn't exist: ", dir)
}

#if (.Platform[["OS.type"]] == "windows") {
if(R.version[["os"]] == "mingw32") {

download_location <- ss3_exe_windows(dir, tag)

} else if(substr(R.version[["os"]], 1, 6) == "darwin") {

download_location <- ss3_exe_darwin(dir, tag, R.version[["arch"]])
} else if(R.version[["os"]] == "linux-gnu") {

download_location <- ss3_exe_linux(dir, tag)
} else {

stop("The Stock Synthesis executable is not available for ", R.version[["os"]], ".") # nocov end
}

return(invisible(download_location))
}




#' @rdname get_ss3_exe
#'
#' @param dir Target Directory
#' @param tag Target ss3 Version release
#'
#' @return A string of the file path to the downloaded executable
#'
#' @keywords internal
#'
ss3_exe_windows <- function (dir, tag) {
if (.Platform[["r_arch"]] == "x32") { # nocov start
warning(
"Stock Synthesis binary is not available for 32-bit ",
.Platform[["OS.type"]], "."
)
} else {
url <- paste0(
"https://github.com/nmfs-ost/ss3-source-code/releases/download/",
tag, "/ss3_win.exe"
)
try_ss <- tryCatch(
suppressWarnings(utils::download.file(url, destfile = file.path(dir, "ss3.exe"), mode = "wb")),
error = function(e) "ss3 name not right for this version, trying ss"
)

if (try_ss == "ss3 name not right for this version, trying ss") {
url <- paste0(
"https://github.com/nmfs-ost/ss3-source-code/releases/download/",
tag, "/ss_win.exe"
)
utils::download.file(url, destfile = file.path(dir, "ss3.exe"), mode = "wb")
}
download_location <- file.path(dir, "ss3.exe")
message(paste0(
"The stock synthesis executable for Windows ", tag, " was downloaded to: ",
download_location
))

return(invisible(download_location))
}
}


#' @rdname get_ss3_exe
#'
#' @param dir Target Directory
#' @param tag Target ss3 Version release
#' @param arch arch
#'
#' @return A string of the file path to the downloaded executable
#'
#' @keywords internal
#'
ss3_exe_darwin <- function(dir, tag, arch = c("aarch64", "x86_64" )) {

arch <- match.arg(arch)

if(arch == "aarch64"){
url <- paste0(
"https://github.com/nmfs-ost/ss3-source-code/releases/download/",
tag, "/ss3_osx_arm64"
)
try_arm64 <- tryCatch(
suppressWarnings(utils::download.file(url, destfile = file.path(dir, "ss3"), mode = "wb")),
error = function(e) "ss3 executable not available for macOS arm64 architecture
computers for versions prior to v.3.30.22.1"
)

if (try_arm64 == "ss3 executable not available for macOS arm64 architecture computers for
versions prior to v.3.30.22.1") {
print(try_arm64)
} else {
Sys.chmod(paths = file.path(dir, "ss3"), mode = "0700")
download_location <- file.path(dir, "ss3")
message(paste0(
"The stock synthesis executable for Mac ", tag, " was downloaded to: ",
download_location
))
}
} else if (arch == "x86_64") {
url <- paste0(
"https://github.com/nmfs-ost/ss3-source-code/releases/download/",
tag, "/ss3_osx"
)
try_ss <- tryCatch(
suppressWarnings(utils::download.file(url, destfile = file.path(dir, "ss3"), mode = "wb")),
error = function(e) "ss3 name not right for this version, trying ss"
)

if (try_ss == "ss3 name not right for this version, trying ss") {
url <- paste0(
"https://github.com/nmfs-ost/ss3-source-code/releases/download/",
tag, "/ss_osx"
)
utils::download.file(url, destfile = file.path(dir, "ss3"), mode = "wb")
}
Sys.chmod(paths = file.path(dir, "ss3"), mode = "0700")
download_location <- file.path(dir, "ss3")
message(paste0(
"The stock synthesis executable for Mac ", tag, " was downloaded to: ",
download_location
))
}
return(invisible(download_location))
}

#' @rdname get_ss3_exe
#'
#' @param dir Target Directory
#' @param tag Target ss3 Version release
#'
#' @return A string of the file path to the downloaded executable
#'
#' @keywords internal
#'
ss3_exe_linux <- function(dir, tag) {
url <- paste0(
"https://github.com/nmfs-ost/ss3-source-code/releases/download/",
tag, "/ss3_linux"
)
try_ss <- tryCatch(
suppressWarnings(utils::download.file(url, destfile = file.path(dir, "ss3"), mode = "wb")),
error = function(e) "ss3 name not right for this version, trying ss"
)

if (try_ss == "ss3 name not right for this version, trying ss") {
url <- paste0(
"https://github.com/nmfs-ost/ss3-source-code/releases/download/",
tag, "/ss_linux"
)
utils::download.file(url, destfile = file.path(dir, "ss3"), mode = "wb")
}
Sys.chmod(paths = file.path(dir, "ss3"), mode = "0700")
Sys.chmod(paths = dir, mode = "0777")
download_location <- file.path(dir, "ss3")
message(paste0(
"The stock synthesis executable for Linux ", tag, " was downloaded to: ",
download_location
))
return(invisible(download_location))
}
59 changes: 59 additions & 0 deletions man/get_ss3_exe.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tests/testthat.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ files_path <- system.file("extdata", package = "ss3diags")
run_tmp <- file.path(tempdir(check = TRUE), "test-runs")
dir.create(run_tmp, showWarnings = FALSE)
file.copy(from = list.files(files_path, full.names = TRUE), to = run_tmp)
r4ss::get_ss3_exe(dir = run_tmp, version = "v3.30.21")
#r4ss::get_ss3_exe(dir = run_tmp, version = "v3.30.21")
ss3diags::get_ss3_exe(dir = run_tmp, version = "v3.30.21")
## Run retrospectives
if (file.exists(file.path(run_tmp, "ss3")) | file.exists(file.path(run_tmp, "ss3.exe"))) {
r4ss::retro(dir = run_tmp, oldsubdir = "", newsubdir = "retrospectives", years = 0:-3, show_in_console = FALSE)
Expand Down

0 comments on commit cb35a2a

Please sign in to comment.