/
consent.R
99 lines (84 loc) · 2.76 KB
/
consent.R
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
#' Consent to usage of renv
#'
#' Provide consent to renv, allowing it to write and update certain files
#' on your filesystem.
#'
#' As part of its normal operation, renv will write and update some files
#' in your project directory, as well as an application-specific cache
#' directory. These paths are documented within [paths].
#'
#' In accordance with the
#' [CRAN Repository Policy](https://cran.r-project.org/web/packages/policies.html),
#' renv must first obtain consent from you, the user, before these actions
#' can be taken. Please call `renv::consent()` first to provide this consent.
#'
#' You can also set the \R option:
#'
#' ```
#' options(renv.consent = TRUE)
#' ```
#'
#' to implicitly provide consent for e.g. non-interactive \R sessions.
#'
#' @param provided The default provided response. If you need to provide
#' consent from a non-interactive \R session, you can invoke
#' `renv::consent(provided = TRUE)` explicitly.
#'
#' @return `TRUE` if consent is provided, or an \R error otherwise.
#'
#' @export
consent <- function(provided = FALSE) {
# assume consent if embedded
if (renv_metadata_embedded())
return(TRUE)
# compute path to root directory
root <- renv_paths_root()
if (renv_file_type(root) == "directory") {
writef("- Consent to use renv has already been provided -- nothing to do.")
return(invisible(TRUE))
}
# write welcome message
template <- system.file("resources/WELCOME", package = "renv")
contents <- readLines(template)
replacements <- list(RENV_PATHS_ROOT = renv_path_pretty(root))
welcome <- renv_template_replace(contents, replacements)
writef(welcome)
# ask user if they want to proceed
response <- catchall(proceed(default = provided))
if (!identical(response, TRUE)) {
msg <- "consent was not provided; operation aborted"
stop(msg, call. = FALSE)
}
# cache the user response
options(renv.consent = TRUE)
ensure_directory(root)
renv_cache_init()
writef("- %s has been created.", renv_path_pretty(root))
invisible(TRUE)
}
renv_consent_check <- function() {
# check for explicit consent
consent <- getOption("renv.consent")
if (identical(consent, TRUE))
return(TRUE)
else if (identical(consent, FALSE))
stopf("consent has been explicitly withdrawn")
# check for existence of root
root <- renv_paths_root()
if (renv_file_type(root) == "directory")
return(TRUE)
# check for implicit consent
consented <-
!interactive() ||
renv_envvar_exists("CI") ||
renv_envvar_exists("GITHUB_ACTION") ||
renv_envvar_exists("RENV_PATHS_ROOT") ||
file.exists("/.singularity.d") ||
renv_virtualization_type() != "native"
if (consented) {
ensure_directory(root)
return(TRUE)
}
# looks like the user's first interactive use of renv
consent()
}