-
-
Notifications
You must be signed in to change notification settings - Fork 419
/
Copy pathpyenv-sh-activate
executable file
·265 lines (241 loc) · 5.96 KB
/
pyenv-sh-activate
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
#!/usr/bin/env bash
#
# Summary: Activate virtual environment
#
# Usage: pyenv activate <virtualenv>
# pyenv activate --unset
#
# Activate a Python virtualenv environment in current shell.
# This acts almost as same as `pyenv shell`, but this invokes the `activate`
# script in your shell.
#
# <virtualenv> should be a string matching a Python version known to pyenv.
set -e
[ -n "$PYENV_DEBUG" ] && set -x
if [ -z "${PYENV_ROOT}" ]; then
PYENV_ROOT="$(pyenv-root)"
fi
resolve_link() {
$(type -p greadlink readlink | head -1) "$1"
}
unset FORCE
unset QUIET
while [ $# -gt 0 ]; do
case "$1" in
"--complete" )
# Provide pyenv completions
echo --unset
exec pyenv-virtualenvs --bare
;;
"-f" | "--force" )
FORCE=1
;;
"-q" | "--quiet" )
QUIET=1
;;
"--unset" )
exec pyenv-sh-deactivate
;;
"-v" | "--verbose" )
unset QUIET
PYENV_VIRTUALENV_VERBOSE_ACTIVATE=1
;;
* )
break
;;
esac
shift 1
done
get_current_versions() {
local IFS=:
current_versions=($(pyenv-version-name 2>/dev/null))
}
no_shell=
versions=("$@")
current_versions=()
if [ -z "${versions}" ]; then
no_shell=1
get_current_versions
versions=("${current_versions[@]}")
fi
if [ -z "${PYENV_VIRTUALENV_INIT}" ]; then
# Backward compatibility issue
# https://github.com/yyuu/pyenv-virtualenv/issues/26
no_shell=
fi
venv="${versions}"
if [ -n "${VIRTUAL_ENV}" ]; then
# exit as success if some virtualenv is already activated outside from pyenv-virtualenv
if [ -z "${PYENV_VIRTUAL_ENV}" ]; then
if [ -z "${FORCE}" ]; then
if [ -z "${QUIET}" ]; then
echo "pyenv-virtualenv: virtualenv \`${VIRTUAL_ENV}' is already activated" 1>&2
fi
echo "true"
exit 0
fi
fi
fi
if ! pyenv-virtualenv-prefix "${venv}" 1>/dev/null 2>&1; then
# fallback to virtualenv of current version
[ -n "${current_versions}" ] || get_current_versions
new_venv="${current_versions%/envs/*}/envs/${venv}"
if pyenv-virtualenv-prefix "${new_venv}" 1>/dev/null 2>&1; then
venv="${new_venv}"
versions[0]="${new_venv}"
else
if [ -z "${QUIET}" ]; then
echo "pyenv-virtualenv: version \`${venv}' is not a virtualenv" 1>&2
fi
echo "false"
exit 1
fi
fi
# exit as error if there are multiple virtualenvs
# https://github.com/yyuu/pyenv-virtualenv/issues/105
for version in "${versions[@]}"; do
if [[ "${version}" != "${venv}" ]]; then
if pyenv-virtualenv-prefix "${version}" 1>/dev/null 2>&1; then
if [ -z "${QUIET}" ]; then
echo "pyenv-virtualenv: cannot activate multiple versions at once: ${versions[@]}" 1>&2
fi
echo "false"
exit 1
fi
fi
done
shell="${PYENV_SHELL:-${SHELL##*/}}"
prefix="$(pyenv-prefix "${venv}")"
if [ -L "${prefix}" ]; then
prefix="$(resolve_link "${prefix}" 2>/dev/null)"
fi
# exit as success if the virtualenv is already activated
if [[ "${VIRTUAL_ENV}" == "${prefix}" ]]; then
if [ -z "${FORCE}" ]; then
if [ -z "${QUIET}" ]; then
echo "pyenv-virtualenv: version \`${venv}' is already activated" 1>&2
fi
echo "true"
exit 0
fi
fi
pyenv-sh-deactivate --force --quiet || true
if [ -n "$PYENV_VIRTUALENV_VERBOSE_ACTIVATE" ]; then
echo "pyenv-virtualenv: activate ${venv}" 1>&2
fi
if [ -z "$no_shell" ]; then
# shell version set in pyenv-sh-activate should be unset
# https://github.com/yyuu/pyenv-virtualenv/issues/61
OLDIFS="$IFS"
IFS=:
case "$shell" in
fish )
cat <<EOS
set -gx PYENV_VERSION "${versions[*]}";
set -gx PYENV_ACTIVATE_SHELL 1;
EOS
;;
* )
cat <<EOS
export PYENV_VERSION="${versions[*]}";
export PYENV_ACTIVATE_SHELL=1;
EOS
;;
esac
IFS="$OLDIFS"
fi
# virtualenv/venv
case "${shell}" in
fish )
cat <<EOS
set -gx PYENV_VIRTUAL_ENV "${prefix}";
set -gx VIRTUAL_ENV "${prefix}";
EOS
;;
* )
cat <<EOS
export PYENV_VIRTUAL_ENV="${prefix}";
export VIRTUAL_ENV="${prefix}";
EOS
;;
esac
# anaconda/miniconda
if [ -d "${prefix}/conda-meta" ] ||
[ -x "${prefix}/bin/conda" ]; then
if [[ "${prefix}" != "${prefix%/envs/*}" ]]; then
CONDA_DEFAULT_ENV="${venv##*/envs/}"
else
CONDA_DEFAULT_ENV="root"
fi
case "${shell}" in
fish )
echo "set -gx CONDA_DEFAULT_ENV \"${CONDA_DEFAULT_ENV}\";"
echo "set -gx CONDA_PREFIX \"${prefix}\";"
;;
* )
echo "export CONDA_DEFAULT_ENV=\"${CONDA_DEFAULT_ENV}\";"
echo "export CONDA_PREFIX=\"${prefix}\";"
;;
esac
fi
if [ -n "${PYTHONHOME}" ]; then
case "${shell}" in
fish )
cat <<EOS
set -gx _OLD_VIRTUAL_PYTHONHOME "${PYTHONHOME}";
set -e PYTHONHOME;
EOS
;;
* )
cat <<EOS
export _OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME}";
unset PYTHONHOME;
EOS
;;
esac
fi
PYENV_VIRTUALENV_DISABLE_PROMPT="${PYENV_VIRTUALENV_DISABLE_PROMPT:-${PYENV_VIRTUAL_ENV_DISABLE_PROMPT}}"
PYENV_VIRTUALENV_DISABLE_PROMPT="${PYENV_VIRTUALENV_DISABLE_PROMPT:-${VIRTUAL_ENV_DISABLE_PROMPT}}"
if [ -z "${PYENV_VIRTUALENV_DISABLE_PROMPT}" ]; then
case "${shell}" in
fish )
if [ -z "${QUIET}" ]; then
echo "pyenv-virtualenv: prompt changing not working for fish." 1>&2
fi
;;
* )
if [ -z "${QUIET}" ]; then
echo "pyenv-virtualenv: prompt changing will be removed from future release. configure \`export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior." 1>&2
fi
cat <<EOS
export _OLD_VIRTUAL_PS1="\${PS1:-}";
export PS1="(${venv}) \${PS1:-}";
EOS
;;
esac
fi
# conda package anaconda/miniconda scripts (#173)
if [ -d "${prefix}/conda-meta" ] ||
[ -x "${prefix}/bin/conda" ]; then
shopt -s nullglob
case "${shell}" in
fish )
# conda 4.4 and above
for script in "${prefix}/etc/fish/conf.d"/*.fish; do
echo "source \"${script}\";"
done
;;
* )
CONDA_PREFIX="$prefix"
echo "export CONDA_PREFIX=\"${CONDA_PREFIX}\";"
for script in "${prefix}/etc/conda/activate.d"/*.sh; do
echo ". \"${script}\";"
done
# conda 4.4 and above
for script in "${prefix}/etc/profile.d"/*.sh; do
echo ". \"${script}\";"
done
;;
esac
shopt -u nullglob
fi