| 1 |
|
| 2 |
renv_package_find <- function(package, lib.loc = NULL) { |
| 3 |
map_chr(package, renv_package_find_impl, lib.loc = lib.loc) |
| 4 |
} |
| 5 |
|
| 6 |
renv_package_find_impl <- function(package, lib.loc = NULL, compat = FALSE) { |
| 7 |
|
| 8 |
# if we've been given the path to an existing package, use it as-is |
| 9 |
if (grepl("/", package) && file.exists(file.path(package, "DESCRIPTION"))) |
| 10 |
return(renv_path_normalize(package, mustWork = TRUE)) |
| 11 |
|
| 12 |
# if we're trying to be compatible with find.package(), then |
| 13 |
# check loaded namespaces before library paths if appropriate |
| 14 |
if (compat) { |
| 15 |
|
| 16 |
if (is.null(lib.loc) && isNamespaceLoaded(package)) { |
| 17 |
path <- renv_namespace_path(package) |
| 18 |
if (file.exists(path)) |
| 19 |
return(path) |
| 20 |
} |
| 21 |
|
| 22 |
} |
| 23 |
|
| 24 |
# check library paths |
| 25 |
for (libpath in (lib.loc %||% .libPaths())) { |
| 26 |
pkgpath <- file.path(libpath, package) |
| 27 |
descpath <- file.path(pkgpath, "DESCRIPTION") |
| 28 |
if (file.exists(descpath)) |
| 29 |
return(pkgpath) |
| 30 |
} |
| 31 |
|
| 32 |
# check for loaded namespaces |
| 33 |
if (!compat) { |
| 34 |
|
| 35 |
if (is.null(lib.loc) && isNamespaceLoaded(package)) { |
| 36 |
path <- renv_namespace_path(package) |
| 37 |
if (file.exists(path)) |
| 38 |
return(path) |
| 39 |
} |
| 40 |
|
| 41 |
} |
| 42 |
|
| 43 |
# failed to find package |
| 44 |
"" |
| 45 |
} |
| 46 |
|
| 47 |
renv_package_installed <- function(package, lib.loc = NULL) { |
| 48 |
paths <- renv_package_find(package, lib.loc = lib.loc %||% renv_libpaths_all()) |
| 49 |
nzchar(paths) |
| 50 |
} |
| 51 |
|
| 52 |
renv_package_available <- function(package) { |
| 53 |
package %in% loadedNamespaces() || renv_package_installed(package) |
| 54 |
} |
| 55 |
|
| 56 |
renv_package_version <- function(package) { |
| 57 |
renv_package_description_field(package, "Version") |
| 58 |
} |
| 59 |
|
| 60 |
renv_package_description_field <- function(package, field) { |
| 61 |
|
| 62 |
path <- renv_package_find(package) |
| 63 |
if (!nzchar(path)) |
| 64 |
return(NULL) |
| 65 |
|
| 66 |
desc <- renv_description_read(path) |
| 67 |
desc[[field]] |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
renv_package_type <- function(path, quiet = FALSE, default = "source") { |
| 72 |
|
| 73 |
info <- renv_file_info(path) |
| 74 |
if (is.na(info$isdir)) |
| 75 |
stopf("no package at path '%s'", renv_path_aliased(path)) |
| 76 |
|
| 77 |
# for directories, check for Meta |
| 78 |
if (info$isdir) { |
| 79 |
hasmeta <- file.exists(file.path(path, "Meta")) |
| 80 |
type <- if (hasmeta) "binary" else "source" |
| 81 |
return(type) |
| 82 |
} |
| 83 |
|
| 84 |
# otherwise, guess based on contents of package |
| 85 |
methods <- list( |
| 86 |
tar = function(path) untar(tarfile = path, list = TRUE), |
| 87 |
zip = function(path) unzip(zipfile = path, list = TRUE)$Name |
| 88 |
) |
| 89 |
|
| 90 |
# guess appropriate method when possible |
| 91 |
type <- renv_archive_type(path) |
| 92 |
if (type %in% c("tar", "zip")) |
| 93 |
methods <- methods[type] |
| 94 |
|
| 95 |
for (method in methods) { |
| 96 |
|
| 97 |
# suppress warnings to avoid issues with e.g. |
| 98 |
# 'skipping pax global extended headers' when |
| 99 |
# using internal tar |
| 100 |
files <- catch(suppressWarnings(method(path))) |
| 101 |
if (inherits(files, "error")) |
| 102 |
next |
| 103 |
|
| 104 |
hasmeta <- any(grepl("^[^/]+/Meta/?$", files)) |
| 105 |
type <- if (hasmeta) "binary" else "source" |
| 106 |
return(type) |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
if (!quiet) { |
| 111 |
fmt <- "failed to determine type of package '%s'; assuming source" |
| 112 |
warningf(fmt, renv_path_aliased(path)) |
| 113 |
} |
| 114 |
|
| 115 |
default |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
renv_package_ext <- function(type) { |
| 120 |
|
| 121 |
# always use '.tar.gz' for source packages |
| 122 |
type <- match.arg(type, c("binary", "source")) |
| 123 |
if (type == "source") |
| 124 |
return(".tar.gz") |
| 125 |
|
| 126 |
# otherwise, infer appropriate extension based on platform |
| 127 |
case( |
| 128 |
renv_platform_macos() ~ ".tgz", |
| 129 |
renv_platform_windows() ~ ".zip", |
| 130 |
renv_platform_unix() ~ ".tar.gz" |
| 131 |
) |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
renv_package_pkgtypes <- function() { |
| 136 |
|
| 137 |
# only use binaries if the user has specifically requested it |
| 138 |
# and binaries are available for this installation of R |
| 139 |
# (users may want to install from sources explicitly to take |
| 140 |
# advantage of custom local compiler configurations) |
| 141 |
binaries <- |
| 142 |
!identical(.Platform$pkgType, "source") && |
| 143 |
!identical(getOption("pkgType"), "source") |
| 144 |
|
| 145 |
if (binaries) c("binary", "source") else "source" |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
renv_package_augment_standard <- function(path, record) { |
| 150 |
|
| 151 |
# use the installed package's DESCRIPTION as the canonical base |
| 152 |
descpath <- file.path(path, "DESCRIPTION") |
| 153 |
desc <- renv_description_read(descpath) |
| 154 |
|
| 155 |
# NOTE: we use '^Remote(?!s$)' to match remote metadata fields |
| 156 |
# (RemoteType, RemoteRef, RemoteSha, etc.) but not the 'Remotes' |
| 157 |
# field, which lists dependency remotes for the package |
| 158 |
pattern <- "^Remote(?!s$)" |
| 159 |
|
| 160 |
# if the record has Remote fields (e.g. from a GitHub resolve or repair), |
| 161 |
# overlay them onto the DESCRIPTION -- these take precedence since the |
| 162 |
# caller is explicitly providing updated remote metadata |
| 163 |
existing <- record[grep(pattern, names(record), perl = TRUE)] |
| 164 |
if (length(existing)) { |
| 165 |
existing$RemoteType <- existing$RemoteType %||% renv_record_source(record) |
| 166 |
return(overlay(desc, existing)) |
| 167 |
} |
| 168 |
|
| 169 |
# if the DESCRIPTION already has Remote fields, use as-is (e.g. r-universe) |
| 170 |
if (any(grepl(pattern, names(desc), perl = TRUE))) |
| 171 |
return(desc) |
| 172 |
|
| 173 |
# check whether we tagged a url + type for building standard remotes |
| 174 |
url <- attr(record, "url", exact = TRUE) |
| 175 |
type <- attr(record, "type", exact = TRUE) |
| 176 |
name <- attr(record, "name", exact = TRUE) |
| 177 |
if (is.null(url) || is.null(type)) |
| 178 |
return(desc) |
| 179 |
|
| 180 |
source <- renv_record_source(record, normalize = TRUE) |
| 181 |
if (!identical(source, "repository")) |
| 182 |
return(desc) |
| 183 |
|
| 184 |
# figure out base of repository URL |
| 185 |
repos <- renv_repos_baseurl(url) |
| 186 |
|
| 187 |
# figure out the platform |
| 188 |
platform <- if (identical(type, "binary")) R.version$platform else "source" |
| 189 |
|
| 190 |
# build pak-compatible standard remote reference |
| 191 |
remotes <- list( |
| 192 |
RemoteType = "standard", |
| 193 |
RemoteRef = desc$Package, |
| 194 |
RemotePkgRef = desc$Package, |
| 195 |
RemoteRepos = repos, |
| 196 |
RemoteReposName = name, |
| 197 |
RemotePkgPlatform = platform, |
| 198 |
RemoteSha = desc$Version |
| 199 |
) |
| 200 |
|
| 201 |
# if the repository value and name are identical, then exclude the name |
| 202 |
if (identical(repos, name)) |
| 203 |
remotes$RemoteReposName <- NULL |
| 204 |
|
| 205 |
overlay(desc, remotes) |
| 206 |
|
| 207 |
} |
| 208 |
|
| 209 |
renv_package_augment <- function(installpath, record) { |
| 210 |
|
| 211 |
# figure out if we should write this as a 'standard' remote |
| 212 |
record <- renv_package_augment_standard(installpath, record) |
| 213 |
|
| 214 |
# check for remotes fields |
| 215 |
remotes <- record[grep("^Remote", names(record))] |
| 216 |
if (empty(remotes)) |
| 217 |
return(FALSE) |
| 218 |
|
| 219 |
# for backwards compatibility with older versions of Packrat, |
| 220 |
# we write out 'Github*' fields as well |
| 221 |
if (identical(tolower(remotes$RemoteType), "github")) { |
| 222 |
|
| 223 |
map <- list( |
| 224 |
"GithubHost" = "RemoteHost", |
| 225 |
"GithubRepo" = "RemoteRepo", |
| 226 |
"GithubUsername" = "RemoteUsername", |
| 227 |
"GithubRef" = "RemoteRef", |
| 228 |
"GithubSHA1" = "RemoteSha" |
| 229 |
) |
| 230 |
|
| 231 |
enumerate(map, function(old, new) { |
| 232 |
remotes[[old]] <<- remotes[[old]] %||% remotes[[new]] |
| 233 |
}) |
| 234 |
|
| 235 |
} |
| 236 |
|
| 237 |
# ensure RemoteType field is written out |
| 238 |
remotes$RemoteType <- remotes$RemoteType %||% renv_record_source(record) |
| 239 |
remotes <- remotes[c("RemoteType", renv_vector_diff(names(remotes), "RemoteType"))] |
| 240 |
|
| 241 |
# update package items |
| 242 |
renv_package_augment_description(installpath, remotes) |
| 243 |
renv_package_augment_metadata(installpath, remotes) |
| 244 |
|
| 245 |
} |
| 246 |
|
| 247 |
renv_package_augment_impl <- function(data, remotes) { |
| 248 |
remotes <- remotes[map_lgl(remotes, Negate(is.null))] |
| 249 |
nonremotes <- grep("^(?:Remote|Github)", names(data), invert = TRUE) |
| 250 |
remotes[["Remotes"]] <- data[["Remotes"]] %||% remotes[["Remotes"]] |
| 251 |
c(data[nonremotes], remotes) |
| 252 |
} |
| 253 |
|
| 254 |
renv_package_augment_description <- function(path, remotes) { |
| 255 |
|
| 256 |
descpath <- file.path(path, "DESCRIPTION") |
| 257 |
|
| 258 |
before <- renv_description_read(descpath) |
| 259 |
after <- renv_package_augment_impl(before, remotes) |
| 260 |
if (identical(before, after)) |
| 261 |
return(FALSE) |
| 262 |
|
| 263 |
renv_dcf_write(after, file = descpath) |
| 264 |
|
| 265 |
} |
| 266 |
|
| 267 |
renv_package_augment_metadata <- function(path, remotes) { |
| 268 |
|
| 269 |
metapath <- file.path(path, "Meta/package.rds") |
| 270 |
if (!file.exists(metapath)) |
| 271 |
return(FALSE) |
| 272 |
|
| 273 |
meta <- readRDS(metapath) |
| 274 |
before <- as.list(meta$DESCRIPTION) |
| 275 |
after <- renv_package_augment_impl(before, remotes) |
| 276 |
if (identical(before, after)) |
| 277 |
return(FALSE) |
| 278 |
|
| 279 |
meta$DESCRIPTION <- map_chr(after, identity) |
| 280 |
saveRDS(meta, file = metapath, version = 2L) |
| 281 |
|
| 282 |
} |
| 283 |
|
| 284 |
# find recursive dependencies of a package. note that this routine |
| 285 |
# doesn't farm out to CRAN; it relies on the package and its dependencies |
| 286 |
# all being installed locally. returns a named vector mapping package names |
| 287 |
# to the path where they were discovered, or an empty string if those packages |
| 288 |
# are not installed |
| 289 |
renv_package_dependencies <- function(packages, |
| 290 |
libpaths = NULL, |
| 291 |
fields = NULL, |
| 292 |
callback = NULL, |
| 293 |
project = NULL) |
| 294 |
{ |
| 295 |
visited <- new.env(parent = emptyenv()) |
| 296 |
ignored <- renv_project_ignored_packages(project = project) |
| 297 |
packages <- renv_vector_diff(packages, ignored) |
| 298 |
libpaths <- libpaths %||% renv_libpaths_all() |
| 299 |
fields <- fields %||% settings$package.dependency.fields(project = project) |
| 300 |
callback <- callback %||% function(package, location, project) location |
| 301 |
project <- renv_project_resolve(project) |
| 302 |
|
| 303 |
for (package in packages) |
| 304 |
renv_package_dependencies_impl(package, visited, libpaths, fields, callback, project) |
| 305 |
|
| 306 |
as.list(visited) |
| 307 |
} |
| 308 |
|
| 309 |
renv_package_dependencies_impl <- function(package, |
| 310 |
visited, |
| 311 |
libpaths, |
| 312 |
fields = NULL, |
| 313 |
callback = NULL, |
| 314 |
project = NULL) |
| 315 |
{ |
| 316 |
# skip the 'R' package |
| 317 |
if (package == "R") |
| 318 |
return() |
| 319 |
|
| 320 |
# if we've already visited this package, bail |
| 321 |
if (!is.null(visited[[package]])) |
| 322 |
return() |
| 323 |
|
| 324 |
# default to unknown path for visited packages |
| 325 |
visited[[package]] <- "" |
| 326 |
|
| 327 |
# find the package -- note that we perform a permissive lookup here |
| 328 |
# because we want to capture potentially invalid / broken package installs |
| 329 |
# (that is, the 'package' we find might be an incomplete or broken package |
| 330 |
# installation at this point) |
| 331 |
location <- find(libpaths, function(libpath) { |
| 332 |
candidate <- file.path(libpath, package) |
| 333 |
if (renv_file_exists(candidate)) |
| 334 |
return(candidate) |
| 335 |
}) |
| 336 |
|
| 337 |
if (is.null(location)) |
| 338 |
return(callback(package, "", project)) |
| 339 |
|
| 340 |
# we know the path, so set it now |
| 341 |
visited[[package]] <- callback(package, location, project) |
| 342 |
|
| 343 |
# find its dependencies from the DESCRIPTION file |
| 344 |
deps <- renv_dependencies_discover_description(location, fields = "strong") |
| 345 |
subpackages <- deps$Package |
| 346 |
for (subpackage in subpackages) |
| 347 |
renv_package_dependencies_impl(subpackage, visited, libpaths, fields, callback, project) |
| 348 |
} |
| 349 |
|
| 350 |
renv_package_hook <- function(package, hook) { |
| 351 |
if (package %in% loadedNamespaces()) |
| 352 |
hook() |
| 353 |
else |
| 354 |
setHook(packageEvent(package, "onLoad"), hook) |
| 355 |
} |
| 356 |
|
| 357 |
renv_package_shlib <- function(package) { |
| 358 |
|
| 359 |
pkgpath <- renv_package_find(package) |
| 360 |
|
| 361 |
pkgname <- basename(package) |
| 362 |
if (pkgname == "data.table") |
| 363 |
pkgname <- "datatable" |
| 364 |
|
| 365 |
libname <- paste0(pkgname, .Platform$dynlib.ext) |
| 366 |
file.path(pkgpath, "libs", libname) |
| 367 |
|
| 368 |
} |
| 369 |
|
| 370 |
renv_package_built <- function(path) { |
| 371 |
|
| 372 |
info <- renv_file_info(path) |
| 373 |
|
| 374 |
# list files in package |
| 375 |
isarchive <- identical(info$isdir, FALSE) |
| 376 |
files <- if (isarchive) |
| 377 |
renv_archive_list(path) |
| 378 |
else |
| 379 |
list.files(path, full.names = TRUE, recursive = TRUE) |
| 380 |
|
| 381 |
# for a source package, the canonical way to determine if it has already |
| 382 |
# been built is the presence of a 'Packaged:' field in the DESCRIPTION file |
| 383 |
# ('Built:' for binary packages) but we want to avoid the overhead of |
| 384 |
# unpacking the package if at all possible |
| 385 |
pattern <- "/(?:MD5$|INDEX/|Meta/package\\.rds$)" |
| 386 |
matches <- grep(pattern, files) |
| 387 |
if (length(matches) != 0L) |
| 388 |
return(TRUE) |
| 389 |
|
| 390 |
# if the above failed, then we'll use the contents of the DESCRIPTION file |
| 391 |
descpaths <- grep("/DESCRIPTION$", files, value = TRUE) |
| 392 |
if (length(descpaths) == 0L) |
| 393 |
return(FALSE) |
| 394 |
|
| 395 |
n <- nchar(descpaths) |
| 396 |
descpath <- descpaths[n == min(n)] |
| 397 |
contents <- if (isarchive) |
| 398 |
renv_archive_read(path, descpath) |
| 399 |
else |
| 400 |
readLines(descpath, warn = FALSE) |
| 401 |
|
| 402 |
# check for signs it was built |
| 403 |
pattern <- "^(?:Packaged|Built):" |
| 404 |
matches <- grep(pattern, contents) |
| 405 |
if (length(matches) != 0L) |
| 406 |
return(TRUE) |
| 407 |
|
| 408 |
# does not appear to be a source package |
| 409 |
FALSE |
| 410 |
|
| 411 |
} |
| 412 |
|
| 413 |
renv_package_unpack <- function(package, path, subdir = "", force = FALSE) { |
| 414 |
|
| 415 |
# if this isn't an archive, nothing to do |
| 416 |
info <- renv_file_info(path) |
| 417 |
if (identical(info$isdir, TRUE)) |
| 418 |
return(path) |
| 419 |
|
| 420 |
# find DESCRIPTION files in the archive |
| 421 |
descpaths <- renv_archive_find(path, "(?:^|/)DESCRIPTION$") |
| 422 |
|
| 423 |
# try to resolve the path to the DESCRIPTION file in the archive |
| 424 |
descpath <- if (nzchar(subdir)) { |
| 425 |
pattern <- sprintf("(?:^|/)\\Q%s\\E/DESCRIPTION$", subdir) |
| 426 |
grep(pattern, descpaths, perl = TRUE, value = TRUE) |
| 427 |
} else { |
| 428 |
n <- nchar(descpaths) |
| 429 |
descpaths[n == min(n)] |
| 430 |
} |
| 431 |
|
| 432 |
# if this failed, check for a top-level DESCRIPTION file |
| 433 |
# this is done in case the archive has been already been re-packed, so that a |
| 434 |
# package originally located within a sub-directory is now at the top level |
| 435 |
if (!force && length(descpath) != 1L) { |
| 436 |
descpath <- grep("^[^/]+/DESCRIPTION$", descpaths, perl = TRUE, value = TRUE) |
| 437 |
if (length(descpath)) |
| 438 |
return(path) |
| 439 |
} |
| 440 |
|
| 441 |
# if this still failed, error |
| 442 |
if (length(descpath) != 1L) { |
| 443 |
fmt <- "internal error: couldn't find DESCRIPTION file for package '%s' in archive '%s'" |
| 444 |
stopf(fmt, package, path) |
| 445 |
} |
| 446 |
|
| 447 |
# create extraction directory |
| 448 |
old <- renv_scope_tempfile("renv-package-old-") |
| 449 |
new <- renv_scope_tempfile("renv-package-new-", scope = parent.frame()) |
| 450 |
ensure_directory(c(old, new)) |
| 451 |
|
| 452 |
# decompress archive to dir |
| 453 |
renv_archive_decompress(path, exdir = old) |
| 454 |
|
| 455 |
# rename (without sub-directory) |
| 456 |
oldpath <- file.path(old, dirname(descpath)) |
| 457 |
newpath <- file.path(new, package) |
| 458 |
|
| 459 |
# https://github.com/rstudio/renv/issues/2156 |
| 460 |
status <- tryCatch(file.rename(oldpath, newpath), condition = identity) |
| 461 |
if (identical(status, TRUE)) |
| 462 |
return(newpath) |
| 463 |
|
| 464 |
fmt <- "file.rename() failed while unpacking package %s; retrying..." |
| 465 |
dlog("package", fmt, package) |
| 466 |
|
| 467 |
for (i in 1:60) { |
| 468 |
Sys.sleep(1) |
| 469 |
status <- tryCatch(file.rename(oldpath, newpath), condition = identity) |
| 470 |
if (identical(status, TRUE)) |
| 471 |
return(newpath) |
| 472 |
} |
| 473 |
|
| 474 |
stop(status) |
| 475 |
|
| 476 |
} |
| 477 |
|
| 478 |
renv_package_libsdir <- function(package, arch = NULL) { |
| 479 |
package <- renv_package_find(package) |
| 480 |
arch <- arch %||% if (nzchar(.Platform$r_arch)) .Platform$r_arch |
| 481 |
paste(c(package, "libs", arch), collapse = "/") |
| 482 |
} |