-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathsysreqs2.R
More file actions
388 lines (355 loc) · 13.2 KB
/
Copy pathsysreqs2.R
File metadata and controls
388 lines (355 loc) · 13.2 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# There is no easy way to update the database in yum/dnf/zypper.
# But FIXME. We'll need to solve this issue when (and if) we'll check the
# installed system packages, and only install/update the ones that are
# missing or out of date. For now this is OK, the first `install` command
# will update the DB (cache it is called I believe), and the rest will
# use it.
# TODO: get this data from the DB
sysreqs2_cmds <- utils::read.table(
stringsAsFactors = FALSE,
header = TRUE,
textConnection(
"
name os id distribution version version_match update_command install_command query_command
'Ubuntu Linux' linux ubuntu ubuntu * NA 'apt-get -y update' 'apt-get -y install' dpkg-query
'Debian Linux' linux debian debian * NA 'apt-get -y update' 'apt-get -y install' dpkg-query
'CentOS Linux' linux centos centos * major NA 'yum install -y' rpm
'Rocky Linux' linux rocky rockylinux * major NA 'dnf install -y' rpm
'Rocky Linux' linux rockylinux rockylinux * major NA 'dnf install -y' rpm
'AlmaLinux' linux almalinux rockylinux * major NA 'dnf install -y' rpm
'Red Hat Enterprise Linux' linux rhel redhat 6 major NA 'yum install -y' rpm
'Red Hat Enterprise Linux' linux rhel redhat 7 major NA 'yum install -y' rpm
'Red Hat Enterprise Linux' linux rhel redhat * major NA 'dnf install -y' rpm
'Red Hat Enterprise Linux' linux redhat redhat 6 major NA 'yum install -y' rpm
'Red Hat Enterprise Linux' linux redhat redhat 7 major NA 'yum install -y' rpm
'Red Hat Enterprise Linux' linux redhat redhat * major NA 'dnf install -y' rpm
'Fedora Linux' linux fedora fedora * NA NA 'dnf install -y' rpm
'openSUSE Linux' linux opensuse opensuse * NA NA 'zypper --non-interactive install' rpm
'openSUSE Linux' linux opensuse-leap opensuse * NA NA 'zypper --non-interactive install' rpm
'openSUSE Linux' linux opensuse-tumbleweed opensuse * NA NA 'zypper --non-interactive install' rpm
'SUSE Linux Enterprise' linux sles sle * NA NA 'zypper --non-interactive install' rpm
'SUSE Linux Enterprise' linux sle sle * NA NA 'zypper --non-interactive install' rpm
'Alpine Linux' linux alpine alpine * minor NA 'apk add --no-cache' apk
"
)
)
# do not use package_version, in case the distro is nor semver
get_major_version <- function(x) {
sub("[.].*$", "", x)
}
get_minor_version <- function(x) {
sub("([.][^.]+)[.].*$", "\\1", x)
}
find_sysreqs_platform <- function(sysreqs_platform = NULL, parsed = NULL) {
plt <- parsed %||%
parse_sysreqs_platform(
sysreqs_platform %||% current_config()$get("sysreqs_platform")
)
plt$version_major <- get_major_version(plt$version)
plt$version_minor <- get_minor_version(plt$version)
which(
sysreqs2_cmds$os == plt$os &
sysreqs2_cmds$id == plt$distribution &
(sysreqs2_cmds$version %in%
c("*", plt$version) |
(sysreqs2_cmds$version_match == "major" &
sysreqs2_cmds$version == plt$version_major) |
(sysreqs2_cmds$version_match == "minor" &
sysreqs2_cmds$version == plt$version_minor))
)[1]
}
canonize_sysreqs_platform <- function(sysreqs_platform) {
parsed <- parse_sysreqs_platform(sysreqs_platform)
known <- find_sysreqs_platform(parsed = parsed)
if (is.na(known)) {
sysreqs_platform
} else {
plt <- sysreqs_platforms()[known, ]
paste0(
plt$distribution,
"-",
if (plt$version != "*") {
plt$version
} else if (identical(plt$version_match, "major")) {
get_major_version(parsed$version)
} else if (identical(plt$version_match, "minor")) {
get_minor_version(parsed$version)
} else {
parsed$version
}
)
}
}
sysreqs2_command <- function(
sysreqs_platform = NULL,
cmd = c("install_command", "update_command", "query_command")
) {
cmd <- match.arg(cmd)
sel <- find_sysreqs_platform(sysreqs_platform)
if (is.na(sel)) {
throw(pkg_error(paste0(
"Unknown OS. Don't know how to query or install system packages for ",
sysreqs_platform,
"."
)))
}
sysreqs2_cmds[[cmd]][sel]
}
sysreqs2_resolve <- function(
sysreqs,
sysreqs_platform = NULL,
config = NULL,
...
) {
synchronize(sysreqs2_async_resolve(sysreqs, sysreqs_platform, config, ...))
}
sysreqs2_async_resolve <- function(sysreqs, sysreqs_platform, config, ...) {
sysreqs
sysreqs_platform
config
list(...)
config <- config %||% current_config()
sysreqs_platform <- sysreqs_platform %||% config$get("sysreqs_platform")
sysreqs2_async_update_metadata(config = config)$then(function() {
sysreqs2_match(
sysreqs,
sysreqs_platform = sysreqs_platform,
config = config,
...
)
})$then(function(recs) {
sysreqs2_scripts(recs, sysreqs_platform)
})
}
sysreqs2_scripts <- function(recs, sysreqs_platform, missing = FALSE) {
sysreqs_platform <- canonize_sysreqs_platform(sysreqs_platform)
plt <- parse_sysreqs_platform(sysreqs_platform)
flatrecs <- unlist(recs, recursive = FALSE)
upd <- sysreqs2_command(sysreqs_platform, "update")
pre <- unique(unlist(lapply(flatrecs, "[[", "pre_install")))
post <- unique(unlist(lapply(flatrecs, "[[", "post_install")))
if (is.na(upd)) {
upd <- character()
}
cmd <- sysreqs2_command(sysreqs_platform, "install")
allpkgs <- unique(unlist(lapply(flatrecs, "[[", "packages")))
misspkgs <- unique(unlist(lapply(flatrecs, function(x) {
if ("packages_missing" %in% names(x)) {
x$packages_missing
} else {
x$packages
}
})))
pkgs <- if (missing) misspkgs else allpkgs
ipkgs <- if (length(pkgs)) paste(pkgs, collapse = " ") else character()
ipkgs <- if (length(ipkgs)) paste(cmd, ipkgs)
# no need to update if nothing to do
if (length(pre) + length(ipkgs) + length(post) == 0) {
upd <- character()
}
res <- list(
os = plt$os,
distribution = plt$distribution,
version = plt$version,
url = NA_character_,
pre_install = c(upd, pre),
install_scripts = ipkgs,
post_install = post,
packages = allpkgs
)
if (missing) {
res$misspkgs <- misspkgs
}
res
}
sysreqs2_git_repo <- function() {
list(
repo = Sys.getenv(
"R_PKG_SYSREQS_GIT_REPO_URL",
"https://github.com/r-hub/r-system-requirements.git"
),
ref = Sys.getenv(
"R_PKG_SYSREQS_GIT_REPO_REF",
"HEAD"
)
)
}
sysreqs2_update_metadata <- function(path = NULL, config = NULL) {
synchronize(sysreqs2_async_update_metadata(path = path, config = config))
}
# Increase this if the syntax breaks or if we need more / different files
# from the r-system-requirements repo.
sysreqs_db_version <- "1"
sysreqs2_async_update_metadata <- function(path = NULL, config = NULL) {
config <- config %||% current_config()
if (!config$get("sysreqs_db_update")) {
return(async_constant())
}
path <- path %||% file.path(get_user_cache_dir()$root, "sysreqs")
head_file <- file.path(path, "HEAD")
ver_file <- file.path(path, "VERSION")
if (file.exists(head_file) && file.exists(ver_file)) {
mt <- file.mtime(head_file)
ver <- readLines(ver_file)
upd <- config$get("metadata_update_after")
if (ver == sysreqs_db_version && !is.na(mt) && Sys.time() - mt < upd) {
return(async_constant())
}
}
upd <- function() {
head <- if (file.exists(head_file)) readLines(head_file)[1] else ""
repo <- sysreqs2_git_repo()
async_git_list_refs_v1(repo$repo)$then(function(refs) {
rem_head <- refs$refs$hash[refs$refs$ref == "HEAD"]
if (rem_head == head) {
# still update the time stamps
Sys.setFileTime(head_file, Sys.time())
return()
}
tmp <- paste0(path, "-new")
async_git_download_repo(repo$repo, rem_head, tmp)$then(function() {
unlink(path, recursive = TRUE, force = TRUE)
file.rename(tmp, path)
writeLines(rem_head, head_file)
writeLines(sysreqs_db_version, ver_file)
})
})
}
timeout <- as.double(
config$get("sysreqs_db_update_timeout"),
units = "secs"
)
async_timeout(upd, timeout)$catch(error = function(e) {
cli::cli_alert_warning(
# nocov start
"Failed to update system requirement mappings,
will use cached mappings.",
wrap = TRUE
) # nocov end
invisible()
})
}
sysreqs2_list_rules <- function(path = NULL) {
if (is.null(path)) {
cached_path <- file.path(get_user_cache_dir()$root, "sysreqs")
head_file <- file.path(cached_path, "HEAD")
ver_file <- file.path(cached_path, "VERSION")
if (file.exists(ver_file) && readLines(ver_file) == sysreqs_db_version) {
path <- cached_path
} else {
path <- system.file("sysreqs", package = "pkgdepends")
}
}
rules <- dir(
file.path(path, "rules"),
pattern = "[.]json$",
full.names = TRUE
)
}
sysreqs2_match <- function(
sysreqs,
path = NULL,
sysreqs_platform = NULL,
config = NULL
) {
rules <- sysreqs2_list_rules(path)
result <- structure(
vector(mode = "list", length(sysreqs)),
names = names(sysreqs)
)
todo <- !is.na(sysreqs) & sysreqs != ""
config <- config %||% current_config()
plt <- parse_sysreqs_platform(canonize_sysreqs_platform(
sysreqs_platform %||% config$get("sysreqs_platform")
))
rsysreqs <- sysreqs[todo]
for (r in rules) {
rule <- jsonlite::fromJSON(r, simplifyVector = FALSE)
pats <- unlist(rule$patterns)
mch <- vapply(
pats,
grepl,
logical(length(rsysreqs)),
x = rsysreqs,
ignore.case = TRUE
)
mch <- apply(rbind(mch), 1, any)
if (!any(mch)) {
next
}
for (dep in rule$dependencies) {
appl <- FALSE
for (const in dep$constraints) {
if (
identical(const$os, plt$os) &&
identical(const$distribution, plt$distribution) &&
(is.null(const$versions) || plt$version %in% const$versions)
) {
appl <- TRUE
break
}
}
if (!appl) {
next
}
sysreq_name <- tools::file_path_sans_ext(basename(r))
rec <- list(
sysreq = sysreq_name,
packages = unname(unlist(dep$packages)),
pre_install = unname(unlist(dep$pre_install)),
post_install = unname(unlist(dep$post_install))
)
for (idx in which(mch)) {
result[todo][[idx]] <- c(result[todo][[idx]], list(rec))
}
break
}
}
result
}
# Same as pkgcache. E.g. https://p3m.dev/cran/__linux__/manylinux_2_28/latest
re_ppm_linux_repo <- function() {
paste0(
"^",
"(?<base>.*/)",
"(?<repo>[^/]+)/",
"__linux__/",
"(?<distro>[a-zA-Z0-9_]+)/",
"(?<version>latest|[-0-9]+)",
"$"
)
}
is_ppm_manylinux_repo <- function(urls) {
mch <- re_match(sub("/+$", "", as.character(urls)), re_ppm_linux_repo())
!is.na(mch$.match) & grepl("^manylinux", mch$distro)
}
binary_needs_no_sysreqs <- function(platform, mirror) {
is_manylinux_platform(platform) |
(!is.na(platform) & platform != "source" & is_ppm_manylinux_repo(mirror))
}
# Manylinux binaries from PPM bundle their system libraries in
# `<pkg>/libs/.libs`, so their system requirements are already embedded, and
# they do not need to be installed. Such a package always has
# `Repository: RSPM` in its DESCRIPTION file.
installed_needs_no_sysreqs <- function(package, repository, libpath) {
if (length(package) == 0 || is.null(repository) || is.null(libpath)) {
return(rep(FALSE, length(package)))
}
repository <- as.character(repository)
!is.na(repository) &
repository == "RSPM" &
dir.exists(file.path(libpath, package, "libs", ".libs"))
}
sysreqs_update_state <- function(sys, spkgs = NULL) {
spkgs <- spkgs %||% sysreqs_list_system_packages()
spkgs <- spkgs[grepl("^.i$", spkgs$status), ]
allspkgs <- unique(unlist(c(spkgs$package, spkgs$provides)))
for (i in seq_along(sys)) {
elt <- sys[[i]]
for (j in seq_along(elt)) {
elt[[j]]$packages_missing <- setdiff(elt[[j]]$packages, allspkgs)
}
if (!is.null(elt)) sys[[i]] <- elt
}
sys
}