]> git.rmz.io Git - dotfiles.git/blob - zsh/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh
a5abb77c3f26a9e0ac6b664f483471090be602e9
[dotfiles.git] / zsh / plugins / virtualenvwrapper / virtualenvwrapper.plugin.zsh
1 wrapsource=`which virtualenvwrapper.sh`
2
3 if [[ -f "$wrapsource" ]]; then
4 source $wrapsource
5
6 if [[ ! $DISABLE_VENV_CD -eq 1 ]]; then
7 # Automatically activate Git projects' virtual environments based on the
8 # directory name of the project. Virtual environment name can be overridden
9 # by placing a .venv file in the project root with a virtualenv name in it
10 function workon_cwd {
11 # Check that this is a Git repo
12 PROJECT_ROOT=`git rev-parse --show-toplevel 2> /dev/null`
13 if (( $? == 0 )); then
14 # Check for virtualenv name override
15 ENV_NAME=`basename "$PROJECT_ROOT"`
16 if [[ -f "$PROJECT_ROOT/.venv" ]]; then
17 ENV_NAME=`cat "$PROJECT_ROOT/.venv"`
18 fi
19 # Activate the environment only if it is not already active
20 if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then
21 if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then
22 workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
23 fi
24 fi
25 elif [ $CD_VIRTUAL_ENV ]; then
26 # We've just left the repo, deactivate the environment
27 # Note: this only happens if the virtualenv was activated automatically
28 deactivate && unset CD_VIRTUAL_ENV
29 fi
30 unset PROJECT_ROOT
31 }
32
33 # New cd function that does the virtualenv magic
34 function cd {
35 builtin cd "$@" && workon_cwd
36 }
37 fi
38 else
39 print "zsh virtualenvwrapper plugin: Cannot find virtualenvwrapper.sh. Please install with \`pip install virtualenvwrapper\`."
40 fi