add archive_packages()

This commit is contained in:
Patrick Schratz 2024-08-30 00:57:49 +02:00
commit 7768d8a881
Signed by: pat-s
GPG key ID: 3C6318841EF78925
7 changed files with 99 additions and 4 deletions

View file

@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand
export(add_to_package_index)
export(archive_package)
export(build_binary_package)
export(build_single_tag)
export(dbcon)

64
R/archive_packages.R Normal file
View file

@ -0,0 +1,64 @@
#' Archive packages in CRAN-like repositories
#' @export
#' @examples
#' archive_package("AATtools", codename = "rhel9")
#'
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") {
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,
)
codename <- set_codename(codename)
if (!build_for_minor) {
local_bin_dir <- sprintf("%s/arm64/%s/latest/src/contrib", local_build_root, codename)
remote_bin_dir <- sprintf("%s/arm64/%s/latest/src/contrib", bucket, codename)
} else {
dir_out_bin <- sprintf("%s/arm64/%s/%s/latest/src/contrib", local_build_root, codename, r_minor_version)
remote_bin_dir <- sprintf("%s/arm64/%s/%s/latest/src/contrib", bucket, codename, r_minor_version)
}
files <- s3fs::s3_dir_ls(remote_bin_dir)
foo <- lapply(package_name, function(.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()[.x, "Version"]
# check if last version is available in repo
if (any(grepl(last_version, all_versions))) {
index <- which(grepl(last_version, all_versions))
old_versions <- all_versions[-index]
} else {
versions = rev(pak::pkg_history(.x)$Version)
for (i in versions) {
if (any(grepl(i, all_versions))) {
index <- which(grepl(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}}.")
}
cli::cli_alert("{.fun archive_package}: Skipping {.pkg {.x}} as only one package versions exists.")
})
return(invisible(TRUE))
}

View file

@ -29,6 +29,8 @@ set_codename <- function(codename) {
}
}
return(codename)
} else {
return(codename)
}
}

View file

@ -31,7 +31,11 @@ process_cran_updates <- function(
all_pkgs <- dplyr::bind_rows(updated_pkgs, new_pkgs)
purrr::walk2(all_pkgs$name, all_pkgs$version, ~ build_binary_package(.x, .y, platform = platform))
purrr::walk2(all_pkgs$name, all_pkgs$version, ~ {
build_binary_package(.x, .y, platform = platform)
archive_package(.x)
}
)
if (prune) {
removed_pkgs <- get_removed_cran_packages(interval)
@ -57,7 +61,7 @@ process_cran_updates <- function(
purrr::walk(removed_pkgs$name, ~ {
cli::cli_alert("{.fun process_cran_updates}: Removing package {.pkg {.x}} from S3.")
files_filtered <- grep(sprintf("%s_", .x), files, value = TRUE)
files_filtered <- grep(sprintf("/%s_", .x), files, value = TRUE)
if (length(files_filtered > 0)) {
s3fs::s3_file_delete(files_filtered)
cli::cli_alert_success("{.fun process_cran_updates}: Successfully removed {.pkg {basename(files_filtered)}} from S3.")

24
man/archive_package.Rd Normal file
View file

@ -0,0 +1,24 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/archive_packages.R
\name{archive_package}
\alias{archive_package}
\title{Archive packages in CRAN-like repositories}
\usage{
archive_package(
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"
)
}
\description{
Archive packages in CRAN-like repositories
}
\examples{
archive_package("AATtools", codename = "rhel9")
}

View file

@ -9,7 +9,7 @@ build_binary_package(
tag = NULL,
codename = NULL,
r_minor_version = NULL,
build_for_minor = TRUE,
build_for_minor = FALSE,
local_build_root = "/root",
local_clone_dir = "/tmp",
platform = NULL,

View file

@ -7,7 +7,7 @@
process_cran_updates(
package_name,
tag,
platform = platform,
platform,
local_clone_dir,
interval = lubridate::today(),
prune = TRUE,