Debian Codegraph @

R/sysreqs.R

raw
1
2 the$sysreqs <- NULL
3
4 #' R System Requirements
5 #'
6 #' Compute the system requirements (system libraries; operating system packages)
7 #' required by a set of \R packages.
8 #'
9 #' This function relies on the database of package system requirements
10 #' maintained by Posit at <https://github.com/rstudio/r-system-requirements>,
11 #' as well as the "meta-CRAN" service at <https://crandb.r-pkg.org>. This
12 #' service primarily exists to map the (free-form) `SystemRequirements` field
13 #' used by \R packages to the system packages made available by a particular
14 #' operating system.
15 #'
16 #' As an example, the `curl` R package depends on the `libcurl` system library,
17 #' and declares this with a `SystemRequirements` field of the form:
18 #'
19 #' - libcurl (>= 7.62): libcurl-devel (rpm) or libcurl4-openssl-dev (deb)
20 #'
21 #' This dependency can be satisfied with the following command line invocations
22 #' on different systems:
23 #'
24 #' - Debian: `sudo apt install libcurl4-openssl-dev`
25 #' - Redhat: `sudo dnf install libcurl-devel`
26 #'
27 #' and so `sysreqs("curl")` would help provide the name of the package
28 #' whose installation would satisfy the `libcurl` dependency.
29 #'
30 #'
31 #' @inheritParams renv-params
32 #'
33 #' @param packages A vector of \R package names. When `NULL`
34 #' (the default), the project's package dependencies as reported via
35 #' [renv::dependencies()] are used.
36 #'
37 #' @param local Boolean; should `renv` rely on locally-installed copies of
38 #' packages when resolving system requirements? When `FALSE`, `renv` will
39 #' use <https://crandb.r-pkg.org> to resolve the system requirements
40 #' for these packages.
41 #'
42 #' @param check Boolean; should `renv` also check whether the requires system
43 #' packages appear to be installed on the current system? Ignored when
44 #' `distro` is supplied.
45 #'
46 #' @param report Boolean; should `renv` also report the commands which could be
47 #' used to install all of the requisite package dependencies?
48 #'
49 #' @param collapse Boolean; when reporting which packages need to be installed,
50 #' should the report be collapsed into a single installation command? When
51 #' `FALSE` (the default), a separate installation line is printed for each
52 #' required system package.
53 #'
54 #' @param distro The name of the Linux distribution for which system requirements
55 #' should be checked -- typical values are "ubuntu", "debian", and "redhat".
56 #' These should match the distribution names used by the R system requirements
57 #' database. A version suffix can be included; for example, "ubuntu:24.04".
58 #'
59 #' @examples
60 #'
61 #' \dontrun{
62 #'
63 #' # report the required system packages for this system
64 #' sysreqs()
65 #'
66 #' # report the required system packages for a specific OS
67 #' sysreqs(platform = "ubuntu")
68 #'
69 #' }
70 #'
71 #' @export
72 sysreqs <- function(packages = NULL,
73 ...,
74 local = FALSE,
75 check = NULL,
76 report = TRUE,
77 distro = NULL,
78 collapse = FALSE,
79 project = NULL)
80 {
81 # allow user to provide additional package names as part of '...'
82 if (!missing(...)) {
83 dots <- list(...)
84 names(dots) <- names(dots) %||% rep.int("", length(dots))
85 packages <- c(packages, dots[!nzchar(names(dots))])
86 }
87
88 # resolve packages
89 packages <- packages %||% {
90 project <- renv_project_resolve(project)
91 deps <- dependencies(project, dev = TRUE)
92 sort(unique(deps$Package))
93 }
94
95 # remove 'base' packages
96 base <- installed_packages(priority = "base")
97 packages <- setdiff(packages, base$Package)
98 names(packages) <- packages
99
100 # resolve check
101 check <- check %||% is.null(distro)
102
103 # resolve distro
104 distro <- distro %||% the$distro
105 if (!identical(distro, the$distro)) {
106 parts <- strsplit(distro, ":", fixed = TRUE)[[1L]]
107 distro <- parts[[1L]]
108 version <- if (length(parts) >= 2L) parts[[2L]]
109 renv_scope_binding(the, "os", "linux")
110 renv_scope_binding(the, "distro", parts[[1L]])
111 renv_scope_binding(the, "platform", list(VERSION_ID = version))
112 }
113
114 # compute package records
115 if (local) {
116 lockfile <- renv_lockfile_create(project, dev = TRUE)
117 records <- renv_lockfile_records(lockfile)
118 } else {
119 callback <- renv_progress_callback(renv_sysreqs_crandb, length(packages))
120 records <- map(packages, callback)
121 }
122
123 # extract and resolve the system requirements
124 sysreqs <- map(records, `[[`, "SystemRequirements")
125 sysdeps <- map(sysreqs, renv_sysreqs_resolve)
126
127 # check the package status if possible
128 if (check)
129 renv_sysreqs_check(sysreqs, prompt = FALSE)
130
131 # report installation commands if requested
132 if (report)
133 renv_sysreqs_report(sysdeps, distro, collapse)
134
135 # return result
136 invisible(sysdeps)
137
138 }
139
140 renv_sysreqs_report <- function(sysdeps, distro, collapse) {
141
142 # collect all system packages
143 syspkgs <- map(sysdeps, `[[`, "packages")
144 allpkgs <- sort(unique(unlist(syspkgs)))
145 if (empty(allpkgs))
146 return()
147
148 # include pre-install commands as well, if any
149 preinstall <- unlist(map(sysdeps, `[[`, "pre_install"))
150 if (length(preinstall)) {
151 if (interactive()) {
152 preamble <- "System pre-requisites can be installed with:"
153 bulletin(preamble, preinstall)
154 } else {
155 writeLines(preinstall)
156 }
157 }
158
159 # generate installation commands
160 installer <- renv_sysreqs_installer(distro)
161 body <- if (collapse) paste(allpkgs, collapse = " ") else allpkgs
162 message <- paste("sudo", installer, "-y", body)
163
164 if (interactive()) {
165 preamble <- "The requisite system packages can be installed with:"
166 bulletin(preamble, message)
167 } else {
168 writeLines(message)
169 }
170
171 }
172
173 renv_sysreqs_crandb <- function(package) {
174 tryCatch(
175 renv_sysreqs_crandb_impl(package),
176 error = warnify
177 )
178 }
179
180 renv_sysreqs_crandb_impl <- function(package) {
181 memoize(
182 key = package,
183 value = renv_sysreqs_crandb_impl_one(package),
184 scope = "sysreqs"
185 )
186 }
187
188 renv_sysreqs_crandb_impl_one <- function(package) {
189 url <- paste("https://crandb.r-pkg.org", package, sep = "/")
190 destfile <- tempfile("renv-crandb-", fileext = ".json")
191 download(url, destfile = destfile, quiet = TRUE)
192 renv_json_read(destfile)
193 }
194
195 renv_sysreqs_resolve <- function(sysreqs, rules = renv_sysreqs_rules()) {
196 matches <- map(sysreqs, renv_sysreqs_match, rules)
197 unlist(matches, recursive = FALSE)
198 }
199
200 renv_sysreqs_rules <- function() {
201 the$sysreqs <- the$sysreqs %||% renv_sysreqs_rules_impl()
202 }
203
204 renv_sysreqs_rules_impl <- function() {
205 rules <- system.file("sysreqs/sysreqs.json", package = "renv")
206 renv_json_read(rules)
207 }
208
209 renv_sysreqs_match <- function(sysreq, rules = renv_sysreqs_rules()) {
210
211 for (rule in rules) {
212 match <- renv_sysreqs_match_impl(sysreq, rule)
213 if (!is.null(match)) {
214 return(match)
215 }
216 }
217
218 }
219
220 renv_sysreqs_match_impl <- function(sysreq, rule) {
221
222 # check for a match in the declared system requirements
223 pattern <- paste(rule$patterns, collapse = "|")
224 matches <- grepl(pattern, sysreq, ignore.case = TRUE, perl = TRUE)
225
226 # if we got a match, pull out the dependent packages
227 if (matches) {
228 for (dependency in rule$dependencies) {
229 for (constraint in dependency$constraints) {
230 if (renv_sysreqs_satisfies(constraint)) {
231 return(dependency)
232 }
233 }
234 }
235 }
236
237 }
238
239 renv_sysreqs_satisfies <- function(constraint) {
240
241 if (constraint$os == the$os) {
242 if (constraint$distribution == the$distro) {
243 if (!is.null(the$platform$VERSION_ID)) {
244 versions <- constraint$versions %||% the$platform$VERSION_ID
245 for (version in versions) {
246 if (startsWith(the$platform$VERSION_ID, version)) {
247 return(TRUE)
248 }
249 }
250 }
251 }
252 }
253
254 FALSE
255
256 }
257
258 renv_sysreqs_aliases <- function(type, syspkgs) {
259 case(
260 type == "deb" ~ renv_sysreqs_aliases_deb(syspkgs),
261 type == "rpm" ~ renv_sysreqs_aliases_rpm(syspkgs)
262 )
263 }
264
265 renv_sysreqs_aliases_deb <- function(pkgs) {
266
267 # https://www.debian.org/doc/debian-policy/ch-relationships.html#s-virtual
268 #
269 # > A virtual package is one which appears in the Provides control field of
270 # > another package. The effect is as if the package(s) which provide a
271 # > particular virtual package name had been listed by name everywhere the
272 # > virtual package name appears. (See also Virtual packages)
273 #
274 # read the package database, look which packages 'provide' others,
275 # and then reverse that map to map virtual packages to the concrete
276 # package which provides them
277 #
278 command <- "dpkg-query -W -f '${Package}=${Provides}\n'"
279 output <- system(command, intern = TRUE)
280 result <- renv_properties_read(text = output, delimiter = "=")
281
282 # keep only packages which provide other packages
283 aliases <- result[nzchar(result)]
284
285 # a package might provide multiple other packages, so split those
286 splat <- lapply(aliases, function(alias) {
287 parts <- strsplit(alias, ",\\s*", perl = TRUE)[[1L]]
288 names(renv_properties_read(text = parts, delimiter = " "))
289 })
290
291 # reverse the map, so that we can map virtual packages to the
292 # concrete packages which they refer to
293 envir <- new.env(parent = emptyenv())
294 enumerate(splat, function(package, virtuals) {
295 for (virtual in virtuals) {
296 envir[[virtual]] <<- c(envir[[virtual]], package)
297 }
298 })
299
300 # convert to intermediate list
301 result <- as.list(envir, all.names = TRUE)
302
303 # return as named character vector
304 convert(result, type = "character")
305
306 }
307
308 renv_sysreqs_aliases_rpm <- function(pkgs) {
309
310 # return early if no packages provided
311 if (empty(pkgs))
312 return(character())
313
314 # for each package, check if there's another package that 'provides' it
315 fmt <- "rpm --query --whatprovides %s --queryformat '%%{Name}\n'"
316 args <- paste(renv_shell_quote(pkgs), collapse = " ")
317 command <- sprintf(fmt, args)
318 result <- suppressWarnings(system(command, intern = TRUE))
319
320 # return as named vector, mapping virtual packages to 'real' packages
321 matches <- grep("no package provides", result, fixed = TRUE, invert = TRUE)
322 aliases <- result[matches]
323 names(aliases) <- pkgs[matches]
324
325 convert(aliases, type = "character")
326
327 }
328
329 renv_sysreqs_type <- function() {
330
331 # try to infer package type from the known distro
332 distro <- the$distro
333 if (!is.null(distro)) {
334
335 deb <- c("debian", "ubuntu")
336 rpm <- c(
337 "centos", "fedora", "opensuse", "opensuse-leap",
338 "opensuse-tumbleweed", "redhat", "rocky", "rockylinux",
339 "sle", "sles"
340 )
341
342 type <- case(
343 distro %in% deb ~ "deb",
344 distro %in% rpm ~ "rpm"
345 )
346
347 if (!is.null(type))
348 return(type)
349
350 }
351
352 # fall back to checking which tools are available
353 case(
354 nzchar(Sys.which("dpkg-query")) ~ "deb",
355 nzchar(Sys.which("rpm")) ~ "rpm"
356 )
357
358 }
359
360 renv_sysreqs_check <- function(sysreqs, prompt) {
361
362 # check the tool used for package queries
363 type <- renv_sysreqs_type()
364 if (is.null(type))
365 return(NULL)
366
367 # figure out which system packages are required
368 sysdeps <- map(sysreqs, renv_sysreqs_resolve)
369 syspkgs <- map(sysdeps, `[[`, "packages")
370
371 # collect list of all packages discovered
372 allsyspkgs <- sort(unique(unlist(syspkgs, use.names = FALSE)))
373
374 # some packages might be virtual packages, and won't be reported as installed
375 # when queried. try to resolve those to the actual underlying packages.
376 # some examples follows:
377 #
378 # Fedora 41: zlib-devel => zlib-ng-compat-devel
379 # Ubuntu 24.04: libfreetype6-dev => libfreetype-dev
380 #
381 aliases <- renv_sysreqs_aliases(type, allsyspkgs)
382 resolvedpkgs <- alias(allsyspkgs, aliases)
383
384 # list all currently-installed packages
385 installedpkgs <- case(
386 type == "deb" ~ system("dpkg-query -W -f '${Package}\n'", intern = TRUE),
387 type == "rpm" ~ system("rpm --query --all --queryformat='%{Name}\n'", intern = TRUE)
388 )
389
390 # check for matches
391 misspkgs <- setdiff(resolvedpkgs, installedpkgs)
392 if (empty(misspkgs))
393 return(TRUE)
394
395 # notify the user
396 preamble <- "The following required system packages are not installed:"
397 postamble <- "The R packages depending on these system packages may fail to install."
398 parts <- map(misspkgs, function(misspkg) {
399 needs <- map_lgl(syspkgs, function(syspkg) misspkg %in% syspkg)
400 list(misspkg, names(syspkgs)[needs])
401 })
402
403 lhs <- extract_chr(parts, 1L)
404 rhs <- map_chr(extract(parts, 2L), paste, collapse = ", ")
405 messages <- sprintf("%s [required by %s]", format(lhs), rhs)
406 bulletin(preamble, messages, postamble)
407
408 installer <- case(
409 nzchar(Sys.which("apt")) ~ "apt install",
410 nzchar(Sys.which("dnf")) ~ "dnf install",
411 nzchar(Sys.which("pacman")) ~ "pacman -S",
412 nzchar(Sys.which("yum")) ~ "yum install",
413 nzchar(Sys.which("zypper")) ~ "zypper install",
414 )
415
416 preamble <- "An administrator can install these packages with:"
417 command <- paste("sudo", installer, paste(misspkgs, collapse = " "))
418 bulletin(preamble, command)
419
420 cancel_if(prompt && !proceed())
421
422 }
423
424 renv_sysreqs_installer <- function(distro) {
425
426 installer <- getOption("renv.sysreqs.installer", default = NULL)
427 if (!is.null(installer))
428 return(installer)
429
430 case(
431 distro == "alpine" ~ "apk add",
432 distro == "debian" ~ "apt install",
433 distro == "fedora" ~ "dnf install",
434 distro == "opensuse" ~ "zypper install",
435 distro == "redhat" ~ "dnf install",
436 distro == "rockylinux" ~ "dnf install",
437 distro == "sle" ~ "zypper install",
438 distro == "ubuntu" ~ "apt install",
439 ~ "<install>"
440 )
441 }
442
443 renv_sysreqs_update <- function() {
444
445 # save path to sysreqs folder
446 dest <- renv_path_normalize("inst/sysreqs/sysreqs.json")
447
448 # move to temporary directory
449 renv_scope_tempdir()
450
451 # clone the system requirements repository
452 args <- c("clone", "--depth", "1", "https://github.com/rstudio/r-system-requirements")
453 renv_system_exec("git", args, action = "cloing rstudio/r-system-requirements")
454
455 # read all of the rules from the requirements repository
456 files <- list.files(
457 path = "r-system-requirements/rules",
458 pattern = "[.]json$",
459 full.names = TRUE
460 )
461
462 contents <- map(files, renv_json_read)
463
464 # give names without extensions for these files
465 names <- basename(files)
466 idx <- map_int(gregexpr(".", names, fixed = TRUE), tail, n = 1L)
467 names(contents) <- substr(names, 1L, idx - 1L)
468
469 # write to sysreqs.json
470 renv_json_write(contents, file = dest)
471
472 }