Debian Codegraph @

R/records.R

raw
1
2 renv_records_select <- function(records, actions, action) {
3 records <- renv_lockfile_records(records)
4 matching <- actions[actions %in% action]
5 keep(records, names(matching))
6 }
7
8 renv_records_sort <- function(records) {
9 records[csort(names(records))]
10 }
11
12 renv_records_override <- function(records) {
13 enumerate(records, renv_options_override, scope = "renv.records")
14 }
15
16 renv_record_names <- function(record, fields = NULL) {
17 fields <- fields %||% c("Package", "Version", "Source")
18 remotes <- grep("^Remote", names(record), value = TRUE)
19 nms <- c(fields, setdiff(remotes, "Remotes"))
20 renv_vector_intersect(nms, names(record))
21 }
22
23 renv_record_cacheable <- function(record) {
24
25 # check if the record has been marked as cacheable
26 cacheable <- record$Cacheable %||% TRUE
27 if (identical(cacheable, FALSE))
28 return(FALSE)
29
30 # check for unknown source
31 source <- renv_record_source(record)
32 if (source == "unknown")
33 return(FALSE)
34
35 # record is ok
36 TRUE
37
38 }
39
40 renv_record_source <- function(record, normalize = FALSE) {
41
42 # retrieve / infer package source from record
43 source <- renv_record_source_impl(record)
44
45 # if this appears to be a file path, then keep it as-is
46 if (grepl("[/\\]", source))
47 return(source)
48
49 # otherwise, try to normalize it
50 source <- tolower(source)
51 if (normalize)
52 source <- renv_record_source_normalize(record, source)
53
54 source
55
56 }
57
58 renv_record_source_impl <- function(record) {
59
60 # if the record has a recorded source, use it
61 source <- record$Source
62 if (!is.null(source))
63 return(source)
64
65 # otherwise, if it has a Repository field, infer 'repository' source
66 repository <- record$Repository
67 if (!is.null(repository))
68 return("repository")
69
70 # otherwise, we don't know
71 "unknown"
72
73 }
74
75 renv_record_source_normalize <- function(record, source) {
76
77 # normalize different types of git remotes
78 if (source %in% c("git2r", "xgit"))
79 source <- "git"
80
81 # handle old lockfiles where 'source' was explicitly set as CRAN
82 if (source %in% c("cran"))
83 source <- "repository"
84
85 # check for ad-hoc requests to install from bioc
86 if (identical(source, "repository")) {
87 repos <- record$Repository %||% ""
88 if (tolower(repos) %in% c("bioc", "bioconductor"))
89 source <- "bioconductor"
90 }
91
92 # all done; return normalized source
93 source
94
95 }
96
97 renv_record_validate <- function(package, record) {
98
99 # check for a record -- minimally, a list with a package name
100 if (is.list(record) && is.character(record$Package))
101 return(record)
102
103 # if we're running tests, or in CI, then report
104 if (renv_tests_running() || renv_envvar_exists("CI")) {
105 fmt <- "! Internal error: unexpected record for package '%s'"
106 writef(fmt, package)
107 print(record)
108 }
109
110 # return record as-is
111 record
112
113 }
114
115 renv_record_format_remote <- function(record,
116 compact = FALSE,
117 versioned = TRUE,
118 pak = FALSE)
119 {
120 # if we have a pkgref, we can use that directly
121 pkgref <- record$RemotePkgRef
122 if (!is.null(pkgref))
123 return(pkgref)
124
125 # extract some of the commonly used fields up-front
126 source <- renv_record_source(record, normalize = TRUE)
127 package <- record[["Package"]]
128 version <- record[["Version"]]
129
130 # handle repository remotes
131 if (source %in% c("cran", "repository", "standard")) {
132 parts <- c(package, if (versioned) version)
133 remote <- paste(parts, collapse = "@")
134 return(remote)
135 }
136
137 # handle bioconductor remotes
138 if (source %in% "bioconductor") {
139 parts <- c(package, if (versioned) version)
140 suffix <- paste(parts, collapse = "@")
141 remote <- paste("bioc", suffix, sep = "::")
142 return(remote)
143 }
144
145 # handle git, svn remotes
146 if (source %in% c("git", "svn")) {
147 url <- record[["RemoteUrl"]]
148 remote <- sprintf("%s::%s", source, url)
149 return(remote)
150 }
151
152 # handle local, url remotes
153 if (source %in% c("local", "url")) {
154 url <- record[["RemoteUrl"]] %||% sub("[^:]+::", "", record[["RemotePkgRef"]])
155 remote <- sprintf("%s=%s::%s", package, source, url)
156 return(remote)
157 }
158
159 # handle other remotes; assumed to be a github-like remote
160 host <- record[["RemoteHost"]]
161 user <- record[["RemoteUsername"]]
162 repo <- record[["RemoteRepo"]]
163 type <- record[["RemoteType"]]
164 ref <- record[["RemoteRef"]]
165 sha <- record[["RemoteSha"]]
166 subdir <- record[["RemoteSubdir"]]
167 subdirsep <- if (pak) "/" else ":"
168
169 # skip type and host if they're defaults
170 if (identical(type, "github")) {
171 if (is.null(host) || identical(host, "api.github.com")) {
172 type <- NULL
173 host <- NULL
174 }
175 }
176
177 # omit gitlab host as well for default host
178 if (identical(type, "gitlab")) {
179 if (identical(host, "gitlab.com")) {
180 host <- NULL
181 }
182 }
183
184 # skip 'boring' refs for compact display
185 if (compact) {
186 if (length(ref) && ref %in% c("HEAD", "main", "master")) {
187 ref <- NULL
188 sha <- NULL
189 }
190 }
191
192 # start building the remote specification
193 stk <- stack(mode = "character")
194
195 if (!identical(package, repo))
196 stk$push(package, "=")
197
198 if (!is.null(type)) {
199 stk$push(type)
200 if (!is.null(host) && nzchar(host))
201 stk$push("@", host)
202 stk$push("::")
203 }
204
205 stk$push(user, "/", repo)
206
207 if (!is.null(subdir) && nzchar(subdir))
208 stk$push(subdirsep, subdir)
209
210 if (versioned) {
211
212 if (pak) {
213 stk$push("@", sha %||% ref %||% "HEAD")
214 } else if (length(sha)) {
215 stk$push("@", if (compact) substring(sha, 1L, 8L) else sha)
216 } else if (length(ref)) {
217 stk$push("@", ref)
218 }
219
220 } else {
221
222 if (length(ref))
223 stk$push("@", ref)
224
225 }
226
227 remote <- paste(stk$data(), collapse = "")
228 return(remote)
229 }
230
231 renv_record_format_short <- function(record, versioned = FALSE) {
232
233 if (is.null(record))
234 return(renv_record_placeholder())
235
236 remotes <- c("RemoteUsername", "RemoteRepo")
237 if (all(remotes %in% names(record))) {
238 remote <- renv_record_format_remote(record, compact = TRUE)
239 if (versioned)
240 remote <- sprintf("%s [%s]", record$Version %||% "<NA>", remote)
241 return(remote)
242 }
243
244 record$Version %||% renv_record_placeholder()
245
246 }
247
248 renv_record_format_pair <- function(lhs, rhs, separator = "->") {
249
250 placeholder <- renv_record_placeholder()
251
252 # check for install / remove
253 if (is.null(lhs) || is.null(rhs)) {
254 lhs <- renv_record_format_short(lhs)
255 rhs <- renv_record_format_short(rhs)
256 return(sprintf("[%s %s %s]", lhs, separator, rhs))
257 }
258
259 map <- list(
260 Source = "src",
261 Repository = "repo",
262 Version = "ver",
263 RemoteHost = "host",
264 RemoteUsername = "user",
265 RemoteRepo = "repo",
266 RemoteRef = "ref",
267 RemoteSha = "sha",
268 RemoteSubdir = "subdir"
269 )
270
271 fields <- names(map)
272
273 # check to see which fields have changed between the two
274 diff <- map_lgl(fields, function(field) {
275 !identical(lhs[[field]], rhs[[field]])
276 })
277
278 changed <- names(which(diff))
279
280 if (empty(changed)) {
281 fmt <- "[%s: unchanged]"
282 lhsf <- renv_record_format_short(lhs)
283 return(sprintf(fmt, lhsf))
284 }
285
286 # check for CRAN packages; in such cases, we typically want to ignore
287 # the Remote fields which might've been added by 'pak' or other tools
288 isrepo <-
289 nzchar(lhs$Version %||% "") &&
290 nzchar(rhs$Version %||% "") &&
291 nzchar(lhs$Repository %||% "") &&
292 nzchar(rhs$Repository %||% "") &&
293 identical(lhs$Repository, rhs$Repository)
294
295 if (isrepo) {
296 fmt <- "[%s %s %s]"
297 lhsf <- renv_record_format_short(lhs)
298 rhsf <- renv_record_format_short(rhs)
299 return(sprintf(fmt, lhsf, separator, rhsf))
300 }
301
302 # check for only sha changed
303 usesha <-
304 setequal(changed, "RemoteSha") ||
305 setequal(changed, c("RemoteSha", "Version"))
306
307 if (usesha) {
308
309 user <- lhs$RemoteUsername %||% placeholder
310 repo <- lhs$RemoteRepo %||% placeholder
311 spec <- paste(user, repo, sep = "/")
312
313 ref <- lhs$RemoteRef %||% placeholder
314 if (!ref %in% c("main", "master", "*"))
315 spec <- paste(spec, ref, sep = "@")
316
317 fmt <- "[%s: %s %s %s]"
318 lsha <- substring(lhs$RemoteSha %||% placeholder, 1L, 8L)
319 rsha <- substring(rhs$RemoteSha %||% placeholder, 1L, 8L)
320
321 return(sprintf(fmt, spec, lsha, separator, rsha))
322
323 }
324
325 # check for only source change
326 if (setequal(changed, "Source")) {
327 fmt <- "[%s: %s %s %s]"
328 ver <- lhs$Version %||% placeholder
329 lhsf <- lhs$Source %||% placeholder
330 rhsf <- rhs$Source %||% placeholder
331 return(sprintf(fmt, ver, lhsf, separator, rhsf))
332 }
333
334 # check only version changed
335 if (setequal(changed, "Version")) {
336 fmt <- "[%s %s %s]"
337 lhsf <- lhs$Version %||% placeholder
338 rhsf <- rhs$Version %||% placeholder
339 return(sprintf(fmt, lhsf, separator, rhsf))
340 }
341
342 # if the source has changed, highlight that
343 if ("Source" %in% changed) {
344 fmt <- "[%s %s %s]"
345 lhsf <- renv_record_format_short(lhs)
346 rhsf <- renv_record_format_short(rhs)
347 return(sprintf(fmt, lhsf, separator, rhsf))
348 }
349
350 # otherwise, report each diff individually
351 diffs <- map_chr(changed, function(field) {
352
353 lhsf <- lhs[[field]] %||% placeholder
354 rhsf <- rhs[[field]] %||% placeholder
355
356 if (field == "RemoteSha") {
357 lhsf <- substring(lhsf, 1L, 8L)
358 rhsf <- substring(rhsf, 1L, 8L)
359 }
360
361 fmt <- "%s: %s %s %s"
362 sprintf(fmt, map[[field]], lhsf, separator, rhsf)
363 })
364
365 sprintf("[%s]", paste(diffs, collapse = "; "))
366
367 }
368
369 renv_records_equal <- function(lhs, rhs) {
370
371 lhs <- reject(lhs, is.null)
372 rhs <- reject(rhs, is.null)
373
374 nm <- setdiff(union(names(lhs), names(rhs)), "Hash")
375 identical(keep(lhs, nm), keep(rhs, nm))
376
377 }
378
379 renv_records_resolve <- function(records, latest = FALSE) {
380
381 enumerate(records, function(package, record) {
382
383 # check for already-resolved records
384 if (is.null(record) || is.list(record))
385 return(record)
386
387 # check for version-only specifications and
388 # prepend the package name in such a case
389 pattern <- "^(?:[[:digit:]]+[.-]){1,}[[:digit:]]+$"
390 if (grepl(pattern, record))
391 record <- paste(package, record, sep = "@")
392
393 # resolve the record
394 renv_remotes_resolve(record, latest)
395
396 })
397
398 }