Merge pull request #1145 from pyenv/rehash-wait
Wait rehash until lock acquisition
This commit is contained in:
commit
df0437f111
@ -10,30 +10,51 @@ PROTOTYPE_SHIM_PATH="${SHIM_PATH}/.pyenv-shim"
|
|||||||
# Create the shims directory if it doesn't already exist.
|
# Create the shims directory if it doesn't already exist.
|
||||||
mkdir -p "$SHIM_PATH"
|
mkdir -p "$SHIM_PATH"
|
||||||
|
|
||||||
# Ensure only one instance of pyenv-rehash is running at a time by
|
acquire_lock() {
|
||||||
# setting the shell's `noclobber` option and attempting to write to
|
# Ensure only one instance of pyenv-rehash is running at a time by
|
||||||
# the prototype shim file. If the file already exists, print a warning
|
# setting the shell's `noclobber` option and attempting to write to
|
||||||
# to stderr and exit with a non-zero status.
|
# the prototype shim file. If the file already exists, print a warning
|
||||||
set -o noclobber
|
# to stderr and exit with a non-zero status.
|
||||||
{ echo > "$PROTOTYPE_SHIM_PATH"
|
local ret
|
||||||
} 2>| /dev/null ||
|
set -o noclobber
|
||||||
{ if [ -w "$SHIM_PATH" ]; then
|
echo > "$PROTOTYPE_SHIM_PATH" 2>| /dev/null || ret=1
|
||||||
echo "pyenv: cannot rehash: $PROTOTYPE_SHIM_PATH exists"
|
set +o noclobber
|
||||||
else
|
[ -z "${ret}" ]
|
||||||
echo "pyenv: cannot rehash: $SHIM_PATH isn't writable"
|
}
|
||||||
fi
|
|
||||||
exit 1
|
|
||||||
} >&2
|
|
||||||
set +o noclobber
|
|
||||||
|
|
||||||
# If we were able to obtain a lock, register a trap to clean up the
|
# If we were able to obtain a lock, register a trap to clean up the
|
||||||
# prototype shim when the process exits.
|
# prototype shim when the process exits.
|
||||||
trap remove_prototype_shim EXIT
|
trap release_lock EXIT
|
||||||
|
|
||||||
remove_prototype_shim() {
|
remove_prototype_shim() {
|
||||||
rm -f "$PROTOTYPE_SHIM_PATH"
|
rm -f "$PROTOTYPE_SHIM_PATH"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
release_lock() {
|
||||||
|
remove_prototype_shim
|
||||||
|
}
|
||||||
|
|
||||||
|
if [ ! -w "$SHIM_PATH" ]; then
|
||||||
|
echo "pyenv: cannot rehash: $SHIM_PATH isn't writable"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
unset acquired
|
||||||
|
for _ in $(seq "${PYENV_REHASH_TIMEOUT:-60}"); do
|
||||||
|
if acquire_lock 2>/dev/null; then
|
||||||
|
acquired=1
|
||||||
|
break
|
||||||
|
else
|
||||||
|
# POSIX sleep(1) doesn't provides time precision of subsecond
|
||||||
|
sleep 1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
if [ -z "${acquired}" ]; then
|
||||||
|
echo "pyenv: cannot rehash: $PROTOTYPE_SHIM_PATH exists"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
# The prototype shim file is a script that re-execs itself, passing
|
# The prototype shim file is a script that re-execs itself, passing
|
||||||
# its filename and any arguments to `pyenv exec`. This file is
|
# its filename and any arguments to `pyenv exec`. This file is
|
||||||
# hard-linked for every executable and then removed. The linking
|
# hard-linked for every executable and then removed. The linking
|
||||||
|
@ -25,12 +25,22 @@ create_executable() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@test "rehash in progress" {
|
@test "rehash in progress" {
|
||||||
|
export PYENV_REHASH_TIMEOUT=1
|
||||||
mkdir -p "${PYENV_ROOT}/shims"
|
mkdir -p "${PYENV_ROOT}/shims"
|
||||||
touch "${PYENV_ROOT}/shims/.pyenv-shim"
|
touch "${PYENV_ROOT}/shims/.pyenv-shim"
|
||||||
run pyenv-rehash
|
run pyenv-rehash
|
||||||
assert_failure "pyenv: cannot rehash: ${PYENV_ROOT}/shims/.pyenv-shim exists"
|
assert_failure "pyenv: cannot rehash: ${PYENV_ROOT}/shims/.pyenv-shim exists"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test "wait until lock acquisition" {
|
||||||
|
export PYENV_REHASH_TIMEOUT=5
|
||||||
|
mkdir -p "${PYENV_ROOT}/shims"
|
||||||
|
touch "${PYENV_ROOT}/shims/.pyenv-shim"
|
||||||
|
bash -c "sleep 1 && rm -f ${PYENV_ROOT}/shims/.pyenv-shim" &
|
||||||
|
run pyenv-rehash
|
||||||
|
assert_success
|
||||||
|
}
|
||||||
|
|
||||||
@test "creates shims" {
|
@test "creates shims" {
|
||||||
create_executable "2.7" "python"
|
create_executable "2.7" "python"
|
||||||
create_executable "2.7" "fab"
|
create_executable "2.7" "fab"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user