Forked from codysoyland/virtualenv-auto-activate.sh
Last active
December 5, 2016 14:15
-
-
Save RafaAguilar/868a687d2cc5f904f36b2122deb5c95b to your computer and use it in GitHub Desktop.
virtualenv-auto-activate
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 | |
# Based on virtualenv-auto-activate.sh | |
# Source: https://gist.github.com/codysoyland/2198913 | |
# | |
# Installation: | |
# Add this line to your .bashrc or .bash-profile: | |
# | |
# source /path/to/virtualenv-auto-activate.sh | |
# | |
# As a difference with the original, this script will look | |
# the virtualen in a sibling folder of the project's folder: | |
# For example: | |
# project3 | |
# ├── .git | |
# │ ├── HEAD | |
# │ ├── config | |
# │ ├── description | |
# │ ├── hooks | |
# │ ├── info | |
# │ ├── objects | |
# │ └── refs | |
# virtualenvs | |
# ├── project1 | |
# ├── project2 | |
# └── project3 | |
# ├── bin | |
# ├── include | |
# └── lib | |
# | |
# The virtualenv will be activated automatically when you enter the directory, | |
# and it will deactivate when you go on a upper directory from project's root dir. | |
contains() { | |
root_path="$PROJECT_PATH" | |
current_path="`pwd`" | |
if echo "$current_path" | grep -q "$root_path" | |
then | |
: | |
else | |
echo Deactivating virtualenv | |
deactivate | |
fi | |
} | |
_virtualenv_auto_activate() { | |
VIRTUALENVS_FOLDERS_NAME='virtualenvs' | |
PROJECT_ROOT_PATH=$(dirname `pwd`) | |
PROJECT_NAME=$(basename `pwd`) | |
if [ -e ".git" ]; then | |
# Check to see if already activated to avoid redundant activating | |
if [ "$VIRTUAL_ENV" != "$PROJECT_ROOT_PATH/$VIRTUALENVS_FOLDERS_NAME/$PROJECT_NAME" ]; then | |
echo Activating virtualenv \"$PROJECT_NAME\"... | |
source $PROJECT_ROOT_PATH/$VIRTUALENVS_FOLDERS_NAME/$PROJECT_NAME/bin/activate | |
export PROJECT_PATH=`echo $PROJECT_ROOT_PATH/$PROJECT_NAME` | |
fi | |
else | |
if [ -z ${VIRTUAL_ENV+x} ] | |
then | |
: | |
else | |
contains | |
fi | |
fi | |
} | |
export PROMPT_COMMAND=_virtualenv_auto_activate | |
# precmd() { eval "$PROMPT_COMMAND" } # Uncomment to use it with ZSH |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It supports auto-deactivate from revision #6