Debian Codegraph @

R/lockfiles.R

raw
1
2 #' Lockfiles
3 #'
4 #' A **lockfile** records the state of a project at some point in time.
5 #'
6 #' A lockfile captures the state of a project's library at some point in time.
7 #' In particular, the package names, their versions, and their sources (when
8 #' known) are recorded in the lockfile.
9 #'
10 #' Projects can be restored from a lockfile using the [restore()] function. This
11 #' implies reinstalling packages into the project's private library, as encoded
12 #' within the lockfile.
13 #'
14 #' While lockfiles are normally generated and used with [snapshot()] /
15 #' [restore()], they can also be edited by hand if so desired. Lockfiles are
16 #' written as `.json`, to allow for easy consumption by other tools.
17 #'
18 #' An example lockfile follows:
19 #'
20 #' ```
21 #' {
22 #' "R": {
23 #' "Version": "3.6.1",
24 #' "Repositories": [
25 #' {
26 #' "Name": "CRAN",
27 #' "URL": "https://cloud.r-project.org"
28 #' }
29 #' ]
30 #' },
31 #' "Packages": {
32 #' "markdown": {
33 #' "Package": "markdown",
34 #' "Version": "1.0",
35 #' "Source": "Repository",
36 #' "Repository": "CRAN",
37 #' "Hash": "4584a57f565dd7987d59dda3a02cfb41"
38 #' },
39 #' "mime": {
40 #' "Package": "mime",
41 #' "Version": "0.7",
42 #' "Source": "Repository",
43 #' "Repository": "CRAN",
44 #' "Hash": "908d95ccbfd1dd274073ef07a7c93934"
45 #' }
46 #' }
47 #' }
48 #' ```
49 #'
50 #' The sections used within a lockfile are described next.
51 #'
52 #' ## renv
53 #'
54 #' Information about the version of renv used to manage this project.
55 #'
56 #' \tabular{ll}{
57 #' \strong{Version} \tab The version of the renv package used with this project. \cr
58 #' }
59 #'
60 #' ## R
61 #'
62 #' Properties related to the version of \R associated with this project.
63 #'
64 #' \tabular{ll}{
65 #' \strong{Version} \tab The version of \R used. \cr
66 #' \strong{Repositories} \tab The \R repositories used in this project. \cr
67 #' }
68 #'
69 #' ## Packages
70 #'
71 #' \R package records, capturing the packages used or required by a project
72 #' at the time when the lockfile was generated.
73 #'
74 #' \tabular{ll}{
75 #' \strong{Package} \tab The package name. \cr
76 #' \strong{Version} \tab The package version. \cr
77 #' \strong{Source} \tab The location from which this package was retrieved. \cr
78 #' \strong{Repository} \tab The name of the repository (if any) from which this package was retrieved. \cr
79 #' \strong{Hash} \tab (Optional) A unique hash for this package, used for package caching. \cr
80 #' }
81 #'
82 #' Additional remote fields, further describing how the package can be
83 #' retrieved from its corresponding source, will also be included as
84 #' appropriate (e.g. for packages installed from GitHub).
85 #'
86 #' ## Python
87 #'
88 #' Metadata related to the version of Python used with this project (if any).
89 #'
90 #' \tabular{ll}{
91 #' \strong{Version} \tab The version of Python being used. \cr
92 #' \strong{Type} \tab The type of Python environment being used ("virtualenv", "conda", "system") \cr
93 #' \strong{Name} \tab The (optional) name of the environment being used.
94 #' }
95 #'
96 #' Note that the `Name` field may be empty. In that case, a project-local Python
97 #' environment will be used instead (when not directly using a system copy of Python).
98 #'
99 #' # Caveats
100 #'
101 #' These functions are primarily intended for expert users -- in most cases,
102 #' [snapshot()] and [restore()] are the primariy tools you will need when
103 #' creating and using lockfiles.
104 #'
105 #' @inheritParams snapshot
106 #' @inheritParams renv-params
107 #'
108 #' @param lockfile An `renv` lockfile; typically created by either
109 #' `lockfile_create()` or `lockfile_read()`.
110 #'
111 #' @param file A file path, or \R connection.
112 #'
113 #' @family reproducibility
114 #' @name lockfiles
115 #' @rdname lockfiles
116 NULL
117
118 #' @param libpaths The library paths to be used when generating the lockfile.
119 #' @rdname lockfiles
120 #' @export
121 lockfile_create <- function(type = settings$snapshot.type(project = project),
122 libpaths = .libPaths(),
123 packages = NULL,
124 exclude = NULL,
125 prompt = interactive(),
126 force = FALSE,
127 ...,
128 project = NULL)
129 {
130 renv_dots_check(...)
131
132 project <- renv_project_resolve(project)
133 renv_scope_verbose_if(prompt)
134
135 renv_lockfile_create(
136 project = project,
137 type = type,
138 libpaths = libpaths,
139 packages = packages,
140 exclude = exclude,
141 prompt = prompt,
142 force = force
143 )
144 }
145
146 #' @rdname lockfiles
147 #' @export
148 lockfile_read <- function(file = NULL, ..., project = NULL) {
149 project <- renv_project_resolve(project)
150 file <- file %||% renv_paths_lockfile(project = project)
151 renv_lockfile_read(file = file)
152 }
153
154 #' @rdname lockfiles
155 #' @export
156 lockfile_write <- function(lockfile, file = NULL, ..., project = NULL) {
157 project <- renv_project_resolve(project)
158 file <- file %||% renv_paths_lockfile(project = project)
159 renv_lockfile_write(lockfile, file = file)
160 }
161
162 #' @param remotes An \R vector of remote specifications.
163 #'
164 #' @param repos A named vector, mapping \R repository names to their URLs.
165 #'
166 #' @rdname lockfiles
167 #' @export
168 lockfile_modify <- function(lockfile = NULL,
169 ...,
170 remotes = NULL,
171 repos = NULL,
172 project = NULL)
173 {
174 renv_dots_check(...)
175
176 project <- renv_project_resolve(project)
177 lockfile <- lockfile %||% renv_lockfile_load(project, strict = TRUE)
178
179 if (!is.null(repos))
180 lockfile$R$Repositories <- as.list(repos)
181
182 if (!is.null(remotes)) {
183 remotes <- renv_records_resolve(remotes, latest = TRUE)
184 names(remotes) <- map_chr(remotes, `[[`, "Package")
185 enumerate(remotes, function(package, remote) {
186 record <- renv_remotes_resolve(remote)
187 renv_lockfile_records(lockfile)[[package]] <<- record
188 })
189 }
190
191 lockfile
192
193 }