Debian Codegraph @

R/record.R

raw
1
2 #' Update package records in a lockfile
3 #'
4 #' Use `record()` to record a new entry within an existing renv lockfile.
5 #'
6 #' This function can be useful when you need to change one or more of the
7 #' package records within an renv lockfile -- for example, because a recorded
8 #' package cannot be restored in a particular environment, and you know of a
9 #' suitable alternative.
10 #'
11 #' # Records
12 #'
13 #' Records can be provided either using the **remotes** short-hand syntax,
14 #' or by using an \R list of entries to record within the lockfile. See
15 #' `?lockfiles` for more information on the structure of a package record.
16 #'
17 #' @inheritParams renv-params
18 #'
19 #' @param records A list of named records, mapping package names to a definition
20 #' of their source. See **Records** for more details.
21 #'
22 #' @param enrich Should resolved records be enriched with the DESCRIPTION
23 #' fields that `snapshot()` would write (`Depends`, `Imports`, `Suggests`,
24 #' `LinkingTo`, `License`, etc.)? Defaults to `TRUE`. When `TRUE`,
25 #' `record()` consults the active package repositories (and crandb, when
26 #' enabled, for CRAN packages) to fill in the additional fields, erroring
27 #' if the remote source is unreachable. The `Hash` field is not computed
28 #' for enriched records. Additional DESCRIPTION-only fields (`Title`,
29 #' `Description`, `Author`, `Maintainer`) are only included when the
30 #' source can supply them -- typically when crandb is reachable for CRAN
31 #' packages, or when the source is a Git host (GitHub, GitLab, Bitbucket).
32 #' Set to `FALSE` to keep the minimal record (`Package`, `Version`,
33 #' `Source`, `Repository`) produced by remote resolution.
34 #'
35 #' @example examples/examples-record.R
36 #' @export
37 record <- function(records,
38 lockfile = NULL,
39 project = NULL,
40 enrich = TRUE)
41 {
42 renv_scope_error_handler()
43
44 project <- renv_project_resolve(project)
45 renv_project_lock(project = project)
46
47 lockfile <- lockfile %||% renv_lockfile_path(project)
48
49 # track which entries came from a character spec; only those entries
50 # are enriched, so caller-supplied list records pass through unchanged
51 # (and offline, since enrichment otherwise requires a reachable source)
52 if (is.character(records)) {
53 enrichable <- rep_len(TRUE, length(records))
54 records <- lapply(records, renv_remotes_resolve, latest = TRUE)
55 } else if (is.list(records)) {
56 enrichable <- map_lgl(records, is.character)
57 records <- renv_records_resolve(records, latest = TRUE)
58 } else {
59 stopf("unexpected records format '%s'", typeof(records))
60 }
61
62 if (enrich) {
63 for (i in seq_along(records)) {
64 if (enrichable[[i]] && !is.null(records[[i]]))
65 records[[i]] <- renv_record_enrich(records[[i]])
66 }
67 }
68
69 names(records) <- enum_chr(records, function(package, record) {
70 if (is.null(package) || is.na(package) || !nzchar(package))
71 record[["Package"]]
72 else
73 package
74 })
75
76 if (is.list(lockfile))
77 return(renv_lockfile_modify(lockfile, records))
78
79 if (!file.exists(lockfile)) {
80 fmt <- "no lockfile exists at path %s"
81 stopf(fmt, renv_path_pretty(lockfile))
82 }
83
84 old <- renv_lockfile_read(lockfile)
85 new <- renv_lockfile_modify(old, records)
86
87 local({
88 renv_scope_options(renv.verbose = FALSE)
89 renv_lockfile_write(new, lockfile)
90 })
91
92 n <- length(records)
93 fmt <- "- Updated %s in %s."
94 writef(fmt, nplural("record", n), renv_path_pretty(lockfile))
95
96 renv <- records[["renv"]]
97 if (!is.null(renv) && !is.null(renv[["Version"]])) {
98 renv_infrastructure_write_activate(
99 project = project,
100 version = renv[["Version"]]
101 )
102 }
103
104 invisible(lockfile)
105
106 }
107
108 renv_record_normalize <- function(record) {
109
110 # normalize source
111 source <- record$Source %||% "unknown"
112 if (source %in% c("CRAN", "P3M", "PPM", "RSPM"))
113 record$Source <- "Repository"
114
115 # drop remotes from records with a repository source
116 if (renv_record_cranlike(record))
117 record <- record[grep("^Remote", names(record), invert = TRUE)]
118
119 # keep only specific records for comparison
120 remotes <- grep("^Remote", names(record), value = TRUE)
121 keep <- c("Package", "Version", "Source", remotes)
122 record <- record[intersect(names(record), keep)]
123
124 # return normalized record
125 record
126
127 }
128
129 renv_record_tag <- function(record, type, url, name) {
130
131 attr(record, "url") <- url
132 attr(record, "type") <- type
133 attr(record, "name") <- name
134
135 record
136
137 }
138
139 renv_record_tagged <- function(record) {
140 attrs <- attributes(record)
141 all(c("url", "type") %in% names(attrs))
142 }
143
144 # abstracted out in case we want to use a different sigil in the future,
145 # like `_`, `<NA>`, or something else
146 renv_record_placeholder <- function() {
147 "*"
148 }
149
150 renv_record_cranlike <- function(record) {
151 type <- record[["RemoteType"]]
152 is.null(type) || tolower(type) %in% c("cran", "repository", "standard")
153 }
154
155 # enrich a resolved record with DESCRIPTION-level fields so the record
156 # matches what snapshot() would write. delegates the actual DESCRIPTION
157 # fetch to renv_graph_description(), which reuses the same infrastructure
158 # the restore graph uses (available.packages metadata + crandb fallback,
159 # Git host APIs, etc.). enrichment never reads the local library: we
160 # always trust the remote description. Hash is not computed.
161 renv_record_enrich <- function(record) {
162
163 source <- renv_record_source(record, normalize = TRUE)
164
165 enrichable <- c(
166 "repository", "bioconductor",
167 "github", "gitlab", "bitbucket", "git"
168 )
169
170 if (!source %in% enrichable)
171 return(record)
172
173 memoize(
174 key = renv_record_enrich_key(record, source = source),
175 value = renv_record_enrich_impl(record),
176 scope = "record-enrich"
177 )
178
179 }
180
181 renv_record_enrich_key <- function(record, source = NULL) {
182
183 source <- source %||% renv_record_source(record, normalize = TRUE)
184
185 # include every field that contributes to source identity. plain git
186 # remotes (Source = "git") have no RemoteSha, so distinct URLs would
187 # otherwise alias to the same cache slot.
188 fields <- c(
189 "Package", "Version", "Source", "Repository",
190 "RemoteType", "RemoteHost", "RemoteUrl", "RemoteUsername",
191 "RemoteRepo", "RemoteRef", "RemoteSha", "RemoteSubdir"
192 )
193 parts <- map_chr(fields, function(field) record[[field]] %||% "")
194
195 # for repository / bioconductor sources, the description fetcher reads
196 # from options('repos'); include that in the key so the cache invalidates
197 # when repos change mid-session. version-pinned specs in particular have
198 # no Repository field, so without this the key collapses across repos.
199 if (source %in% c("repository", "bioconductor")) {
200 repos <- getOption("repos")
201 parts <- c(
202 parts,
203 paste(names(repos) %||% "", repos, sep = "=", collapse = ",")
204 )
205 }
206
207 paste(parts, collapse = "|")
208
209 }
210
211 renv_record_enrich_impl <- function(record) {
212
213 package <- record[["Package"]]
214 if (is.null(package) || !nzchar(package))
215 stopf("cannot enrich record: missing 'Package' field")
216
217 # delegate to the graph description fetcher; it handles every source
218 # type we recognize and errors loudly when the source is unreachable
219 desc <- renv_graph_description(record)
220
221 # carry forward fields from the resolved record (Source, Repository,
222 # Remote*) so they aren't lost if the description-fetcher didn't set them
223 for (field in names(record))
224 if (is.null(desc[[field]]))
225 desc[[field]] <- record[[field]]
226
227 # apply the v2 snapshot transform so output matches what snapshot() writes
228 enriched <- renv_snapshot_description_impl_v2(desc, path = NULL)
229
230 # prefer the named form of Repository (e.g. "CRAN") that snapshot() writes
231 # when reading an installed DESCRIPTION. the description fetcher leaves
232 # Repository as the contrib URL from available.packages(); use the input
233 # record's value if set, else the Name stamped onto the entry by
234 # renv_available_packages_entry().
235 enriched[["Source"]] <- record[["Source"]] %||% enriched[["Source"]]
236 enriched[["Repository"]] <-
237 record[["Repository"]] %||%
238 desc[["Name"]] %||%
239 enriched[["Repository"]]
240
241 # Name is a remnant of the available.packages entry, not a lockfile field
242 enriched[["Name"]] <- NULL
243
244 # Hash is not computed for enriched records; see ?record
245 enriched[["Hash"]] <- NULL
246
247 enriched
248
249 }