2019-04-23 10:23:33 -04:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
2013-12-27 04:44:54 -05:00
|
|
|
# Summary: Install a Python version using python-build
|
2013-01-30 23:20:59 -05:00
|
|
|
#
|
2022-12-22 12:35:25 -05:00
|
|
|
# Usage: pyenv install [-f] [-kvp] <version>...
|
2013-12-27 04:44:54 -05:00
|
|
|
# pyenv install [-f] [-kvp] <definition-file>
|
2013-01-30 23:20:59 -05:00
|
|
|
# pyenv install -l|--list
|
2016-02-07 19:01:02 -05:00
|
|
|
# pyenv install --version
|
2013-01-30 23:20:59 -05:00
|
|
|
#
|
2014-04-09 08:23:46 -04:00
|
|
|
# -l/--list List all available versions
|
|
|
|
# -f/--force Install even if the version appears to be installed already
|
|
|
|
# -s/--skip-existing Skip if the version appears to be installed already
|
2013-12-27 04:44:54 -05:00
|
|
|
#
|
|
|
|
# python-build options:
|
|
|
|
#
|
2014-04-09 08:23:46 -04:00
|
|
|
# -k/--keep Keep source tree in $PYENV_BUILD_ROOT after installation
|
|
|
|
# (defaults to $PYENV_ROOT/sources)
|
|
|
|
# -p/--patch Apply a patch from stdin before building
|
2016-02-07 19:01:02 -05:00
|
|
|
# -v/--verbose Verbose mode: print compilation status to stdout
|
|
|
|
# --version Show version of python-build
|
2014-04-09 08:23:46 -04:00
|
|
|
# -g/--debug Build a debug version
|
2013-01-30 23:20:59 -05:00
|
|
|
#
|
|
|
|
# For detailed information on installing Python versions with
|
|
|
|
# python-build, including a list of environment variables for adjusting
|
2017-03-05 23:31:25 -05:00
|
|
|
# compilation, see: https://github.com/pyenv/pyenv#readme
|
2013-01-30 23:20:59 -05:00
|
|
|
#
|
2012-08-31 02:32:23 -04:00
|
|
|
set -e
|
|
|
|
[ -n "$PYENV_DEBUG" ] && set -x
|
|
|
|
|
2014-10-29 06:35:29 -04:00
|
|
|
# Add `share/python-build/` directory from each pyenv plugin to the list of
|
|
|
|
# paths where build definitions are looked up.
|
|
|
|
shopt -s nullglob
|
|
|
|
for plugin_path in "$PYENV_ROOT"/plugins/*/share/python-build; do
|
|
|
|
PYTHON_BUILD_DEFINITIONS="${PYTHON_BUILD_DEFINITIONS}:${plugin_path}"
|
|
|
|
done
|
|
|
|
export PYTHON_BUILD_DEFINITIONS
|
|
|
|
shopt -u nullglob
|
|
|
|
|
2012-08-31 02:32:23 -04:00
|
|
|
# Provide pyenv completions
|
|
|
|
if [ "$1" = "--complete" ]; then
|
2016-02-07 19:01:02 -05:00
|
|
|
echo --list
|
|
|
|
echo --force
|
|
|
|
echo --skip-existing
|
|
|
|
echo --keep
|
|
|
|
echo --patch
|
|
|
|
echo --verbose
|
|
|
|
echo --version
|
|
|
|
echo --debug
|
2012-08-31 02:32:23 -04:00
|
|
|
exec python-build --definitions
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Load shared library functions
|
|
|
|
eval "$(python-build --lib)"
|
|
|
|
|
|
|
|
usage() {
|
2015-08-25 02:37:31 -04:00
|
|
|
pyenv-help install 2>/dev/null
|
2012-08-31 02:32:23 -04:00
|
|
|
[ -z "$1" ] || exit "$1"
|
|
|
|
}
|
|
|
|
|
2013-04-19 06:26:11 -04:00
|
|
|
definitions() {
|
|
|
|
local query="$1"
|
2023-02-22 12:11:48 -05:00
|
|
|
python-build --definitions | $(type -P ggrep grep | head -n1) -F "$query" || true
|
2013-04-19 06:26:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
indent() {
|
|
|
|
sed 's/^/ /'
|
|
|
|
}
|
|
|
|
|
2013-01-30 23:20:59 -05:00
|
|
|
unset FORCE
|
2014-04-09 08:23:46 -04:00
|
|
|
unset SKIP_EXISTING
|
2012-08-31 02:32:23 -04:00
|
|
|
unset KEEP
|
|
|
|
unset VERBOSE
|
2013-12-27 04:44:54 -05:00
|
|
|
unset HAS_PATCH
|
2013-03-27 23:04:58 -04:00
|
|
|
unset DEBUG
|
2012-08-31 02:32:23 -04:00
|
|
|
|
2022-04-30 22:49:09 -04:00
|
|
|
[ -n "$PYENV_DEBUG" ] && VERBOSE="-v"
|
|
|
|
|
2012-08-31 02:32:23 -04:00
|
|
|
parse_options "$@"
|
|
|
|
for option in "${OPTIONS[@]}"; do
|
|
|
|
case "$option" in
|
|
|
|
"h" | "help" )
|
|
|
|
usage 0
|
|
|
|
;;
|
|
|
|
"l" | "list" )
|
|
|
|
echo "Available versions:"
|
2013-04-19 06:26:11 -04:00
|
|
|
definitions | indent
|
2012-08-31 02:32:23 -04:00
|
|
|
exit
|
|
|
|
;;
|
2013-01-30 23:20:59 -05:00
|
|
|
"f" | "force" )
|
|
|
|
FORCE=true
|
|
|
|
;;
|
2014-04-09 08:23:46 -04:00
|
|
|
"s" | "skip-existing" )
|
|
|
|
SKIP_EXISTING=true
|
|
|
|
;;
|
2012-08-31 02:32:23 -04:00
|
|
|
"k" | "keep" )
|
|
|
|
[ -n "${PYENV_BUILD_ROOT}" ] || PYENV_BUILD_ROOT="${PYENV_ROOT}/sources"
|
|
|
|
;;
|
|
|
|
"v" | "verbose" )
|
|
|
|
VERBOSE="-v"
|
|
|
|
;;
|
2013-12-27 04:44:54 -05:00
|
|
|
"p" | "patch" )
|
|
|
|
HAS_PATCH="-p"
|
|
|
|
;;
|
2013-03-27 23:04:58 -04:00
|
|
|
"g" | "debug" )
|
|
|
|
DEBUG="-g"
|
|
|
|
;;
|
2012-08-31 02:32:23 -04:00
|
|
|
"version" )
|
|
|
|
exec python-build --version
|
|
|
|
;;
|
|
|
|
* )
|
2015-05-26 12:05:46 -04:00
|
|
|
usage 1 >&2
|
2012-08-31 02:32:23 -04:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2013-01-30 23:20:59 -05:00
|
|
|
unset VERSION_NAME
|
|
|
|
|
2021-06-14 10:08:46 -04:00
|
|
|
# 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.
|
2022-12-22 12:35:25 -05:00
|
|
|
DEFINITIONS=("${ARGUMENTS[@]}")
|
2023-01-21 21:49:40 -05:00
|
|
|
[[ "${#DEFINITIONS[*]}" -eq 0 ]] && DEFINITIONS=($(pyenv-local 2>/dev/null || true))
|
|
|
|
[[ "${#DEFINITIONS[*]}" -eq 0 ]] && usage 1 >&2
|
2013-01-30 23:20:59 -05: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"
|
|
|
|
}
|
|
|
|
|
2022-12-22 12:35:25 -05:00
|
|
|
# Plan cleanup on unsuccessful installation.
|
|
|
|
cleanup() {
|
|
|
|
[ -z "${PREFIX_EXISTS}" ] && rm -rf "$PREFIX"
|
|
|
|
}
|
|
|
|
|
|
|
|
trap cleanup SIGINT
|
|
|
|
|
|
|
|
|
2014-01-10 06:05:01 -05:00
|
|
|
OLDIFS="$IFS"
|
|
|
|
IFS=$'\n' scripts=(`pyenv-hooks install`)
|
|
|
|
IFS="$OLDIFS"
|
|
|
|
for script in "${scripts[@]}"; do source "$script"; done
|
2012-08-31 02:32:23 -04:00
|
|
|
|
2023-01-21 21:49:40 -05:00
|
|
|
COMBINED_STATUS=0
|
|
|
|
for DEFINITION in "${DEFINITIONS[@]}"; do
|
|
|
|
STATUS=0
|
|
|
|
|
2022-12-22 12:35:25 -05:00
|
|
|
# Try to resolve a prefix if user indeed gave a prefix.
|
|
|
|
# We install the version under the resolved name
|
|
|
|
# and hooks also see the resolved name
|
|
|
|
DEFINITION="$(pyenv-latest -q -k "$DEFINITION" || echo "$DEFINITION")"
|
|
|
|
|
|
|
|
# Set VERSION_NAME from $DEFINITION. Then compute the installation prefix.
|
|
|
|
VERSION_NAME="${DEFINITION##*/}"
|
|
|
|
[ -n "$DEBUG" ] && VERSION_NAME="${VERSION_NAME}-debug"
|
|
|
|
PREFIX="${PYENV_ROOT}/versions/${VERSION_NAME}"
|
|
|
|
|
|
|
|
[ -d "${PREFIX}" ] && PREFIX_EXISTS=1
|
|
|
|
|
|
|
|
# If the installation prefix exists, prompt for confirmation unless
|
|
|
|
# the --force option was specified.
|
|
|
|
if [ -d "${PREFIX}/bin" ]; then
|
|
|
|
if [ -z "$FORCE" ] && [ -z "$SKIP_EXISTING" ]; then
|
|
|
|
echo "pyenv: $PREFIX already exists" >&2
|
|
|
|
read -p "continue with installation? (y/N) "
|
|
|
|
|
|
|
|
case "$REPLY" in
|
|
|
|
y | Y | yes | YES ) ;;
|
2023-01-21 21:49:40 -05:00
|
|
|
* ) { STATUS=1; [[ $STATUS -gt $COMBINED_STATUS ]] && COMBINED_STATUS=$STATUS; }; continue ;;
|
2022-12-22 12:35:25 -05:00
|
|
|
esac
|
|
|
|
elif [ -n "$SKIP_EXISTING" ]; then
|
|
|
|
# Since we know the python version is already installed, and are opting to
|
|
|
|
# not force installation of existing versions, we just `exit 0` here to
|
|
|
|
# leave things happy
|
|
|
|
continue
|
|
|
|
fi
|
2014-04-09 08:23:46 -04:00
|
|
|
fi
|
2013-01-30 23:20:59 -05:00
|
|
|
|
2022-12-22 12:35:25 -05:00
|
|
|
# If PYENV_BUILD_ROOT is set, always pass keep options to python-build.
|
|
|
|
if [ -n "${PYENV_BUILD_ROOT}" ]; then
|
|
|
|
export PYTHON_BUILD_BUILD_PATH="${PYENV_BUILD_ROOT}/${VERSION_NAME}"
|
|
|
|
KEEP="-k"
|
|
|
|
fi
|
2012-08-31 02:32:23 -04:00
|
|
|
|
2022-12-22 12:35:25 -05: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-01-30 23:20:59 -05:00
|
|
|
|
2022-12-22 12:35:25 -05:00
|
|
|
if [ -z "${PYENV_BOOTSTRAP_VERSION}" ]; then
|
|
|
|
case "${VERSION_NAME}" in
|
|
|
|
[23]"."* )
|
|
|
|
# Default PYENV_VERSION to the friendly Python version. (The
|
|
|
|
# CPython installer requires an existing Python installation to run. An
|
|
|
|
# unsatisfied local .python-version file can cause the installer to
|
|
|
|
# fail.)
|
|
|
|
for version_info in "${VERSION_NAME%-dev}" "${VERSION_NAME%.*}" "${VERSION_NAME%%.*}"; do
|
|
|
|
# Anaconda's `curl` doesn't work on platform where `/etc/pki/tls/certs/ca-bundle.crt` isn't available (e.g. Debian)
|
|
|
|
for version in $(pyenv-whence "python${version_info}" 2>/dev/null || true); do
|
|
|
|
if [[ "${version}" != "anaconda"* ]] && [[ "${version}" != "miniconda"* ]]; then
|
|
|
|
PYENV_BOOTSTRAP_VERSION="${version}"
|
|
|
|
break 2
|
|
|
|
fi
|
|
|
|
done
|
2015-12-07 00:10:11 -05:00
|
|
|
done
|
2022-12-22 12:35:25 -05:00
|
|
|
;;
|
|
|
|
"pypy"*"-dev" | "pypy"*"-src" )
|
|
|
|
# PyPy/PyPy3 requires existing Python 2.7 to build
|
|
|
|
if [ -n "${PYENV_RPYTHON_VERSION}" ]; then
|
|
|
|
PYENV_BOOTSTRAP_VERSION="${PYENV_RPYTHON_VERSION}"
|
|
|
|
else
|
|
|
|
for version in $(pyenv-versions --bare | sort -r); do
|
|
|
|
if [[ "${version}" == "2.7"* ]]; then
|
|
|
|
PYENV_BOOTSTRAP_VERSION="$version"
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
if [ -n "$PYENV_BOOTSTRAP_VERSION" ]; then
|
|
|
|
for dep in pycparser; do
|
|
|
|
if ! PYENV_VERSION="$PYENV_BOOTSTRAP_VERSION" pyenv-exec python -c "import ${dep}" 1>/dev/null 2>&1; then
|
|
|
|
echo "pyenv-install: $VERSION_NAME: PyPy requires \`${dep}' in $PYENV_BOOTSTRAP_VERSION to build from source." >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
else
|
|
|
|
echo "pyenv-install: $VERSION_NAME: PyPy requires Python 2.7 to build from source." >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
2013-04-19 06:26:11 -04:00
|
|
|
|
2022-12-22 12:35:25 -05:00
|
|
|
if [ -n "${PYENV_BOOTSTRAP_VERSION}" ]; then
|
|
|
|
export PYENV_VERSION="${PYENV_BOOTSTRAP_VERSION}"
|
|
|
|
fi
|
2013-01-30 23:20:59 -05:00
|
|
|
|
2022-12-22 12:35:25 -05:00
|
|
|
# Execute `before_install` hooks.
|
|
|
|
for hook in "${before_hooks[@]}"; do eval "$hook"; done
|
|
|
|
|
|
|
|
# Invoke `python-build` and record the exit status in $STATUS.
|
2023-01-21 21:49:40 -05:00
|
|
|
python-build $KEEP $VERBOSE $HAS_PATCH $DEBUG "$DEFINITION" "$PREFIX" || \
|
|
|
|
{ STATUS=$?; [[ $STATUS -gt $COMBINED_STATUS ]] && COMBINED_STATUS=$STATUS; }
|
2022-12-22 12:35:25 -05:00
|
|
|
|
|
|
|
# Display a more helpful message if the definition wasn't found.
|
|
|
|
if [ "$STATUS" == "2" ]; then
|
|
|
|
{ candidates="$(definitions "$DEFINITION")"
|
|
|
|
here="$(dirname "${0%/*}")/../.."
|
|
|
|
if [ -n "$candidates" ]; then
|
|
|
|
echo
|
|
|
|
echo "The following versions contain \`$DEFINITION' in the name:"
|
|
|
|
echo "$candidates" | indent
|
|
|
|
fi
|
2013-04-19 06:26:11 -04:00
|
|
|
echo
|
2022-12-22 12:35:25 -05:00
|
|
|
echo "See all available versions with \`pyenv install --list'."
|
|
|
|
echo
|
|
|
|
echo -n "If the version you need is missing, try upgrading pyenv"
|
|
|
|
if [ "$here" != "${here#$(brew --prefix 2>/dev/null)}" ]; then
|
|
|
|
printf ":\n\n"
|
|
|
|
echo " brew update && brew upgrade pyenv"
|
|
|
|
elif [ -d "${here}/.git" ]; then
|
|
|
|
printf ":\n\n"
|
|
|
|
echo " cd ${here} && git pull && cd -"
|
|
|
|
else
|
|
|
|
printf ".\n"
|
|
|
|
fi
|
|
|
|
} >&2
|
|
|
|
fi
|
2013-04-19 06:26:11 -04:00
|
|
|
|
2022-12-22 12:35:25 -05:00
|
|
|
# Execute `after_install` hooks.
|
|
|
|
for hook in "${after_hooks[@]}"; do eval "$hook"; done
|
2013-01-30 23:20:59 -05:00
|
|
|
|
2022-12-22 12:35:25 -05:00
|
|
|
# Run `pyenv-rehash` after a successful installation.
|
2023-01-21 21:49:40 -05:00
|
|
|
if [[ $STATUS -eq 0 ]]; then
|
2022-12-22 12:35:25 -05:00
|
|
|
pyenv-rehash
|
|
|
|
else
|
|
|
|
cleanup
|
2023-01-21 21:49:40 -05:00
|
|
|
break
|
2022-12-22 12:35:25 -05:00
|
|
|
fi
|
|
|
|
|
|
|
|
done
|
2013-01-30 23:20:59 -05:00
|
|
|
|
2023-01-21 21:49:40 -05:00
|
|
|
|
|
|
|
exit "${COMBINED_STATUS}"
|