| 1 | #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash |
| 2 | # Summary: Rehash pyenv shims (run this after installing executables) |
| 3 | |
| 4 | set -e |
| 5 | [[ -n "$PYENV_DEBUG" ]] && set -x |
| 6 | |
| 7 | SHIM_PATH="${PYENV_ROOT}/shims" |
| 8 | PROTOTYPE_SHIM_PATH="${SHIM_PATH}/.pyenv-shim" |
| 9 | |
| 10 | # Create the shims directory if it doesn't already exist. |
| 11 | mkdirscip-shell system . . mkdir. -p "$SHIM_PATH" |
| 12 | |
| 13 | declare last_acquire_error |
| 14 | |
| 15 | acquire_lockscip-shell shell shell-project 0.0.0 acquire_lock().() { |
| 16 | # Ensure only one instance of pyenv-rehash is running at a time by |
| 17 | # setting the shell's `noclobber` option and attempting to write to |
| 18 | # the prototype shim file. |
| 19 | local ret |
| 20 | set -o noclobber |
| 21 | # Assuming an old lockfile is stale |
| 22 | # Unknown why this happens for some users but this at least unblocks them |
| 23 | last_acquire_error="$( { ( echoscip-shell system . . echo. -n > "$PROTOTYPE_SHIM_PATH"; ) 2>&1 1>&3 3>&1-; } 3>&1)" \ |
| 24 | || { findscip-shell system . . find. "$PROTOTYPE_SHIM_PATH" -mmin +10 -exec rm -f {} \; ; ret=1; } |
| 25 | set +o noclobber |
| 26 | [[ -z "${ret}" ]] |
| 27 | } |
| 28 | |
| 29 | remove_prototype_shimscip-shell shell shell-project 0.0.0 remove_prototype_shim().() { |
| 30 | rmscip-shell system . . rm. -f "$PROTOTYPE_SHIM_PATH" |
| 31 | } |
| 32 | |
| 33 | release_lockscip-shell shell shell-project 0.0.0 release_lock().() { |
| 34 | remove_prototype_shimscip-shell shell shell-project 0.0.0 remove_prototype_shim(). |
| 35 | } |
| 36 | |
| 37 | if [scip-shell system . . `[`. ! -w "$SHIM_PATH" ]; then |
| 38 | echoscip-shell system . . echo. "pyenv: cannot rehash: $SHIM_PATH isn't writable" >&2 |
| 39 | exit 1 |
| 40 | fi |
| 41 | |
| 42 | declare acquired tested_for_other_write_errors |
| 43 | declare start=$SECONDS |
| 44 | PYENV_REHASH_TIMEOUT=${PYENV_REHASH_TIMEOUT:-60} |
| 45 | while (( SECONDS <= start + PYENV_REHASH_TIMEOUT )); do |
| 46 | if acquire_lockscip-shell shell shell-project 0.0.0 acquire_lock().; then |
| 47 | acquired=1 |
| 48 | |
| 49 | # If we were able to obtain a lock, register a trap to clean up the |
| 50 | # prototype shim when the process exits. |
| 51 | trap release_lock EXIT |
| 52 | |
| 53 | break |
| 54 | else |
| 55 | #Landlock sandbox subsystem in the Linux kernel returns false information in access() as of 6.14.0, |
| 56 | # making -w "$SHIM_PATH" not catch the fact that the shims dir is not writable in this case. |
| 57 | #Bash doesn't provide access to errno to check for non-EEXIST error code in acquire_lock. |
| 58 | #So check for writablity by trying to write to a different file, |
| 59 | # in a way that taxes the usual use case as little as possible. |
| 60 | if [[ -z $tested_for_other_write_errors ]]; then |
| 61 | ( t="$(TMPDIR="$SHIM_PATH" mktempscip-shell system . . mktemp.)" && rmscip-shell system . . rm. "$t" ) && tested_for_other_write_errors=1 || |
| 62 | { echoscip-shell system . . echo. "pyenv: cannot rehash: $SHIM_PATH isn't writable" >&2; break; } |
| 63 | fi |
| 64 | # POSIX sleep(1) doesn't provide subsecond precision, but many others do |
| 65 | sleepscip-shell system . . sleep. 0.1 2>/dev/nullscip-shell filesystem . . `/dev/null`. || sleepscip-shell system . . sleep. 1 |
| 66 | fi |
| 67 | done |
| 68 | |
| 69 | if [scip-shell system . . `[`. -z "${acquired}" ]; then |
| 70 | if [[ -n $tested_for_other_write_errors ]]; then |
| 71 | echoscip-shell system . . echo. "pyenv: cannot rehash: couldn't acquire lock"\ |
| 72 | "$PROTOTYPE_SHIM_PATH for $PYENV_REHASH_TIMEOUT seconds. Last error message:" >&2 |
| 73 | echoscip-shell system . . echo. "$last_acquire_error" >&2 |
| 74 | fi |
| 75 | exit 1 |
| 76 | fi |
| 77 | unset tested_for_other_write_errors |
| 78 | |
| 79 | # The prototype shim file is a script that re-execs itself, passing |
| 80 | # its filename and any arguments to `pyenv exec`. This file is |
| 81 | # hard-linked for every executable and then removed. The linking |
| 82 | # technique is fast, uses less disk space than unique files, and also |
| 83 | # serves as a locking mechanism. |
| 84 | create_prototype_shimscip-shell shell shell-project 0.0.0 create_prototype_shim().() { |
| 85 | catscip-shell system . . cat. > "$PROTOTYPE_SHIM_PATH" <<SH |
| 86 | #!/usr/bin/env bash |
| 87 | set -e |
| 88 | [ -n "\$PYENV_DEBUG" ] && set -x |
| 89 | |
| 90 | program="\${0##*/}" |
| 91 | |
| 92 | export PYENV_ROOT="$PYENV_ROOT" |
| 93 | SHIM_PATH=\${0%/*} |
| 94 | if [[ \$SHIM_PATH != "$PYENV_ROOT/shims" ]]; then |
| 95 | export _PYENV_SHIM_PATH="\$SHIM_PATH" |
| 96 | fi |
| 97 | exec "$(command -v pyenv)" exec "\$program" "\$@" |
| 98 | SH |
| 99 | chmodscip-shell system . . chmod. +x "$PROTOTYPE_SHIM_PATH" |
| 100 | } |
| 101 | |
| 102 | # If the contents of the prototype shim file differ from the contents |
| 103 | # of the first shim in the shims directory, assume pyenv has been |
| 104 | # upgraded and the existing shims need to be removed. |
| 105 | remove_outdated_shimsscip-shell shell shell-project 0.0.0 remove_outdated_shims().() { |
| 106 | local shim |
| 107 | for shim in "$SHIM_PATH"/*; do |
| 108 | if ! diffscip-shell system . . diff. "$PROTOTYPE_SHIM_PATH" "$shim" >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 109 | rmscip-shell system . . rm. -f "$SHIM_PATH"/* |
| 110 | fi |
| 111 | break |
| 112 | done |
| 113 | } |
| 114 | |
| 115 | # The basename of each argument passed to `make_shims` will be |
| 116 | # registered for installation as a shim. In this way, plugins may call |
| 117 | # `make_shims` with a glob to register many shims at once. |
| 118 | make_shimsscip-shell shell shell-project 0.0.0 make_shims().() { |
| 119 | local file shim |
| 120 | for file; do |
| 121 | shim="${file##*/}" |
| 122 | register_shimscip-shell shell shell-project 0.0.0 register_shim(). "$shim" |
| 123 | done |
| 124 | } |
| 125 | |
| 126 | if ((${BASH_VERSINFO[0]} > 3)); then |
| 127 | |
| 128 | declare -A registered_shims |
| 129 | |
| 130 | # Registers the name of a shim to be generated. |
| 131 | register_shimscip-shell shell shell-project 0.0.0 register_shim().() { |
| 132 | registered_shims["$1"]=1 |
| 133 | } |
| 134 | |
| 135 | # Install all shims registered via `make_shims` or `register_shim` directly. |
| 136 | install_registered_shimsscip-shell shell shell-project 0.0.0 install_registered_shims().() { |
| 137 | local shim file |
| 138 | for shim in "${!registered_shims[@]}"; do |
| 139 | file="${SHIM_PATH}/${shim}" |
| 140 | [scip-shell system . . `[`. -e "$file" ] || cpscip-shell system . . cp. "$PROTOTYPE_SHIM_PATH" "$file" |
| 141 | done |
| 142 | } |
| 143 | |
| 144 | # Once the registered shims have been installed, we make a second pass |
| 145 | # over the contents of the shims directory. Any file that is present |
| 146 | # in the directory but has not been registered as a shim should be |
| 147 | # removed. |
| 148 | remove_stale_shimsscip-shell shell shell-project 0.0.0 remove_stale_shims().() { |
| 149 | local shim |
| 150 | for shim in "$SHIM_PATH"/*; do |
| 151 | if [[ ! ${registered_shims["${shim##*/}"]} ]]; then |
| 152 | rmscip-shell system . . rm. -f "$shim" |
| 153 | fi |
| 154 | done |
| 155 | } |
| 156 | |
| 157 | else # Same for bash < 4. |
| 158 | |
| 159 | registered_shims=" " |
| 160 | |
| 161 | register_shimscip-shell shell shell-project 0.0.0 register_shim().() { |
| 162 | registered_shims="${registered_shims}${1} " |
| 163 | } |
| 164 | |
| 165 | install_registered_shimsscip-shell shell shell-project 0.0.0 install_registered_shims().() { |
| 166 | local shim file |
| 167 | for shim in $registered_shims; do |
| 168 | file="${SHIM_PATH}/${shim}" |
| 169 | [scip-shell system . . `[`. -e "$file" ] || cpscip-shell system . . cp. "$PROTOTYPE_SHIM_PATH" "$file" |
| 170 | done |
| 171 | } |
| 172 | |
| 173 | remove_stale_shimsscip-shell shell shell-project 0.0.0 remove_stale_shims().() { |
| 174 | local shim |
| 175 | for shim in "$SHIM_PATH"/*; do |
| 176 | if [[ "$registered_shims" != *" ${shim##*/} "* ]]; then |
| 177 | rmscip-shell system . . rm. -f "$shim" |
| 178 | fi |
| 179 | done |
| 180 | } |
| 181 | fi |
| 182 | |
| 183 | shopt -s nullglob |
| 184 | |
| 185 | # Create the prototype shim, then register shims for all known |
| 186 | # executables. |
| 187 | create_prototype_shimscip-shell shell shell-project 0.0.0 create_prototype_shim(). |
| 188 | remove_outdated_shimsscip-shell shell shell-project 0.0.0 remove_outdated_shims(). |
| 189 | # shellcheck disable=SC2046 |
| 190 | make_shimsscip-shell shell shell-project 0.0.0 make_shims(). $(pyenv-versions --executables) |
| 191 | |
| 192 | |
| 193 | # Allow plugins to register shims. |
| 194 | OLDIFS="$IFS" |
| 195 | IFS=$'\n' scripts=(`pyenv-hooks rehash`) |
| 196 | IFS="$OLDIFS" |
| 197 | |
| 198 | for script in "${scripts[@]}"; do |
| 199 | source "$script" |
| 200 | done |
| 201 | |
| 202 | install_registered_shimsscip-shell shell shell-project 0.0.0 install_registered_shims(). |
| 203 | remove_stale_shimsscip-shell shell shell-project 0.0.0 remove_stale_shims(). |