Debian Codegraph @

R/scope.R

raw
1
2 renv_scope_tempdir <- function(pattern = "renv-tempdir-",
3 tmpdir = tempdir(),
4 umask = NULL,
5 scope = parent.frame())
6 {
7 dir <- renv_scope_tempfile(pattern = pattern, tmpdir = tmpdir, scope = scope)
8 ensure_directory(dir, umask = umask)
9
10 renv_scope_wd(dir, scope = scope)
11 dir
12 }
13
14 renv_scope_auth <- function(record, scope = parent.frame()) {
15
16 package <- if (is.list(record)) record$Package else record
17 auth <- renv_options_override("renv.auth", package, extra = record)
18
19 if (empty(auth))
20 return(FALSE)
21
22 envvars <- catch({
23 if (is.function(auth))
24 auth(record)
25 else
26 auth
27 })
28
29 # warn user if auth appears invalid
30 if (inherits(envvars, "error")) {
31 warning(envvars)
32 return(FALSE)
33 }
34
35 if (empty(envvars))
36 return(FALSE)
37
38 renv_scope_envvars(list = as.list(envvars), scope = scope)
39 return(TRUE)
40
41 }
42
43 renv_scope_libpaths <- function(new = .libPaths(), scope = parent.frame()) {
44 old <- renv_libpaths_set(new)
45 defer(renv_libpaths_set(old), scope = scope)
46 }
47
48 renv_scope_options <- function(..., scope = parent.frame()) {
49 new <- list(...)
50 old <- options(new)
51 defer(options(old), scope = scope)
52 }
53
54 renv_scope_locale <- function(category = "LC_ALL", locale = "", scope = parent.frame()) {
55 saved <- Sys.getlocale(category)
56 Sys.setlocale(category, locale)
57 defer(Sys.setlocale(category, saved), scope = scope)
58 }
59
60 renv_scope_envvars <- function(..., list = NULL, scope = parent.frame()) {
61
62 dots <- list %||% list(...)
63 old <- as.list(Sys.getenv(names(dots), unset = NA))
64 names(old) <- names(dots)
65
66 unset <- map_lgl(dots, is.null)
67 Sys.unsetenv(names(dots[unset]))
68 if (length(dots[!unset]))
69 do.call(Sys.setenv, dots[!unset])
70
71 defer({
72 na <- is.na(old)
73 Sys.unsetenv(names(old[na]))
74 if (length(old[!na]))
75 do.call(Sys.setenv, old[!na])
76 }, scope = scope)
77
78 }
79
80 renv_scope_error_handler <- function(scope = parent.frame()) {
81
82 error <- getOption("error")
83 if (!is.null(error))
84 return(FALSE)
85
86 call <- renv_error_handler_call()
87 options(error = call)
88
89 defer({
90 if (identical(getOption("error"), call))
91 options(error = error)
92 }, scope = scope)
93
94 TRUE
95
96 }
97
98 # used to enforce usage of curl 7.64.1 within the
99 # renv_paths_extsoft folder when available on Windows
100
101 # nocov start
102 renv_scope_downloader <- function(scope = parent.frame()) {
103
104 if (!renv_platform_windows())
105 return(FALSE)
106
107 if (nzchar(Sys.which("curl")))
108 return(FALSE)
109
110 curlroot <- sprintf("curl-%s-win32-mingw", renv_extsoft_curl_version())
111 curl <- renv_paths_extsoft(curlroot, "bin/curl.exe")
112 if (!file.exists(curl))
113 return(FALSE)
114
115 old <- Sys.getenv("PATH", unset = NA)
116 if (is.na(old))
117 return(FALSE)
118
119 new <- paste(renv_path_normalize(dirname(curl)), old, sep = .Platform$path.sep)
120
121 renv_scope_envvars(PATH = new, scope = scope)
122
123 }
124 # nocov end
125
126 # nocov start
127 renv_scope_rtools <- function(scope = parent.frame()) {
128
129 if (!renv_platform_windows())
130 return(FALSE)
131
132 # check for Rtools
133 root <- renv_paths_rtools()
134 if (!file.exists(root))
135 return(FALSE)
136
137 # get environment variables appropriate for version of Rtools
138 vars <- renv_rtools_envvars(root)
139
140 # scope envvars in parent
141 renv_scope_envvars(list = vars, scope = scope)
142
143 }
144 # nocov end
145
146 # nocov start
147 renv_scope_install <- function(scope = parent.frame()) {
148
149 if (renv_platform_macos())
150 renv_scope_install_macos(scope)
151
152 if (renv_platform_wsl())
153 renv_scope_install_wsl(scope)
154
155 }
156
157 renv_scope_install_macos <- function(scope = parent.frame()) {
158
159 # check that we have command line tools available before invoking
160 # R CMD config, as this might fail otherwise
161 if (once()) {
162 if (!renv_xcode_available()) {
163 message("")
164 message("- macOS is reporting that command line tools (CLT) are not installed.")
165 message("- Run 'xcode-select --install' to install command line tools.")
166 message("- Without CLT, attempts to install packages from sources may fail.")
167 message("")
168 }
169 }
170
171 # get the current compiler
172 args <- c("CMD", "config", "CC")
173 cc <- renv_system_exec(command = R(), args = args, action = "executing R CMD config CC")
174
175 # just in case
176 cc <- tail(cc, n = 1L)
177 if (is.null(cc) || !nzchar(cc))
178 cc <- "clang"
179
180 # check to see if we're using the system toolchain
181 # (need to be careful since users might put e.g. ccache or other flags
182 # into the CC variable)
183
184 # helper for creating regex matching compiler bits
185 matches <- function(pattern) {
186 regex <- paste("(?:[[:space:]]|^)", pattern, "(?:[[:space:]]|$)", sep = "")
187 grepl(regex, cc)
188 }
189
190 sysclang <- case(
191 matches("/usr/bin/clang") ~ TRUE,
192 matches("clang") ~ Sys.which("clang") == "/usr/bin/clang",
193 FALSE
194 )
195
196 # check for an appropriate LLVM toolchain -- if it exists, use it
197 spec <- renv_equip_macos_spec()
198 if (sysclang && !is.null(spec) && file.exists(spec$dst)) {
199 path <- paste(file.path(spec$dst, "bin"), Sys.getenv("PATH"), sep = ":")
200 renv_scope_envvars(PATH = path, scope = scope)
201 }
202
203 # generate a custom makevars that should better handle compilation
204 # with the system toolchain (or other toolchains)
205 makevars <- stack()
206
207 # if we don't have an LLVM toolchain available, then try to generate
208 # a Makeconf that shields compilation from usages of '-fopenmp'
209 if (sysclang) {
210
211 makeconf <- readLines(file.path(R.home("etc"), "Makeconf"), warn = FALSE)
212 mplines <- grep(" -fopenmp", makeconf, fixed = TRUE, value = TRUE)
213
214 # read a user makevars (if any)
215 contents <- character()
216 mvsite <- Sys.getenv(
217 "R_MAKEVARS_SITE",
218 unset = file.path(R.home("etc"), "Makevars.site")
219 )
220
221 if (file.exists(mvsite))
222 contents <- readLines(mvsite, warn = FALSE)
223
224 # override usages of '-fopenmp'
225 replaced <- gsub(" -fopenmp", "", mplines, fixed = TRUE)
226 amended <- unique(c(contents, replaced))
227 makevars$push(amended)
228
229 }
230
231 # write makevars to file
232 path <- tempfile("Makevars-")
233 contents <- unlist(makevars$data(), recursive = TRUE, use.names = FALSE)
234 if (length(contents)) {
235 writeLines(contents, con = path)
236 renv_scope_envvars(R_MAKEVARS_SITE = path, scope = scope)
237 }
238
239 TRUE
240
241 }
242
243 renv_scope_install_wsl <- function(scope = parent.frame()) {
244 renv_scope_envvars(R_INSTALL_STAGED = "FALSE", scope = scope)
245 }
246 # nocov end
247
248 renv_scope_restore <- function(..., scope = parent.frame()) {
249 state <- renv_restore_begin(...)
250 defer(renv_restore_end(state), scope = scope)
251 }
252
253 renv_scope_git_auth <- function(scope = parent.frame()) {
254
255 # try and tell git to be non-interactive by default
256 if (renv_platform_windows()) {
257 renv_scope_envvars(
258 GIT_TERMINAL_PROMPT = "0",
259 scope = scope
260 )
261 } else {
262 renv_scope_envvars(
263 GIT_TERMINAL_PROMPT = "0",
264 GIT_ASKPASS = "/bin/echo",
265 scope = scope
266 )
267 }
268
269 # use GIT_PAT when provided
270 pat <- Sys.getenv("GIT_PAT", unset = NA)
271 if (!is.na(pat)) {
272 renv_scope_envvars(
273 GIT_USERNAME = pat,
274 GIT_PASSWORD = "x-oauth-basic",
275 scope = scope
276 )
277 }
278
279 # only set askpass when GIT_USERNAME + GIT_PASSWORD are set
280 user <-
281 Sys.getenv("GIT_USERNAME", unset = NA) %NA%
282 Sys.getenv("GIT_USER", unset = NA)
283
284 pass <-
285 Sys.getenv("GIT_PASSWORD", unset = NA) %NA%
286 Sys.getenv("GIT_PASS", unset = NA)
287
288 if (is.na(user) || is.na(pass))
289 return(FALSE)
290
291 askpass <- if (renv_platform_windows())
292 system.file("resources/scripts-git-askpass.cmd", package = "renv")
293 else
294 system.file("resources/scripts-git-askpass.sh", package = "renv")
295
296 renv_scope_envvars(GIT_ASKPASS = askpass, scope = scope)
297 return(TRUE)
298
299 }
300
301 renv_scope_bioconductor <- function(project = NULL,
302 version = NULL,
303 scope = parent.frame())
304 {
305 # get current repository
306 repos <- getOption("repos")
307
308 # remove old / stale bioc repositories
309 stale <- grepl("Bioc", names(repos))
310 repos <- repos[!stale]
311
312 # retrieve bioconductor repositories appropriate for this project
313 biocrepos <- renv_bioconductor_repos(project = project, version = version)
314
315 # put it all together
316 allrepos <- c(repos, biocrepos)
317
318 # activate repositories in this context
319 renv_scope_options(repos = renv_vector_unique(allrepos), scope = scope)
320 }
321
322 renv_scope_lock <- function(path = NULL, scope = parent.frame()) {
323 renv_lock_acquire(path)
324 defer(renv_lock_release(path), scope = scope)
325 }
326
327 renv_scope_trace <- function(what, tracer, scope = parent.frame()) {
328
329 call <- sys.call()
330 call[[1L]] <- base::trace
331 call[["print"]] <- FALSE
332 defer(suppressMessages(untrace(substitute(what))), scope = scope)
333
334 suppressMessages(eval(call, envir = parent.frame()))
335
336 }
337
338
339 renv_scope_binding <- function(envir, symbol, replacement, scope = parent.frame()) {
340 if (exists(symbol, envir, inherits = FALSE)) {
341 old <- renv_binding_replace(envir, symbol, replacement)
342 defer(renv_binding_replace(envir, symbol, old), scope = scope)
343 } else {
344 assign(symbol, replacement, envir)
345 defer(rm(list = symbol, envir = envir, inherits = FALSE), scope = scope)
346 }
347 }
348
349 renv_scope_tempfile <- function(pattern = "renv-tempfile-",
350 tmpdir = tempdir(),
351 fileext = "",
352 scope = parent.frame())
353 {
354 tmpdir <- normalizePath(tmpdir, winslash = "/", mustWork = TRUE)
355 path <- renv_path_normalize(tempfile(pattern, tmpdir, fileext))
356 defer(unlink(path, recursive = TRUE, force = TRUE), scope = scope)
357 invisible(path)
358 }
359
360 renv_scope_umask <- function(umask, scope = parent.frame()) {
361 oldmask <- Sys.umask(umask)
362 defer(Sys.umask(oldmask), scope = scope)
363 invisible(oldmask)
364 }
365
366 renv_scope_wd <- function(dir = getwd(), scope = parent.frame()) {
367 owd <- setwd(dir)
368 defer(setwd(owd), scope = scope)
369 invisible(owd)
370 }
371
372 renv_scope_sandbox <- function(scope = parent.frame()) {
373 sandbox <- renv_sandbox_activate()
374 defer(renv_sandbox_deactivate(), scope = scope)
375 invisible(sandbox)
376 }
377
378 renv_scope_biocmanager <- function(scope = parent.frame()) {
379
380 # silence BiocManager messages when setting repositories
381 renv_scope_options(BiocManager.check_repositories = FALSE, scope = scope)
382
383 # R-devel (4.4.0) warns when BiocManager calls .make_numeric_version() without
384 # a character argument, so just suppress those warnings in this scope
385 #
386 # https://github.com/wch/r-source/commit/1338a95618ddcc8a0af77dc06e4018625de06ec3
387 renv_scope_options(warn = -1L, scope = scope)
388
389 # return reference to BiocManager namespace
390 renv_namespace_load("BiocManager")
391
392 }
393
394 renv_scope_caution <- function(value) {
395 renv_scope_options(
396 renv.caution.verbose = value,
397 scope = parent.frame()
398 )
399 }
400
401 renv_scope_verbose_if <- function(value, scope = parent.frame()) {
402 if (value) {
403 renv_scope_options(
404 renv.verbose = TRUE,
405 scope = scope
406 )
407 }
408 }