Forked from codysoyland/virtualenv-auto-activate.sh
Last active
August 31, 2016 19:11
-
-
Save kumichou/67ca589c54da6c9ae0fb to your computer and use it in GitHub Desktop.
Updating to deactivate virtualenv is we're not within a project
This file contains 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
#!/bin/bash | |
# virtualenv-auto-activate.sh | |
# | |
# Installation: | |
# Add this line to your .bashrc or .bash-profile: | |
# | |
# source /path/to/virtualenv-auto-activate.sh | |
# | |
# Go to your project folder, run "virtualenv .venv", so your project folder | |
# has a .venv folder at the top level, next to your version control directory. | |
# For example: | |
# . | |
# ├── .git | |
# │ ├── HEAD | |
# │ ├── config | |
# │ ├── description | |
# │ ├── hooks | |
# │ ├── info | |
# │ ├── objects | |
# │ └── refs | |
# └── .venv | |
# ├── bin | |
# ├── include | |
# └── lib | |
# | |
# The virtualenv will be activated automatically when you enter the directory. | |
upsearch () { | |
slashes=${PWD//[^\/]/} | |
directory="$PWD" | |
for (( n=${#slashes}; n>0; --n )); do | |
test -e "$directory/$1" && echo "$directory/$1" && return | |
directory="$directory/.." | |
done | |
echo $(pwd) | |
} | |
_virtualenv_auto_activate() { | |
if [ -e ".venv" ]; then | |
# Check to see if already activated to avoid redundant activating | |
if [ "$VIRTUAL_ENV" != "$(pwd -P)/.venv" ]; then | |
_VENV_NAME=$(basename `pwd`) | |
echo Activating virtualenv \"$_VENV_NAME\"... | |
VIRTUAL_ENV_DISABLE_PROMPT=1 | |
source .venv/bin/activate | |
_OLD_VIRTUAL_PS1="$PS1" | |
PS1="($_VENV_NAME) $PS1" | |
export PS1 | |
fi | |
else | |
if [ "$(upsearch ".venv")" == "$(pwd)" ] && (type "deactivate" > /dev/null 2>&1;) then | |
IFS='/' # We want to separate on / not <space><tab><newline> | |
VPATH=($VIRTUAL_ENV) | |
VNAME=${VPATH[${#VPATH[@]}-2]} # Get parent directory of Virtual Env | |
echo Deactivating virtualenv \"$VNAME\"... | |
deactivate | |
IFS=$' \t\n' # Reset IFS to system default | |
fi | |
fi | |
} | |
export PROMPT_COMMAND="_virtualenv_auto_activate; $PROMPT_COMMAND" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment