Debian Codegraph @

R/cleanse.R

raw
1
2 # tools for cleaning up renv's cached data
3 cleanse <- function() {
4
5 enabled <- Sys.getenv("RENV_CLEANSE_ENABLED", unset = "TRUE")
6 if (!truthy(enabled))
7 return(invisible(FALSE))
8
9 # remove unused sandbox directories
10 renv_cleanse_sandbox(path = renv_paths_sandbox())
11
12 # remove empty directories in the root directory
13 # we can't do this on Windows, as some empty directories
14 # might also be broken junctions, and we want to keep
15 # those around so we can inform the user that they need
16 # to repair that
17 if (!renv_platform_windows())
18 renv_cleanse_empty(path = renv_paths_root())
19
20 invisible(TRUE)
21
22 }
23
24 renv_cleanse_sandbox <- function(path) {
25
26 # get sandbox root path
27 root <- dirname(path)
28 if (!file.exists(root))
29 return(FALSE)
30
31 # list directories within
32 dirs <- list.files(root, full.names = TRUE)
33
34 # look for apparently-unused sandbox directories
35 info <- suppressWarnings(file.info(dirs, extra_cols = FALSE))
36 age <- difftime(Sys.time(), info$mtime, units = "days")
37 old <- age >= 7
38
39 # remove the old sandbox directories
40 unlink(dirs[old], recursive = TRUE, force = TRUE)
41
42 }
43
44 renv_cleanse_empty <- function(path) {
45
46 # no-op for Solaris
47 if (renv_platform_solaris())
48 return(FALSE)
49
50 if (!file.exists(path))
51 return(FALSE)
52
53 renv_scope_wd(path)
54
55 # execute system command for removing empty directories
56 action <- "removing empty directories"
57 if (renv_platform_windows()) {
58 args <- c(".", ".", "/S", "/MOVE")
59 renv_system_exec("robocopy", args, action, 0:8)
60 } else {
61 args <- c(".", "-type", "d", "-empty", "-delete")
62 renv_system_exec("find", args, action)
63 }
64
65 TRUE
66
67 }