Debian Codegraph @

R/ppm.R

raw
1
2 renv_ppm_parse <- function(url) {
3
4 pattern <- paste0(
5 "^", # start of url
6 "(?<root>", # start of root of url
7 "(?<scheme>[^:]+)://", # scheme
8 "(?<authority>[^/]+)", # authority
9 ")/", # end of root of url
10 "(?<repos>[^/]+)/", # repository name
11 "(?:", # begin optional binary parts
12 "(?<binary>__[^_]+__)/", # binary prefix
13 "(?<platform>[^/]+)/", # platform for binaries
14 ")?", # end optional binary parts
15 "(?<snapshot>[^/]+)", # snapshot
16 "$"
17 )
18
19 matches <- gregexpr(pattern, url, perl = TRUE)[[1]]
20 starts <- attr(matches, "capture.start")
21 ends <- starts + attr(matches, "capture.length") - 1
22 strings <- substring(url, starts, ends)
23 names(strings) <- attr(matches, "capture.names")
24
25 if (length(strings) == 0L || !any(nzchar(strings)))
26 return(NULL)
27
28 as.list(c(url = url, strings))
29
30 }
31
32 renv_ppm_normalize <- function(url) {
33 sub("/__[^_]+__/[^/]+/", "/", url)
34 }
35
36 renv_ppm_is_manylinux <- function(url) {
37 grepl("/__linux__/manylinux_\\d+_\\d+/", url)
38 }
39
40 renv_ppm_transform <- function(repos = getOption("repos")) {
41 map_chr(repos, function(url) {
42 tryCatch(
43 renv_ppm_transform_impl(url),
44 error = function(e) url
45 )
46 })
47 }
48
49 renv_ppm_transform_impl <- function(url) {
50
51 # if this function is being called as part of `install(..., type = "source')`
52 # then we want to transform binary URLs to source URLs here
53 if (identical(the$install_pkg_type, "source"))
54 return(renv_ppm_normalize(url))
55
56 # repository URL transformation is only necessary on Linux
57 os <- renv_ppm_os()
58 if (!identical(os, "__linux__"))
59 return(url)
60
61 # check for a known platform
62 platform <- renv_ppm_platform()
63 if (is.null(platform))
64 return(url)
65
66 # don't transform non-https URLs
67 if (!grepl("^https?://", url))
68 return(url)
69
70 # manylinux URLs are already in the desired format
71 if (renv_ppm_is_manylinux(url))
72 return(url)
73
74 # if this already appears to be a binary URL, then avoid
75 # transforming it
76 if (grepl("/__[^_]+__/", url))
77 return(url)
78
79 # try to parse the repository URL
80 parts <- catch(renv_url_parse(url))
81 if (inherits(parts, "error"))
82 return(url)
83
84 # only attempt to transform URLs that are formatted like PPM urls:
85 #
86 # https://ppm.company.org/cran/checkpoint/id
87 #
88 # in particular, there should be at least two trailing
89 # alphanumeric path components
90 pattern <- "/[^/]+/[^/]+"
91 if (!grepl(pattern, parts$path))
92 return(url)
93
94 # check if this is an 'ignored' URL; that is, a repository which we
95 # know is not a PPM URL
96 mirrors <- renv_cran_mirrors()
97 ignored <- c(
98 getOption("renv.ppm.ignoredUrls", default = character()),
99 settings$ppm.ignored.urls(),
100 mirrors
101 )
102
103 if (sub("/+$", "", url) %in% sub("/+$", "", ignored))
104 return(url)
105
106 # if this is a 'known' PPM instance, then skip the query step
107 known <- c(
108 dirname(dirname(config$ppm.url())),
109 getOption("renv.ppm.repos", default = NULL)
110 )
111
112 if (any(startsWith(url, known))) {
113 parts <- c(dirname(url), "__linux__", platform, basename(url))
114 binurl <- paste(parts, collapse = "/")
115 return(binurl)
116 }
117
118 # try to query the status endpoint -- if this fails,
119 # assume the repository is not a ppm URL
120 base <- dirname(dirname(url))
121 status <- catch(renv_ppm_status(base))
122 if (inherits(status, "error"))
123 return(url)
124
125 # iterate through distros and check for a match
126 for (distro in status$distros) {
127
128 ok <-
129 identical(distro$binaryURL, platform) &&
130 identical(distro$binaries, TRUE)
131
132 if (ok) {
133 parts <- c(dirname(url), "__linux__", platform, basename(url))
134 binurl <- paste(parts, collapse = "/")
135 return(binurl)
136 }
137
138 }
139
140 # no match; return url as-is
141 url
142
143 }
144
145 renv_ppm_status <- function(base) {
146 memoize(
147 key = base,
148 value = catch(renv_ppm_status_impl(base))
149 )
150 }
151
152 renv_ppm_status_impl <- function(base) {
153
154 # use a shorter delay to avoid hanging a session
155 renv_scope_options(
156 renv.config.connect.timeout = 10L,
157 renv.config.connect.retry = 1L
158 )
159
160 # attempt the download
161 endpoint <- file.path(base, "__api__/status")
162 destfile <- renv_scope_tempfile("renv-ppm-status-", fileext = ".json")
163 quietly(download(endpoint, destfile))
164
165 # read the downloaded JSON
166 renv_json_read(destfile)
167
168 }
169
170 renv_ppm_platform <- function(file = "/etc/os-release") {
171
172 platform <- Sys.getenv("RENV_PPM_PLATFORM", unset = NA)
173 if (!is.na(platform))
174 return(platform)
175
176 platform <- Sys.getenv("RENV_RSPM_PLATFORM", unset = NA)
177 if (!is.na(platform))
178 return(platform)
179
180 if (renv_platform_windows())
181 return("windows")
182
183 if (renv_platform_macos())
184 return("macos")
185
186 platform <- renv_ppm_platform_impl(file)
187
188 # https://github.com/rstudio/renv/issues/2227
189 if (startsWith(platform, "opensuse15"))
190 return("opensuse156")
191
192 platform
193
194 }
195
196 renv_ppm_platform_impl <- function(file = "/etc/os-release") {
197
198 if (file.exists(file)) {
199
200 properties <- renv_properties_read(
201 path = file,
202 delimiter = "=",
203 dequote = TRUE
204 )
205
206 id <- properties$ID %||% ""
207
208 # handle distros based on Ubuntu
209 if ("UBUNTU_CODENAME" %in% names(properties)) {
210 id <- "ubuntu"
211 properties$VERSION_CODENAME <- properties$UBUNTU_CODENAME
212 }
213
214 case(
215 identical(id, "ubuntu") ~ renv_ppm_platform_ubuntu(properties),
216 identical(id, "centos") ~ renv_ppm_platform_centos(properties),
217 identical(id, "rhel") ~ renv_ppm_platform_rhel(properties),
218 identical(id, "rocky") ~ renv_ppm_platform_rocky(properties),
219 identical(id, "almalinux") ~ renv_ppm_platform_alma(properties),
220 grepl("suse\\b", id) ~ renv_ppm_platform_suse(properties),
221 identical(id, "sles") ~ renv_ppm_platform_sles(properties),
222 identical(id, "debian") ~ renv_ppm_platform_debian(properties),
223 identical(id, "amzn") ~ renv_ppm_platform_amzn(properties)
224 )
225
226 }
227
228 }
229
230 renv_ppm_platform_ubuntu <- function(properties) {
231
232 codename <- properties$VERSION_CODENAME
233 if (is.null(codename))
234 return(NULL)
235
236 codename
237
238 }
239
240 renv_ppm_platform_centos <- function(properties) {
241
242 id <- properties$VERSION_ID
243 if (is.null(id))
244 return(NULL)
245
246 paste0("centos", substring(id, 1L, 1L))
247
248 }
249
250 renv_ppm_platform_rhel <- function(properties) {
251
252 id <- properties$VERSION_ID
253 if (is.null(id))
254 return(NULL)
255
256 name <- ifelse(numeric_version(id) < "9", "centos", "rhel")
257 version <- strsplit(id, ".", fixed = TRUE)[[1L]][[1L]]
258 paste0(name, version)
259
260 }
261
262 renv_ppm_platform_rocky <- function(properties) {
263
264 id <- properties$VERSION_ID
265 if (is.null(id))
266 return(NULL)
267
268 version <- ifelse(numeric_version(id) < "9", "centos", "rhel")
269 paste0(version, substring(id, 1L, 1L))
270
271 }
272
273 renv_ppm_platform_alma <- function(properties) {
274
275 id <- properties$VERSION_ID
276 if (is.null(id))
277 return(NULL)
278
279 version <- ifelse(numeric_version(id) < "9", "centos", "rhel")
280 paste0(version, substring(id, 1L, 1L))
281
282 }
283
284 renv_ppm_platform_suse <- function(properties) {
285
286 id <- properties$VERSION_ID
287 if (is.null(id))
288 return(NULL)
289
290 parts <- strsplit(id, ".", fixed = TRUE)[[1L]]
291 paste0("opensuse", parts[[1L]], parts[[2L]])
292
293 }
294
295 renv_ppm_platform_sles <- function(properties) {
296
297 id <- properties$VERSION_ID
298 if (is.null(id))
299 return(NULL)
300
301 parts <- strsplit(id, ".", fixed = TRUE)[[1L]]
302 paste0("opensuse", parts[[1L]], parts[[2L]])
303
304 }
305
306 renv_ppm_platform_debian <- function(properties) {
307
308 codename <- properties$VERSION_CODENAME
309 if (is.null(codename))
310 return(NULL)
311
312 codename
313
314 }
315
316 renv_ppm_platform_amzn <- function(properties) {
317
318 id <- properties$VERSION_ID
319 if (is.null(id))
320 return(NULL)
321
322 if (numeric_version(id) == "2")
323 return("centos7")
324
325 return(NULL)
326
327 }
328
329 renv_ppm_os <- function() {
330
331 os <- Sys.getenv("RENV_PPM_OS", unset = NA)
332 if (!is.na(os))
333 return(os)
334
335 os <- Sys.getenv("RENV_RSPM_OS", unset = NA)
336 if (!is.na(os))
337 return(os)
338
339 if (renv_platform_windows())
340 "__windows__"
341 else if (renv_platform_macos())
342 "__macos__"
343 else if (renv_platform_linux())
344 "__linux__"
345
346 }
347
348
349 renv_ppm_enabled <- function() {
350
351 # allow environment variable override
352 enabled <- Sys.getenv("RENV_PPM_ENABLED", unset = NA)
353 if (!is.na(enabled))
354 return(truthy(enabled, default = TRUE))
355
356 # support older options as well
357 enabled <- Sys.getenv("RENV_RSPM_ENABLED", unset = NA)
358 if (!is.na(enabled))
359 return(truthy(enabled, default = TRUE))
360
361 # check for project setting
362 enabled <- settings$ppm.enabled()
363 if (!is.null(enabled))
364 return(enabled)
365
366 # otherwise, use configuration option
367 config$ppm.enabled()
368
369 }