-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathmktmpenv
executable file
·45 lines (36 loc) · 1.03 KB
/
mktmpenv
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
#!/bin/sh
set -e
# set -x
# Make sure pyenv is setup.
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
venv_options=
while [ "${1#-}" != "$1" ]; do
venv_options="$venv_options $1"
shift
done
if [ -n "$1" ]; then
pyenv_version="$1"
else
pyenv_version=$(pyenv version-name | cut -f1 -d:)
fi
if [ -n "$2" ]; then
venv_name="$2"
else
# Use -u (short for --dry-run) for compatibility (MacOS).
venv_name=$(mktemp -u -d "tmp-${pyenv_version}-${PWD##*/}-XXXXXX")
fi
# shellcheck disable=SC2086
pyenv virtualenv $venv_options "${pyenv_version}" "${venv_name}"
echo
echo "Entering a new shell session with virtualenv '${venv_name}' (based on ${pyenv_version})."
echo "It will be removed when exiting (ctrl+d or 'exit')."
echo "NOTE: to keep it just rename/move it while still in there."
pyenv shell "$venv_name"
# NOTE: the shell will return the exit code from the last command, which might
# have failed.
ret=0
$SHELL || ret=$?
echo "Destroying temporary virtualenv '${venv_name}'."
pyenv uninstall -f "${venv_name}"
exit "$ret"