-
Notifications
You must be signed in to change notification settings - Fork 326
/
pyenv.R
307 lines (235 loc) · 8.52 KB
/
pyenv.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
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
pyenv_root <- function() {
root <- user_data_dir("r-reticulate")
dir.create(root, showWarnings = FALSE, recursive = TRUE)
norm <- normalizePath(root, winslash = "/", mustWork = TRUE)
file.path(norm, "pyenv")
}
pyenv_resolve_latest_patch <- function(version, installed = TRUE, pyenv = pyenv_find()) {
# if version = "3.8:latest", resolve latest installed
# recent versions of pyenv on Mac/Linux can accept a ":latest" suffix on install,
# but then we can't easily resolve the latest from what's locally installed.
# windows pyenv can't handle :latest at all.
# So we do it all in R.
stopifnot(endsWith(version, ":latest"))
version <- substr(version, 1L, nchar(version) - nchar(":latest"))
available <- pyenv_list(pyenv, installed)
available <- available[startsWith(available, version)]
out <- as.character(max(numeric_version(available, strict = FALSE),
na.rm = TRUE))
if (!length(out))
stop(
sprintf("Python release version '%s' not found. ", version),
"Run `install_python(list = TRUE)` to see available versions."
)
out
}
pyenv_python <- function(version) {
if (is.null(version))
return(NULL)
if(grepl("^3\\.[0-9]+$", version))
version <- paste0(version, ":latest")
if (endsWith(version, ":latest"))
version <- pyenv_resolve_latest_patch(version, installed = TRUE)
# on Windows, Python will be installed as part of the pyenv installation
prefix <- if (is_windows()) {
pyenv <- pyenv_find()
if (endsWith(pyenv, ".cmd")) {
# check if it's a scoop shim, resolve the actual installation if so
if (file.exists(actual_pyenv <-
str_drop_prefix(readLines(pyenv, 1), "@rem ")))
pyenv <- actual_pyenv
}
file.path(pyenv, "../../versions", version)
} else {
root <- Sys.getenv("PYENV_ROOT", unset = "~/.pyenv")
file.path(root, "versions", version)
}
if (!file.exists(prefix)) {
fmt <- paste(
"Python %s does not appear to be installed.",
"Try installing it with install_python(version = %s).",
sep = "\n"
)
msg <- sprintf(fmt, version, shQuote(version))
stop(msg)
}
stem <- if (is_windows()) "python.exe" else "bin/python"
normalizePath(
file.path(prefix, stem),
winslash = "/",
mustWork = TRUE
)
}
pyenv_list <- function(pyenv = NULL, installed = FALSE) {
# resolve pyenv
pyenv <- normalizePath(
pyenv %||% pyenv_find(),
winslash = "/",
mustWork = TRUE
)
if (installed)
return(system2(pyenv, c("versions", "--bare"), stdout = TRUE))
# request list of Python packages
output <- system2(pyenv, c("install", "--list"), stdout = TRUE, stderr = TRUE)
# clean up output
# on some platforms, warnings from cmd.exe appear in the output
# also, there is a header like ":: [Info] :: Mirror: https://www.python.org/ftp/python"
# https://github.com/rstudio/reticulate/issues/1390
header_end <- max(1L, grep("^:: \\[Info\\] :: .+$", output))
versions <- tail(output, n = -header_end)
cleaned <- gsub("^\\s*", "", versions)
# only include CPython interpreters for now
grep("^[[:digit:]]", cleaned, value = TRUE)
}
pyenv_find <- function(install = TRUE) {
pyenv <- pyenv_find_impl(install = install)
if (isFALSE(install) && is.null(pyenv))
return(NULL)
canonical_path(pyenv)
}
pyenv_find_impl <- function(install = TRUE) {
# check for pyenv binary specified via option
pyenv <- getOption("reticulate.pyenv", default = NULL)
if (!is.null(pyenv) && file.exists(pyenv))
return(pyenv)
# check for pyenv executable on the PATH
pyenv <- Sys.which("pyenv")
if (nzchar(pyenv))
return(pyenv)
# form stem path to pyenv binary (it differs between pyenv and pyenv-win)
stem <- if (is_windows()) "pyenv-win/bin/pyenv" else "bin/pyenv"
# check for a binary in the PYENV_ROOT folder
root <- Sys.getenv("PYENV_ROOT", unset = "~/.pyenv")
pyenv <- file.path(root, stem)
if (file.exists(pyenv))
return(pyenv)
# check for reticulate's own pyenv
root <- pyenv_root()
pyenv <- file.path(root, stem)
if (file.exists(pyenv))
return(pyenv)
# all else fails, try to manually install pyenv
if(install)
pyenv_bootstrap()
else
NULL
}
pyenv_install <- function(version, force, pyenv = NULL) {
pyenv <- canonical_path(pyenv %||% pyenv_find())
stopifnot(file.exists(pyenv))
# set options
withr::local_envvar(PYTHON_CONFIGURE_OPTS = "--enable-shared")
if(is_macos() &&
Sys.which("brew") == "" &&
file.exists("/opt/homebrew/bin/brew"))
withr::local_path("/opt/homebrew/bin")
# build install arguments
force <- if (force)
"--force"
else if (!is_windows())
"--skip-existing"
args <- c("install", force, version)
system2t(pyenv, args)
}
pyenv_bootstrap <- function() {
if (is_windows())
pyenv_bootstrap_windows()
else
pyenv_bootstrap_unix()
}
pyenv_bootstrap_windows <- function() {
# get path to pyenv
root <- normalizePath(
pyenv_root(),
winslash = "/",
mustWork = FALSE
)
if (Sys.which("git") == "")
stop("Please install git and ensure it is on your PATH")
# clone if necessary
if (!file.exists(root)) {
url <- "https://github.com/pyenv-win/pyenv-win"
system2("git", c("clone", shQuote(url), shQuote(root)))
}
# ensure up-to-date
owd <- setwd(root)
on.exit(setwd(owd), add = TRUE)
system("git pull")
# path to pyenv binary
pyenv <- file.path(root, "pyenv-win/bin/pyenv")
# running 'update' after install on windows is basically required
# https://github.com/pyenv-win/pyenv-win/issues/280#issuecomment-1045027625
system2(pyenv, "update")
# return path to pyenv binary
pyenv
}
pyenv_bootstrap_unix <- function() {
if (!nzchar(Sys.which("git")))
stop("bootstrapping of pyenv requires git to be installed")
# move to tempdir
owd <- setwd(tempdir())
on.exit(setwd(owd), add = TRUE)
# pyenv python builds are substantially faster on macOS if we pre-install
# some dependencies (especially openssl) as pre-built but "untapped kegs"
# (i.e., unlinked to somewhere on the PATH but tucked away under $BREW_ROOT/Cellar).
if (is_macos()) {
brew <- Sys.which("brew")
if(brew == "" && file.exists(brew <- "/opt/homebrew/bin/brew"))
withr::local_path("/opt/homebrew/bin")
if(file.exists(brew)) {
system2t(brew, c("install -q openssl readline sqlite3 xz zlib tcl-tk"))
system2t(brew, c("install --only-dependencies pyenv python"))
}
}
# download the installer
url <- "https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer"
name <- basename(url)
download.file(url, destfile = name, mode = "wb")
# ensure it's runnable
Sys.chmod(name, mode = "0755")
# set root directory
root <- pyenv_root()
withr::local_envvar(PYENV_ROOT = root)
# run the script -- for some reason, the pyenv installer will return
# a non-zero status code even on success?
writeLines("Installing pyenv ...")
suppressWarnings(system("./pyenv-installer", intern = TRUE))
path <- file.path(root, "bin/pyenv")
if (!file.exists(path))
stop("installation of pyenv failed")
writeLines(sprintf("Done! pyenv has been installed to '%s'.", path))
path
}
pyenv_update <- function(pyenv = pyenv_find()) {
if (startsWith(pyenv, root <- pyenv_root())) {
# this pyenv installation is fully managed by reticulate
# root == where .../bin/pyenv lives
withr::with_dir(root, system2("git", "pull", stdout = FALSE, stderr = FALSE))
}
if (is_windows()) {
# `pyenv update` is very slow on windows, we need to throttle it.
# only update it once every 30 days
cache_file <- file.path(dirname(dirname(pyenv)), ".versions_cache.xml")
if (!file.exists(cache_file) ||
(Sys.Date() - as.Date(file.mtime(cache_file)) > 30))
system2t(pyenv, "update")
return()
}
# $ git clone https://github.com/pyenv/pyenv-update.git $(pyenv root)/plugins/pyenv-update
# root == ~/.pyenv == where installed pythons live
root <- system2(pyenv, "root", stdout = TRUE)
if (!dir.exists(file.path(root, "plugins/pyenv-update")))
system2("git", c("clone", "https://github.com/pyenv/pyenv-update.git",
file.path(root, "plugins/pyenv-update")))
result <- system2t(pyenv, "update", stdout = FALSE, stderr = FALSE)
if (!identical(result, 0L)) {
fmt <- "Error updating pyenv [exit code %i]"
warningf(fmt, result)
}
}
#export PATH="$HOME/.local/share/r-reticulate/pyenv/bin/:$PATH"
#eval "$(pyenv init --path)"
#eval "$(pyenv virtualenv-init -)"
#export PATH="$HOME/.pyenv/bin:$PATH"
#eval "$(pyenv init --path)"
#eval "$(pyenv virtualenv-init -)"