Created
March 30, 2018 11:27
-
-
Save drslump/e71f4108a30f9b14ff4d73905458049e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# Summary: Creates a temporary virtualenv based on a version | |
# | |
# Usage: pyenv COMMAND [version] [tmp-name] | |
# | |
# When no version is provided it'll use the currently configured | |
# one. | |
# | |
set -e | |
[ -n "$PYENV_DEBUG" ] && set -x | |
VERSION="$1" | |
NAME="$2" | |
[ -z "$VERSION" ] && VERSION=$( pyenv version-name ) | |
[ -z "$NAME" ] && NAME=$(mktemp -u "tmp-$VERSION-XXXX") | |
pyenv versions --bare | grep -qx "$VERSION" || { | |
>&2 echo "WARNING: triggering $VERSION install" | |
pyenv install "$VERSION" | |
} | |
>&2 echo "Creating temporary virtualenv '$NAME' (from $VERSION)..." | |
pyenv virtualenv "$VERSION" "$NAME" &> /dev/null || { | |
>&2 echo 'ERROR: Unable to create temporary virtualenv' | |
exit 1 | |
} | |
trap 'pyenv uninstall -f "$NAME"' EXIT | |
>&2 echo "Entering shell with virtualenv '$NAME' (removed on exit)" | |
PYENV_VERSION="$NAME" "$SHELL" | |
>&2 echo "Destroying temporary virtualenv '$NAME'..." | |
# the EXIT trap will uninstall it |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment