build-cran-binaries/local/repair-built-stamp.R
pat-s c61d951875
Some checks failed
ci/crow/cron/process-updates/9 Pipeline was successful
ci/crow/cron/process-updates/15 Pipeline was successful
ci/crow/cron/process-updates/16 Pipeline was successful
ci/crow/cron/process-updates/13 Pipeline was successful
ci/crow/cron/process-updates/12 Pipeline was successful
ci/crow/cron/process-updates/10 Pipeline was successful
ci/crow/cron/process-updates/14 Pipeline was successful
ci/crow/cron/process-updates/4 Pipeline was successful
ci/crow/cron/process-updates/3 Pipeline was successful
ci/crow/cron/process-updates/8 Pipeline failed
ci/crow/cron/process-updates/1 Pipeline was successful
ci/crow/cron/process-updates/2 Pipeline was successful
ci/crow/cron/process-updates/5 Pipeline was successful
ci/crow/cron/process-updates/6 Pipeline was successful
ci/crow/cron/process-updates/7 Pipeline was successful
ci/crow/cron/process-updates/11 Pipeline was successful
fix(repair-built-stamp): patch the published PACKAGES.rds once bincraft drops cranlike (#236)
Follow-up to https://codefloe.com/rpkgs/bincraft/pulls/228 (bincraft#33), which drops the cranlike and desc forks.

## Why

After that change, `upload_package_index()` no longer keeps a `PACKAGES.db`.
It reuses the records of the slot's published `PACKAGES.rds`, as long as each tarball is still listed with the same ETag.
`local/repair-built-stamp.R` patched `PACKAGES.db`, so it would stop having any effect.

## Changes

- `local/repair-built-stamp.R` now patches `Built` in each slot's `PACKAGES.rds` and then re-emits the index.
  - Only the slot's own rows are patched: the generic slot's rows without a `Path`, or a per-minor slot's rows whose `Path` is its minor.
  - A missing stamp is not counted as broken, since bincraft clears it on purpose for source fallbacks.
  - With a bincraft release that still loads cranlike, the script patches `PACKAGES.db` as before, so merge order does not matter.
  - `bincraft::built_stamp()` was never exported, so the script failed at that line before this change; it now calls `bincraft:::built_stamp()`.
- The `GITHUB_PAT` comments in `reindex`, `weekly-rebuild-reindex` and `backfill-flat-safety` now give the current reason: bincraft reads DESCRIPTIONs from raw.githubusercontent.com.
- The README section on index files describes `upload_package_index()` instead of the forks.

## Testing

Tested against a moto S3 bucket, with bincraft installed from the bincraft#228 branch:

- A slot was indexed, then 3 of its 5 `Built` stamps were set to `R 4.5.0; NA; ...`.
- The dry run reported 5 entries, 3 broken.
- `--apply` rewrote the 3 stamps, and the re-emit logged `Index records: 5 reused, 0 read, 0 unreadable.`
- The published `PACKAGES` then had a valid stamp on all 5 records.
- With the released bincraft, which still loads cranlike, the script chose `PACKAGES.db`.

The stale `PACKAGES.db` objects in the slots can be deleted once a bincraft release with #228 is in use.

Reviewed-on: #236
2026-10-18 11:55:21 +00:00

274 lines
8.8 KiB
R

#!/usr/bin/env Rscript
### Rewrite a broken `Built` stamp across one arch/codename slot.
###
### A slot's index can end up advertising a stamp whose platform triple is
### unusable, e.g. `Built: R 4.5.0; NA; ...`. uvr picks binary vs source by
### matching that triple plus the R minor, so no client matches it and the whole
### slot silently reverts to source-only, which makes uvr compile everything and
### fail wherever a system `-dev` library is missing.
###
### Rewriting it is not a matter of re-running the normal index update.
### `upload_package_index()` uses the slot's published `PACKAGES.rds` as its
### cache: a record whose tarball is still listed with the same ETag is reused,
### stamp included, and only new or replaced tarballs are read and stamped. A
### full re-read would mean 25k requests to raw.githubusercontent.com per slot
### and a real risk of being rate-limited part-way through.
###
### Only the `Built` column is wrong, so correct it in place instead: patch the
### column in the published `PACKAGES.rds`, put it back, and let
### `upload_package_index()` re-emit `PACKAGES*` from it. No tarball is re-read
### and nothing is fetched from GitHub.
###
### bincraft releases before the switch away from cranlike (bincraft#33) read
### the slot's `PACKAGES.db` instead, so with one of those installed the script
### patches that database, as it always did.
###
### The replacement comes from bincraft's `built_stamp()` under the R running
### this script, so run it under the R version the slot should advertise (the
### `R_VERSION` its entry in `.crow/process-updates.yaml` uses). That is what a
### healthy `upload_package_index()` run would have written.
###
### Usage, inside the platform's build image:
### Rscript local/repair-built-stamp.R <arch> [--apply]
###
### Without `--apply` it reports what it would change and touches nothing.
suppressPackageStartupMessages({
library(bincraft)
})
args <- commandArgs(trailingOnly = TRUE)
arch <- args[1L]
apply_changes <- "--apply" %in% args
if (is.na(arch) || !nzchar(arch)) {
stop(
"Usage: Rscript local/repair-built-stamp.R <arch> [--apply]",
call. = FALSE
)
}
bucket <- "devxy-rpkgs-binaries"
endpoint <- "https://s3.eu-central-003.backblazeb2.com"
region <- "eu-central-003"
codename <- bincraft::set_codename(NULL)
if (is.null(codename) || is.na(codename) || !nzchar(codename)) {
stop(
"Could not detect a codename from /etc/os-release; run this in a build image.",
call. = FALSE
)
}
# built_stamp() refuses an unusable platform, so a broken build image fails here
# rather than writing a second bad stamp over the first one. It is internal to
# bincraft, hence `:::`.
stamp <- bincraft:::built_stamp()
message(sprintf("Slot: %s/%s", arch, codename))
message(sprintf("New stamp: %s", stamp))
s3fs::s3_file_system(
aws_access_key_id = Sys.getenv("B2_S3_ACCESS_KEY"),
aws_secret_access_key = Sys.getenv("B2_S3_SECRET_KEY"),
endpoint = endpoint,
region_name = region,
refresh = TRUE
)
base_dir <- file.path(bucket, arch, codename, "latest", "src", "contrib")
# A stamp is broken when its platform component is literally "NA" or empty.
broken_stamp <- function(built) {
!is.na(built) & grepl("; NA;|; ;", built)
}
# The same test for PACKAGES.db, which also counts a missing stamp as broken:
# the database never holds a deliberately cleared one.
broken_stamp_where <- paste(
"Built IS NULL",
"OR Built LIKE '%; NA;%'",
"OR Built LIKE '%; ;%'"
)
# The generic slot plus every per-minor sub-slot, which carry the same stamp and
# are poisoned by the same run.
r_minors <- sub(
"^.*/R/([0-9]+\\.[0-9]+)\\.[0-9]+$",
"\\1",
list.dirs("/opt/R", recursive = FALSE)
)
r_minors <- unique(grep("^[0-9]+\\.[0-9]+$", r_minors, value = TRUE))
slots <- c(
stats::setNames(base_dir, NA_character_),
stats::setNames(file.path(base_dir, r_minors), r_minors)
)
# bincraft imports cranlike for as long as it keeps the index in PACKAGES.db.
uses_packages_db <- "cranlike" %in% loadedNamespaces()
repair_packages_rds <- function(slot, r_minor) {
rds_remote <- file.path(slot, "PACKAGES.rds")
if (!s3fs::s3_file_exists(rds_remote)) {
message(sprintf(" %s: no PACKAGES.rds, skipping", slot))
return(invisible(NULL))
}
rds_local <- tempfile(fileext = ".rds")
on.exit(unlink(rds_local), add = TRUE)
s3fs::s3_file_download(rds_remote, rds_local, overwrite = TRUE)
records <- readRDS(rds_local)
# A per-minor index is published as a union with the generic slot, and only
# the rows carrying the slot's own `Path` are reused from it; the generic ones
# are re-read from the generic slot's index, which is repaired on its own.
own <- if (!("Path" %in% colnames(records))) {
rep(TRUE, nrow(records))
} else if (is.na(r_minor)) {
is.na(records[, "Path"])
} else {
records[, "Path"] %in% r_minor
}
# A missing stamp is not broken here: bincraft clears it on purpose for a
# package whose build failed and that is served as its CRAN source.
broken <- own & broken_stamp(records[, "Built"])
message(sprintf(
" %s: %s entries, %s broken",
slot,
sum(own),
sum(broken)
))
if (!any(broken)) {
return(invisible(NULL))
}
if (!apply_changes) {
message(" (dry run, pass --apply to rewrite)")
return(invisible(NULL))
}
records[broken, "Built"] <- stamp
saveRDS(records, rds_local, compress = "xz")
s3fs::s3_file_upload(
rds_local,
rds_remote,
overwrite = TRUE,
CacheControl = "no-store"
)
message(sprintf(
" rewrote %s entries and uploaded PACKAGES.rds",
sum(broken)
))
invisible(NULL)
}
repair_packages_db <- function(slot, r_minor) {
db_remote <- file.path(slot, "PACKAGES.db")
if (!s3fs::s3_file_exists(db_remote)) {
message(sprintf(" %s: no PACKAGES.db, skipping", slot))
return(invisible(NULL))
}
db_local <- tempfile(fileext = ".db")
s3fs::s3_file_download(db_remote, db_local, overwrite = TRUE)
con <- DBI::dbConnect(RSQLite::SQLite(), db_local)
on.exit(DBI::dbDisconnect(con), add = TRUE)
total <- DBI::dbGetQuery(con, "SELECT COUNT(*) AS n FROM packages")$n
broken <- DBI::dbGetQuery(
con,
sprintf("SELECT COUNT(*) AS n FROM packages WHERE %s", broken_stamp_where)
)$n
message(sprintf(" %s: %s entries, %s broken", slot, total, broken))
if (broken == 0L) {
return(invisible(NULL))
}
if (!apply_changes) {
message(" (dry run, pass --apply to rewrite)")
return(invisible(NULL))
}
DBI::dbExecute(
con,
sprintf("UPDATE packages SET Built = ? WHERE %s", broken_stamp_where),
params = list(stamp)
)
DBI::dbDisconnect(con)
on.exit()
s3fs::s3_file_upload(db_local, db_remote, overwrite = TRUE)
message(sprintf(" rewrote %s entries and uploaded PACKAGES.db", broken))
invisible(NULL)
}
repair_slot <- if (uses_packages_db) repair_packages_db else repair_packages_rds
message(sprintf(
"Patching: %s",
if (uses_packages_db) "PACKAGES.db" else "PACKAGES.rds"
))
invisible(Map(repair_slot, slots, names(slots)))
if (!apply_changes) {
message("Dry run complete; nothing was changed.")
quit(save = "no")
}
# Re-emit PACKAGES/PACKAGES.gz/PACKAGES.rds from the corrected records. Nothing
# is reparsed, because no tarball changed.
message("Re-emitting index files from the corrected records...")
bincraft::upload_package_index(
codename = codename,
s3_endpoint = endpoint,
s3_region = region,
s3_bucket = bucket,
s3_access_key_id = Sys.getenv("B2_S3_ACCESS_KEY"),
s3_secret_access_key = Sys.getenv("B2_S3_SECRET_KEY")
)
# A per-minor index reads the ABI classifier cache and the flat-object safety
# table from the build database. A failed minor does not stop the others, but
# it does fail the run once they are done.
failed_minors <- character(0L)
for (minor in r_minors) {
ok <- tryCatch(
{
bincraft::upload_package_index(
codename = codename,
r_minor = minor,
s3_endpoint = endpoint,
s3_region = region,
s3_bucket = bucket,
s3_access_key_id = Sys.getenv("B2_S3_ACCESS_KEY"),
s3_secret_access_key = Sys.getenv("B2_S3_SECRET_KEY"),
metadata_db_host = "r-binaries.devxy.io",
metadata_db_name = "build_metadata",
metadata_db_port = 15432L,
metadata_db_user = "rpkgs",
metadata_db_password = Sys.getenv("PGPASS"),
metadata_db_sslmode = "verify-full"
)
TRUE
},
error = function(e) {
message(sprintf("Re-indexing %s failed: %s", minor, conditionMessage(e)))
FALSE
}
)
if (!ok) {
failed_minors <- c(failed_minors, minor)
}
}
if (length(failed_minors) > 0L) {
stop(
"Could not republish the per-minor index for: ",
paste(failed_minors, collapse = ", "),
call. = FALSE
)
}
message("Done. Verify with:")
message(sprintf(
" curl -sS https://cran.devxy.io/%s/%s/latest/src/contrib/PACKAGES | grep '^Built:' | sort | uniq -c",
arch,
codename
))