build-cran-binaries/R/archive_packages.R

93 lines
3.5 KiB
R

#' Archive packages in CRAN-like repositories
#' @importFrom stringr str_split
#' @export
#' @examples
#' archive_package("AATtools", codename = "rhel9")
#' archive_package("adw", codename = "rhel8", arch = "amd64")
#'
archive_package <- function(
package_name,
build_for_minor = FALSE,
codename = NULL,
r_minor_version = NULL,
local_build_root = ".",
endpoint = "https://s3.eu-central-003.backblazeb2.com",
region = "eu-central-003",
bucket = "devxy-arm64-r-binaries",
pause = NULL,
arch = NULL,
debug = FALSE) {
s3fs::s3_file_system(
aws_access_key_id = Sys.getenv("AWS_ACCESS_KEY_ID"),
aws_secret_access_key = Sys.getenv("AWS_SECRET_ACCESS_KEY"),
endpoint = endpoint,
region_name = region,
)
if (debug) {
message(sprintf("DEBUG archive_package: package_name: %s", package_name))
}
codename <- set_codename(codename)
if (is.null(arch)) {
local_arch <- Sys.info()[["machine"]]
if (grepl("arm64", local_arch) || grepl("aarch64", local_arch)) {
arch <- "arm64"
} else if (grepl("amd64", local_arch) || grepl("x86_64", local_arch)) {
arch <- "amd64"
}
}
if (!build_for_minor) {
local_bin_dir <- set_bin_path(r_minor_version, build_for_minor, local_build_root, codename)
remote_bin_dir <- sprintf("%s/%s/%s/latest/src/contrib", bucket, arch, codename)
} else {
dir_out_bin <- set_bin_path(r_minor_version, build_for_minor, local_build_root, codename)
remote_bin_dir <- sprintf("%s/%s/%s/%s/latest/src/contrib", bucket, arch, codename, r_minor_version)
}
# suppress progressr output here
progressr::handlers("void")
# don't parallelise
future::plan("sequential")
files <- s3fs::s3_dir_ls(remote_bin_dir)
foo <- lapply(package_name, function(.x) {
cli::cli_h2("Archiving ({.pkg {.x}})")
if (!s3fs::s3_dir_exists(sprintf("%s/Archive/%s", remote_bin_dir, .x))) {
s3fs::s3_dir_create(sprintf("%s/Archive/%s", remote_bin_dir, .x))
}
all_versions <- grep(sprintf("/%s_", .x), files, value = TRUE)
# only archive if more than one package exists in the root
if (length(all_versions) > 1) {
# get most recent version from CRAN
last_version <- available.packages("https://cloud.r-project.org/src/contrib")[.x, "Version"]
# check if last version is available in repo
if (any(grepl(sprintf("^%s_%s.tar.gz", package_name, last_version), all_versions))) {
index <- which(grepl(sprintf("_%s.tar.gz", last_version), all_versions, fixed = TRUE))
old_versions <- all_versions[-index]
} else {
versions <- rev(pak::pkg_history(.x)$Version)
for (i in versions) {
if (any(grepl(paste0("^", i, "$"), stringr::str_split(stringr::str_split(all_versions, "_", simplify = T)[, 2], ".tar.gz", simplify = TRUE)[, 1]))) {
index <- which(grepl(sprintf("_%s.tar.gz", i), all_versions))
old_versions <- all_versions[-index]
break
}
}
}
cli::cli_alert("{.fun archive_package}: Archiving {.field {basename(old_versions)}}, keeping {.field {basename(all_versions[index])}}.")
s3fs::s3_file_move(old_versions, sprintf("%s/Archive/%s/%s", remote_bin_dir, .x, basename(old_versions)), overwrite = TRUE)
cli::cli_alert_success("{.fun archive_package}: Successfully archived package {.pkg {.x}}.")
if (!is.null(pause)) {
Sys.sleep(pause)
}
} else {
cli::cli_alert("{.fun archive_package}: Skipping {.pkg {.x}} as only one package versions exists.")
}
})
return(invisible(TRUE))
}