mirror of
https://github.com/pyenv/pyenv.git
synced 2025-11-10 12:33:48 -05:00
import recent changes from ruby-build:
* verify checksum of downloaded archives. * add PYTHON_BUILD_MIRROR_URL to use mirror site. But we don't have CloudFront setup as of now :-( * rbenv 0.4.x style help messages
This commit is contained in:
@@ -1,4 +1,21 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Summary: Install a Python version using the python-build plugin
|
||||
#
|
||||
# Usage: pyenv install [-f|--force] [-k|--keep] [-v|--verbose] <version>
|
||||
# pyenv install [-f|--force] [-k|--keep] [-v|--verbose] <definition-file>
|
||||
# pyenv install -l|--list
|
||||
#
|
||||
# -l/--list List all available versions
|
||||
# -f/--force Install even if the version appears to be installed already
|
||||
# -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
|
||||
#
|
||||
# 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
|
||||
#
|
||||
set -e
|
||||
[ -n "$PYENV_DEBUG" ] && set -x
|
||||
|
||||
@@ -15,20 +32,12 @@ fi
|
||||
eval "$(python-build --lib)"
|
||||
|
||||
usage() {
|
||||
{ echo "usage: pyenv install [-k|--keep] [-v|--verbose] VERSION"
|
||||
echo " pyenv install [-k|--keep] [-v|--verbose] /path/to/definition"
|
||||
echo " pyenv install -l|--list"
|
||||
echo
|
||||
echo " -l/--list List all available versions"
|
||||
echo " -k/--keep Keep source tree in \$PYENV_BUILD_ROOT after installation"
|
||||
echo " (defaults to ${PYENV_ROOT}/sources)"
|
||||
echo " -v/--verbose Verbose mode: print compilation status to stdout"
|
||||
echo
|
||||
} >&2
|
||||
|
||||
# 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"
|
||||
[ -z "$1" ] || exit "$1"
|
||||
}
|
||||
|
||||
unset FORCE
|
||||
unset KEEP
|
||||
unset VERBOSE
|
||||
|
||||
@@ -43,6 +52,9 @@ for option in "${OPTIONS[@]}"; do
|
||||
python-build --definitions | sed 's/^/ /'
|
||||
exit
|
||||
;;
|
||||
"f" | "force" )
|
||||
FORCE=true
|
||||
;;
|
||||
"k" | "keep" )
|
||||
[ -n "${PYENV_BUILD_ROOT}" ] || PYENV_BUILD_ROOT="${PYENV_ROOT}/sources"
|
||||
;;
|
||||
@@ -58,21 +70,82 @@ for option in "${OPTIONS[@]}"; do
|
||||
esac
|
||||
done
|
||||
|
||||
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.
|
||||
DEFINITION="${ARGUMENTS[0]}"
|
||||
[ -n "$DEFINITION" ] || DEFINITION="$(pyenv local 2>/dev/null || true)"
|
||||
[ -n "$DEFINITION" ] || usage 1
|
||||
|
||||
|
||||
|
||||
# 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"
|
||||
}
|
||||
|
||||
# Load plugin hooks.
|
||||
for script in $(pyenv-hooks install); do
|
||||
source "$script"
|
||||
done
|
||||
|
||||
VERSION_NAME="${DEFINITION##*/}"
|
||||
|
||||
# Set VERSION_NAME from $DEFINITION, if it is not already set. Then
|
||||
# compute the installation prefix.
|
||||
[ -n "$VERSION_NAME" ] || VERSION_NAME="${DEFINITION##*/}"
|
||||
PREFIX="${PYENV_ROOT}/versions/${VERSION_NAME}"
|
||||
|
||||
# If PYENV_BUILD_ROOT is set, then always pass keep options to python-build
|
||||
# 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
|
||||
|
||||
# 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
|
||||
|
||||
python-build $KEEP $VERBOSE "$DEFINITION" "$PREFIX"
|
||||
pyenv rehash
|
||||
# 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
|
||||
|
||||
|
||||
# Execute `before_install` hooks.
|
||||
for hook in "${before_hooks[@]}"; do eval "$hook"; done
|
||||
|
||||
|
||||
# Invoke `python-build` and record the exit status in $STATUS. Run
|
||||
# `pyenv rehash` after a successful installation.
|
||||
STATUS=0
|
||||
python-build $KEEP $VERBOSE "$DEFINITION" "$PREFIX" || STATUS="$?"
|
||||
|
||||
# Execute `after_install` hooks.
|
||||
for hook in "${after_hooks[@]}"; do eval "$hook"; done
|
||||
|
||||
# Run `pyenv-rehash` after a successful installation.
|
||||
[ "$STATUS" != "0" ] || pyenv rehash
|
||||
|
||||
exit "$STATUS"
|
||||
|
||||
@@ -1,4 +1,15 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Summary: Uninstall a specific Python version
|
||||
#
|
||||
# Usage: pyenv uninstall [-f|--force] <version>
|
||||
#
|
||||
# -f Attempt to remove the specified version without prompting
|
||||
# for confirmation. If the version does not exist, do not
|
||||
# display an error message.
|
||||
#
|
||||
# See `pyenv versions` for a complete list of installed versions.
|
||||
#
|
||||
set -e
|
||||
|
||||
# Provide pyenv completions
|
||||
@@ -11,7 +22,7 @@ if [ -z "$PYENV_ROOT" ]; then
|
||||
fi
|
||||
|
||||
unset FORCE
|
||||
if [ "$1" = "-f" ]; then
|
||||
if [ "$1" = "-f" ] || [ "$1" = "--force" ]; then
|
||||
FORCE=true
|
||||
shift
|
||||
fi
|
||||
@@ -19,15 +30,9 @@ fi
|
||||
DEFINITION="$1"
|
||||
case "$DEFINITION" in
|
||||
"" | -* )
|
||||
{ echo "usage: pyenv uninstall [-f] VERSION"
|
||||
echo
|
||||
echo " -f Attempt to remove the specified version without prompting"
|
||||
echo " for confirmation. If the version does not exist, do not"
|
||||
echo " display an error message."
|
||||
echo
|
||||
echo "Installed versions:"
|
||||
pyenv versions --bare | sed 's/^/ /'
|
||||
echo
|
||||
# We can remove the sed fallback once pyenv 0.4.0 is widely available.
|
||||
{ pyenv-help uninstall 2>/dev/null ||
|
||||
sed -ne '/^#/!q;s/.\{1,2\}//;1,4d;p' < "$0"
|
||||
} >&2
|
||||
exit 1
|
||||
;;
|
||||
|
||||
@@ -112,7 +112,9 @@ install_package_using() {
|
||||
make_package "$package_name" $*
|
||||
popd >&4
|
||||
|
||||
echo "Installed ${package_name} to ${PREFIX_PATH}" >&2
|
||||
{ echo "Installed ${package_name} to ${PREFIX_PATH}"
|
||||
echo
|
||||
} >&2
|
||||
}
|
||||
|
||||
make_package() {
|
||||
@@ -127,27 +129,139 @@ make_package() {
|
||||
popd >&4
|
||||
}
|
||||
|
||||
fetch_url() {
|
||||
compute_md5() {
|
||||
if type md5 >/dev/null; then
|
||||
md5 -q
|
||||
elif type openssl &>/dev/null; then
|
||||
local output="$(openssl md5)"
|
||||
echo "${output##* }"
|
||||
elif type md5sum &>/dev/null; then
|
||||
local output="$(md5sum -b)"
|
||||
echo "${output% *}"
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
verify_checksum() {
|
||||
# If there's no MD5 support, return success
|
||||
[ -n "$HAS_MD5_SUPPORT" ] || return 0
|
||||
|
||||
# If the specified filename doesn't exist, return success
|
||||
local filename="$1"
|
||||
[ -e "$filename" ] || return 0
|
||||
|
||||
# If there's no expected checksum, return success
|
||||
local expected_checksum="$2"
|
||||
[ -n "$expected_checksum" ] || return 0
|
||||
|
||||
# If the computed checksum is empty, return failure
|
||||
local computed_checksum="$(compute_md5 < "$filename")"
|
||||
[ -n "$computed_checksum" ] || return 1
|
||||
|
||||
if [ "$expected_checksum" != "$computed_checksum" ]; then
|
||||
{ echo
|
||||
echo "checksum mismatch: ${filename} (file is corrupt)"
|
||||
echo "expected $expected_checksum, got $computed_checksum"
|
||||
echo
|
||||
} >&4
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
http() {
|
||||
local method="$1"
|
||||
local url="$2"
|
||||
[ -n "$url" ] || return 1
|
||||
|
||||
if type curl &>/dev/null; then
|
||||
curl -L "$@"
|
||||
"http_${method}_curl" "$url"
|
||||
elif type wget &>/dev/null; then
|
||||
wget -O- "$@"
|
||||
"http_${method}_wget" "$url"
|
||||
else
|
||||
echo "error: please install \`curl\` or \`wget\` and try again" >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
http_head_curl() {
|
||||
curl -sILf "$1" >&4 2>&1
|
||||
}
|
||||
|
||||
http_get_curl() {
|
||||
curl -sSLf "$1"
|
||||
}
|
||||
|
||||
http_head_wget() {
|
||||
wget -q --spider "$1" >&4 2>&1
|
||||
}
|
||||
|
||||
http_get_wget() {
|
||||
wget -nv -O- "$1"
|
||||
}
|
||||
|
||||
fetch_tarball() {
|
||||
local package_name="$1"
|
||||
local package_url="$2"
|
||||
local mirror_url
|
||||
local checksum
|
||||
|
||||
echo "Downloading ${package_url}..." >&2
|
||||
{ fetch_url "$package_url" > "${package_name}.tar"
|
||||
tar xvf "${package_name}.tar"
|
||||
if [ "$package_url" != "${package_url/\#}" ]; then
|
||||
checksum="${package_url#*#}"
|
||||
package_url="${package_url%%#*}"
|
||||
|
||||
if [ -n "$PYTHON_BUILD_MIRROR_URL" ]; then
|
||||
mirror_url="${PYTHON_BUILD_MIRROR_URL}/$checksum"
|
||||
fi
|
||||
fi
|
||||
|
||||
local package_filename="${package_name}.tar" # later tar can read compression algorithm from file
|
||||
symlink_tarball_from_cache "$package_filename" "$checksum" || {
|
||||
echo "Downloading ${package_filename}..." >&2
|
||||
{ http head "$mirror_url" &&
|
||||
download_tarball "$mirror_url" "$package_filename" "$checksum"
|
||||
} ||
|
||||
download_tarball "$package_url" "$package_filename" "$checksum"
|
||||
}
|
||||
|
||||
{ tar xvf "$package_filename"
|
||||
rm -f "$package_filename"
|
||||
} >&4 2>&1
|
||||
}
|
||||
|
||||
symlink_tarball_from_cache() {
|
||||
[ -n "$PYTHON_BUILD_CACHE_PATH" ] || return 1
|
||||
|
||||
local package_filename="$1"
|
||||
local cached_package_filename="${PYTHON_BUILD_CACHE_PATH}/$package_filename"
|
||||
local checksum="$2"
|
||||
|
||||
[ -e "$cached_package_filename" ] || return 1
|
||||
verify_checksum "$cached_package_filename" "$checksum" >&4 2>&1 || return 1
|
||||
ln -s "$cached_package_filename" "$package_filename" >&4 2>&1 || return 1
|
||||
}
|
||||
|
||||
download_tarball() {
|
||||
local package_url="$1"
|
||||
[ -n "$package_url" ] || return 1
|
||||
|
||||
local package_filename="$2"
|
||||
local checksum="$3"
|
||||
|
||||
echo "-> $package_url" >&2
|
||||
|
||||
{ http get "$package_url" > "$package_filename"
|
||||
verify_checksum "$package_filename" "$checksum"
|
||||
} >&4 2>&1 || return 1
|
||||
|
||||
if [ -n "$PYTHON_BUILD_CACHE_PATH" ]; then
|
||||
local cached_package_filename="${PYTHON_BUILD_CACHE_PATH}/$package_filename"
|
||||
{ mv "$package_filename" "$cached_package_filename"
|
||||
ln -s "$cached_package_filename" "$package_filename"
|
||||
} >&4 2>&1 || return 1
|
||||
fi
|
||||
}
|
||||
|
||||
fetch_git() {
|
||||
local package_name="$1"
|
||||
local git_url="$2"
|
||||
@@ -163,6 +277,21 @@ fetch_git() {
|
||||
fi
|
||||
}
|
||||
|
||||
fetch_svn() {
|
||||
local package_name="$1"
|
||||
local svn_url="$2"
|
||||
local svn_rev="$3"
|
||||
|
||||
echo "Checking out ${svn_url}..." >&2
|
||||
|
||||
if type svn &>/dev/null; then
|
||||
svn co -r "$svn_rev" "$svn_url" "${package_name}" >&4 2>&1
|
||||
else
|
||||
echo "error: please install \`svn\` and try again" >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
fetch_jar() {
|
||||
local package_name="$1"
|
||||
local package_url="$2"
|
||||
@@ -186,7 +315,7 @@ build_package() {
|
||||
echo "Installing ${package_name}..." >&2
|
||||
|
||||
for command in $commands; do
|
||||
"build_package_${command}" "${package_name}"
|
||||
"build_package_${command}"
|
||||
done
|
||||
|
||||
if [ ! -f "$PYTHON_BIN" ]; then
|
||||
@@ -252,10 +381,6 @@ build_package_copy() {
|
||||
cp -R . "$PREFIX_PATH"
|
||||
}
|
||||
|
||||
bild_package_noop() {
|
||||
echo "Nothing to do."
|
||||
}
|
||||
|
||||
before_install_package() {
|
||||
local stub=1
|
||||
}
|
||||
@@ -266,7 +391,7 @@ after_install_package() {
|
||||
|
||||
fix_directory_permissions() {
|
||||
# Ensure installed directories are not world-writable to avoid Bundler warnings
|
||||
find "$PREFIX_PATH" -type d -exec chmod go-w {} \;
|
||||
find "$PREFIX_PATH" -type d \( -perm -020 -o -perm -002 \) -exec chmod go-w {} \;
|
||||
}
|
||||
|
||||
require_gcc() {
|
||||
@@ -480,6 +605,33 @@ else
|
||||
TMP="${TMPDIR%/}"
|
||||
fi
|
||||
|
||||
if [ -z "$MAKE" ]; then
|
||||
MAKE="make"
|
||||
fi
|
||||
|
||||
if [ -n "$PYTHON_BUILD_CACHE_PATH" ] && [ -d "$PYTHON_BUILD_CACHE_PATH" ]; then
|
||||
PYTHON_BUILD_CACHE_PATH="${PYTHON_BUILD_CACHE_PATH%/}"
|
||||
else
|
||||
unset PYTHON_BUILD_CACHE_PATH
|
||||
fi
|
||||
|
||||
if [ -z "$PYTHON_BUILD_MIRROR_URL" ]; then
|
||||
PYTHON_BUILD_MIRROR_URL="" # FIXME: setup mirror site
|
||||
else
|
||||
PYTHON_BUILD_MIRROR_URL="${PYTHON_BUILD_MIRROR_URL%/}"
|
||||
fi
|
||||
|
||||
if [ -n "$PYTHON_BUILD_SKIP_MIRROR" ]; then
|
||||
unset PYTHON_BUILD_MIRROR_URL
|
||||
fi
|
||||
|
||||
if echo test | compute_md5 >/dev/null; then
|
||||
HAS_MD5_SUPPORT=1
|
||||
else
|
||||
unset HAS_MD5_SUPPORT
|
||||
unset PYTHON_BUILD_MIRROR_URL
|
||||
fi
|
||||
|
||||
SEED="$(date "+%Y%m%d%H%M%S").$$"
|
||||
LOG_PATH="${TMP}/python-build.${SEED}.log"
|
||||
PYTHON_BIN="${PREFIX_PATH}/bin/python"
|
||||
@@ -500,8 +652,8 @@ fi
|
||||
export LDFLAGS="-L'${PREFIX_PATH}/lib' ${LDFLAGS}"
|
||||
export CPPFLAGS="-I'${PREFIX_PATH}/include' ${CPPFLAGS}"
|
||||
|
||||
unset PYTHONOPT
|
||||
unset PYTHONLIB
|
||||
unset PYTHONHOME
|
||||
unset PYTHONPATH
|
||||
|
||||
trap build_failed ERR
|
||||
mkdir -p "$BUILD_PATH"
|
||||
|
||||
Reference in New Issue
Block a user