| 1 |
|
| 2 |
renv_p3m_enabled <- function() { |
| 3 |
!identical(getOption("pkgType"), "source") && config$ppm.enabled() |
| 4 |
} |
| 5 |
|
| 6 |
renv_p3m_database_path <- function() { |
| 7 |
renv_paths_p3m("packages.rds") |
| 8 |
} |
| 9 |
|
| 10 |
renv_p3m_database_encode <- function(database) { |
| 11 |
database <- as.list(database) |
| 12 |
encoded <- lapply(database, renv_p3m_database_encode_impl) |
| 13 |
encoded[order(names(encoded))] |
| 14 |
} |
| 15 |
|
| 16 |
renv_p3m_database_encode_impl <- function(entry) { |
| 17 |
|
| 18 |
entry <- as.list(entry) |
| 19 |
keys <- names(entry) |
| 20 |
vals <- unlist(entry) |
| 21 |
|
| 22 |
splat <- strsplit(keys, " ", fixed = TRUE) |
| 23 |
|
| 24 |
encoded <- data_frame( |
| 25 |
Package = map_chr(splat, `[[`, 1L), |
| 26 |
Version = map_chr(splat, `[[`, 2L), |
| 27 |
Date = as.integer(vals) |
| 28 |
) |
| 29 |
|
| 30 |
encoded <- encoded[order(encoded$Package, encoded$Version), ] |
| 31 |
rownames(encoded) <- NULL |
| 32 |
|
| 33 |
encoded$Package <- as.factor(encoded$Package) |
| 34 |
encoded$Version <- as.factor(encoded$Version) |
| 35 |
|
| 36 |
encoded |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
renv_p3m_database_decode <- function(encoded) { |
| 41 |
decoded <- lapply(encoded, renv_p3m_database_decode_impl) |
| 42 |
list2env(decoded, parent = emptyenv()) |
| 43 |
} |
| 44 |
|
| 45 |
renv_p3m_database_decode_impl <- function(entry) { |
| 46 |
|
| 47 |
entry$Package <- as.character(entry$Package) |
| 48 |
entry$Version <- as.character(entry$Version) |
| 49 |
|
| 50 |
keys <- paste(entry$Package, entry$Version) |
| 51 |
vals <- as.list(entry$Date) |
| 52 |
names(vals) <- keys |
| 53 |
|
| 54 |
envir <- list2env(vals, parent = emptyenv()) |
| 55 |
attr(envir, "keys") <- keys |
| 56 |
|
| 57 |
envir |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
renv_p3m_database_save <- function(database, path = NULL) { |
| 62 |
|
| 63 |
path <- path %||% renv_p3m_database_path() |
| 64 |
ensure_parent_directory(path) |
| 65 |
|
| 66 |
encoded <- renv_p3m_database_encode(database) |
| 67 |
|
| 68 |
conn <- xzfile(path) |
| 69 |
defer(close(conn)) |
| 70 |
saveRDS(encoded, file = conn, version = 2L) |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
renv_p3m_database_load <- function(path = NULL) { |
| 75 |
|
| 76 |
filebacked( |
| 77 |
context = "renv_p3m_database_load", |
| 78 |
path = path %||% renv_p3m_database_path(), |
| 79 |
callback = renv_p3m_database_load_impl |
| 80 |
) |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
renv_p3m_database_load_impl <- function(path) { |
| 85 |
|
| 86 |
# read from database file if it exists |
| 87 |
if (file.exists(path)) { |
| 88 |
encoded <- readRDS(path) |
| 89 |
return(renv_p3m_database_decode(encoded)) |
| 90 |
} |
| 91 |
|
| 92 |
# otherwise, initialize a new database |
| 93 |
new.env(parent = emptyenv()) |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
renv_p3m_database_dates <- function(version, all = TRUE) { |
| 98 |
|
| 99 |
# release dates for old versions of R |
| 100 |
releases <- c( |
| 101 |
"3.2" = "2015-04-16", |
| 102 |
"3.3" = "2016-05-03", |
| 103 |
"3.4" = "2017-04-21", |
| 104 |
"3.5" = "2018-04-23", |
| 105 |
"3.6" = "2019-04-26", |
| 106 |
"4.0" = "2020-04-24", |
| 107 |
"4.1" = "2021-05-18", |
| 108 |
"4.2" = "2022-04-22", |
| 109 |
"4.3" = "2023-04-21", |
| 110 |
"4.4" = "2024-04-24", |
| 111 |
"4.5" = "2025-05-18", # a guess |
| 112 |
"4.6" = "2026-05-18", # a guess |
| 113 |
NULL |
| 114 |
) |
| 115 |
|
| 116 |
# find the start date |
| 117 |
index <- match(version, names(releases)) |
| 118 |
if (is.na(index)) |
| 119 |
stopf("no known release date for R %s", version) |
| 120 |
|
| 121 |
start <- as.Date(releases[[index]]) |
| 122 |
if (!all) |
| 123 |
return(start) |
| 124 |
|
| 125 |
# form end date (ensure not in future) |
| 126 |
# we look 2 releases in the future as R builds binaries for |
| 127 |
# the previous releases of R as well |
| 128 |
end <- min( |
| 129 |
as.Date(releases[[index + 2L]]), |
| 130 |
as.Date(Sys.time(), tz = "UTC") |
| 131 |
) |
| 132 |
|
| 133 |
# generate list of dates |
| 134 |
seq(start, end, by = 1L) |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
renv_p3m_database_key <- function(platform, version) { |
| 139 |
sprintf("/bin/%s/contrib/%s", platform, version) |
| 140 |
} |
| 141 |
|
| 142 |
renv_p3m_database_update <- function(platform, version, dates = NULL) { |
| 143 |
|
| 144 |
# load database |
| 145 |
database <- renv_p3m_database_load() |
| 146 |
|
| 147 |
# get reference to entry in database (initialize if not yet created) |
| 148 |
suffix <- renv_p3m_database_key(platform, version) |
| 149 |
database[[suffix]] <- database[[suffix]] %||% new.env(parent = emptyenv()) |
| 150 |
entry <- database[[suffix]] |
| 151 |
|
| 152 |
# rough release dates for R releases |
| 153 |
dates <- as.list(dates %||% renv_p3m_database_dates(version)) |
| 154 |
|
| 155 |
for (date in dates) { |
| 156 |
|
| 157 |
# attempt to update our database entry for this date |
| 158 |
url <- renv_p3m_url(date, suffix) |
| 159 |
tryCatch( |
| 160 |
renv_p3m_database_update_impl(date, url, entry), |
| 161 |
error = warnify |
| 162 |
) |
| 163 |
|
| 164 |
} |
| 165 |
|
| 166 |
# save at end |
| 167 |
printf("[%s]: saving database ... ", date) |
| 168 |
renv_p3m_database_save(database) |
| 169 |
writef("DONE") |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
renv_p3m_database_update_impl <- function(date, url, entry) { |
| 174 |
|
| 175 |
printf("[%s]: updating package database ... ", date) |
| 176 |
|
| 177 |
# get date as number of days since epoch |
| 178 |
idate <- as.integer(date) |
| 179 |
|
| 180 |
# retrieve available packages |
| 181 |
errors <- new.env(parent = emptyenv()) |
| 182 |
db <- renv_available_packages_query_impl(url, "binary", errors) |
| 183 |
if (is.null(db)) { |
| 184 |
writef("ERROR") |
| 185 |
return(FALSE) |
| 186 |
} |
| 187 |
|
| 188 |
# insert packages into database |
| 189 |
for (i in seq_len(nrow(db))) { |
| 190 |
|
| 191 |
# construct key for index |
| 192 |
name <- db[i, "Package"] |
| 193 |
vers <- db[i, "Version"] |
| 194 |
key <- paste(name, vers) |
| 195 |
|
| 196 |
# update database |
| 197 |
entry[[key]] <- max(entry[[key]] %||% 0L, idate) |
| 198 |
|
| 199 |
} |
| 200 |
|
| 201 |
writef("OK") |
| 202 |
TRUE |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
renv_p3m_url <- function(date, suffix) { |
| 207 |
default <- "https://packagemanager.posit.co/cran" |
| 208 |
root <- Sys.getenv("RENV_MRAN_URL", unset = default) |
| 209 |
snapshot <- file.path(root, date) |
| 210 |
paste(snapshot, suffix, sep = "") |
| 211 |
} |
| 212 |
|
| 213 |
renv_p3m_database_url <- function() { |
| 214 |
default <- "https://rstudio-buildtools.s3.amazonaws.com/renv/package-manager/packages.rds" |
| 215 |
Sys.getenv("RENV_MRAN_DATABASE_URL", unset = default) |
| 216 |
} |
| 217 |
|
| 218 |
renv_p3m_database_refresh <- function(explicit = TRUE) { |
| 219 |
|
| 220 |
if (explicit || renv_p3m_database_refresh_required()) |
| 221 |
renv_p3m_database_refresh_impl() |
| 222 |
|
| 223 |
} |
| 224 |
|
| 225 |
renv_p3m_database_refresh_required <- function() { |
| 226 |
dynamic( |
| 227 |
key = list(), |
| 228 |
value = renv_p3m_database_refresh_required_impl() |
| 229 |
) |
| 230 |
} |
| 231 |
|
| 232 |
renv_p3m_database_refresh_required_impl <- function() { |
| 233 |
|
| 234 |
# if the cache doesn't exist, we must refresh |
| 235 |
path <- renv_p3m_database_path() |
| 236 |
if (!file.exists(path)) |
| 237 |
return(TRUE) |
| 238 |
|
| 239 |
# if we're using an older version of R, but we have newer package |
| 240 |
# versions available in the cache, we don't need to refresh |
| 241 |
db <- tryCatch(renv_p3m_database_load(), error = identity) |
| 242 |
if (!inherits(db, "error")) { |
| 243 |
keys <- names(db) |
| 244 |
versions <- unique(basename(keys)) |
| 245 |
if (any(versions > getRversion())) |
| 246 |
return(FALSE) |
| 247 |
} |
| 248 |
|
| 249 |
# read the file mtime |
| 250 |
info <- renv_file_info(path) |
| 251 |
if (is.na(info$mtime)) |
| 252 |
return(FALSE) |
| 253 |
|
| 254 |
# if it's older than a day, then we should update |
| 255 |
difftime(Sys.time(), info$mtime, units = "days") > 1 |
| 256 |
|
| 257 |
} |
| 258 |
|
| 259 |
renv_p3m_database_refresh_impl <- function() { |
| 260 |
|
| 261 |
url <- renv_p3m_database_url() |
| 262 |
path <- renv_p3m_database_path() |
| 263 |
|
| 264 |
if (nzchar(url) && nzchar(path)) { |
| 265 |
ensure_parent_directory(path) |
| 266 |
download(url = url, destfile = path, quiet = TRUE) |
| 267 |
} |
| 268 |
|
| 269 |
} |
| 270 |
|
| 271 |
renv_p3m_database_sync <- function(platform, version) { |
| 272 |
|
| 273 |
# read database |
| 274 |
database <- renv_p3m_database_load() |
| 275 |
|
| 276 |
# read entry for this platform + version combo |
| 277 |
key <- renv_p3m_database_key(platform, version) |
| 278 |
entry <- database[[key]] |
| 279 |
if (is.null(entry)) { |
| 280 |
database[[key]] <- new.env(parent = emptyenv()) |
| 281 |
entry <- database[[key]] |
| 282 |
} |
| 283 |
|
| 284 |
# get the last known updated date |
| 285 |
last <- max(0L, as.integer(as.list(entry))) |
| 286 |
if (identical(last, 0L)) { |
| 287 |
date <- renv_p3m_database_dates(version, all = FALSE) |
| 288 |
last <- as.integer(date) |
| 289 |
} |
| 290 |
|
| 291 |
# get yesterday's date |
| 292 |
now <- as.integer(as.Date(Sys.time(), tz = "UTC")) - 1L |
| 293 |
|
| 294 |
# sync up to the last version's release date |
| 295 |
dates <- as.integer(renv_p3m_database_dates(version)) |
| 296 |
now <- min(now, max(dates)) |
| 297 |
|
| 298 |
# if we've already in sync, nothing to do |
| 299 |
if (last >= now) |
| 300 |
return(FALSE) |
| 301 |
|
| 302 |
# invoke update for missing dates |
| 303 |
writef("==> Synchronizing database (%s/%s)", platform, version) |
| 304 |
dates <- as.Date(seq(last + 1L, now, by = 1L), origin = "1970-01-01") |
| 305 |
renv_p3m_database_update(platform, version, dates) |
| 306 |
writef("Finished synchronizing database (%s/%s)", platform, version) |
| 307 |
|
| 308 |
# return TRUE to indicate update occurred |
| 309 |
return(TRUE) |
| 310 |
|
| 311 |
} |
| 312 |
|
| 313 |
renv_p3m_database_sync_all <- function() { |
| 314 |
|
| 315 |
tryCatch( |
| 316 |
renv_p3m_database_sync_all_impl(), |
| 317 |
interrupt = identity |
| 318 |
) |
| 319 |
|
| 320 |
} |
| 321 |
|
| 322 |
renv_p3m_database_sync_all_impl <- function() { |
| 323 |
|
| 324 |
# NOTE: this needs to be manually updated since the binary URL for |
| 325 |
# packages can change from version to version, especially on macOS |
| 326 |
|
| 327 |
# # R 3.2 |
| 328 |
# renv_p3m_database_sync("windows", "3.2") |
| 329 |
# renv_p3m_database_sync("macosx/mavericks", "3.2") |
| 330 |
# |
| 331 |
# # R 3.3 |
| 332 |
# renv_p3m_database_sync("windows", "3.3") |
| 333 |
# renv_p3m_database_sync("macosx/mavericks", "3.3") |
| 334 |
# |
| 335 |
# # R 3.4 |
| 336 |
# renv_p3m_database_sync("windows", "3.4") |
| 337 |
# renv_p3m_database_sync("macosx/el-capitan", "3.4") |
| 338 |
# |
| 339 |
# # R 3.5 |
| 340 |
# renv_p3m_database_sync("windows", "3.5") |
| 341 |
# renv_p3m_database_sync("macosx/el-capitan", "3.5") |
| 342 |
# |
| 343 |
# # R 3.6 |
| 344 |
# renv_p3m_database_sync("windows", "3.6") |
| 345 |
# renv_p3m_database_sync("macosx/el-capitan", "3.6") |
| 346 |
# |
| 347 |
# # R 4.0 |
| 348 |
# renv_p3m_database_sync("windows", "4.0") |
| 349 |
# renv_p3m_database_sync("macosx", "4.0") |
| 350 |
|
| 351 |
# R 4.1 |
| 352 |
renv_p3m_database_sync("windows", "4.1") |
| 353 |
renv_p3m_database_sync("macosx", "4.1") |
| 354 |
renv_p3m_database_sync("macosx/big-sur-arm64", "4.1") |
| 355 |
|
| 356 |
# R 4.2 |
| 357 |
renv_p3m_database_sync("windows", "4.2") |
| 358 |
renv_p3m_database_sync("macosx", "4.2") |
| 359 |
renv_p3m_database_sync("macosx/big-sur-arm64", "4.2") |
| 360 |
|
| 361 |
# R 4.3 |
| 362 |
renv_p3m_database_sync("windows", "4.3") |
| 363 |
renv_p3m_database_sync("macosx", "4.3") |
| 364 |
renv_p3m_database_sync("macosx/big-sur-arm64", "4.3") |
| 365 |
|
| 366 |
# R 4.4 |
| 367 |
renv_p3m_database_sync("windows", "4.4") |
| 368 |
renv_p3m_database_sync("macosx", "4.4") |
| 369 |
renv_p3m_database_sync("macosx/big-sur-arm64", "4.4") |
| 370 |
|
| 371 |
} |