From 85959c735ddab2fcf70c01a25b01c4440e7179b4 Mon Sep 17 00:00:00 2001 From: Yamashita Yuu Date: Fri, 17 Jan 2014 20:17:15 +0900 Subject: [PATCH] Add sh commands to activate/deactivate virtualenv into current shell --- bin/pyenv-sh-activate | 48 +++++++++++++++++++++++++++++++++++++++++ bin/pyenv-sh-deactivate | 17 +++++++++++++++ 2 files changed, 65 insertions(+) create mode 100755 bin/pyenv-sh-activate create mode 100755 bin/pyenv-sh-deactivate diff --git a/bin/pyenv-sh-activate b/bin/pyenv-sh-activate new file mode 100755 index 0000000..dff7e79 --- /dev/null +++ b/bin/pyenv-sh-activate @@ -0,0 +1,48 @@ +#!/usr/bin/env bash +# +# Summary: Activate virtual environment +# +# Usage: pyenv activate +# pyenv activate --unset +# +# Activate a Python virtualenv environment in current shell. +# This acts almost as same as `pyenv shell`, but this invokes the `activate` +# script in your shell. +# +# should be a string matching a Python version known to pyenv. + +set -e +[ -n "$PYENV_DEBUG" ] && set -x + +# Provide pyenv completions +if [ "$1" = "--complete" ]; then + echo --unset + exec pyenv-virtualenvs --bare +fi + +versions=("$@") +shell="$(basename "${PYENV_SHELL:-$SHELL}")" + +if [ -z "$versions" ]; then + OLDIFS="$IFS" + IFS=: versions=($(pyenv-version-name)) + IFS="$OLDIFS" +fi + +if [ "$1" = "--unset" ]; then + echo "pyenv deactivate" + exit +fi + +if [ "${#versions[@]}" -gt 1 ]; then + echo "pyenv-virtualenv: cannot activate multiple versions at once: ${versions[@]}" 1>&2 + exit 1 +fi + +pyenv-virtualenv-prefix "${versions}" + +echo "pyenv shell \"${versions}\"" +case "$shell" in +fish ) echo ". \"$(pyenv-prefix "${versions}")/bin/activate.fish\"" ;; +* ) echo "source \"$(pyenv-prefix "${versions}")/bin/activate\"" ;; +esac diff --git a/bin/pyenv-sh-deactivate b/bin/pyenv-sh-deactivate new file mode 100755 index 0000000..0bf0cb5 --- /dev/null +++ b/bin/pyenv-sh-deactivate @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +# +# Summary: Deactivate virtual environment +# +# Usage: pyenv deactivate +# +# Deactivate a Python virtual environment. + +set -e +[ -n "$PYENV_DEBUG" ] && set -x + +shell="$(basename "${PYENV_SHELL:-$SHELL}")" +case "$shell" in +fish ) echo "functions -q deactivate; and deactivate";; +* ) echo "declare -f deactivate && deactivate";; +esac +echo "pyenv shell --unset"