2019-04-23 10:23:33 -04:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
2013-01-18 03:41:41 -05:00
|
|
|
# Summary: Set or show the shell-specific Python version
|
|
|
|
#
|
2017-06-05 09:02:24 -04:00
|
|
|
# Usage: pyenv shell <version>...
|
|
|
|
# pyenv shell -
|
2013-01-18 03:41:41 -05:00
|
|
|
# pyenv shell --unset
|
|
|
|
#
|
|
|
|
# Sets a shell-specific Python version by setting the `PYENV_VERSION'
|
|
|
|
# environment variable in your shell. This version overrides local
|
|
|
|
# application-specific versions and the global version.
|
|
|
|
#
|
|
|
|
# <version> should be a string matching a Python version known to pyenv.
|
|
|
|
# The special version string `system' will use your default system Python.
|
|
|
|
# Run `pyenv versions' for a list of available Python versions.
|
2017-06-05 09:02:24 -04:00
|
|
|
#
|
|
|
|
# When `-` is passed instead of the version string, the previously set
|
|
|
|
# version will be restored. With `--unset`, the `PYENV_VERSION`
|
|
|
|
# environment variable gets unset, restoring the environment to the
|
|
|
|
# state before the first `pyenv shell` call.
|
2013-01-18 03:41:41 -05:00
|
|
|
|
2012-08-31 02:23:41 -04:00
|
|
|
set -e
|
|
|
|
[ -n "$PYENV_DEBUG" ] && set -x
|
|
|
|
|
|
|
|
# Provide pyenv completions
|
|
|
|
if [ "$1" = "--complete" ]; then
|
|
|
|
echo --unset
|
|
|
|
echo system
|
|
|
|
exec pyenv-versions --bare
|
|
|
|
fi
|
|
|
|
|
2012-09-07 06:16:42 -04:00
|
|
|
versions=("$@")
|
2013-12-25 23:48:43 -05:00
|
|
|
shell="$(basename "${PYENV_SHELL:-$SHELL}")"
|
2012-08-31 02:23:41 -04:00
|
|
|
|
2012-09-07 06:16:42 -04:00
|
|
|
if [ -z "$versions" ]; then
|
2012-08-31 06:39:29 -04:00
|
|
|
if [ -z "$PYENV_VERSION" ]; then
|
2012-08-31 02:23:41 -04:00
|
|
|
echo "pyenv: no shell-specific version configured" >&2
|
|
|
|
exit 1
|
|
|
|
else
|
2017-06-05 09:02:24 -04:00
|
|
|
echo 'echo "$PYENV_VERSION"'
|
2012-08-31 02:23:41 -04:00
|
|
|
exit
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2012-09-07 06:16:42 -04:00
|
|
|
if [ "$versions" = "--unset" ]; then
|
2013-09-30 05:02:12 -04:00
|
|
|
case "$shell" in
|
|
|
|
fish )
|
2017-06-05 09:02:24 -04:00
|
|
|
echo 'set -gu PYENV_VERSION_OLD "$PYENV_VERSION"'
|
2013-09-30 05:02:12 -04:00
|
|
|
echo "set -e PYENV_VERSION"
|
|
|
|
;;
|
|
|
|
* )
|
2017-06-05 09:02:24 -04:00
|
|
|
echo 'PYENV_VERSION_OLD="$PYENV_VERSION"'
|
2013-09-30 05:02:12 -04:00
|
|
|
echo "unset PYENV_VERSION"
|
|
|
|
;;
|
|
|
|
esac
|
2013-01-18 04:57:08 -05:00
|
|
|
exit
|
2012-08-31 02:23:41 -04:00
|
|
|
fi
|
|
|
|
|
2017-06-05 09:02:24 -04:00
|
|
|
if [ "$versions" = "-" ]; then
|
2013-09-30 05:02:12 -04:00
|
|
|
case "$shell" in
|
|
|
|
fish )
|
2017-06-05 09:02:24 -04:00
|
|
|
cat <<EOS
|
|
|
|
if set -q PYENV_VERSION_OLD
|
|
|
|
if [ -n "\$PYENV_VERSION_OLD" ]
|
|
|
|
set PYENV_VERSION_OLD_ "\$PYENV_VERSION"
|
|
|
|
set -gx PYENV_VERSION "\$PYENV_VERSION_OLD"
|
|
|
|
set -gu PYENV_VERSION_OLD "\$PYENV_VERSION_OLD_"
|
|
|
|
set -e PYENV_VERSION_OLD_
|
|
|
|
else
|
|
|
|
set -gu PYENV_VERSION_OLD "\$PYENV_VERSION"
|
|
|
|
set -e PYENV_VERSION
|
|
|
|
end
|
|
|
|
else
|
|
|
|
echo "pyenv: PYENV_VERSION_OLD is not set" >&2
|
|
|
|
false
|
|
|
|
end
|
|
|
|
EOS
|
2013-09-30 05:02:12 -04:00
|
|
|
;;
|
|
|
|
* )
|
2017-06-05 09:02:24 -04:00
|
|
|
cat <<EOS
|
|
|
|
if [ -n "\${PYENV_VERSION_OLD+x}" ]; then
|
|
|
|
if [ -n "\$PYENV_VERSION_OLD" ]; then
|
|
|
|
PYENV_VERSION_OLD_="\$PYENV_VERSION"
|
|
|
|
export PYENV_VERSION="\$PYENV_VERSION_OLD"
|
|
|
|
PYENV_VERSION_OLD="\$PYENV_VERSION_OLD_"
|
|
|
|
unset PYENV_VERSION_OLD_
|
|
|
|
else
|
|
|
|
PYENV_VERSION_OLD="\$PYENV_VERSION"
|
|
|
|
unset PYENV_VERSION
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "pyenv: PYENV_VERSION_OLD is not set" >&2
|
|
|
|
false
|
|
|
|
fi
|
|
|
|
EOS
|
2013-09-30 05:02:12 -04:00
|
|
|
;;
|
|
|
|
esac
|
2017-06-05 09:02:24 -04:00
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Make sure the specified version is installed.
|
|
|
|
if pyenv-prefix "${versions[@]}" >/dev/null; then
|
|
|
|
OLDIFS="$IFS"
|
|
|
|
IFS=: version="${versions[*]}"
|
|
|
|
IFS="$OLDIFS"
|
|
|
|
if [ "$version" != "$PYENV_VERSION" ]; then
|
|
|
|
case "$shell" in
|
|
|
|
fish )
|
|
|
|
echo 'set -gu PYENV_VERSION_OLD "$PYENV_VERSION"'
|
|
|
|
echo "set -gx PYENV_VERSION \"$version\""
|
|
|
|
;;
|
|
|
|
* )
|
|
|
|
echo 'PYENV_VERSION_OLD="$PYENV_VERSION"'
|
|
|
|
echo "export PYENV_VERSION=\"${version}\""
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
2013-01-18 04:57:08 -05:00
|
|
|
else
|
2013-09-30 05:02:12 -04:00
|
|
|
echo "false"
|
2013-01-18 04:57:08 -05:00
|
|
|
exit 1
|
|
|
|
fi
|