2012-08-31 06:32:23 +00:00
|
|
|
#!/usr/bin/env bash
|
2013-01-31 04:20:59 +00:00
|
|
|
#
|
2013-12-27 09:44:54 +00:00
|
|
|
# Summary: Install a Python version using python-build
|
2013-01-31 04:20:59 +00:00
|
|
|
#
|
2013-12-27 09:44:54 +00:00
|
|
|
# Usage: pyenv install [-f] [-kvp] <version>
|
|
|
|
# pyenv install [-f] [-kvp] <definition-file>
|
2013-01-31 04:20:59 +00:00
|
|
|
# pyenv install -l|--list
|
|
|
|
#
|
|
|
|
# -l/--list List all available versions
|
|
|
|
# -f/--force Install even if the version appears to be installed already
|
2013-12-27 09:44:54 +00:00
|
|
|
#
|
|
|
|
# python-build options:
|
|
|
|
#
|
2013-01-31 04:20:59 +00:00
|
|
|
# -k/--keep Keep source tree in $PYENV_BUILD_ROOT after installation
|
|
|
|
# (defaults to $PYENV_ROOT/sources)
|
|
|
|
# -v/--verbose Verbose mode: print compilation status to stdout
|
2013-12-27 09:44:54 +00:00
|
|
|
# -p/--patch Apply a patch from stdin before building
|
|
|
|
# -g/--debug Build a debug version
|
2013-01-31 04:20:59 +00:00
|
|
|
#
|
|
|
|
# For detailed information on installing Python versions with
|
|
|
|
# python-build, including a list of environment variables for adjusting
|
|
|
|
# compilation, see: https://github.com/yyuu/pyenv#readme
|
|
|
|
#
|
2012-08-31 06:32:23 +00:00
|
|
|
set -e
|
|
|
|
[ -n "$PYENV_DEBUG" ] && set -x
|
|
|
|
|
|
|
|
# Provide pyenv completions
|
|
|
|
if [ "$1" = "--complete" ]; then
|
|
|
|
exec python-build --definitions
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$PYENV_ROOT" ]; then
|
|
|
|
PYENV_ROOT="${HOME}/.pyenv"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Load shared library functions
|
|
|
|
eval "$(python-build --lib)"
|
|
|
|
|
|
|
|
usage() {
|
2013-01-31 04:20:59 +00:00
|
|
|
# We can remove the sed fallback once pyenv 0.4.0 is widely available.
|
|
|
|
pyenv-help install 2>/dev/null || sed -ne '/^#/!q;s/.//;s/.//;1,4d;p' < "$0"
|
2012-08-31 06:32:23 +00:00
|
|
|
[ -z "$1" ] || exit "$1"
|
|
|
|
}
|
|
|
|
|
2013-04-19 10:26:11 +00:00
|
|
|
definitions() {
|
|
|
|
local query="$1"
|
|
|
|
python-build --definitions | grep -F "$query" || true
|
|
|
|
}
|
|
|
|
|
|
|
|
indent() {
|
|
|
|
sed 's/^/ /'
|
|
|
|
}
|
|
|
|
|
2013-01-31 04:20:59 +00:00
|
|
|
unset FORCE
|
2012-08-31 06:32:23 +00:00
|
|
|
unset KEEP
|
|
|
|
unset VERBOSE
|
2013-12-27 09:44:54 +00:00
|
|
|
unset HAS_PATCH
|
2013-03-28 03:04:58 +00:00
|
|
|
unset DEBUG
|
2012-08-31 06:32:23 +00:00
|
|
|
|
|
|
|
parse_options "$@"
|
|
|
|
for option in "${OPTIONS[@]}"; do
|
|
|
|
case "$option" in
|
|
|
|
"h" | "help" )
|
|
|
|
usage 0
|
|
|
|
;;
|
|
|
|
"l" | "list" )
|
|
|
|
echo "Available versions:"
|
2013-04-19 10:26:11 +00:00
|
|
|
definitions | indent
|
2012-08-31 06:32:23 +00:00
|
|
|
exit
|
|
|
|
;;
|
2013-01-31 04:20:59 +00:00
|
|
|
"f" | "force" )
|
|
|
|
FORCE=true
|
|
|
|
;;
|
2012-08-31 06:32:23 +00:00
|
|
|
"k" | "keep" )
|
|
|
|
[ -n "${PYENV_BUILD_ROOT}" ] || PYENV_BUILD_ROOT="${PYENV_ROOT}/sources"
|
|
|
|
;;
|
|
|
|
"v" | "verbose" )
|
|
|
|
VERBOSE="-v"
|
|
|
|
;;
|
2013-12-27 09:44:54 +00:00
|
|
|
"p" | "patch" )
|
|
|
|
HAS_PATCH="-p"
|
|
|
|
;;
|
2013-03-28 03:04:58 +00:00
|
|
|
"g" | "debug" )
|
|
|
|
DEBUG="-g"
|
|
|
|
;;
|
2012-08-31 06:32:23 +00:00
|
|
|
"version" )
|
|
|
|
exec python-build --version
|
|
|
|
;;
|
|
|
|
* )
|
|
|
|
usage 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2013-01-31 04:20:59 +00:00
|
|
|
unset VERSION_NAME
|
|
|
|
|
|
|
|
# The first argument contains the definition to install. If the
|
|
|
|
# argument is missing, try to install whatever local app-specific
|
|
|
|
# version is specified by pyenv. Show usage instructions if a local
|
|
|
|
# version is not specified.
|
2012-08-31 06:32:23 +00:00
|
|
|
DEFINITION="${ARGUMENTS[0]}"
|
2014-01-10 11:05:01 +00:00
|
|
|
[ -n "$DEFINITION" ] || DEFINITION="$(pyenv-local 2>/dev/null || true)"
|
2012-08-31 06:32:23 +00:00
|
|
|
[ -n "$DEFINITION" ] || usage 1
|
|
|
|
|
2013-01-31 04:20:59 +00:00
|
|
|
|
|
|
|
# Define `before_install` and `after_install` 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_install() {
|
|
|
|
local hook="$1"
|
|
|
|
before_hooks["${#before_hooks[@]}"]="$hook"
|
|
|
|
}
|
|
|
|
|
|
|
|
after_install() {
|
|
|
|
local hook="$1"
|
|
|
|
after_hooks["${#after_hooks[@]}"]="$hook"
|
|
|
|
}
|
|
|
|
|
2014-01-10 11:05:01 +00:00
|
|
|
OLDIFS="$IFS"
|
|
|
|
IFS=$'\n' scripts=(`pyenv-hooks install`)
|
|
|
|
IFS="$OLDIFS"
|
|
|
|
for script in "${scripts[@]}"; do source "$script"; done
|
2012-08-31 06:32:23 +00:00
|
|
|
|
2013-01-31 04:20:59 +00:00
|
|
|
|
|
|
|
# Set VERSION_NAME from $DEFINITION, if it is not already set. Then
|
|
|
|
# compute the installation prefix.
|
|
|
|
[ -n "$VERSION_NAME" ] || VERSION_NAME="${DEFINITION##*/}"
|
2013-03-28 03:04:58 +00:00
|
|
|
[ -n "$DEBUG" ] && VERSION_NAME="${VERSION_NAME}-debug"
|
2012-08-31 06:32:23 +00:00
|
|
|
PREFIX="${PYENV_ROOT}/versions/${VERSION_NAME}"
|
|
|
|
|
2013-04-19 10:26:11 +00:00
|
|
|
[ -d "${PREFIX}" ] && PREFIX_EXISTS=1
|
|
|
|
|
2013-01-31 04:20:59 +00:00
|
|
|
# If the installation prefix exists, prompt for confirmation unless
|
|
|
|
# the --force option was specified.
|
|
|
|
if [ -z "$FORCE" ] && [ -d "${PREFIX}/bin" ]; then
|
|
|
|
echo "pyenv: $PREFIX already exists" >&2
|
|
|
|
read -p "continue with installation? (y/N) "
|
|
|
|
|
|
|
|
case "$REPLY" in
|
|
|
|
y* | Y* ) ;;
|
|
|
|
* ) exit 1 ;;
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
|
2013-02-07 08:32:10 +00:00
|
|
|
# If PYENV_BUILD_ROOT is set, always pass keep options to python-build.
|
2012-08-31 06:32:23 +00:00
|
|
|
if [ -n "${PYENV_BUILD_ROOT}" ]; then
|
|
|
|
export PYTHON_BUILD_BUILD_PATH="${PYENV_BUILD_ROOT}/${VERSION_NAME}"
|
|
|
|
KEEP="-k"
|
|
|
|
fi
|
|
|
|
|
2013-01-31 04:20:59 +00:00
|
|
|
# Set PYTHON_BUILD_CACHE_PATH to $PYENV_ROOT/cache, if the directory
|
|
|
|
# exists and the variable is not already set.
|
|
|
|
if [ -z "${PYTHON_BUILD_CACHE_PATH}" ] && [ -d "${PYENV_ROOT}/cache" ]; then
|
|
|
|
export PYTHON_BUILD_CACHE_PATH="${PYENV_ROOT}/cache"
|
|
|
|
fi
|
|
|
|
|
2013-11-19 08:58:58 +00:00
|
|
|
# Default PYENV_VERSION to the friendly Python version. (The
|
2013-07-24 14:23:35 +00:00
|
|
|
# CPython installer requires an existing Python installation to run. An
|
2013-11-19 08:58:58 +00:00
|
|
|
# unsatisfied local python version can cause the installer to
|
2013-05-21 07:38:11 +00:00
|
|
|
# fail.)
|
2013-11-19 08:58:58 +00:00
|
|
|
if [[ "${VERSION_NAME}" == [23]"."* ]]; then
|
|
|
|
export PYENV_VERSION="$(pyenv-whence "python${VERSION_NAME%%.${VERSION_NAME##*.}}" 2>/dev/null | tail -1 || true)"
|
|
|
|
fi
|
2013-05-21 02:43:01 +00:00
|
|
|
|
2013-01-31 04:20:59 +00:00
|
|
|
|
|
|
|
# Execute `before_install` hooks.
|
|
|
|
for hook in "${before_hooks[@]}"; do eval "$hook"; done
|
|
|
|
|
2013-04-19 10:26:11 +00:00
|
|
|
# Plan cleanup on unsuccessful installation.
|
|
|
|
cleanup() {
|
|
|
|
[ -z "${PREFIX_EXISTS}" ] && rm -rf "$PREFIX"
|
|
|
|
}
|
|
|
|
|
|
|
|
trap cleanup SIGINT
|
|
|
|
|
|
|
|
# Invoke `python-build` and record the exit status in $STATUS.
|
2013-01-31 04:20:59 +00:00
|
|
|
STATUS=0
|
2013-12-27 09:44:54 +00:00
|
|
|
python-build $KEEP $VERBOSE $HAS_PATCH $DEBUG "$DEFINITION" "$PREFIX" || STATUS="$?"
|
2013-01-31 04:20:59 +00:00
|
|
|
|
2013-04-19 10:26:11 +00:00
|
|
|
# Display a more helpful message if the definition wasn't found.
|
|
|
|
if [ "$STATUS" == "2" ]; then
|
|
|
|
{ candidates="$(definitions "$DEFINITION")"
|
|
|
|
if [ -n "$candidates" ]; then
|
|
|
|
echo
|
|
|
|
echo "The following versions contain \`$DEFINITION' in the name:"
|
|
|
|
echo "$candidates" | indent
|
|
|
|
fi
|
|
|
|
echo
|
|
|
|
echo "You can list all available versions with \`pyenv install --list'."
|
|
|
|
echo
|
|
|
|
echo "If the version you're looking for is not present, first try upgrading"
|
|
|
|
echo "pyenv. If it's still missing, open a request on the pyenv"
|
|
|
|
echo "issue tracker: https://github.com/yyuu/pyenv/issues"
|
|
|
|
} >&2
|
|
|
|
fi
|
|
|
|
|
2013-01-31 04:20:59 +00:00
|
|
|
# Execute `after_install` hooks.
|
|
|
|
for hook in "${after_hooks[@]}"; do eval "$hook"; done
|
|
|
|
|
|
|
|
# Run `pyenv-rehash` after a successful installation.
|
2013-04-19 10:26:11 +00:00
|
|
|
if [ "$STATUS" == "0" ]; then
|
2014-01-10 11:05:01 +00:00
|
|
|
pyenv-rehash
|
2013-04-19 10:26:11 +00:00
|
|
|
else
|
|
|
|
cleanup
|
|
|
|
fi
|
2013-01-31 04:20:59 +00:00
|
|
|
|
|
|
|
exit "$STATUS"
|