| 1 |
|
| 2 |
#' Clean a project |
| 3 |
#' |
| 4 |
#' Clean up a project and its associated \R libraries. |
| 5 |
#' |
| 6 |
#' # Actions |
| 7 |
#' |
| 8 |
#' The following clean actions are available: |
| 9 |
#' |
| 10 |
#' \describe{ |
| 11 |
#' |
| 12 |
#' \item{`package.locks`}{ |
| 13 |
#' |
| 14 |
#' During package installation, \R will create package locks in the |
| 15 |
#' library path, typically named `00LOCK-<package>`. On occasion, if package |
| 16 |
#' installation fails or \R is terminated while installing a package, these |
| 17 |
#' locks can be left behind and will inhibit future attempts to reinstall |
| 18 |
#' that package. Use this action to remove such left-over package locks. |
| 19 |
#' |
| 20 |
#' } |
| 21 |
#' |
| 22 |
#' \item{`library.tempdirs`}{ |
| 23 |
#' |
| 24 |
#' During package installation, \R may create temporary directories with |
| 25 |
#' names of the form `file\w{12}`, and on occasion those files can be |
| 26 |
#' left behind even after they are no longer in use. Use this action to |
| 27 |
#' remove such left-over directories. |
| 28 |
#' } |
| 29 |
#' |
| 30 |
#' \item{`system.library`}{ |
| 31 |
#' |
| 32 |
#' In general, it is recommended that only packages distributed with \R |
| 33 |
#' are installed into the default library (the library path referred to |
| 34 |
#' by `.Library`). Use this action to remove any user-installed packages |
| 35 |
#' that have been installed to the system library. |
| 36 |
#' |
| 37 |
#' Because this action is destructive, it is by default never run -- it |
| 38 |
#' must be explicitly requested by the user. |
| 39 |
#' |
| 40 |
#' } |
| 41 |
#' |
| 42 |
#' \item{`unused.packages`}{ |
| 43 |
#' |
| 44 |
#' Remove packages that are installed in the project library, but no longer |
| 45 |
#' appear to be used in the project sources. |
| 46 |
#' |
| 47 |
#' Because this action is destructive, it is by default only run in |
| 48 |
#' interactive sessions when prompting is enabled. |
| 49 |
#' |
| 50 |
#' } |
| 51 |
#' |
| 52 |
#' } |
| 53 |
#' |
| 54 |
#' |
| 55 |
#' @inherit renv-params |
| 56 |
#' |
| 57 |
#' @param actions The set of clean actions to take. See the documentation in |
| 58 |
#' **Actions** for a list of available actions, and the default actions |
| 59 |
#' taken when no actions are supplied. |
| 60 |
#' |
| 61 |
#' @export |
| 62 |
#' |
| 63 |
#' @examples |
| 64 |
#' \dontrun{ |
| 65 |
#' |
| 66 |
#' # clean the current project |
| 67 |
#' renv::clean() |
| 68 |
#' |
| 69 |
#' } |
| 70 |
clean <- function(project = NULL, |
| 71 |
..., |
| 72 |
actions = NULL, |
| 73 |
prompt = interactive()) |
| 74 |
{ |
| 75 |
renv_scope_error_handler() |
| 76 |
renv_dots_check(...) |
| 77 |
|
| 78 |
project <- renv_project_resolve(project) |
| 79 |
renv_project_lock(project = project) |
| 80 |
renv_scope_verbose_if(prompt) |
| 81 |
|
| 82 |
renv_activate_prompt("clean", NULL, prompt, project) |
| 83 |
|
| 84 |
actions <- actions %||% renv_clean_actions(prompt) |
| 85 |
|
| 86 |
all <- list( |
| 87 |
package.locks = renv_clean_package_locks, |
| 88 |
library.tempdirs = renv_clean_library_tempdirs, |
| 89 |
system.library = renv_clean_system_library, |
| 90 |
unused.packages = renv_clean_unused_packages |
| 91 |
) |
| 92 |
|
| 93 |
methods <- all[actions] |
| 94 |
for (method in methods) |
| 95 |
tryCatch(method(project, prompt), error = warnify) |
| 96 |
|
| 97 |
writef("- The project has been cleaned.") |
| 98 |
invisible(project) |
| 99 |
} |
| 100 |
|
| 101 |
renv_clean_actions <- function(prompt) { |
| 102 |
|
| 103 |
default <- c( |
| 104 |
"package.locks", |
| 105 |
"library.tempdirs" |
| 106 |
) |
| 107 |
|
| 108 |
unsafe <- c( |
| 109 |
# "system.library", |
| 110 |
"unused.packages" |
| 111 |
) |
| 112 |
|
| 113 |
c(default, if (prompt) unsafe) |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
renv_clean_library_tempdirs <- function(project, prompt) { |
| 118 |
|
| 119 |
ntd <- function() { |
| 120 |
writef("- No temporary directories were found in the project library.") |
| 121 |
FALSE |
| 122 |
} |
| 123 |
|
| 124 |
library <- renv_paths_library(project = project) |
| 125 |
children <- list.files(library, full.names = TRUE) |
| 126 |
|
| 127 |
bad <- grep("/file\\w{12}$", children, value = TRUE) |
| 128 |
if (empty(bad)) |
| 129 |
return(ntd()) |
| 130 |
|
| 131 |
# nocov start |
| 132 |
if (prompt || renv_verbose()) { |
| 133 |
|
| 134 |
bulletin("The following directories will be removed:", bad) |
| 135 |
|
| 136 |
if (prompt && !proceed()) |
| 137 |
cancel() |
| 138 |
|
| 139 |
} |
| 140 |
# nocov end |
| 141 |
|
| 142 |
unlink(bad, recursive = TRUE) |
| 143 |
TRUE |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
# remove user packages in system library |
| 149 |
renv_clean_system_library <- function(project, prompt) { |
| 150 |
|
| 151 |
ntd <- function() { |
| 152 |
writef("- No non-system packages were discovered in the system library.") |
| 153 |
FALSE |
| 154 |
} |
| 155 |
|
| 156 |
# explicitly query for packages |
| 157 |
syslib <- renv_path_normalize(renv_libpaths_system()) |
| 158 |
db <- installed_packages(lib.loc = syslib, priority = "NA") |
| 159 |
packages <- setdiff(db$Package, "translations") |
| 160 |
|
| 161 |
# also look for leftover package folders |
| 162 |
# (primarily for Windows, where .dlls from old packages can be left behind) |
| 163 |
|
| 164 |
# nocov start |
| 165 |
if (renv_platform_windows()) { |
| 166 |
folders <- list.files(syslib, full.names = TRUE) |
| 167 |
descpaths <- file.path(folders, "DESCRIPTION") |
| 168 |
missing <- !file.exists(descpaths) |
| 169 |
packages <- union(packages, basename(folders)[missing]) |
| 170 |
} |
| 171 |
# nocov end |
| 172 |
|
| 173 |
# check for any packages needing removal |
| 174 |
if (empty(packages)) |
| 175 |
return(ntd()) |
| 176 |
|
| 177 |
# nocov start |
| 178 |
if (prompt || renv_verbose()) { |
| 179 |
|
| 180 |
bulletin( |
| 181 |
"The following non-system packages are installed in the system library:", |
| 182 |
packages, |
| 183 |
c( |
| 184 |
"Normally, only packages distributed with R should be installed in the system library.", |
| 185 |
"These packages will be removed.", |
| 186 |
"If necessary, consider reinstalling these packages in your site library." |
| 187 |
) |
| 188 |
) |
| 189 |
|
| 190 |
if (prompt && !proceed()) |
| 191 |
cancel() |
| 192 |
|
| 193 |
} |
| 194 |
# nocov end |
| 195 |
|
| 196 |
remove(packages, library = syslib) |
| 197 |
TRUE |
| 198 |
|
| 199 |
} |
| 200 |
|
| 201 |
renv_clean_unused_packages <- function(project, prompt) { |
| 202 |
|
| 203 |
ntd <- function() { |
| 204 |
writef("- No unused packages were found in the project library.") |
| 205 |
FALSE |
| 206 |
} |
| 207 |
|
| 208 |
# find packages installed in the project library |
| 209 |
library <- renv_paths_library(project = project) |
| 210 |
installed <- list.files(library, pattern = renv_regexps_package_name()) |
| 211 |
if (empty(installed)) |
| 212 |
return(ntd()) |
| 213 |
|
| 214 |
# ignore 'pak' if we're configured to use it |
| 215 |
installed <- setdiff(installed, if (config$pak.enabled()) "pak") |
| 216 |
|
| 217 |
# find packages used in the project and their recursive dependencies |
| 218 |
packages <- renv_snapshot_dependencies(project, dev = TRUE) |
| 219 |
paths <- renv_package_dependencies(packages, project = project) |
| 220 |
packages <- names(paths) |
| 221 |
|
| 222 |
# figure out which packages aren't needed |
| 223 |
removable <- renv_vector_diff(installed, packages) |
| 224 |
if (empty(removable)) |
| 225 |
return(ntd()) |
| 226 |
|
| 227 |
# nocov start |
| 228 |
if (prompt || renv_verbose()) { |
| 229 |
|
| 230 |
bulletin( |
| 231 |
c( |
| 232 |
"The following packages are installed in the project library,", |
| 233 |
"but appear to be no longer used in your project." |
| 234 |
), |
| 235 |
removable, |
| 236 |
"These packages will be removed." |
| 237 |
) |
| 238 |
|
| 239 |
if (prompt && !proceed()) |
| 240 |
cancel() |
| 241 |
|
| 242 |
} |
| 243 |
# nocov end |
| 244 |
|
| 245 |
remove(removable, library = library) |
| 246 |
return(TRUE) |
| 247 |
|
| 248 |
} |
| 249 |
|
| 250 |
renv_clean_package_locks <- function(project, prompt) { |
| 251 |
|
| 252 |
ntd <- function() { |
| 253 |
writef("- No stale package locks were found.") |
| 254 |
FALSE |
| 255 |
} |
| 256 |
|
| 257 |
# find 00LOCK directories in library |
| 258 |
library <- renv_paths_library(project = project) |
| 259 |
lock <- list.files(path = library, pattern = "^00LOCK", full.names = TRUE) |
| 260 |
if (empty(lock)) |
| 261 |
return(ntd()) |
| 262 |
|
| 263 |
# check to see which are old |
| 264 |
now <- Sys.time() |
| 265 |
mtime <- file.mtime(lock) |
| 266 |
mtime[is.na(mtime)] <- now |
| 267 |
diff <- difftime(now, mtime, units = "secs") |
| 268 |
old <- lock[diff > 120] |
| 269 |
if (empty(old)) |
| 270 |
return(ntd()) |
| 271 |
|
| 272 |
# nocov start |
| 273 |
if (prompt || renv_verbose()) { |
| 274 |
|
| 275 |
bulletin( |
| 276 |
"The following stale package locks were discovered in your library:", |
| 277 |
basename(old), |
| 278 |
"These locks will be removed." |
| 279 |
) |
| 280 |
|
| 281 |
if (prompt && !proceed()) |
| 282 |
cancel() |
| 283 |
|
| 284 |
} |
| 285 |
# nocov end |
| 286 |
|
| 287 |
unlink(old, recursive = TRUE) |
| 288 |
TRUE |
| 289 |
} |
| 290 |
|
| 291 |
# nocov start |
| 292 |
renv_clean_cache <- function(project, prompt) { |
| 293 |
|
| 294 |
ntd <- function() { |
| 295 |
writef("- No unused packages were found in the renv cache.") |
| 296 |
FALSE |
| 297 |
} |
| 298 |
|
| 299 |
# find projects monitored by renv |
| 300 |
projects <- renv_paths_root("projects") |
| 301 |
projlist <- character() |
| 302 |
if (file.exists(projects)) |
| 303 |
projlist <- readLines(projects, warn = FALSE, encoding = "UTF-8") |
| 304 |
|
| 305 |
# inform user if any projects are missing |
| 306 |
missing <- !file.exists(projlist) |
| 307 |
if (any(missing)) { |
| 308 |
|
| 309 |
bulletin( |
| 310 |
"The following projects are monitored by renv, but no longer exist:", |
| 311 |
projlist[missing], |
| 312 |
"These projects will be removed from renv's project list." |
| 313 |
) |
| 314 |
|
| 315 |
if (prompt && !proceed()) |
| 316 |
cancel() |
| 317 |
|
| 318 |
writeLines(projlist[!missing], con = projects, useBytes = TRUE) |
| 319 |
|
| 320 |
} |
| 321 |
|
| 322 |
action <- function(project) { |
| 323 |
library <- renv_paths_library(project = project) |
| 324 |
packages <- list.files(library, full.names = TRUE) |
| 325 |
descs <- file.path(packages, "DESCRIPTION") |
| 326 |
existing <- file.exists(descs) |
| 327 |
map_chr(descs[existing], renv_cache_path, USE.NAMES = FALSE) |
| 328 |
} |
| 329 |
|
| 330 |
# for each project, find packages used in their renv private library, |
| 331 |
# and look for entries in the cache |
| 332 |
projlist <- projlist[!missing] |
| 333 |
callback <- renv_progress_callback(action, length(projlist)) |
| 334 |
used <- uapply(projlist, callback) |
| 335 |
|
| 336 |
# check what packages are actually available in the cache |
| 337 |
available <- renv_cache_list() |
| 338 |
|
| 339 |
diff <- renv_vector_diff(available, used) |
| 340 |
if (empty(diff)) |
| 341 |
return(ntd()) |
| 342 |
|
| 343 |
if (prompt || renv_verbose()) { |
| 344 |
|
| 345 |
bulletin( |
| 346 |
"The following packages are installed in the cache but no longer used:", |
| 347 |
renv_cache_format_path(diff), |
| 348 |
"These packages will be removed." |
| 349 |
) |
| 350 |
|
| 351 |
if (prompt && !proceed()) |
| 352 |
cancel() |
| 353 |
|
| 354 |
} |
| 355 |
|
| 356 |
# remove the directories |
| 357 |
unlink(diff, recursive = TRUE) |
| 358 |
renv_cache_clean_empty() |
| 359 |
|
| 360 |
writef("- %i package(s) have been removed.", length(diff)) |
| 361 |
TRUE |
| 362 |
|
| 363 |
} |
| 364 |
# nocov end |