2019-04-23 10:23:33 -04:00
|
|
|
#!/usr/bin/env bash
|
2013-01-18 03:41:41 -05:00
|
|
|
# Summary: Rehash pyenv shims (run this after installing executables)
|
|
|
|
|
2012-08-31 02:23:41 -04:00
|
|
|
set -e
|
|
|
|
[ -n "$PYENV_DEBUG" ] && set -x
|
|
|
|
|
|
|
|
SHIM_PATH="${PYENV_ROOT}/shims"
|
|
|
|
PROTOTYPE_SHIM_PATH="${SHIM_PATH}/.pyenv-shim"
|
|
|
|
|
|
|
|
# Create the shims directory if it doesn't already exist.
|
|
|
|
mkdir -p "$SHIM_PATH"
|
|
|
|
|
2018-04-18 21:02:16 -04:00
|
|
|
acquire_lock() {
|
|
|
|
# Ensure only one instance of pyenv-rehash is running at a time by
|
|
|
|
# setting the shell's `noclobber` option and attempting to write to
|
|
|
|
# the prototype shim file. If the file already exists, print a warning
|
|
|
|
# to stderr and exit with a non-zero status.
|
|
|
|
local ret
|
|
|
|
set -o noclobber
|
|
|
|
echo > "$PROTOTYPE_SHIM_PATH" 2>| /dev/null || ret=1
|
|
|
|
set +o noclobber
|
|
|
|
[ -z "${ret}" ]
|
|
|
|
}
|
2012-08-31 02:23:41 -04:00
|
|
|
|
|
|
|
# If we were able to obtain a lock, register a trap to clean up the
|
|
|
|
# prototype shim when the process exits.
|
2018-04-18 21:02:16 -04:00
|
|
|
trap release_lock EXIT
|
2012-08-31 02:23:41 -04:00
|
|
|
|
|
|
|
remove_prototype_shim() {
|
|
|
|
rm -f "$PROTOTYPE_SHIM_PATH"
|
|
|
|
}
|
|
|
|
|
2018-04-18 21:02:16 -04:00
|
|
|
release_lock() {
|
|
|
|
remove_prototype_shim
|
|
|
|
}
|
|
|
|
|
2018-04-23 20:37:29 -04:00
|
|
|
if [ ! -w "$SHIM_PATH" ]; then
|
|
|
|
echo "pyenv: cannot rehash: $SHIM_PATH isn't writable"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2018-04-18 21:02:16 -04:00
|
|
|
unset acquired
|
2021-01-09 03:21:11 -05:00
|
|
|
start=$SECONDS
|
|
|
|
while (( SECONDS <= start + ${PYENV_REHASH_TIMEOUT:-60} )); do
|
2018-04-18 21:02:16 -04:00
|
|
|
if acquire_lock 2>/dev/null; then
|
|
|
|
acquired=1
|
|
|
|
break
|
|
|
|
else
|
2020-12-05 11:08:16 -05:00
|
|
|
# POSIX sleep(1) doesn't provide subsecond precision, but many others do
|
|
|
|
sleep 0.1 2>/dev/null || sleep 1
|
2018-04-18 21:02:16 -04:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ -z "${acquired}" ]; then
|
2018-04-23 20:37:29 -04:00
|
|
|
echo "pyenv: cannot rehash: $PROTOTYPE_SHIM_PATH exists"
|
2018-04-18 21:02:16 -04:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2012-08-31 02:23:41 -04:00
|
|
|
# The prototype shim file is a script that re-execs itself, passing
|
|
|
|
# its filename and any arguments to `pyenv exec`. This file is
|
2013-01-18 04:57:08 -05:00
|
|
|
# hard-linked for every executable and then removed. The linking
|
|
|
|
# technique is fast, uses less disk space than unique files, and also
|
|
|
|
# serves as a locking mechanism.
|
2012-08-31 02:23:41 -04:00
|
|
|
create_prototype_shim() {
|
|
|
|
cat > "$PROTOTYPE_SHIM_PATH" <<SH
|
2019-04-23 10:23:33 -04:00
|
|
|
#!/usr/bin/env bash
|
2012-08-31 02:23:41 -04:00
|
|
|
set -e
|
2013-01-18 04:57:08 -05:00
|
|
|
[ -n "\$PYENV_DEBUG" ] && set -x
|
|
|
|
|
|
|
|
program="\${0##*/}"
|
|
|
|
|
2012-08-31 02:23:41 -04:00
|
|
|
export PYENV_ROOT="$PYENV_ROOT"
|
2013-01-18 04:57:08 -05:00
|
|
|
exec "$(command -v pyenv)" exec "\$program" "\$@"
|
2012-08-31 02:23:41 -04:00
|
|
|
SH
|
|
|
|
chmod +x "$PROTOTYPE_SHIM_PATH"
|
|
|
|
}
|
|
|
|
|
2013-01-18 04:57:08 -05:00
|
|
|
# If the contents of the prototype shim file differ from the contents
|
2013-02-06 02:05:29 -05:00
|
|
|
# of the first shim in the shims directory, assume pyenv has been
|
2013-01-18 04:57:08 -05:00
|
|
|
# upgraded and the existing shims need to be removed.
|
|
|
|
remove_outdated_shims() {
|
2014-11-30 10:20:53 -05:00
|
|
|
local shim
|
|
|
|
for shim in "$SHIM_PATH"/*; do
|
2013-01-18 04:57:08 -05:00
|
|
|
if ! diff "$PROTOTYPE_SHIM_PATH" "$shim" >/dev/null 2>&1; then
|
2014-11-30 10:20:53 -05:00
|
|
|
rm -f "$SHIM_PATH"/*
|
2013-01-18 04:57:08 -05:00
|
|
|
fi
|
|
|
|
break
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2014-11-30 10:20:53 -05:00
|
|
|
# List basenames of executables for every Python version
|
|
|
|
list_executable_names() {
|
2015-10-27 05:49:29 -04:00
|
|
|
local version file
|
2015-11-20 23:13:52 -05:00
|
|
|
pyenv-versions --bare --skip-aliases | \
|
2017-06-23 18:12:09 -04:00
|
|
|
while read -r version; do
|
2015-11-20 23:13:52 -05:00
|
|
|
for file in "${PYENV_ROOT}/versions/${version}/bin/"*; do
|
2015-10-27 05:49:29 -04:00
|
|
|
echo "${file##*/}"
|
|
|
|
done
|
2014-11-30 10:20:53 -05:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2012-08-31 02:23:41 -04:00
|
|
|
# The basename of each argument passed to `make_shims` will be
|
|
|
|
# registered for installation as a shim. In this way, plugins may call
|
|
|
|
# `make_shims` with a glob to register many shims at once.
|
|
|
|
make_shims() {
|
2014-11-30 10:20:53 -05:00
|
|
|
local file shim
|
|
|
|
for file; do
|
|
|
|
shim="${file##*/}"
|
2012-08-31 02:23:41 -04:00
|
|
|
register_shim "$shim"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2020-12-05 07:36:52 -05:00
|
|
|
if ((${BASH_VERSINFO[0]} > 3)); then
|
2012-08-31 02:23:41 -04:00
|
|
|
|
2020-12-05 07:36:52 -05:00
|
|
|
declare -A registered_shims
|
2012-08-31 02:23:41 -04:00
|
|
|
|
2020-12-05 07:36:52 -05:00
|
|
|
# Registers the name of a shim to be generated.
|
|
|
|
register_shim() {
|
|
|
|
registered_shims["$1"]=1
|
|
|
|
}
|
2012-08-31 02:23:41 -04:00
|
|
|
|
2020-12-05 07:36:52 -05:00
|
|
|
# Install all shims registered via `make_shims` or `register_shim` directly.
|
|
|
|
install_registered_shims() {
|
|
|
|
local shim file
|
|
|
|
for shim in "${!registered_shims[@]}"; do
|
|
|
|
file="${SHIM_PATH}/${shim}"
|
|
|
|
[ -e "$file" ] || cp "$PROTOTYPE_SHIM_PATH" "$file"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# Once the registered shims have been installed, we make a second pass
|
|
|
|
# over the contents of the shims directory. Any file that is present
|
|
|
|
# in the directory but has not been registered as a shim should be
|
|
|
|
# removed.
|
|
|
|
remove_stale_shims() {
|
|
|
|
local shim
|
|
|
|
for shim in "$SHIM_PATH"/*; do
|
|
|
|
if [[ ! ${registered_shims["${shim##*/}"]} ]]; then
|
|
|
|
rm -f "$shim"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
else # Same for bash < 4.
|
|
|
|
|
|
|
|
registered_shims=" "
|
|
|
|
|
|
|
|
register_shim() {
|
|
|
|
registered_shims="${registered_shims}${1} "
|
|
|
|
}
|
|
|
|
|
|
|
|
install_registered_shims() {
|
|
|
|
local shim file
|
|
|
|
for shim in $registered_shims; do
|
|
|
|
file="${SHIM_PATH}/${shim}"
|
|
|
|
[ -e "$file" ] || cp "$PROTOTYPE_SHIM_PATH" "$file"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
remove_stale_shims() {
|
|
|
|
local shim
|
|
|
|
for shim in "$SHIM_PATH"/*; do
|
|
|
|
if [[ "$registered_shims" != *" ${shim##*/} "* ]]; then
|
|
|
|
rm -f "$shim"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
fi
|
2012-08-31 02:23:41 -04:00
|
|
|
|
2013-01-18 04:57:08 -05:00
|
|
|
shopt -s nullglob
|
2012-08-31 02:23:41 -04:00
|
|
|
|
2013-01-18 04:57:08 -05:00
|
|
|
# Create the prototype shim, then register shims for all known
|
|
|
|
# executables.
|
2012-08-31 02:23:41 -04:00
|
|
|
create_prototype_shim
|
2013-01-18 04:57:08 -05:00
|
|
|
remove_outdated_shims
|
2017-06-23 18:12:09 -04:00
|
|
|
# shellcheck disable=SC2046
|
2014-11-30 10:20:53 -05:00
|
|
|
make_shims $(list_executable_names | sort -u)
|
2012-08-31 02:23:41 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Allow plugins to register shims.
|
2014-01-02 08:26:22 -05:00
|
|
|
OLDIFS="$IFS"
|
|
|
|
IFS=$'\n' scripts=(`pyenv-hooks rehash`)
|
|
|
|
IFS="$OLDIFS"
|
|
|
|
|
|
|
|
for script in "${scripts[@]}"; do
|
2012-08-31 02:23:41 -04:00
|
|
|
source "$script"
|
|
|
|
done
|
|
|
|
|
|
|
|
install_registered_shims
|
|
|
|
remove_stale_shims
|