| 1 |
the$rejected_packages <- new.env(parent = emptyenv()) |
| 2 |
|
| 3 |
# tools for querying information about packages available on CRAN. |
| 4 |
# note that this does _not_ merge package entries from multiple repositories; |
| 5 |
# rather, a list of databases is returned (one for each repository) |
| 6 |
available_packages <- function(type, |
| 7 |
repos = NULL, |
| 8 |
limit = NULL, |
| 9 |
quiet = FALSE, |
| 10 |
cellar = FALSE) |
| 11 |
{ |
| 12 |
dynamic( |
| 13 |
|
| 14 |
key = list( |
| 15 |
type = type, |
| 16 |
repos = repos %||% getOption("repos"), |
| 17 |
cellar = cellar |
| 18 |
), |
| 19 |
|
| 20 |
value = renv_available_packages_impl( |
| 21 |
type = type, |
| 22 |
repos = repos, |
| 23 |
limit = limit, |
| 24 |
quiet = quiet, |
| 25 |
cellar = cellar |
| 26 |
) |
| 27 |
|
| 28 |
) |
| 29 |
} |
| 30 |
|
| 31 |
renv_available_packages_impl <- function(type, |
| 32 |
repos = NULL, |
| 33 |
limit = NULL, |
| 34 |
quiet = FALSE, |
| 35 |
cellar = FALSE) |
| 36 |
{ |
| 37 |
limit <- limit %||% Sys.getenv("R_AVAILABLE_PACKAGES_CACHE_CONTROL_MAX_AGE", "3600") |
| 38 |
repos <- renv_repos_normalize(repos %||% getOption("repos")) |
| 39 |
|
| 40 |
# invalidate cache if http_proxy or https_proxy environment variables change, |
| 41 |
# since those could effect (or even re-direct?) repository URLs |
| 42 |
envkeys <- c("http_proxy", "https_proxy", "HTTP_PROXY", "HTTPS_PROXY") |
| 43 |
envvals <- Sys.getenv(envkeys, unset = NA) |
| 44 |
|
| 45 |
# invalidate the cache if 'renv.download.headers' changes as well |
| 46 |
headers <- getOption("renv.download.headers") |
| 47 |
key <- list(repos = repos, type = type, headers = headers, envvals) |
| 48 |
|
| 49 |
# retrieve available packages |
| 50 |
dbs <- if (length(repos)) index( |
| 51 |
scope = "available-packages", |
| 52 |
key = key, |
| 53 |
value = renv_available_packages_query(type, repos, quiet), |
| 54 |
limit = as.integer(limit) |
| 55 |
) |
| 56 |
|
| 57 |
# include cellar if requested |
| 58 |
dbs[["__renv_cellar__"]] <- if (cellar) |
| 59 |
renv_available_packages_cellar(type = type) |
| 60 |
|
| 61 |
dbs |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
renv_available_packages_query <- function(type, repos, quiet = FALSE) { |
| 66 |
|
| 67 |
if (quiet) |
| 68 |
renv_scope_options(renv.verbose = FALSE) |
| 69 |
|
| 70 |
fmt <- "- Querying repositories for available %s packages ... " |
| 71 |
printf(fmt, type) |
| 72 |
|
| 73 |
# exclude repositories which are known to not have packages available |
| 74 |
if (type == "binary") { |
| 75 |
ignored <- setdiff(grep("^BioC", names(repos), value = TRUE), "BioCsoft") |
| 76 |
repos <- repos[setdiff(names(repos), ignored)] |
| 77 |
} |
| 78 |
|
| 79 |
# request repositories |
| 80 |
urls <- contrib.url(repos, type) |
| 81 |
errors <- new.env(parent = emptyenv()) |
| 82 |
dbs <- map(urls, renv_available_packages_query_impl, type = type, errors = errors) |
| 83 |
names(dbs) <- names(repos) |
| 84 |
|
| 85 |
# notify finished |
| 86 |
writef("Done!") |
| 87 |
|
| 88 |
# propagate errors |
| 89 |
errors <- as.list(errors) |
| 90 |
if (empty(errors)) |
| 91 |
return(dbs) |
| 92 |
|
| 93 |
header <- "renv was unable to query available packages from the following repositories:" |
| 94 |
msgs <- enum_chr(errors, function(url, cnds) { |
| 95 |
msgs <- map_chr(cnds, conditionMessage) |
| 96 |
paste(c(header(url), msgs, ""), collapse = "\n") |
| 97 |
}) |
| 98 |
|
| 99 |
bulletin(header, msgs) |
| 100 |
filter(dbs, Negate(is.null)) |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
renv_available_packages_query_impl_packages_rds <- function(url) { |
| 105 |
path <- file.path(url, "PACKAGES.rds") |
| 106 |
destfile <- renv_scope_tempfile("renv-packages-", fileext = ".rds") |
| 107 |
|
| 108 |
download(url = path, destfile = destfile, quiet = TRUE) |
| 109 |
suppressWarnings(readRDS(destfile)) |
| 110 |
} |
| 111 |
|
| 112 |
renv_available_packages_query_impl_packages_gz <- function(url) { |
| 113 |
path <- file.path(url, "PACKAGES.gz") |
| 114 |
destfile <- renv_scope_tempfile("renv-packages-", fileext = ".gz") |
| 115 |
|
| 116 |
download(url = path, destfile = destfile, quiet = TRUE) |
| 117 |
suppressWarnings(read.dcf(destfile)) |
| 118 |
} |
| 119 |
|
| 120 |
renv_available_packages_query_impl_packages <- function(url) { |
| 121 |
path <- file.path(url, "PACKAGES") |
| 122 |
destfile <- renv_scope_tempfile("renv-packages-") |
| 123 |
|
| 124 |
download(url = path, destfile = destfile, quiet = TRUE) |
| 125 |
suppressWarnings(read.dcf(destfile)) |
| 126 |
} |
| 127 |
|
| 128 |
renv_available_packages_query_impl <- function(url, type, errors) { |
| 129 |
|
| 130 |
# define query_impl methods for the different PACKAGES |
| 131 |
methods <- list( |
| 132 |
renv_available_packages_query_impl_packages_rds, |
| 133 |
renv_available_packages_query_impl_packages_gz, |
| 134 |
renv_available_packages_query_impl_packages |
| 135 |
) |
| 136 |
|
| 137 |
stack <- stack() |
| 138 |
seize <- function(restart) { |
| 139 |
function(condition) { |
| 140 |
stack$push(condition) |
| 141 |
invokeRestart(restart) |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
for (method in methods) { |
| 146 |
|
| 147 |
db <- withCallingHandlers( |
| 148 |
catch(method(url)), |
| 149 |
warning = seize(restart = "muffleWarning"), |
| 150 |
message = seize(restart = "muffleMessage") |
| 151 |
) |
| 152 |
|
| 153 |
if (inherits(db, "error")) { |
| 154 |
stack$push(db) |
| 155 |
next |
| 156 |
} |
| 157 |
|
| 158 |
return(renv_available_packages_success(db, url, type)) |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
assign(url, stack$data(), envir = errors) |
| 163 |
NULL |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
renv_available_packages_success <- function(db, url, type) { |
| 168 |
|
| 169 |
# https://github.com/rstudio/renv/issues/1706 |
| 170 |
if (empty(db)) |
| 171 |
return(data.frame()) |
| 172 |
|
| 173 |
# convert to data.frame |
| 174 |
db <- as_data_frame(db) |
| 175 |
if (nrow(db) == 0L) |
| 176 |
return(db) |
| 177 |
|
| 178 |
# build repository url |
| 179 |
repository <- rep.int(url, nrow(db)) |
| 180 |
|
| 181 |
# update with path |
| 182 |
path <- db$Path |
| 183 |
if (length(path)) { |
| 184 |
set <- !is.na(path) |
| 185 |
repository[set] <- paste(url, path[set], sep = "/") |
| 186 |
} |
| 187 |
|
| 188 |
# set it |
| 189 |
db$Repository <- repository |
| 190 |
|
| 191 |
# add in necessary missing columns |
| 192 |
required <- c( |
| 193 |
"Package", "Version", "Priority", |
| 194 |
"Depends", "Imports", "LinkingTo", "Suggests", "Enhances", "Remotes", |
| 195 |
"License", "License_is_FOSS", "License_restricts_use", |
| 196 |
"OS_type", "Archs", "MD5sum", |
| 197 |
if (type %in% "source") "NeedsCompilation", |
| 198 |
"File", "Repository" |
| 199 |
) |
| 200 |
|
| 201 |
missing <- setdiff(required, names(db)) |
| 202 |
db[missing] <- NA_character_ |
| 203 |
db <- db[required] |
| 204 |
|
| 205 |
# filter as appropriate |
| 206 |
db <- renv_available_packages_filter(db) |
| 207 |
|
| 208 |
# remove row names |
| 209 |
row.names(db) <- NULL |
| 210 |
|
| 211 |
# ok |
| 212 |
db |
| 213 |
|
| 214 |
} |
| 215 |
|
| 216 |
renv_available_packages_entry <- function(package, |
| 217 |
type = "source", |
| 218 |
repos = NULL, |
| 219 |
filter = NULL, |
| 220 |
quiet = FALSE, |
| 221 |
prefer = NULL) |
| 222 |
{ |
| 223 |
|
| 224 |
# if filter is a string, treat it as an explicit version requirement |
| 225 |
version <- NULL |
| 226 |
if (is.character(filter)) { |
| 227 |
version <- filter |
| 228 |
filter <- function(entries) { |
| 229 |
matches <- which(entries$Version == version) |
| 230 |
candidate <- head(matches, n = 1L) |
| 231 |
entries[candidate, ] |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
# by default, provide a filter that selects the newest-available package |
| 236 |
filter <- filter %||% function(entries) { |
| 237 |
version <- numeric_version(entries$Version) |
| 238 |
ordered <- order(version, decreasing = TRUE) |
| 239 |
entries[ordered[[1]], ] |
| 240 |
} |
| 241 |
|
| 242 |
# if 'prefer' was supplied as a repository URL, include it |
| 243 |
repos <- repos %||% as.list(getOption("repos")) |
| 244 |
if (is.character(prefer)) { |
| 245 |
|
| 246 |
# ensure repositories are named |
| 247 |
nms <- names(prefer) %||% rep.int("", length(prefer)) |
| 248 |
nms[!nzchar(nms)] <- prefer[!nzchar(nms)] |
| 249 |
names(prefer) <- nms |
| 250 |
|
| 251 |
# include any 'prefer' repositories that were supplied as URLs |
| 252 |
# normalize first, as these may already include a contrib url suffix |
| 253 |
isurl <- grep("^\\w+://", prefer) |
| 254 |
if (length(isurl)) |
| 255 |
prefer[isurl] <- renv_repos_baseurl(prefer[isurl]) |
| 256 |
repos <- c(prefer[isurl], repos) |
| 257 |
|
| 258 |
} |
| 259 |
|
| 260 |
# read available packages |
| 261 |
dbs <- available_packages( |
| 262 |
type = type, |
| 263 |
repos = repos, |
| 264 |
quiet = quiet |
| 265 |
) |
| 266 |
|
| 267 |
# if a preferred repository is marked and available, prefer using that |
| 268 |
if (is.character(prefer)) { |
| 269 |
idx <- omit_if(match(names(prefer), names(dbs)), is.na) |
| 270 |
ord <- c(idx, setdiff(seq_along(dbs), idx)) |
| 271 |
dbs <- dbs[ord] |
| 272 |
} |
| 273 |
|
| 274 |
# iterate through repositories, and find first matching |
| 275 |
for (i in seq_along(dbs)) { |
| 276 |
|
| 277 |
db <- dbs[[i]] |
| 278 |
matches <- which(db$Package == package) |
| 279 |
if (empty(matches)) |
| 280 |
next |
| 281 |
|
| 282 |
entries <- db[matches, ] |
| 283 |
entry <- filter(entries) |
| 284 |
if (nrow(entry) == 0) |
| 285 |
next |
| 286 |
|
| 287 |
entry[["Type"]] <- type |
| 288 |
entry[["Name"]] <- names(dbs)[[i]] %||% "" |
| 289 |
return(entry) |
| 290 |
|
| 291 |
} |
| 292 |
|
| 293 |
# report package + version if both available |
| 294 |
pkgver <- if (length(version)) |
| 295 |
paste(package, version) |
| 296 |
else |
| 297 |
package |
| 298 |
|
| 299 |
fmt <- "failed to find %s for '%s' in package repositories" |
| 300 |
stopf(fmt, type, pkgver) |
| 301 |
|
| 302 |
} |
| 303 |
|
| 304 |
renv_available_packages_record <- function(entry, type) { |
| 305 |
|
| 306 |
# check if this record was already tagged |
| 307 |
if (renv_record_tagged(entry)) |
| 308 |
return(entry) |
| 309 |
|
| 310 |
# otherwise, construct it |
| 311 |
record <- entry |
| 312 |
|
| 313 |
if (identical(record$Name, "__renv_cellar__")) { |
| 314 |
record$Source <- "Cellar" |
| 315 |
record$Repository <- NULL |
| 316 |
record$Name <- NULL |
| 317 |
} else { |
| 318 |
record$Source <- "Repository" |
| 319 |
record$Repository <- entry$Name |
| 320 |
record$Name <- NULL |
| 321 |
} |
| 322 |
|
| 323 |
# form url |
| 324 |
url <- entry$Repository |
| 325 |
path <- entry$Path |
| 326 |
name <- entry$Name |
| 327 |
|
| 328 |
# if path was supplied, it should be used relative |
| 329 |
# to the repository URL |
| 330 |
if (length(path) && !is.na(path)) |
| 331 |
url <- paste(url, path, sep = "/") |
| 332 |
|
| 333 |
renv_record_tag( |
| 334 |
record = record, |
| 335 |
type = type, |
| 336 |
url = url, |
| 337 |
name = name |
| 338 |
) |
| 339 |
|
| 340 |
} |
| 341 |
|
| 342 |
renv_available_packages_latest_repos_impl <- function(package, type, repos) { |
| 343 |
|
| 344 |
# get available packages |
| 345 |
dbs <- available_packages( |
| 346 |
type = type, |
| 347 |
repos = repos, |
| 348 |
quiet = TRUE, |
| 349 |
cellar = TRUE |
| 350 |
) |
| 351 |
|
| 352 |
fields <- c( |
| 353 |
"Package", "Version", |
| 354 |
"OS_type", "NeedsCompilation", |
| 355 |
"Repository", "Path", "File" |
| 356 |
) |
| 357 |
|
| 358 |
entries <- bapply(dbs, function(db) { |
| 359 |
|
| 360 |
# extract entries for this package |
| 361 |
entries <- rows(db, db$Package == package) |
| 362 |
if (nrow(entries) == 0L) |
| 363 |
return(entries) |
| 364 |
|
| 365 |
# keep only compatible rows + the required fields |
| 366 |
cols(entries, intersect(fields, names(db))) |
| 367 |
|
| 368 |
}, index = "Name") |
| 369 |
|
| 370 |
if (is.null(entries)) |
| 371 |
return(NULL) |
| 372 |
|
| 373 |
# sort based on version |
| 374 |
version <- numeric_version(entries$Version) |
| 375 |
ordered <- order(version, decreasing = TRUE) |
| 376 |
|
| 377 |
# extract newest entry |
| 378 |
entry <- as.list(entries[ordered[[1L]], ]) |
| 379 |
|
| 380 |
# remove an NA file entry if necessary |
| 381 |
# https://github.com/rstudio/renv/issues/1045 |
| 382 |
if (length(entry$File) && is.na(entry$File)) |
| 383 |
entry$File <- NULL |
| 384 |
|
| 385 |
# return newest-available version |
| 386 |
renv_available_packages_record(entry, type) |
| 387 |
|
| 388 |
} |
| 389 |
|
| 390 |
renv_available_packages_latest <- function(package, |
| 391 |
type = NULL, |
| 392 |
repos = NULL) |
| 393 |
{ |
| 394 |
methods <- list( |
| 395 |
renv_available_packages_latest_repos, |
| 396 |
renv_available_packages_latest_crandb, |
| 397 |
if (getOption("renv.install.allowArchivedPackages", default = FALSE)) |
| 398 |
renv_available_packages_latest_archive, |
| 399 |
if (renv_p3m_enabled()) |
| 400 |
renv_available_packages_latest_p3m |
| 401 |
) |
| 402 |
|
| 403 |
errors <- stack() |
| 404 |
|
| 405 |
entries <- lapply(methods, function(method) { |
| 406 |
|
| 407 |
if (is.null(method)) |
| 408 |
return(NULL) |
| 409 |
|
| 410 |
entry <- catch(method(package, type, repos)) |
| 411 |
if (inherits(entry, "error")) { |
| 412 |
errors$push(entry) |
| 413 |
return(NULL) |
| 414 |
} |
| 415 |
|
| 416 |
entry |
| 417 |
|
| 418 |
}) |
| 419 |
|
| 420 |
# if both entries are null, error |
| 421 |
if (all(map_lgl(entries, is.null))) { |
| 422 |
map(errors$data(), warning) |
| 423 |
entry <- the$rejected_packages[[package]] |
| 424 |
if (!is.null(entry)) |
| 425 |
stopf("package '%s' is not available\n- %s", package, entry$reason) |
| 426 |
stopf("package '%s' is not available", package) |
| 427 |
} else if (is.null(entries[[2L]])) { |
| 428 |
return(entries[[1L]]) |
| 429 |
} else if (is.null(entries[[1L]])) { |
| 430 |
return(entries[[2L]]) |
| 431 |
} |
| 432 |
|
| 433 |
# extract both entries |
| 434 |
lhs <- entries[[1L]] |
| 435 |
rhs <- entries[[2L]] |
| 436 |
|
| 437 |
# extract versions |
| 438 |
lhsv <- package_version(lhs$Version %||% "0.0") |
| 439 |
rhsv <- package_version(rhs$Version %||% "0.0") |
| 440 |
|
| 441 |
# if the versions don't match, take the newest one |
| 442 |
if (lhsv > rhsv) |
| 443 |
return(lhs) |
| 444 |
else if (rhsv > lhsv) |
| 445 |
return(rhs) |
| 446 |
|
| 447 |
# otherwise, if we have a binary from the active package repositories, |
| 448 |
# use those; otherwise, use the mran binary |
| 449 |
if (identical(lhsv, rhsv)) { |
| 450 |
if (identical(attr(lhs, "type", exact = TRUE), "binary")) |
| 451 |
return(lhs) |
| 452 |
# when rhs is from crandb it has no URL; prefer lhs to avoid archive |
| 453 |
# fallback which fails for packages still live on CRAN (#1735) |
| 454 |
# renv_record_tagged is true if the record has a url and type. Crandb does not have URL. |
| 455 |
if (!renv_record_tagged(rhs)) |
| 456 |
return(lhs) |
| 457 |
return(rhs) |
| 458 |
} |
| 459 |
|
| 460 |
# otherwise, return the regular repository entry |
| 461 |
lhs |
| 462 |
|
| 463 |
} |
| 464 |
|
| 465 |
renv_available_packages_latest_p3m <- function(package, |
| 466 |
type = NULL, |
| 467 |
repos = NULL) |
| 468 |
{ |
| 469 |
type <- type %||% getOption("pkgType") |
| 470 |
if (identical(type, "source")) |
| 471 |
stop("binary packages are not available") |
| 472 |
|
| 473 |
# ensure local p3m database is up-to-date |
| 474 |
renv_p3m_database_refresh(explicit = FALSE) |
| 475 |
|
| 476 |
# attempt to read it |
| 477 |
database <- catch(renv_p3m_database_load()) |
| 478 |
if (inherits(database, "error")) |
| 479 |
return(database) |
| 480 |
|
| 481 |
# get entry for this version of R + platform |
| 482 |
suffix <- contrib.url("", type = "binary") |
| 483 |
entry <- database[[suffix]] |
| 484 |
if (is.null(entry)) |
| 485 |
stopf("no records available from repository URL '%s'", suffix) |
| 486 |
|
| 487 |
# find all available packages |
| 488 |
keys <- attr(entry, "keys") |
| 489 |
pattern <- paste0("^", package, " ") |
| 490 |
matching <- grep(pattern, keys, perl = TRUE, value = TRUE) |
| 491 |
if (empty(matching)) |
| 492 |
stopf("package '%s' is not available", package) |
| 493 |
|
| 494 |
# take the latest-available package |
| 495 |
entries <- unlist(mget(matching, envir = entry)) |
| 496 |
sorted <- sort(entries, decreasing = TRUE) |
| 497 |
key <- names(sorted)[[1L]] |
| 498 |
idate <- sorted[[1L]] |
| 499 |
|
| 500 |
# split into package, version |
| 501 |
index <- regexpr(" ", key, fixed = TRUE) |
| 502 |
version <- substring(key, index + 1) |
| 503 |
|
| 504 |
# return an appropriate record |
| 505 |
record <- list( |
| 506 |
Package = package, |
| 507 |
Version = version, |
| 508 |
Source = "Repository", |
| 509 |
Repository = "P3M" |
| 510 |
) |
| 511 |
|
| 512 |
# convert from integer to date |
| 513 |
date <- as.Date(idate, origin = "1970-01-01") |
| 514 |
|
| 515 |
# form url to binary package |
| 516 |
base <- renv_p3m_url(date, suffix) |
| 517 |
name <- renv_retrieve_name(record, type = "binary") |
| 518 |
url <- file.path(base, name) |
| 519 |
|
| 520 |
# tag record with url + type |
| 521 |
renv_record_tag( |
| 522 |
record = record, |
| 523 |
type = "binary", |
| 524 |
url = dirname(url), |
| 525 |
name = "P3M" |
| 526 |
) |
| 527 |
} |
| 528 |
|
| 529 |
renv_available_packages_latest_archive_query <- function(repo) { |
| 530 |
|
| 531 |
index( |
| 532 |
scope = "available-packages-archive", |
| 533 |
key = repo, |
| 534 |
value = tryCatch( |
| 535 |
renv_available_packages_latest_archive_query_impl(repo), |
| 536 |
error = function(e) list() |
| 537 |
) |
| 538 |
) |
| 539 |
|
| 540 |
} |
| 541 |
|
| 542 |
renv_available_packages_latest_archive_query_impl <- function(repo) { |
| 543 |
url <- file.path(repo, "src/contrib/Meta/archive.rds") |
| 544 |
destfile <- download(url, destfile = tempfile("archive-", fileext = ".rds"), quiet = TRUE) |
| 545 |
readRDS(destfile) |
| 546 |
} |
| 547 |
|
| 548 |
renv_available_packages_latest_archive <- function(package, |
| 549 |
type = NULL, |
| 550 |
repos = NULL) |
| 551 |
{ |
| 552 |
type <- type %||% getOption("pkgType") |
| 553 |
repos <- repos %||% getOption("repos") |
| 554 |
|
| 555 |
for (i in seq_along(repos)) { |
| 556 |
|
| 557 |
# extract pieces of interest |
| 558 |
name <- names(repos)[[i]] |
| 559 |
repo <- repos[[i]] |
| 560 |
|
| 561 |
# check for potential packages in archive |
| 562 |
archive <- renv_available_packages_latest_archive_query(repo) |
| 563 |
entries <- archive[[package]] |
| 564 |
if (NROW(entries) == 0L) |
| 565 |
next |
| 566 |
|
| 567 |
# parse the package name + version from the row names |
| 568 |
rns <- basename(row.names(entries)) |
| 569 |
|
| 570 |
# grab files that look like packages |
| 571 |
extpat <- "(?:\\.tar\\.gz|\\.tgz|\\.zip)$" |
| 572 |
parts <- strsplit(rns, "_", fixed = TRUE) |
| 573 |
package <- map_chr(parts, `[[`, 1L) |
| 574 |
rest <- map_chr(parts, `[[`, 2L) |
| 575 |
version <- sub(extpat, "", rest) |
| 576 |
|
| 577 |
# put it into a data.frame |
| 578 |
data <- data.frame(Package = package, Version = version) |
| 579 |
|
| 580 |
# take the newest version |
| 581 |
ord <- order(numeric_version(version), decreasing = TRUE) |
| 582 |
entry <- as.list(data[ord[[1L]], ]) |
| 583 |
entry$Source <- "Repository" |
| 584 |
entry$Repository <- name |
| 585 |
|
| 586 |
return(entry) |
| 587 |
|
| 588 |
} |
| 589 |
|
| 590 |
NULL |
| 591 |
|
| 592 |
} |
| 593 |
|
| 594 |
# Query crandb API to find the newest version of a package |
| 595 |
# that is compatible with the current version of R |
| 596 |
renv_available_packages_latest_crandb <- function(package, |
| 597 |
type = NULL, |
| 598 |
repos = NULL) |
| 599 |
{ |
| 600 |
# check if crandb lookups are enabled |
| 601 |
if (!config$crandb.enabled()) |
| 602 |
return(NULL) |
| 603 |
|
| 604 |
# query crandb for all versions of this package |
| 605 |
json <- renv_available_packages_crandb_query(package) |
| 606 |
if (is.null(json)) |
| 607 |
return(NULL) |
| 608 |
|
| 609 |
versions <- json$versions |
| 610 |
if (is.null(versions) || length(versions) == 0L) |
| 611 |
return(NULL) |
| 612 |
|
| 613 |
# get current R version |
| 614 |
rversion <- getRversion() |
| 615 |
|
| 616 |
# find versions compatible with this R version |
| 617 |
compatible <- Filter(function(v) { |
| 618 |
renv_available_packages_crandb_r_compatible(versions[[v]], rversion) |
| 619 |
}, names(versions)) |
| 620 |
|
| 621 |
if (length(compatible) == 0L) |
| 622 |
return(NULL) |
| 623 |
|
| 624 |
# sort by version and take the newest; keep original version string (e.g. "1.6-5") |
| 625 |
# so archive tarball names match CRAN (Matrix_1.6-5.tar.gz not Matrix_1.6.5.tar.gz) |
| 626 |
ord <- order(numeric_version(compatible), decreasing = TRUE) |
| 627 |
newest <- compatible[ord[[1L]]] |
| 628 |
|
| 629 |
# use first repository name if available |
| 630 |
repos <- repos %||% getOption("repos") |
| 631 |
reponame <- names(repos)[[1L]] %||% "CRAN" |
| 632 |
|
| 633 |
list( |
| 634 |
Package = package, |
| 635 |
Version = newest, |
| 636 |
Source = "Repository", |
| 637 |
Repository = reponame |
| 638 |
) |
| 639 |
|
| 640 |
} |
| 641 |
|
| 642 |
renv_available_packages_crandb_query <- function(package) { |
| 643 |
|
| 644 |
memoize( |
| 645 |
key = package, |
| 646 |
value = renv_available_packages_crandb_query_impl(package), |
| 647 |
scope = "crandb-history" |
| 648 |
) |
| 649 |
|
| 650 |
} |
| 651 |
|
| 652 |
renv_available_packages_crandb_query_impl <- function(package) { |
| 653 |
|
| 654 |
url <- sprintf("https://crandb.r-pkg.org/%s/all", URLencode(package, reserved = TRUE)) |
| 655 |
destfile <- tempfile("renv-crandb-", fileext = ".json") |
| 656 |
|
| 657 |
status <- catch(download(url, destfile = destfile, quiet = TRUE)) |
| 658 |
if (inherits(status, "error")) |
| 659 |
return(NULL) |
| 660 |
|
| 661 |
catch(renv_json_read(destfile)) |
| 662 |
|
| 663 |
} |
| 664 |
|
| 665 |
renv_available_packages_crandb_r_compatible <- function(entry, rversion) { |
| 666 |
|
| 667 |
# if no Depends field, assume compatible |
| 668 |
depends <- entry$Depends |
| 669 |
if (is.null(depends)) |
| 670 |
return(TRUE) |
| 671 |
|
| 672 |
# check for R dependency |
| 673 |
rdep <- depends$R |
| 674 |
if (is.null(rdep)) |
| 675 |
return(TRUE) |
| 676 |
|
| 677 |
# parse the requirement (e.g., ">= 3.4", ">= 4.0.0") |
| 678 |
renv_version_requirement_satisfied(rversion, rdep) |
| 679 |
|
| 680 |
} |
| 681 |
|
| 682 |
renv_version_requirement_satisfied <- function(version, requirement) { |
| 683 |
|
| 684 |
# handle "*" or empty requirements |
| 685 |
if (is.null(requirement) || requirement %in% c("*", "")) |
| 686 |
return(TRUE) |
| 687 |
|
| 688 |
# parse operator and version from requirement like ">= 3.4" |
| 689 |
pattern <- "^\\s*([><=!]+)\\s*([0-9.]+)\\s*$" |
| 690 |
if (!grepl(pattern, requirement)) |
| 691 |
return(TRUE) |
| 692 |
|
| 693 |
op <- sub(pattern, "\\1", requirement) |
| 694 |
reqver <- sub(pattern, "\\2", requirement) |
| 695 |
|
| 696 |
# perform comparison |
| 697 |
cmp <- renv_version_compare(version, reqver) |
| 698 |
|
| 699 |
switch( |
| 700 |
op, |
| 701 |
">=" = cmp >= 0L, |
| 702 |
">" = cmp > 0L, |
| 703 |
"<=" = cmp <= 0L, |
| 704 |
"<" = cmp < 0L, |
| 705 |
"==" = cmp == 0L, |
| 706 |
"!=" = cmp != 0L, |
| 707 |
TRUE |
| 708 |
) |
| 709 |
|
| 710 |
} |
| 711 |
|
| 712 |
renv_available_packages_latest_repos <- function(package, |
| 713 |
type = NULL, |
| 714 |
repos = NULL) |
| 715 |
{ |
| 716 |
type <- type %||% getOption("pkgType") |
| 717 |
repos <- repos %||% getOption("repos") |
| 718 |
|
| 719 |
# on platforms where only source packages are supported, `type = "both"` has |
| 720 |
# no meaningful distinction from source: base R rejects it outright, and |
| 721 |
# `contrib.url(repos, "binary")` simply collapses to the source URL. treating |
| 722 |
# it as "both" here would tag a source tarball as binary and cause the |
| 723 |
# installer to skip `R CMD INSTALL` (#2275). |
| 724 |
if (identical(.Platform$pkgType, "source") && identical(type, "both")) |
| 725 |
type <- "source" |
| 726 |
|
| 727 |
# detect requests for only source packages |
| 728 |
if (identical(type, "source")) |
| 729 |
return(renv_available_packages_latest_repos_impl(package, "source", repos)) |
| 730 |
|
| 731 |
# detect requests for only binary packages |
| 732 |
if (grepl("\\bbinary\\b", type)) |
| 733 |
return(renv_available_packages_latest_repos_impl(package, "binary", repos)) |
| 734 |
|
| 735 |
# otherwise, check both source and binary repositories |
| 736 |
src <- renv_available_packages_latest_repos_impl(package, "source", repos) |
| 737 |
bin <- renv_available_packages_latest_repos_impl(package, "binary", repos) |
| 738 |
|
| 739 |
# choose an appropriate record |
| 740 |
if (is.null(src) && is.null(bin)) |
| 741 |
stopf("package '%s' is not available", package) |
| 742 |
else if (is.null(src)) |
| 743 |
renv_available_packages_record(bin, "binary") |
| 744 |
else if (is.null(bin)) |
| 745 |
renv_available_packages_record(src, "source") |
| 746 |
else |
| 747 |
renv_available_packages_latest_select(src, bin) |
| 748 |
} |
| 749 |
|
| 750 |
renv_available_packages_latest_select <- function(src, bin) { |
| 751 |
|
| 752 |
# if the binary is at least as old as the source version, |
| 753 |
# then use the binary version |
| 754 |
if (renv_version_compare(bin$Version, src$Version) >= 0) |
| 755 |
return(renv_available_packages_record(bin, "binary")) |
| 756 |
|
| 757 |
# if the user has requested we skip source repositories, |
| 758 |
# use the binary anyway |
| 759 |
ipcs <- getOption("install.packages.check.source", default = "yes") |
| 760 |
if (!identical(ipcs, "yes")) |
| 761 |
return(renv_available_packages_record(bin, "binary")) |
| 762 |
|
| 763 |
# if the package requires compilation, check to see whether |
| 764 |
# the user has opted in to compiling packages from source |
| 765 |
nc <- identical(src$NeedsCompilation, "yes") |
| 766 |
if (nc) { |
| 767 |
|
| 768 |
# check user preference re: compilation from source |
| 769 |
ipcfs <- getOption( |
| 770 |
"install.packages.compile.from.source", |
| 771 |
default = Sys.getenv("R_COMPILE_AND_INSTALL_PACKAGES") |
| 772 |
) |
| 773 |
|
| 774 |
# if make is not available, then we can't build from source |
| 775 |
make <- Sys.getenv("MAKE", unset = "make") |
| 776 |
if (!nzchar(Sys.which(make))) |
| 777 |
ipcfs <- "never" |
| 778 |
|
| 779 |
# if we're on macOS and command line tools are not available, |
| 780 |
# then we can't build from sources |
| 781 |
if (renv_platform_macos() && !renv_xcode_available()) |
| 782 |
ipcfs <- "never" |
| 783 |
|
| 784 |
if (identical(ipcfs, "never")) |
| 785 |
return(renv_available_packages_record(bin, "binary")) |
| 786 |
|
| 787 |
} |
| 788 |
|
| 789 |
# take the source version |
| 790 |
renv_available_packages_record(src, "source") |
| 791 |
|
| 792 |
} |
| 793 |
|
| 794 |
renv_available_packages_cellar <- function(type, project = NULL) { |
| 795 |
|
| 796 |
# look in the cellar |
| 797 |
project <- renv_project_resolve(project) |
| 798 |
roots <- renv_cellar_roots(project = project) |
| 799 |
|
| 800 |
# look for packages |
| 801 |
all <- list.files( |
| 802 |
path = roots, |
| 803 |
all.files = TRUE, |
| 804 |
full.names = TRUE, |
| 805 |
recursive = TRUE, |
| 806 |
include.dirs = FALSE |
| 807 |
) |
| 808 |
|
| 809 |
# keep only files with matching extensions |
| 810 |
ext <- renv_package_ext(type = type) |
| 811 |
keep <- all[fileext(all) %in% ext] |
| 812 |
|
| 813 |
# construct records for each cellar entry |
| 814 |
records <- lapply(keep, function(path) { |
| 815 |
|
| 816 |
# infer package name, version from tarball name |
| 817 |
base <- basename(keep) |
| 818 |
idx <- regexpr("_", base, fixed = TRUE) |
| 819 |
package <- substring(base, 1L, idx - 1L) |
| 820 |
version <- substring(base, idx + 1L, nchar(base) - nchar(ext)) |
| 821 |
|
| 822 |
# set the Repository field |
| 823 |
prefix <- if (renv_platform_windows()) "file:///" else "file://" |
| 824 |
repository <- paste0(prefix, dirname(path)) |
| 825 |
|
| 826 |
# build record |
| 827 |
list( |
| 828 |
Package = package, |
| 829 |
Version = version, |
| 830 |
Repository = repository |
| 831 |
) |
| 832 |
|
| 833 |
}) |
| 834 |
|
| 835 |
bind(records) |
| 836 |
|
| 837 |
} |
| 838 |
|
| 839 |
renv_available_packages_filter <- function(db) { |
| 840 |
|
| 841 |
# sanity check |
| 842 |
if (is.null(db) || nrow(db) == 0L) |
| 843 |
return(db) |
| 844 |
|
| 845 |
# TODO: subarch? duplicates? |
| 846 |
# remove packages which won't work on this OS |
| 847 |
db <- renv_available_packages_filter_ostype(db) |
| 848 |
db <- renv_available_packages_filter_version(db) |
| 849 |
|
| 850 |
# return filtered database |
| 851 |
db |
| 852 |
|
| 853 |
} |
| 854 |
|
| 855 |
renv_available_packages_filter_ostype <- function(db) { |
| 856 |
ostype <- db$OS_type |
| 857 |
ok <- is.na(ostype) | ostype %in% .Platform$OS.type |
| 858 |
rows(db, ok) |
| 859 |
} |
| 860 |
|
| 861 |
renv_available_packages_filter_version <- function(db) { |
| 862 |
|
| 863 |
depends <- db$Depends |
| 864 |
|
| 865 |
# find the packages which express an R dependency |
| 866 |
splat <- strsplit(depends, "\\s*,\\s*", perl = TRUE) |
| 867 |
|
| 868 |
# remove the non-R dependencies |
| 869 |
table <- c("R ", "R\n", "R(") |
| 870 |
splat <- map(splat, function(requirements) { |
| 871 |
requirements[match(substr(requirements, 1L, 2L), table, 0L) != 0L] |
| 872 |
}) |
| 873 |
|
| 874 |
# collect the unique R dependencies |
| 875 |
dependencies <- unique(unlist(splat)) |
| 876 |
|
| 877 |
# convert this to a simpler form |
| 878 |
pattern <- "^R\\s*\\(([^\\d\\s+]+)\\s*([^\\)]+)\\)$" |
| 879 |
matches <- gsub(pattern, "\\1 \\2", dependencies, perl = TRUE) |
| 880 |
|
| 881 |
# split into operator and version |
| 882 |
idx <- regexpr(" ", matches, fixed = TRUE) |
| 883 |
ops <- substring(matches, 1L, idx - 1L) |
| 884 |
version <- numeric_version(substring(matches, idx + 1L)) |
| 885 |
|
| 886 |
# bundle the calls for efficiency |
| 887 |
ok <- rep.int(NA, length(ops)) |
| 888 |
names(ok) <- dependencies |
| 889 |
|
| 890 |
# iterate over the operations, and update our vector |
| 891 |
rversion <- getRversion() |
| 892 |
for (op in unique(ops)) { |
| 893 |
idx <- ops == op |
| 894 |
ok[idx] <- do.call(op, list(rversion, version[idx])) |
| 895 |
} |
| 896 |
|
| 897 |
# now, map the names back to their computed values, and check whether |
| 898 |
# all requirements were satisfied |
| 899 |
ok <- map_lgl(splat, function(requirements) { |
| 900 |
all(ok[requirements]) |
| 901 |
}) |
| 902 |
|
| 903 |
# record packages rejected due to R version incompatibility |
| 904 |
rejected <- which(!ok) |
| 905 |
if (length(rejected)) { |
| 906 |
packages <- db$Package[rejected] |
| 907 |
versions <- db$Version[rejected] |
| 908 |
rdeps <- map_chr(splat[rejected], paste, collapse = ", ") |
| 909 |
for (i in seq_along(packages)) { |
| 910 |
fmt <- "%s %s requires %s, but R %s is being used" |
| 911 |
the$rejected_packages[[packages[[i]]]] <- list( |
| 912 |
package = packages[[i]], |
| 913 |
reason = sprintf(fmt, packages[[i]], versions[[i]], rdeps[[i]], rversion) |
| 914 |
) |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
rows(db, ok) |
| 919 |
|
| 920 |
} |
| 921 |
|
| 922 |
# flattens available packages, keeping only the newest version |
| 923 |
renv_available_packages_flatten <- function(dbs) { |
| 924 |
|
| 925 |
# stack the databases together |
| 926 |
stacked <- bind(dbs) |
| 927 |
|
| 928 |
# order by package + version (descending) |
| 929 |
vrank <- renv_version_rank(stacked$Version) |
| 930 |
index <- order(stacked$Package, vrank, decreasing = TRUE) |
| 931 |
ordered <- rows(stacked, index) |
| 932 |
|
| 933 |
# remove duplicates |
| 934 |
dupes <- duplicated(ordered$Package) |
| 935 |
filtered <- rows(ordered, !dupes) |
| 936 |
|
| 937 |
# ready to return |
| 938 |
filtered |
| 939 |
|
| 940 |
} |