| 1 |
|
| 2 |
#' Checkout a repository |
| 3 |
#' |
| 4 |
#' `renv::checkout()` can be used to retrieve the latest-available packages from |
| 5 |
#' a (set of) package repositories. |
| 6 |
#' |
| 7 |
#' `renv::checkout()` is most useful with services like the Posit's |
| 8 |
#' [Package Manager](https://packagemanager.rstudio.com/), as it |
| 9 |
#' can be used to switch between different repository snapshots within an |
| 10 |
#' renv project. In this way, you can upgrade (or downgrade) all of the |
| 11 |
#' packages used in a particular renv project to the package versions |
| 12 |
#' provided by a particular snapshot. |
| 13 |
#' |
| 14 |
#' Note that calling `renv::checkout()` will also install the version of `renv` |
| 15 |
#' available as of the requested snapshot date, which might be older or lack |
| 16 |
#' features available in the currently-installed version of `renv`. In addition, |
| 17 |
#' the project's `renv/activate.R` script will be re-generated after checkout. |
| 18 |
#' If this is undesired, you can re-install a newer version of `renv` after |
| 19 |
#' checkout from your regular \R package repository. |
| 20 |
#' |
| 21 |
#' @section Caveats: |
| 22 |
#' |
| 23 |
#' If your library contains packages installed from other remote sources (e.g. |
| 24 |
#' GitHub), but a version of a package of the same name is provided by the |
| 25 |
#' repositories being checked out, then please be aware that the package will be |
| 26 |
#' replaced with the version provided by the requested repositories. This could |
| 27 |
#' be a concern if your project uses \R packages from GitHub whose name matches |
| 28 |
#' that of an existing CRAN package, but is otherwise unrelated to the package |
| 29 |
#' on CRAN. |
| 30 |
#' |
| 31 |
#' @inheritParams renv-params |
| 32 |
#' |
| 33 |
#' @param repos The \R package repositories to use. |
| 34 |
#' |
| 35 |
#' @param packages The packages to be installed. When `NULL` (the default), |
| 36 |
#' all packages currently used in the project will be installed, as |
| 37 |
#' determined by [renv::dependencies()]. The recursive dependencies of these |
| 38 |
#' packages will be included as well. |
| 39 |
#' |
| 40 |
#' @param date The snapshot date to use. When set, the associated snapshot as |
| 41 |
#' available from the Posit's public |
| 42 |
#' [Package Manager](https://packagemanager.rstudio.com/) instance will be |
| 43 |
#' used. Ignored if `repos` is non-`NULL`. |
| 44 |
#' |
| 45 |
#' @param restart Should the \R session be restarted after the new |
| 46 |
#' packages have been checked out? When `NULL` (the default), the |
| 47 |
#' session is restarted if the `"restore"` action was taken. |
| 48 |
#' |
| 49 |
#' @param actions The action(s) to perform with the requested repositories. |
| 50 |
#' This can either be `"snapshot"`, in which `renv` will generate a lockfile |
| 51 |
#' based on the latest versions of the packages available from `repos`, or |
| 52 |
#' `"restore"` if you'd like to install those packages. You can use |
| 53 |
#' `c("snapshot", "restore")` if you'd like to generate a lockfile and |
| 54 |
#' install those packages in a single call. When `actions` contains |
| 55 |
#' `"snapshot"`, the lockfile is generated from repository metadata |
| 56 |
#' (the `PACKAGES` file) without installing packages, so it may contain |
| 57 |
#' only a subset of the fields present in a lockfile generated by |
| 58 |
#' [renv::snapshot()]. |
| 59 |
#' |
| 60 |
#' @examples |
| 61 |
#' \dontrun{ |
| 62 |
#' |
| 63 |
#' # check out packages from PPM using the date '2023-01-02' |
| 64 |
#' renv::checkout(date = "2023-01-02") |
| 65 |
#' |
| 66 |
#' # alternatively, supply the full repository path |
| 67 |
#' renv::checkout(repos = c(PPM = "https://packagemanager.rstudio.com/cran/2023-01-02")) |
| 68 |
#' |
| 69 |
#' # only check out some subset of packages (and their recursive dependencies) |
| 70 |
#' renv::checkout(packages = "dplyr", date = "2023-01-02") |
| 71 |
#' |
| 72 |
#' # generate a lockfile based on a snapshot date |
| 73 |
#' renv::checkout(date = "2023-01-02", actions = "snapshot") |
| 74 |
#' |
| 75 |
#' } |
| 76 |
#' @export |
| 77 |
checkout <- function(repos = NULL, |
| 78 |
..., |
| 79 |
packages = NULL, |
| 80 |
date = NULL, |
| 81 |
clean = FALSE, |
| 82 |
actions = "restore", |
| 83 |
dependencies = NULL, |
| 84 |
restart = NULL, |
| 85 |
project = NULL) |
| 86 |
{ |
| 87 |
renv_consent_check() |
| 88 |
renv_scope_error_handler() |
| 89 |
renv_dots_check(...) |
| 90 |
|
| 91 |
project <- renv_project_resolve(project) |
| 92 |
renv_project_lock(project = project) |
| 93 |
|
| 94 |
# set new repositories |
| 95 |
repos <- repos %||% renv_checkout_repos(date) |
| 96 |
options(repos = repos) |
| 97 |
|
| 98 |
# TODO: Activate Bioconductor if it appears to be used by this project |
| 99 |
|
| 100 |
# select packages to install |
| 101 |
requested <- packages |
| 102 |
packages <- packages %||% renv_checkout_packages(project = project) |
| 103 |
|
| 104 |
# expand dependency fields |
| 105 |
fields <- renv_dependencies_fields(dependencies, project = project) |
| 106 |
|
| 107 |
# get available packages database |
| 108 |
dbs <- available_packages(type = "source") |
| 109 |
if (is.null(dbs)) |
| 110 |
stop("no package repositories are available") |
| 111 |
|
| 112 |
db <- renv_available_packages_flatten(dbs) |
| 113 |
|
| 114 |
# warn about user-requested packages not available in repositories |
| 115 |
renv_checkout_report(requested, db) |
| 116 |
|
| 117 |
# resolve the full recursive dependency tree |
| 118 |
packages <- renv_checkout_resolve(packages, db, fields, project = project) |
| 119 |
|
| 120 |
# build enriched lockfile records from available packages metadata |
| 121 |
records <- renv_checkout_records(packages, db) |
| 122 |
|
| 123 |
# create a lockfile matching this request |
| 124 |
lockfile <- renv_lockfile_init(project) |
| 125 |
lockfile$Packages <- records |
| 126 |
|
| 127 |
# suppress activate.R regeneration during restore / snapshot so that |
| 128 |
# stale in-memory code doesn't overwrite the template with unreplaced |
| 129 |
# placeholders; the activate script is regenerated via a subprocess at |
| 130 |
# the end of checkout instead |
| 131 |
renv_scope_binding(the, "activate_deferred", TRUE) |
| 132 |
|
| 133 |
if ("restore" %in% actions) { |
| 134 |
|
| 135 |
# install the requested packages |
| 136 |
restore(lockfile = lockfile, clean = clean) |
| 137 |
|
| 138 |
# update the renv lockfile record |
| 139 |
# (note: it might not be available when running tests) |
| 140 |
renv <- renv_lockfile_records(lockfile)[["renv"]] |
| 141 |
if (!is.null(renv)) { |
| 142 |
renv_scope_options(renv.verbose = FALSE) |
| 143 |
record(records = list(renv = renv), project = project) |
| 144 |
} |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
# write the lockfile if requested |
| 149 |
if ("snapshot" %in% actions) { |
| 150 |
if ("restore" %in% actions) { |
| 151 |
snapshot(project = project) |
| 152 |
} else { |
| 153 |
lockfile <- renv_lockfile_fini(lockfile, project) |
| 154 |
class(lockfile) <- "renv_lockfile" |
| 155 |
renv_lockfile_write(lockfile, file = paths$lockfile(project = project)) |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
# regenerate the activate script via a subprocess so the newly-installed |
| 160 |
# version of renv (if any) writes its own template correctly |
| 161 |
if ("restore" %in% actions) { |
| 162 |
path <- renv_namespace_path("renv") |
| 163 |
renv_scope_libpaths(c(dirname(path), renv_libpaths_all())) |
| 164 |
args <- c("--vanilla", "-s", "-e", shQuote("renv::activate()")) |
| 165 |
r(args, stdout = TRUE, stderr = TRUE) |
| 166 |
} |
| 167 |
|
| 168 |
# try to restart the session if we installed some packages |
| 169 |
restart <- restart %||% "restore" %in% actions |
| 170 |
if (restart) |
| 171 |
renv_restart_request(project = project, reason = "packages have been updated") |
| 172 |
|
| 173 |
invisible(lockfile) |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
renv_checkout_packages <- function(project) { |
| 178 |
renv_dependencies_impl( |
| 179 |
project, |
| 180 |
field = "Package", |
| 181 |
dev = TRUE |
| 182 |
) |
| 183 |
} |
| 184 |
|
| 185 |
renv_checkout_report <- function(requested, db) { |
| 186 |
|
| 187 |
if (is.null(requested)) |
| 188 |
return() |
| 189 |
|
| 190 |
missing <- setdiff(requested, db$Package) |
| 191 |
bulletin( |
| 192 |
"The following package(s) are not available in the configured repositories:", |
| 193 |
missing |
| 194 |
) |
| 195 |
|
| 196 |
} |
| 197 |
|
| 198 |
renv_checkout_resolve <- function(packages, db, fields, project = NULL) { |
| 199 |
|
| 200 |
# keep only packages which appear to be available in the repositories |
| 201 |
packages <- intersect(packages, db$Package) |
| 202 |
|
| 203 |
# remove ignored packages -- note we intentionally do this before |
| 204 |
# computing recursive dependencies as we don't want to allow users |
| 205 |
# to ignore a recursive dependency of a required package |
| 206 |
ignored <- renv_project_ignored_packages(project) |
| 207 |
packages <- setdiff(packages, ignored) |
| 208 |
|
| 209 |
# compute recursive dependencies for these packages |
| 210 |
renv_checkout_recdeps(packages, db, fields) |
| 211 |
|
| 212 |
} |
| 213 |
|
| 214 |
renv_checkout_records <- function(packages, db) { |
| 215 |
|
| 216 |
depfields <- c("Depends", "Imports", "LinkingTo", "Suggests", "Enhances") |
| 217 |
|
| 218 |
records <- enumerate(packages, function(package, remote) { |
| 219 |
|
| 220 |
# find the entry in the available packages database |
| 221 |
entry <- rows(db, db$Package == package) |
| 222 |
if (nrow(entry) == 0L) |
| 223 |
return(NULL) |
| 224 |
|
| 225 |
# build the record from available packages metadata |
| 226 |
record <- list( |
| 227 |
Package = package, |
| 228 |
Version = entry$Version, |
| 229 |
Source = "Repository", |
| 230 |
Repository = entry$Index |
| 231 |
) |
| 232 |
|
| 233 |
# include dependency fields, split into lists |
| 234 |
for (field in depfields) { |
| 235 |
value <- entry[[field]] |
| 236 |
if (!is.null(value) && !is.na(value) && nzchar(value)) { |
| 237 |
parts <- strsplit(value, ",", fixed = TRUE) |
| 238 |
cleaned <- gsub("\\s+", " ", trimws(parts[[1L]]), perl = TRUE) |
| 239 |
record[[field]] <- as.list(cleaned) |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
record |
| 244 |
|
| 245 |
}) |
| 246 |
|
| 247 |
# drop any NULL entries (packages not found) |
| 248 |
filter(records, Negate(is.null)) |
| 249 |
|
| 250 |
} |
| 251 |
|
| 252 |
renv_checkout_recdeps <- function(packages, db, fields) { |
| 253 |
|
| 254 |
# initialize environment (will map package names to discovered remotes) |
| 255 |
envir <- new.env(parent = emptyenv()) |
| 256 |
|
| 257 |
# set R to NA since it's a common non-package 'dependency' for packages |
| 258 |
envir$R <- NA |
| 259 |
|
| 260 |
# iterate through dependencies |
| 261 |
for (package in packages) |
| 262 |
renv_checkout_recdeps_impl(package, db, envir, fields) |
| 263 |
|
| 264 |
# get list of discovered dependencies |
| 265 |
recdeps <- as.list.environment(envir, all.names = TRUE) |
| 266 |
|
| 267 |
# drop any NA values |
| 268 |
recdeps <- filter(recdeps, Negate(is.na)) |
| 269 |
|
| 270 |
# return sorted vector |
| 271 |
recdeps[csort(names(recdeps))] |
| 272 |
|
| 273 |
} |
| 274 |
|
| 275 |
renv_checkout_recdeps_impl <- function(package, db, envir, fields) { |
| 276 |
|
| 277 |
# check if we've already visited this package |
| 278 |
if (!is.null(envir[[package]])) |
| 279 |
return() |
| 280 |
|
| 281 |
# get entry from database |
| 282 |
entry <- rows(db, db$Package == package) |
| 283 |
if (nrow(entry) == 0L) { |
| 284 |
envir[[package]] <- NA_character_ |
| 285 |
return() |
| 286 |
} |
| 287 |
|
| 288 |
# set discovered remote |
| 289 |
envir[[package]] <- with(entry, paste(Package, Version, sep = "@")) |
| 290 |
|
| 291 |
# iterate through requested dependency fields |
| 292 |
for (field in fields) { |
| 293 |
value <- entry[[field]] |
| 294 |
if (!is.null(value) && !is.na(value)) { |
| 295 |
value <- renv_description_parse_field(entry[[field]]) |
| 296 |
for (package in value$Package) |
| 297 |
if (is.null(envir[[package]])) |
| 298 |
renv_checkout_recdeps_impl(package, db, envir, fields) |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
} |
| 303 |
|
| 304 |
renv_checkout_repos <- function(date) { |
| 305 |
|
| 306 |
# if no date was provided, just use default repositories |
| 307 |
if (is.null(date)) |
| 308 |
return(getOption("repos")) |
| 309 |
|
| 310 |
# build path to repository snapshot location |
| 311 |
root <- dirname(config$ppm.url()) |
| 312 |
url <- file.path(root, date) |
| 313 |
if (renv_download_available(file.path(url, "src/contrib/PACKAGES"))) |
| 314 |
return(c(PPM = url)) |
| 315 |
|
| 316 |
# requested date not available; try to search a bit |
| 317 |
candidate <- date |
| 318 |
for (i in 1:7) { |
| 319 |
candidate <- format(as.Date(candidate) - 1L) |
| 320 |
url <- file.path(root, candidate) |
| 321 |
if (renv_download_available(file.path(url, "src/contrib/PACKAGES"))) { |
| 322 |
fmt <- "- Snapshot date '%s' not available; using '%s' instead" |
| 323 |
printf(fmt, date, candidate) |
| 324 |
return(c(PPM = url)) |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
stopf("repository snapshot '%s' not available", date) |
| 329 |
|
| 330 |
} |