Debian Codegraph @

R/python-virtualenv.R

raw
1
2 renv_python_virtualenv_home <- function() {
3 Sys.getenv("WORKON_HOME", unset = "~/.virtualenvs")
4 }
5
6 renv_python_virtualenv_path <- function(name) {
7
8 # if the name contains a slash, use it as-is
9 if (grepl("/", name, fixed = TRUE))
10 return(renv_path_canonicalize(name))
11
12 # treat names starting with '.' specially
13 if (substring(name, 1L, 1L) == ".")
14 return(renv_path_canonicalize(name))
15
16 # otherwise, resolve relative to virtualenv home
17 home <- renv_python_virtualenv_home()
18 file.path(home, name)
19
20 }
21
22 renv_python_virtualenv_validate <- function(path, version) {
23
24 # get path to python executable
25 python <- renv_python_exe(path)
26
27 # compare requested + actual versions
28 if (!is.null(version)) {
29 request <- renv_version_maj_min(version)
30 current <- renv_version_maj_min(renv_python_version(python))
31 if (request != current) {
32 fmt <- "Project requested Python version '%s' but '%s' is currently being used"
33 warningf(fmt, request, current)
34 }
35 }
36
37 python
38
39 }
40
41 renv_python_virtualenv_create <- function(python, path) {
42
43 ensure_parent_directory(path)
44
45 python <- renv_path_canonicalize(python)
46 version <- renv_python_version(python)
47 module <- if (numeric_version(version) > "3.2") "venv" else "virtualenv"
48 args <- c("-m", module, renv_shell_path(path))
49 renv_system_exec(python, args, "creating virtual environment")
50
51 info <- renv_python_info(path)
52 info$python
53
54 }
55
56 renv_python_virtualenv_update <- function(python) {
57
58 # resolve python executable path
59 python <- renv_python_exe(python)
60 python <- renv_path_canonicalize(python)
61
62 # resolve packages
63 packages <- c("pip", "setuptools", "wheel")
64
65 # don't upgrade these packages for older versions of python, as we may
66 # end up installing versions of packages that aren't actually compatible
67 # with the version of python we're running
68 version <- renv_python_version(python)
69 if (renv_version_lt(version, "3.6"))
70 return(TRUE)
71
72 # perform the install
73 # make errors non-fatal as the environment will still be functional even
74 # if we're not able to install or update these packages
75 status <- catch(pip_install(packages, python = python))
76 if (inherits(status, "error"))
77 warnify(status)
78
79 TRUE
80
81 }
82
83 renv_python_virtualenv_snapshot <- function(project, prompt, python) {
84
85 renv_scope_wd(project)
86
87 path <- file.path(project, "requirements.txt")
88 before <- character()
89 if (file.exists(path))
90 before <- readLines(path, warn = FALSE)
91
92 after <- pip_freeze(python = python)
93 if (setequal(before, after)) {
94 writef("- Python requirements are already up to date.")
95 return(FALSE)
96 }
97
98 bulletin("The following will be written to requirements.txt:", after)
99
100 cancel_if(prompt && !proceed())
101
102 writeLines(after, con = path)
103
104 fmt <- "- Wrote Python packages to %s."
105 writef(fmt, renv_path_pretty(path))
106 return(TRUE)
107
108 }
109
110 renv_python_virtualenv_restore <- function(project, prompt, python) {
111
112 renv_scope_wd(project)
113
114 path <- file.path(project, "requirements.txt")
115 if (!file.exists(path))
116 return(FALSE)
117
118 saved <- readLines(path, warn = FALSE)
119 current <- pip_freeze(python = python)
120 diff <- renv_vector_diff(saved, current)
121 if (empty(diff)) {
122 writef("- The Python library is already up to date.")
123 return(FALSE)
124 }
125
126 bulletin("The following Python packages will be restored:", diff)
127 cancel_if(prompt && !proceed())
128
129 pip_install_requirements(saved, python = python, stream = TRUE)
130 TRUE
131
132 }