Skip to content

Instantly share code, notes, and snippets.

@philpennock
Created September 12, 2016 17:17
Show Gist options
  • Select an option

  • Save philpennock/82b52a5be6e63a5afa7beec8959797b8 to your computer and use it in GitHub Desktop.

Select an option

Save philpennock/82b52a5be6e63a5afa7beec8959797b8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env zsh -f
#
# Wrap the execution of a command which is installed via a Python
# pyenv/virtualenv so that we can invoke it like a normal command, with
# python/env set up correctly for us. Assumes that the command uses a
# virtualenv named for the command, and that one common python version
# is being used for all such commands.
set -e
# -u will fail with _OLD_VIRTUAL_PATH unset
# don't expect these to be set; they're not exported by us and won't nest
# cleanly, but hey they're available for an emergency, I guess
: "${shimmed_python_version:=2.7.12}"
: "${shimmed_virtualenv:=${0:t}}"
: "${shimmed_command:=${0:t}}"
# This will fail if the shimmed command calls another command which calls the
# shimmed command again, but only if the $PATH is reset somewhere in the
# middle.
#
varname="SHIM_LOOP_DETECTION_${0:t}"
if [[ -n ${(P)varname:-} ]]; then
printf >&2 "%s: %s\n" "${0:t}" "aborting for invocation loop via $varname"
exit 1
fi
export ${varname}=true
export PYENV_VIRTUALENV_DISABLE_PROMPT=1
eval "$(pyenv sh-shell "$shimmed_python_version")"
eval "$(pyenv sh-activate "$shimmed_virtualenv")"
path[(r)${0:h}]=()
exec "$shimmed_command" "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment