-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathcache.R
280 lines (216 loc) · 6.89 KB
/
cache.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# tools for interacting with the renv global package cache
renv_cache_version <- function() {
"v4"
}
renv_cache_package_path <- function(record) {
# validate required fields -- if any are missing, we can't use the cache
required <- c("Package", "Version")
missing <- renv_vector_diff(required, names(record))
if (length(missing))
return("")
# if we have a hash, use it directly
if (!is.null(record$Hash)) {
path <- with(record, renv_paths_cache(Package, Version, Hash, Package))
return(path)
}
# figure out the R version to be used when constructing
# the cache package path
built <- record$Built
version <- if (is.null(built))
getRversion()
else
substring(built, 3, regexpr(";", built, fixed = TRUE) - 1L)
# if the record doesn't have a hash, check to see if we can still locate a
# compatible package version within the cache
root <- with(record, renv_paths_cache(Package, Version, version = version))
hashes <- list.files(root, full.names = TRUE)
packages <- list.files(hashes, full.names = TRUE)
# iterate over package paths, read DESCRIPTION, and look
# for something compatible with the requested record
for (package in packages) {
dcf <- catch(as.list(renv_description_read(package)))
if (inherits(dcf, "error"))
next
# if we're requesting an install from CRAN,
# and the cached package has a "Repository" field,
# then use it
cran <-
identical(record$Source, "CRAN") &&
"Repository" %in% names(dcf)
if (cran)
return(package)
# otherwise, match on other fields
fields <- renv_record_names(record, c("Package", "Version"))
# drop unnamed fields
record <- record[nzchar(record)]; dcf <- dcf[nzchar(dcf)]
# check identical
if (identical(record[fields], dcf[fields]))
return(package)
}
# failed; return "" as proxy for missing file
""
}
renv_cache_synchronize <- function(record, linkable = FALSE) {
# construct path to package in library
library <- renv_libpaths_default()
path <- file.path(library, record$Package)
if (!file.exists(path))
return(FALSE)
# bail if the package source is unknown (assume that packages with an
# unknown source are not cacheable)
desc <- renv_description_read(path)
source <- renv_snapshot_description_source(desc)
if (identical(source, "unknown"))
return(FALSE)
# bail if record not cacheable
if (!renv_record_cacheable(record))
return(FALSE)
# if we don't have a hash, compute it now
record$Hash <- record$Hash %||% renv_hash_description(path)
# construct cache entry
cache <- renv_cache_package_path(record)
if (!nzchar(cache))
return(FALSE)
# if our cache -> path link is already up to date, then nothing to do
if (renv_file_same(cache, path))
return(TRUE)
# try to create the cache directory target
# (catch errors due to permissions, etc)
parent <- dirname(cache)
status <- catchall(ensure_directory(parent))
if (inherits(status, "error"))
return(FALSE)
# double-check that the cache is writable
writable <- local({
file <- tempfile("renv-tempfile-", tmpdir = parent)
on.exit(unlink(file, force = TRUE), add = TRUE)
status <- catchall(file.create(file))
file.exists(file)
})
if (!writable)
return(FALSE)
# if we already have a cache entry, back it up
callback <- renv_file_backup(cache)
on.exit(callback(), add = TRUE)
# copy into cache and link back into requested directory
if (linkable) {
renv_file_move(path, cache)
renv_file_link(cache, path, overwrite = TRUE)
} else {
vprintf("* Copying '%s' into the cache ... ", record$Package)
renv_file_copy(path, cache)
vwritef("Done!")
}
TRUE
}
renv_cache_list <- function(packages = NULL) {
cache <- renv_paths_cache()
names <- file.path(cache, packages %||% list.files(cache))
versions <- list.files(names, full.names = TRUE)
hashes <- list.files(versions, full.names = TRUE)
paths <- list.files(hashes, full.names = TRUE)
paths
}
renv_cache_diagnose_missing_descriptions <- function(paths, problems, verbose) {
descpaths <- file.path(paths, "DESCRIPTION")
info <- file.info(descpaths, extra_cols = FALSE)
missing <- is.na(info$isdir)
bad <- rownames(info)[missing]
if (empty(bad))
return(paths)
# nocov start
if (verbose) {
renv_pretty_print(
renv_cache_format_path(dirname(bad)),
"The following packages are missing DESCRIPTION files in the cache:",
"These packages should be purged and re-installed.",
wrap = FALSE
)
}
# nocov end
path <- dirname(bad)
package <- path_component(bad, 1)
version <- path_component(bad, 3)
data <- data.frame(
Package = package,
Version = version,
Path = path,
Reason = "missing",
stringsAsFactors = FALSE
)
problems$push(data)
paths[!missing]
}
renv_cache_diagnose_bad_hash <- function(paths, problems, verbose) {
hash <- path_component(paths, 2)
computed <- map_chr(paths, renv_hash_description)
diff <- hash != computed
bad <- names(computed)[diff]
if (empty(bad))
return(paths)
package <- path_component(bad, 1)
version <- path_component(bad, 3)
# nocov start
if (verbose) {
fmt <- "%s %s [Hash: %s != %s]"
entries <- sprintf(
fmt,
format(package),
format(version),
format(hash[diff]),
format(computed[diff])
)
renv_pretty_print(
entries,
"The following packages have incorrect hashes:",
"These packages should be purged and re-installed.",
wrap = FALSE
)
}
# nocov end
data <- data.frame(
Package = package,
Version = version,
Path = dirname(bad),
Reason = "badhash",
stringsAsFactors = FALSE
)
problems$push(data)
paths
}
renv_cache_diagnose <- function(verbose = NULL) {
verbose <- verbose %||% renv_verbose()
problems <- stack()
paths <- renv_cache_list()
paths <- renv_cache_diagnose_missing_descriptions(paths, problems, verbose)
paths <- renv_cache_diagnose_bad_hash(paths, problems, verbose)
invisible(bind_list(problems$data()))
}
renv_cache_move <- function(source, target, overwrite = FALSE) {
file.exists(source) || renv_file_move(target, source)
renv_file_link(source, target, overwrite = TRUE)
}
# nocov start
renv_cache_format_path <- function(paths) {
names <- format(path_component(paths, 1))
hashes <- format(path_component(paths, 2))
versions <- format(path_component(paths, 3))
fmt <- "%s %s [Hash: %s]"
sprintf(fmt, names, versions, hashes)
}
# nocov end
renv_cache_clean_empty <- function() {
# move to cache root
root <- renv_paths_cache()
owd <- setwd(root)
on.exit(setwd(owd), add = TRUE)
# try using find utility
command <- case(
nzchar(Sys.which("find")) ~ "find . -type d -empty -delete",
nzchar(Sys.which("robocopy")) ~ "robocopy . . /S /MOVE"
)
if (is.null(command))
return(FALSE)
system(command, ignore.stdout = TRUE, ignore.stderr = TRUE)
TRUE
}