-
-
Notifications
You must be signed in to change notification settings - Fork 420
/
Copy pathpyenv-virtualenv
executable file
·667 lines (595 loc) · 18.9 KB
/
pyenv-virtualenv
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
#!/usr/bin/env bash
#
# Summary: Create a Python virtualenv using the pyenv-virtualenv plugin
#
# Usage: pyenv virtualenv [-f|--force] [VIRTUALENV_OPTIONS] [version] <virtualenv-name>
# pyenv virtualenv --version
# pyenv virtualenv --help
#
# -f/--force Install even if the version appears to be installed already. Skip
# prompting for confirmation
#
# Notable VIRTUALENV_OPTIONS passed to venv-creating executable, if applicable:
# -u/--upgrade Imply --force
#
PYENV_VIRTUALENV_VERSION="1.2.4"
set -e
[ -n "$PYENV_DEBUG" ] && set -x
if [ -z "${PYENV_ROOT}" ]; then
PYENV_ROOT="$(pyenv-root)"
fi
# Provide pyenv completions
if [ "$1" = "--complete" ]; then
exec pyenv-versions --bare --skip-envs --skip-aliases
fi
unset PIP_REQUIRE_VENV
unset PIP_REQUIRE_VIRTUALENV
# Define library functions
parse_options() {
OPTIONS=()
ARGUMENTS=()
local arg option index
for arg in "$@"; do
if [ "${arg:0:1}" = "-" ]; then
if [ "${arg:1:1}" = "-" ]; then
OPTIONS[${#OPTIONS[*]}]="${arg:2}"
else
index=1
while option="${arg:$index:1}"; do
[ -n "$option" ] || break
OPTIONS[${#OPTIONS[*]}]="$option"
index=$(($index+1))
done
fi
else
ARGUMENTS[${#ARGUMENTS[*]}]="$arg"
fi
done
}
colorize() {
if [ -t 1 ]; then printf "\e[%sm%s\e[m" "$1" "$2"
else echo -n "$2"
fi
}
resolve_link() {
$(type -p greadlink readlink | head -1) "$1"
}
abs_dirname() {
local cwd="$(pwd)"
local path="$1"
while [ -n "$path" ]; do
cd "${path%/*}"
local name="${path##*/}"
path="$(resolve_link "$name" || true)"
done
pwd
cd "$cwd"
}
http() {
local method="$1"
local url="$2"
local file="$3"
[ -n "$url" ] || return 1
if type curl &>/dev/null; then
"http_${method}_curl" "$url" "$file"
elif type wget &>/dev/null; then
"http_${method}_wget" "$url" "$file"
else
echo "error: please install \`curl\` or \`wget\` and try again" >&2
exit 1
fi
}
http_head_curl() {
curl -qsILf "$1" >&4 2>&1
}
http_get_curl() {
curl -C - -o "${2:--}" -qsSLf "$1"
}
http_head_wget() {
wget -q --spider "$1" >&4 2>&1
}
http_get_wget() {
wget -nv -c -O "${2:--}" "$1"
}
version() {
if [[ -z "${PYENV_VERSION:-}" ]]; then
# `PYENV_VERSION` might not be declared if this was invoked via `--version`
export PYENV_VERSION="$(pyenv-version-name)"
fi
detect_venv
local version
if [ -n "${USE_CONDA}" ]; then
version="$(pyenv-exec conda --version 2>/dev/null || true)"
echo "pyenv-virtualenv ${PYENV_VIRTUALENV_VERSION} (conda ${version:-unknown})"
else
if [ -n "$USE_M_VENV" ]; then
echo "pyenv-virtualenv ${PYENV_VIRTUALENV_VERSION} (${M_VENV_PYTHON_BIN:-python} -m venv)"
else
version="$(pyenv-exec virtualenv --version 2>/dev/null || true)"
echo "pyenv-virtualenv ${PYENV_VIRTUALENV_VERSION} (virtualenv ${version:-unknown})"
fi
fi
}
usage() {
# We can remove the sed fallback once pyenv 0.2.0 is widely available.
pyenv-help virtualenv 2>/dev/null || sed -ne '/^#/!q;s/.//;s/.//;1,4d;p' < "$0"
if [ -n "${USE_CONDA}" ]; then
pyenv-exec conda create --help 2>/dev/null || true
else
if [ -n "${USE_M_VENV}" ]; then
pyenv-exec "${M_VENV_PYTHON_BIN:-python}" -m venv --help 2>/dev/null || true
else
pyenv-exec virtualenv --help 2>/dev/null || true
fi
fi
[ -z "$1" ] || exit "$1"
}
detect_venv() {
# Check the existence of executables as a workaround for the issue with pyenv-which-ext
# https://github.com/yyuu/pyenv-virtualenv/issues/26
local prefix="$(pyenv-prefix)"
if [ -x "${prefix}/bin/conda" ]; then
HAS_CONDA=1
else
if [ -x "${prefix}/bin/virtualenv" ]; then
HAS_VIRTUALENV=1
fi
local python
local -a pythons
if [[ $PYENV_VERSION == "system" ]]; then
# Prefer `python3.x` executable if available (#206, #282)
pythons=("python3" "python" "python2")
else
# as per PEP 394, custom activated Python environments should provide the "python" command
# this includes Pyenv-provided installations
pythons=("python")
fi
for python in "${pythons[@]}"; do
if pyenv-exec "${python}" -m venv --help 1>/dev/null 2>&1; then
HAS_M_VENV=1
M_VENV_PYTHON_BIN="${python}"
break
fi
done
fi
# Use `python -m venv` only if there is venv available, virtualenv is not installed, and `-p` not given
if [ -n "${HAS_CONDA}" ]; then
USE_CONDA=1
else
if [ -n "${HAS_M_VENV}" ] && [ -z "${HAS_VIRTUALENV}" ] && [ -z "${VIRTUALENV_PYTHON}" ]; then
USE_M_VENV=1
fi
fi
}
build_package_ez_setup() {
local ez_setup="${PYENV_VIRTUALENV_CACHE_PATH}/ez_setup.py"
rm -f "${ez_setup}"
{ if [ "${EZ_SETUP+defined}" ] && [ -f "${EZ_SETUP}" ]; then
echo "Installing setuptools from ${EZ_SETUP}..." 1>&2
cat "${EZ_SETUP}"
else
[ -n "${EZ_SETUP_URL}" ]
echo "Installing setuptools from ${EZ_SETUP_URL}..." 1>&2
http get "${EZ_SETUP_URL}"
fi
} 1> "${ez_setup}"
pyenv-exec python -s "${ez_setup}" ${EZ_SETUP_OPTS} 1>&2 || {
echo "error: failed to install setuptools via ez_setup.py" >&2
return 1
}
}
build_package_get_pip() {
local get_pip="${PYENV_VIRTUALENV_CACHE_PATH}/get-pip.py"
rm -f "${get_pip}"
{ if [ "${GET_PIP+defined}" ] && [ -f "${GET_PIP}" ]; then
echo "Installing pip from ${GET_PIP}..." 1>&2
cat "${GET_PIP}"
else
[ -n "${GET_PIP_URL}" ]
echo "Installing pip from ${GET_PIP_URL}..." 1>&2
http get "${GET_PIP_URL}"
fi
} 1> "${get_pip}"
pyenv-exec python -s "${get_pip}" ${GET_PIP_OPTS} 1>&2 || {
echo "error: failed to install pip via get-pip.py" >&2
return 1
}
}
build_package_ensurepip() {
pyenv-exec python -s -m ensurepip 2>/dev/null || build_package_get_pip "$@" || return 1
}
prepare_requirements() {
pyenv-exec pip freeze > "${REQUIREMENTS}"
mv -f "${VIRTUALENV_PATH}" "${VIRTUALENV_ORIG}"
}
install_requirements() {
if [ -f "${REQUIREMENTS}" ]; then
## Migrate previously installed packages from requirements.txt
pyenv-exec pip install $QUIET $VERBOSE --requirement "${REQUIREMENTS}" || {
echo
echo "PIP INSTALL FAILED"
echo
echo "Inspect or clean up the original tree at ${VIRTUALENV_ORIG}"
echo
echo "Package list:"
cat "${REQUIREMENTS}" | sed 's/^/ * /'
return 1
} 1>&2
rm -f "${REQUIREMENTS}"
rm -fr "${VIRTUALENV_ORIG}"
fi
}
PYENV_VIRTUALENV_ROOT="$(abs_dirname "$0")/.."
if [ -z "${PYENV_VIRTUALENV_CACHE_PATH}" ]; then
PYENV_VIRTUALENV_CACHE_PATH="${PYTHON_BUILD_CACHE_PATH:-${PYENV_ROOT}/cache}"
fi
VIRTUALENV_OPTIONS=()
unset FORCE
unset NO_ENSUREPIP
unset QUIET
unset UPGRADE
unset VERBOSE
unset VIRTUALENV_PYTHON
parse_options "$@"
for option in "${OPTIONS[@]}"; do
case "$option" in
"f" | "force" )
FORCE=true
;;
"h" | "help" )
usage 0
;;
"no-pip" )
NO_ENSUREPIP=1
VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="--$option"
;;
"no-setuptools" )
NO_ENSUREPIP=1
VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="--$option"
;;
"p" | "python" )
VIRTUALENV_PYTHON="${ARGUMENTS[0]}"
ARGUMENTS=("${ARGUMENTS[@]:1}") # shift 1
;;
"q" | "quiet" )
QUIET="--quiet"
;;
"u" | "upgrade" )
UPGRADE=true
;;
"v" | "verbose" )
VERBOSE="--verbose"
;;
"version" )
version
exit 0
;;
"without-pip" )
NO_ENSUREPIP=1
VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="--$option"
;;
* ) # virtualenv long options
if [[ "$option" == "python="* ]]; then
VIRTUALENV_PYTHON="${option#python=}"
else
VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="--$option"
fi
;;
esac
done
if [[ "${#ARGUMENTS[@]}" == 0 ]]; then
echo "pyenv-virtualenv: no virtualenv name given." 1>&2
exit 1
elif [[ "${#ARGUMENTS[@]}" == 1 ]]; then
# If only one argument given, use current version as source version
OLDIFS="${IFS}"
IFS=:
VERSION_NAMES=($(pyenv-version-name))
IFS="${OLDIFS}"
VERSION_NAME="${VERSION_NAMES}"
VIRTUALENV_NAME="${ARGUMENTS[0]}"
else
# Otherwise, use former as source version, and latter as virtualenv version
VERSION_NAME="${ARGUMENTS[0]}"
VIRTUALENV_NAME="${ARGUMENTS[1]}"
fi
if [[ -n "${VERSION_NAME}" ]] && command -v pyenv-latest >/dev/null; then
VERSION_NAME="$(pyenv-latest -f "${VERSION_NAME}")"
fi
if [ -z "${VERSION_NAME}" ] || [ -z "${VIRTUALENV_NAME}" ]; then
usage 1
fi
if [[ "${VIRTUALENV_NAME##*/}" == "system" ]]; then
echo "pyenv-virtualenv: \`system' is not allowed as virtualenv name." 1>&2
exit 1
fi
if [ "$VIRTUALENV_NAME" != "${VIRTUALENV_NAME%[[:space:]]*}" ]; then
echo "pyenv-virtualenv: no whitespace allowed in virtualenv name." 1>&2
exit 1
fi
if [ "${VIRTUALENV_NAME}" != "${VIRTUALENV_NAME%/*}" ] && [[ "${VIRTUALENV_NAME}" != "${VERSION_NAME%%/*}/envs/${VIRTUALENV_NAME##*/}" ]] ; then
echo "pyenv-virtualenv: no slash allowed in virtualenv name." 1>&2
exit 1
fi
# Set VERSION_NAME as default version in this script
export PYENV_VERSION="${VERSION_NAME}"
not_installed_message() {
local is_available=$(python-build --definitions | grep -F -x "$1")
echo "pyenv-virtualenv: \`${1}' is not installed in pyenv." 1>&2
if [[ $is_available ]]; then
echo "Run \`pyenv install ${1}' to install it." 1>&2
else
echo "It does not look like a valid Python version. See \`pyenv install --list' for available versions." 1>&2
fi
}
# Source version must exist before creating virtualenv.
PREFIX="$(pyenv-prefix 2>/dev/null || true)"
if [ ! -d "${PREFIX}" ]; then
not_installed_message "${PYENV_VERSION}"
exit 1
fi
if [ -z "$TMPDIR" ]; then
TMP="/tmp"
else
TMP="${TMPDIR%/}"
fi
# Not create `system/envs` directory even if source version is `system`
if [[ "${VERSION_NAME%/envs/*}" == "system" ]]; then
VIRTUALENV_NAME="${VIRTUALENV_NAME##*/}"
else
VIRTUALENV_PREFIX="$(pyenv-virtualenv-prefix 2>/dev/null || true)"
if [[ "${VIRTUALENV_PREFIX%/*}" == "${PYENV_ROOT}/versions" ]]; then
VIRTUALENV_NAME="${VIRTUALENV_PREFIX#${PYENV_ROOT}/versions/}/envs/${VIRTUALENV_NAME##*/}"
else
VIRTUALENV_NAME="${VERSION_NAME}/envs/${VIRTUALENV_NAME##*/}"
fi
fi
VIRTUALENV_PATH="${PYENV_ROOT}/versions/${VIRTUALENV_NAME}"
if [[ "${VIRTUALENV_PATH/*/envs/*}" != "${PYENV_ROOT}/versions" ]]; then
COMPAT_VIRTUALENV_PATH="${PYENV_ROOT}/versions/${VIRTUALENV_NAME##*/}"
fi
if [ -n "${COMPAT_VIRTUALENV_PATH}" ]; then
if [ -z ${FORCE} ]; then
if [ -e "${COMPAT_VIRTUALENV_PATH}" ] || [ -L "${COMPAT_VIRTUALENV_PATH}" ]; then
echo "pyenv-virtualenv: \`${COMPAT_VIRTUALENV_PATH}' already exists." 1>&2
exit 1
fi
fi
fi
unset HAS_VIRTUALENV
unset HAS_M_VENV
unset USE_CONDA
unset USE_M_VENV
detect_venv
SEED="$(date "+%Y%m%d%H%M%S").$$"
VIRTUALENV_ORIG="${VIRTUALENV_PATH}.${SEED}"
REQUIREMENTS="${TMP}/requirements.${SEED}.txt"
# Upgrade existing virtualenv
if [ -n "$UPGRADE" ]; then
FORCE=1
# `python -m venv` has `--upgrade` by default
if [ -n "${USE_M_VENV}" ]; then
unset UPGRADE
VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="--upgrade"
fi
fi
if [ -z "${VIRTUALENV_VERSION}" ]; then
case "${PYENV_VERSION}" in
"3.0"* )
NO_ENSUREPIP=1
;;
"3.1"* )
NO_ENSUREPIP=1
;;
"3.2"* | "stackless-3.2"* )
# pip 8.x (bundled with virtualenv 14+) doesn't support 3.2 anymore
# https://github.com/yyuu/pyenv/issues/531
VIRTUALENV_VERSION="13.1.2"
NO_ENSUREPIP=1
;;
esac
fi
if [ -n "${USE_CONDA}" ]; then
# e.g. `conda create -n py35 python=3.5 anaconda`
if [ -n "${VIRTUALENV_PYTHON}" ]; then
VIRTUALENV_PYTHON="${VIRTUALENV_PYTHON##*/}"
VIRTUALENV_PYTHON="${VIRTUALENV_PYTHON#python}"
if [ -n "${VIRTUALENV_PYTHON}" ]; then
VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="python=${VIRTUALENV_PYTHON}"
fi
fi
else
if [ -n "${USE_M_VENV}" ]; then
# Unset some arguments not supported by `python -m venv`
unset QUIET
unset VERBOSE
if [ -n "${VIRTUALENV_PYTHON}" ]; then
echo "pyenv-virtualenv: \`--python=${VIRTUALENV_PYTHON}' is not supported by \`python -m venv'." 1>&2
exit 1
fi
else
if [ -n "${VIRTUALENV_PYTHON}" ]; then
if [[ "${VIRTUALENV_PYTHON}" == "${VIRTUALENV_PYTHON##*/}" ]] || [[ "${VIRTUALENV_PYTHON}" == "${PYENV_ROOT}/shims/"* ]]; then
python="$(pyenv-which "${VIRTUALENV_PYTHON##*/}" 2>/dev/null || true)"
if [ -x "${python}" ]; then
VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="--python=${python}"
else
python="$(PYENV_VERSION="$(pyenv-whence "${VIRTUALENV_PYTHON##*/}" 2>/dev/null | tail -n 1 || true)" pyenv-which "${VIRTUALENV_PYTHON##*/}" 2>/dev/null || true)"
if [ -x "${python}" ]; then
VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="--python=${python}"
else
# echo "pyenv-virtualenv: \`${VIRTUALENV_PYTHON##*/}' is not installed in pyenv." 1>&2
not_installed_message "${VIRTUALENV_PYTHON##*/}"
exit 1
fi
fi
else
VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="--python=${VIRTUALENV_PYTHON}"
fi
fi
if [ -z "${HAS_VIRTUALENV}" ]; then
if [ -n "${VIRTUALENV_VERSION}" ]; then
virtualenv_spec="virtualenv==${VIRTUALENV_VERSION}"
else
virtualenv_spec="virtualenv"
fi
pyenv-exec pip install $QUIET $VERBOSE "${virtualenv_spec}"
HAS_VIRTUALENV=1
fi
fi
fi
# Unset environment variables which start with `VIRTUALENV_`.
# These variables are reserved for virtualenv.
unset VIRTUALENV_VERSION
# Download specified version of ez_setup.py/get-pip.py.
if [ -z "${EZ_SETUP_URL}" ]; then
if [ -n "${SETUPTOOLS_VERSION}" ]; then
EZ_SETUP_URL="https://bitbucket.org/pypa/setuptools/raw/${SETUPTOOLS_VERSION}/ez_setup.py"
unset SETUPTOOLS_VERSION
else
EZ_SETUP_URL="https://bootstrap.pypa.io/ez_setup.py"
fi
fi
if [ -z "${GET_PIP_URL}" ]; then
if [ -n "${PIP_VERSION}" ]; then
{ colorize 1 "WARNING"
echo ": Setting PIP_VERSION=${PIP_VERSION} is no longer supported and may cause failures during the install process."
} 1>&2
GET_PIP_URL="https://raw.githubusercontent.com/pypa/pip/${PIP_VERSION}/contrib/get-pip.py"
# Unset `PIP_VERSION` from environment before invoking `get-pip.py` to deal with "ValueError: invalid truth value" (pypa/pip#4528)
unset PIP_VERSION
else
# Use custom get-pip URL based on the target version (#1127)
case "${PYENV_VERSION}" in
2.6 | 2.6.* )
GET_PIP_URL="https://bootstrap.pypa.io/pip/2.6/get-pip.py"
;;
2.7 | 2.7.* )
GET_PIP_URL="https://bootstrap.pypa.io/pip/2.7/get-pip.py"
;;
3.2 | 3.2.* )
GET_PIP_URL="https://bootstrap.pypa.io/pip/3.2/get-pip.py"
;;
3.3 | 3.3.* )
GET_PIP_URL="https://bootstrap.pypa.io/pip/3.3/get-pip.py"
;;
3.4 | 3.4.* )
GET_PIP_URL="https://bootstrap.pypa.io/pip/3.4/get-pip.py"
;;
3.5 | 3.5.* )
GET_PIP_URL="https://bootstrap.pypa.io/pip/3.5/get-pip.py"
;;
3.6 | 3.6.* )
GET_PIP_URL="https://bootstrap.pypa.io/pip/3.6/get-pip.py"
;;
* )
GET_PIP_URL="https://bootstrap.pypa.io/pip/get-pip.py"
;;
esac
fi
fi
# Define `before_virtualenv` and `after_virtualenv` functions that allow
# plugin hooks to register a string of code for execution before or
# after the installation process.
declare -a before_hooks after_hooks
before_virtualenv() {
local hook="$1"
before_hooks["${#before_hooks[@]}"]="$hook"
}
after_virtualenv() {
local hook="$1"
after_hooks["${#after_hooks[@]}"]="$hook"
}
# Load plugin hooks.
OLDIFS="$IFS"
IFS=$'\n' scripts=(`pyenv-hooks virtualenv`)
IFS="$OLDIFS"
for script in "${scripts[@]}"; do source "$script"; done
[ -d "${VIRTUALENV_PATH}" ] && PREFIX_EXISTS=1
# If the virtualenv exists, prompt for confirmation unless
# the --force option was specified.
if [ -d "${VIRTUALENV_PATH}/bin" ]; then
if [ -z "$FORCE" ]; then
echo "pyenv-virtualenv: ${VIRTUALENV_PATH} already exists" 1>&2
read -p "continue with installation? (y/N) "
case "$REPLY" in
y* | Y* ) ;;
* ) exit 1 ;;
esac
fi
if [ -n "$UPGRADE" ]; then
if [ -n "${NO_ENSUREPIP}" ]; then
echo "pyenv-virtualenv: upgrading will not work with --no-setuptools or --no-pip" 1>&2
exit 1
else
PYENV_VERSION="${VIRTUALENV_NAME}" prepare_requirements
fi
fi
fi
# Execute `before_virtualenv` hooks.
for hook in "${before_hooks[@]}"; do eval "$hook"; done
# Plan cleanup on unsuccessful installation.
cleanup() {
[[ -L "${COMPAT_VIRTUALENV_PATH}" ]] && rm "${COMPAT_VIRTUALENV_PATH}"
[ -z "${PREFIX_EXISTS}" ] && rm -rf "$VIRTUALENV_PATH"
}
trap cleanup SIGINT ERR
# Invoke virtualenv and record exit status in $STATUS.
STATUS=0
# virtualenv may download distribute/setuptools into the current directory.
# Change to cache directory to reuse them between invocations.
mkdir -p "${PYENV_VIRTUALENV_CACHE_PATH}"
cd "${PYENV_VIRTUALENV_CACHE_PATH}"
if [ -n "${USE_CONDA}" ]; then
if [ -z "$VIRTUALENV_PYTHON" ]; then
#process substitution doesn't seem to work unless it's directly inserted to the command line
#if we add it to VIRTUALENV_OPTIONS instead, "broken pipe" happens
pyenv-exec conda create $QUIET $VERBOSE --name "${VIRTUALENV_PATH##*/}" --yes "${VIRTUALENV_OPTIONS[@]}" --file <(pyenv-exec conda list python --full-name --export) || STATUS="$?"
else
pyenv-exec conda create $QUIET $VERBOSE --name "${VIRTUALENV_PATH##*/}" --yes "${VIRTUALENV_OPTIONS[@]}" || STATUS="$?"
fi
else
if [ -n "${USE_M_VENV}" ]; then
pyenv-exec "${M_VENV_PYTHON_BIN:-python}" -m venv $QUIET $VERBOSE "${VIRTUALENV_OPTIONS[@]}" "${VIRTUALENV_PATH}" || STATUS="$?"
else
pyenv-exec virtualenv $QUIET $VERBOSE "${VIRTUALENV_OPTIONS[@]}" "${VIRTUALENV_PATH}" || STATUS="$?"
fi
fi
(
shopt -s nullglob
for extra_binary in "$PREFIX"/bin/python*-config; do
extra_binary_linkname="$VIRTUALENV_PATH/bin/$(basename $extra_binary)"
[[ -e "$extra_binary_linkname" ]] || \
ln -s "$extra_binary" "$extra_binary_linkname"
done
)
## Create symlink in the `versions` directory for backward compatibility
if [ -d "${VIRTUALENV_PATH}" ] && [ -n "${COMPAT_VIRTUALENV_PATH}" ]; then
# Overwrite an existing link, can happen if running with -f
ln -fsn "${VIRTUALENV_PATH}" "${COMPAT_VIRTUALENV_PATH}"
fi
if [ ! -e "${VIRTUALENV_PATH}/bin/pydoc" ]; then
mkdir -p "${VIRTUALENV_PATH}/bin"
cat <<EOS > "${VIRTUALENV_PATH}/bin/pydoc"
#!${VIRTUALENV_PATH}/bin/python
import pydoc
if __name__ == '__main__':
pydoc.cli()
EOS
chmod +x "${VIRTUALENV_PATH}/bin/pydoc"
fi
if [ -z "${NO_ENSUREPIP}" ]; then
## Install setuptools and pip.
PYENV_VERSION="${VIRTUALENV_NAME}" build_package_ensurepip
## Migrate previously installed packages from requirements.txt.
PYENV_VERSION="${VIRTUALENV_NAME}" install_requirements || true
fi
# Execute `after_virtualenv` hooks.
for hook in "${after_hooks[@]}"; do eval "$hook"; done
# Run `pyenv-rehash` after a successful installation.
if [ "$STATUS" == "0" ]; then
pyenv-rehash
else
cleanup
fi
exit "$STATUS"