| 1 |
#!/usr/bin/env bash |
| 2 |
# |
| 3 |
# Summary: Install a Python version using python-build |
| 4 |
# |
| 5 |
# Usage: pyenv install [-f] [-kvp] <version>... |
| 6 |
# pyenv install [-f] [-kvp] <definition-file> |
| 7 |
# pyenv install -l|--list |
| 8 |
# pyenv install --version |
| 9 |
# |
| 10 |
# -l/--list List all available versions |
| 11 |
# -f/--force Install even if the version appears to be installed already |
| 12 |
# -s/--skip-existing Skip if the version appears to be installed already |
| 13 |
# |
| 14 |
# python-build options: |
| 15 |
# |
| 16 |
# -k/--keep Keep source tree in $PYENV_BUILD_ROOT after installation |
| 17 |
# (defaults to $PYENV_ROOT/sources) |
| 18 |
# -p/--patch Apply a patch from stdin before building |
| 19 |
# -v/--verbose Verbose mode: print compilation status to stdout |
| 20 |
# --version Show version of python-build |
| 21 |
# -g/--debug Build a debug version |
| 22 |
# |
| 23 |
# For detailed information on installing Python versions with |
| 24 |
# python-build, including a list of environment variables for adjusting |
| 25 |
# compilation, see: https://github.com/pyenv/pyenv#readme |
| 26 |
# |
| 27 |
set -e |
| 28 |
[ -n "$PYENV_DEBUG" ] && set -x |
| 29 |
|
| 30 |
# Add `share/python-build/` directory from each pyenv plugin to the list of |
| 31 |
# paths where build definitions are looked up. |
| 32 |
shopt -s nullglob |
| 33 |
for plugin_path in "$PYENV_ROOT"/plugins/*/share/python-build; do |
| 34 |
PYTHON_BUILD_DEFINITIONS="${PYTHON_BUILD_DEFINITIONS}:${plugin_path}" |
| 35 |
done |
| 36 |
export PYTHON_BUILD_DEFINITIONS |
| 37 |
shopt -u nullglob |
| 38 |
|
| 39 |
# Provide pyenv completions |
| 40 |
if [ "$1" = "--complete" ]; then |
| 41 |
echo --list |
| 42 |
echo --force |
| 43 |
echo --skip-existing |
| 44 |
echo --keep |
| 45 |
echo --patch |
| 46 |
echo --verbose |
| 47 |
echo --version |
| 48 |
echo --debug |
| 49 |
exec python-build --definitions |
| 50 |
fi |
| 51 |
|
| 52 |
# Load shared library functions |
| 53 |
eval "$(python-build --lib)" |
| 54 |
|
| 55 |
usage() { |
| 56 |
pyenv-help install 2>/dev/null |
| 57 |
[ -z "$1" ] || exit "$1" |
| 58 |
} |
| 59 |
|
| 60 |
definitions() { |
| 61 |
local query="$1" |
| 62 |
python-build --definitions | $(type -P ggrep grep | head -n1) -F "$query" || true |
| 63 |
} |
| 64 |
|
| 65 |
indent() { |
| 66 |
sed 's/^/ /' |
| 67 |
} |
| 68 |
|
| 69 |
unset FORCE |
| 70 |
unset SKIP_EXISTING |
| 71 |
unset KEEP |
| 72 |
unset VERBOSE |
| 73 |
unset HAS_PATCH |
| 74 |
unset DEBUG |
| 75 |
|
| 76 |
[ -n "$PYENV_DEBUG" ] && VERBOSE="-v" |
| 77 |
|
| 78 |
parse_options "$@" |
| 79 |
for option in "${OPTIONS[@]}"; do |
| 80 |
case "$option" in |
| 81 |
"h" | "help" ) |
| 82 |
usage 0 |
| 83 |
;; |
| 84 |
"l" | "list" ) |
| 85 |
echo "Available versions:" |
| 86 |
definitions | indent |
| 87 |
exit |
| 88 |
;; |
| 89 |
"f" | "force" ) |
| 90 |
FORCE=true |
| 91 |
;; |
| 92 |
"s" | "skip-existing" ) |
| 93 |
SKIP_EXISTING=true |
| 94 |
;; |
| 95 |
"k" | "keep" ) |
| 96 |
[ -n "${PYENV_BUILD_ROOT}" ] || PYENV_BUILD_ROOT="${PYENV_ROOT}/sources" |
| 97 |
;; |
| 98 |
"v" | "verbose" ) |
| 99 |
VERBOSE="-v" |
| 100 |
;; |
| 101 |
"p" | "patch" ) |
| 102 |
HAS_PATCH="-p" |
| 103 |
;; |
| 104 |
"g" | "debug" ) |
| 105 |
DEBUG="-g" |
| 106 |
;; |
| 107 |
"version" ) |
| 108 |
exec python-build --version |
| 109 |
;; |
| 110 |
* ) |
| 111 |
usage 1 >&2 |
| 112 |
;; |
| 113 |
esac |
| 114 |
done |
| 115 |
|
| 116 |
unset VERSION_NAME |
| 117 |
|
| 118 |
# The first argument contains the definition to install. If the |
| 119 |
# argument is missing, try to install whatever local app-specific |
| 120 |
# version is specified by pyenv. Show usage instructions if a local |
| 121 |
# version is not specified. |
| 122 |
DEFINITIONS=("${ARGUMENTS[@]}") |
| 123 |
[[ "${#DEFINITIONS[*]}" -eq 0 ]] && DEFINITIONS=($(pyenv-local 2>/dev/null || true)) |
| 124 |
[[ "${#DEFINITIONS[*]}" -eq 0 ]] && usage 1 >&2 |
| 125 |
|
| 126 |
# Define `before_install` and `after_install` functions that allow |
| 127 |
# plugin hooks to register a string of code for execution before or |
| 128 |
# after the installation process. |
| 129 |
declare -a before_hooks after_hooks |
| 130 |
|
| 131 |
before_install() { |
| 132 |
local hook="$1" |
| 133 |
before_hooks["${#before_hooks[@]}"]="$hook" |
| 134 |
} |
| 135 |
|
| 136 |
after_install() { |
| 137 |
local hook="$1" |
| 138 |
after_hooks["${#after_hooks[@]}"]="$hook" |
| 139 |
} |
| 140 |
|
| 141 |
# Plan cleanup on unsuccessful installation. |
| 142 |
cleanup() { |
| 143 |
[ -z "${PREFIX_EXISTS}" ] && rm -rf "$PREFIX" |
| 144 |
} |
| 145 |
|
| 146 |
trap cleanup SIGINT |
| 147 |
|
| 148 |
|
| 149 |
OLDIFS="$IFS" |
| 150 |
IFS=$'\n' scripts=(`pyenv-hooks install`) |
| 151 |
IFS="$OLDIFS" |
| 152 |
for script in "${scripts[@]}"; do source "$script"; done |
| 153 |
|
| 154 |
COMBINED_STATUS=0 |
| 155 |
for DEFINITION in "${DEFINITIONS[@]}"; do |
| 156 |
STATUS=0 |
| 157 |
|
| 158 |
# Try to resolve a prefix if user indeed gave a prefix. |
| 159 |
# We install the version under the resolved name |
| 160 |
# and hooks also see the resolved name |
| 161 |
DEFINITION="$(pyenv-latest -f -k "$DEFINITION")" |
| 162 |
|
| 163 |
# Set VERSION_NAME from $DEFINITION. Then compute the installation prefix. |
| 164 |
VERSION_NAME="${DEFINITION##*/}" |
| 165 |
[ -n "$DEBUG" ] && VERSION_NAME="${VERSION_NAME}-debug" |
| 166 |
PREFIX="${PYENV_ROOT}/versions/${VERSION_NAME}" |
| 167 |
|
| 168 |
[ -d "${PREFIX}" ] && PREFIX_EXISTS=1 |
| 169 |
|
| 170 |
# If the installation prefix exists, prompt for confirmation unless |
| 171 |
# the --force option was specified. |
| 172 |
if [ -d "${PREFIX}/bin" ]; then |
| 173 |
if [ -z "$FORCE" ] && [ -z "$SKIP_EXISTING" ]; then |
| 174 |
echo "pyenv: $PREFIX already exists" >&2 |
| 175 |
read -p "continue with installation? (y/N) " |
| 176 |
|
| 177 |
case "$REPLY" in |
| 178 |
y | Y | yes | YES ) ;; |
| 179 |
* ) { STATUS=1; [[ $STATUS -gt $COMBINED_STATUS ]] && COMBINED_STATUS=$STATUS; }; continue ;; |
| 180 |
esac |
| 181 |
elif [ -n "$SKIP_EXISTING" ]; then |
| 182 |
# Since we know the python version is already installed, and are opting to |
| 183 |
# not force installation of existing versions, we just `exit 0` here to |
| 184 |
# leave things happy |
| 185 |
continue |
| 186 |
fi |
| 187 |
fi |
| 188 |
|
| 189 |
# If PYENV_BUILD_ROOT is set, always pass keep options to python-build. |
| 190 |
if [ -n "${PYENV_BUILD_ROOT}" ]; then |
| 191 |
export PYTHON_BUILD_BUILD_PATH="${PYENV_BUILD_ROOT}/${VERSION_NAME}" |
| 192 |
KEEP="-k" |
| 193 |
fi |
| 194 |
|
| 195 |
# Set PYTHON_BUILD_CACHE_PATH to $PYENV_ROOT/cache, if the directory |
| 196 |
# exists and the variable is not already set. |
| 197 |
if [ -z "${PYTHON_BUILD_CACHE_PATH}" ] && [ -d "${PYENV_ROOT}/cache" ]; then |
| 198 |
export PYTHON_BUILD_CACHE_PATH="${PYENV_ROOT}/cache" |
| 199 |
fi |
| 200 |
|
| 201 |
if [ -z "${PYENV_BOOTSTRAP_VERSION}" ]; then |
| 202 |
case "${VERSION_NAME}" in |
| 203 |
[23]"."* ) |
| 204 |
# Default PYENV_VERSION to the friendly Python version. (The |
| 205 |
# CPython installer requires an existing Python installation to run. An |
| 206 |
# unsatisfied local .python-version file can cause the installer to |
| 207 |
# fail.) |
| 208 |
for version_info in "${VERSION_NAME%-dev}" "${VERSION_NAME%.*}" "${VERSION_NAME%%.*}"; do |
| 209 |
# Anaconda's `curl` doesn't work on platform where `/etc/pki/tls/certs/ca-bundle.crt` isn't available (e.g. Debian) |
| 210 |
for version in $(pyenv-whence "python${version_info}" 2>/dev/null || true); do |
| 211 |
if [[ "${version}" != "anaconda"* ]] && [[ "${version}" != "miniconda"* ]]; then |
| 212 |
PYENV_BOOTSTRAP_VERSION="${version}" |
| 213 |
break 2 |
| 214 |
fi |
| 215 |
done |
| 216 |
done |
| 217 |
;; |
| 218 |
"pypy"*"-dev" | "pypy"*"-src" ) |
| 219 |
# PyPy/PyPy3 requires existing Python 2.7 to build |
| 220 |
if [ -n "${PYENV_RPYTHON_VERSION}" ]; then |
| 221 |
PYENV_BOOTSTRAP_VERSION="${PYENV_RPYTHON_VERSION}" |
| 222 |
else |
| 223 |
for version in $(pyenv-versions --bare | sort -r); do |
| 224 |
if [[ "${version}" == "2.7"* ]]; then |
| 225 |
PYENV_BOOTSTRAP_VERSION="$version" |
| 226 |
break |
| 227 |
fi |
| 228 |
done |
| 229 |
fi |
| 230 |
if [ -n "$PYENV_BOOTSTRAP_VERSION" ]; then |
| 231 |
for dep in pycparser; do |
| 232 |
if ! PYENV_VERSION="$PYENV_BOOTSTRAP_VERSION" pyenv-exec python -c "import ${dep}" 1>/dev/null 2>&1; then |
| 233 |
echo "pyenv-install: $VERSION_NAME: PyPy requires \`${dep}' in $PYENV_BOOTSTRAP_VERSION to build from source." >&2 |
| 234 |
exit 1 |
| 235 |
fi |
| 236 |
done |
| 237 |
else |
| 238 |
echo "pyenv-install: $VERSION_NAME: PyPy requires Python 2.7 to build from source." >&2 |
| 239 |
exit 1 |
| 240 |
fi |
| 241 |
;; |
| 242 |
esac |
| 243 |
fi |
| 244 |
|
| 245 |
if [ -n "${PYENV_BOOTSTRAP_VERSION}" ]; then |
| 246 |
export PYENV_VERSION="${PYENV_BOOTSTRAP_VERSION}" |
| 247 |
fi |
| 248 |
|
| 249 |
# Execute `before_install` hooks. |
| 250 |
for hook in "${before_hooks[@]}"; do eval "$hook"; done |
| 251 |
|
| 252 |
# Invoke `python-build` and record the exit status in $STATUS. |
| 253 |
python-build $KEEP $VERBOSE $HAS_PATCH $DEBUG "$DEFINITION" "$PREFIX" || \ |
| 254 |
{ STATUS=$?; [[ $STATUS -gt $COMBINED_STATUS ]] && COMBINED_STATUS=$STATUS; } |
| 255 |
|
| 256 |
# Display a more helpful message if the definition wasn't found. |
| 257 |
if [ "$STATUS" == "2" ]; then |
| 258 |
{ candidates="$(definitions "$DEFINITION")" |
| 259 |
if [ -n "$candidates" ]; then |
| 260 |
echo |
| 261 |
echo "The following versions contain \`$DEFINITION' in the name:" |
| 262 |
echo "$candidates" | indent |
| 263 |
fi |
| 264 |
echo |
| 265 |
echo "See all available versions with \`pyenv install --list'." |
| 266 |
echo |
| 267 |
echo -n "If the version you need is missing, try upgrading pyenv" |
| 268 |
declare brew_prefix |
| 269 |
if brew_prefix="$(brew --prefix 2>/dev/null)" && [[ $_PYENV_INSTALL_PREFIX/ == "$brew_prefix/"* ]]; then |
| 270 |
printf ":\n\n" |
| 271 |
echo " brew update && brew upgrade pyenv" |
| 272 |
elif [ -d "${_PYENV_INSTALL_PREFIX}/.git" ]; then |
| 273 |
printf ":\n\n" |
| 274 |
echo " cd ${_PYENV_INSTALL_PREFIX} && git pull && cd -" |
| 275 |
else |
| 276 |
printf ".\n" |
| 277 |
fi |
| 278 |
} >&2 |
| 279 |
fi |
| 280 |
|
| 281 |
# Execute `after_install` hooks. |
| 282 |
for hook in "${after_hooks[@]}"; do eval "$hook"; done |
| 283 |
|
| 284 |
# Run `pyenv-rehash` after a successful installation. |
| 285 |
if [[ $STATUS -eq 0 ]]; then |
| 286 |
pyenv-rehash |
| 287 |
else |
| 288 |
cleanup |
| 289 |
break |
| 290 |
fi |
| 291 |
|
| 292 |
done |
| 293 |
|
| 294 |
|
| 295 |
exit "${COMBINED_STATUS}" |