Debian Codegraph @

R/patch.R

raw
1
2 renv_patch_init <- function() {
3 renv_patch_rprofile()
4 renv_patch_tar()
5 renv_patch_repos()
6 renv_patch_golem()
7 renv_patch_methods_table()
8 }
9
10 renv_patch_rprofile <- function() {
11
12 # resolve path to user profile
13 path <- Sys.getenv("R_PROFILE_USER", unset = "~/.Rprofile")
14 info <- renv_file_info(path)
15 if (!identical(info$isdir, FALSE))
16 return(FALSE)
17
18 # if the .Rprofile is empty, do nothing
19 if (info$size == 0)
20 return(TRUE)
21
22 # check for trailing newline
23 data <- readBin(path, raw(), n = info$size)
24 if (empty(data))
25 return(TRUE)
26
27 last <- data[length(data)]
28 endings <- as.raw(c(0x0a, 0x0d))
29 if (last %in% endings)
30 return(TRUE)
31
32 # if it's missing, inform the user
33 warningf("%s is missing a trailing newline", renv_path_pretty(path))
34 FALSE
35
36 }
37
38 renv_patch_tar <- function() {
39
40 # read value of TAR
41 tar <- Sys.getenv("TAR", unset = "")
42
43 # on Windows, if TAR is unset, then force the usage
44 # of R's internal tar implementation. this is done to
45 # avoid issues where e.g. versions of tar which do not
46 # understand Windows paths are on the PATH
47 #
48 # https://github.com/rstudio/renv/issues/521
49 if (renv_platform_windows() && !nzchar(tar)) {
50 Sys.setenv(TAR = "internal")
51 return(TRUE)
52 }
53
54 # otherwise, allow empty / internal tars
55 if (tar %in% c("", "internal"))
56 return(TRUE)
57
58 # the user (or R itself) has set the TAR environment variable
59 # validate that it exists (resolve from PATH)
60 #
61 # note that the user can set TAR to be a full command; e.g.
62 #
63 # TAR = /path/to/tar --force-local
64 #
65 # so we need to handle that case appropriately
66 whitespace <- gregexpr("(?:\\s+|$)", tar, perl = TRUE)[[1L]]
67 for (index in whitespace) {
68 candidate <- substring(tar, 1L, index - 1L)
69 resolved <- Sys.which(candidate)
70 if (nzchar(resolved))
71 return(TRUE)
72 }
73
74 # TAR appears to be set but invalid; override it
75 # and warn the user
76 newtar <- Sys.which("tar")
77 if (!nzchar(newtar))
78 newtar <- "internal"
79
80 Sys.setenv(TAR = newtar)
81
82 # report to the user
83 fmt <- "requested TAR '%s' does not exist; using '%s' instead"
84 warningf(fmt, tar, newtar)
85
86 }
87
88 renv_patch_golem <- function() {
89 renv_package_hook("golem", renv_patch_golem_impl)
90 }
91
92 renv_patch_golem_impl <- function(...) {
93
94 if (packageVersion("golem") != "0.2.1")
95 return()
96
97 golem <- getNamespace("golem")
98
99 replacement <- function(file, pattern, replace) {
100
101 # skip .rds files
102 if (grepl("[.]rds$", file))
103 return()
104
105 # skip files containing nul bytes
106 info <- renv_file_info(file)
107 bytes <- readBin(file, "raw", info$size)
108 if (any(bytes == 0L))
109 return()
110
111 # otherwise, attempt replacement
112 old <- readLines(file)
113 new <- gsub(pattern, replace, old)
114 writeLines(new, con = file)
115
116 }
117
118 environment(replacement) <- golem
119
120 if ("compiler" %in% loadedNamespaces())
121 replacement <- compiler::cmpfun(replacement)
122
123 renv_binding_replace(golem, "replace_word", replacement)
124
125 }
126
127 renv_patch_methods_table <- function() {
128 catchall(renv_patch_methods_table_impl())
129 }
130
131 renv_patch_methods_table_impl <- function() {
132
133 # ensure promises in S3 methods table are forced
134 # https://bugs.r-project.org/bugzilla/show_bug.cgi?id=16644
135 for (envir in list(.BaseNamespaceEnv, renv_namespace_load("utils"))) {
136
137 # unlock binding if it's locked
138 binding <- ".__S3MethodsTable__."
139 base <- baseenv()
140 if (base$bindingIsLocked(binding, env = envir)) {
141 base$unlockBinding(binding, env = envir)
142 defer(base$lockBinding(binding, envir))
143 }
144
145 # force everything defined in the environment
146 table <- envir[[binding]]
147 for (key in ls(envir = table, all.names = TRUE))
148 table[[key]] <- force(table[[key]])
149
150 }
151
152 }
153
154 # puts the current version of renv into an on-disk package repository,
155 # so that packages using renv can find this version of renv in tests
156 # this helps renv survive CRAN revdep checks (e.g. jetpack)
157 renv_patch_repos <- function() {
158
159 # nothing to do in embedded mode
160 if (renv_metadata_embedded())
161 return()
162
163 # nothing to do if we're not running tests
164 checking <- checking()
165 if (!checking)
166 return()
167
168 # nothing to do if we're running our own tests
169 name <- Sys.getenv("_R_CHECK_PACKAGE_NAME_", unset = NA)
170 if (identical(name, "renv"))
171 return()
172
173 # presumably this will never happen when the dev version of renv is
174 # installed, so we skip to avoid parsing a sha as version
175 sha <- attr(the$metadata$version, "sha")
176 if (!is.null(sha))
177 return()
178
179 # nothing to do if this version of 'renv' is already available
180 version <- renv_metadata_version()
181 entry <- catch(renv_available_packages_entry("renv", filter = version, quiet = TRUE))
182 if (!inherits(entry, "error"))
183 return()
184
185 # check if we've already set repos
186 if ("RENV" %in% names(getOption("repos")))
187 return()
188
189 # use package-local repository path
190 repopath <- system.file("repos", package = "renv", mustWork = FALSE)
191 if (!file.exists(repopath))
192 return()
193
194 # update our repos option
195 fmt <- if (renv_platform_windows()) "file:///%s" else "file://%s"
196 repourl <- sprintf(fmt, repopath)
197
198 # renv needs to be first so the right version is found?
199 repos <- c(RENV = repourl, getOption("repos"))
200 names(repos) <- make.names(names(repos))
201 options(repos = repos)
202
203 # make sure these repositories are used in restore too
204 options(renv.config.repos.override = repos)
205
206 }