]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh
...
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / virtualenvwrapper / virtualenvwrapper.plugin.zsh
1 function {
2     # search in these locations for the init script:
3     for virtualenvwrapper in $commands[virtualenvwrapper_lazy.sh] \
4       $commands[virtualenvwrapper.sh] \
5       /usr/share/virtualenvwrapper/virtualenvwrapper{_lazy,}.sh \
6       /usr/local/bin/virtualenvwrapper{_lazy,}.sh \
7       /etc/bash_completion.d/virtualenvwrapper \
8       /usr/share/bash-completion/completions/virtualenvwrapper \
9       $HOME/.local/bin/virtualenvwrapper.sh
10     do
11         if [[ -f "$virtualenvwrapper" ]]; then
12             source "$virtualenvwrapper"
13             return
14         fi
15     done
16     print "[oh-my-zsh] virtualenvwrapper plugin: Cannot find virtualenvwrapper.sh.\n"\
17           "Please install with \`pip install virtualenvwrapper\`" >&2
18     return 1
19 }
20
21 if [[ $? -eq 0 ]] && ! type workon &>/dev/null; then
22   print "[oh-my-zsh] virtualenvwrapper plugin: shell function 'workon' not defined.\n"\
23         "Please check ${virtualenvwrapper}" >&2
24   return
25 fi
26
27 if [[ -z "$WORKON_HOME" ]]; then
28   WORKON_HOME="$HOME/.virtualenvs"
29 fi
30
31 if [[ ! $DISABLE_VENV_CD -eq 1 ]]; then
32   # Automatically activate Git projects or other customized virtualenvwrapper projects based on the
33   # directory name of the project. Virtual environment name can be overridden
34   # by placing a .venv file in the project root with a virtualenv name in it.
35   function workon_cwd {
36     if [[ -z "$WORKON_CWD" ]]; then
37       local WORKON_CWD=1
38       # Get absolute path, resolving symlinks
39       local PROJECT_ROOT="${PWD:A}"
40       while [[ "$PROJECT_ROOT" != "/" && ! -e "$PROJECT_ROOT/.venv" \
41           && ! -d "$PROJECT_ROOT/.git" ]]; do
42         PROJECT_ROOT="${PROJECT_ROOT:h}"
43       done
44
45       # Check for virtualenv name override
46       if [[ -f "$PROJECT_ROOT/.venv" ]]; then
47         ENV_NAME="$(cat "$PROJECT_ROOT/.venv")"
48       elif [[ -f "$PROJECT_ROOT/.venv/bin/activate" ]];then
49         ENV_NAME="$PROJECT_ROOT/.venv"
50       elif [[ "$PROJECT_ROOT" != "/" ]]; then
51         ENV_NAME="${PROJECT_ROOT:t}"
52       else
53         ENV_NAME=""
54       fi
55       
56       if [[ -n $CD_VIRTUAL_ENV && "$ENV_NAME" != "$CD_VIRTUAL_ENV" ]]; then
57         # We've just left the repo, deactivate the environment
58         # Note: this only happens if the virtualenv was activated automatically
59         if [[ -n "$VIRTUAL_ENV" ]]; then
60           # Only deactivate if VIRTUAL_ENV was set
61           # User may have deactivated manually or via another mechanism
62           deactivate
63         fi
64         # clean up regardless
65         unset CD_VIRTUAL_ENV
66       fi
67       if [[ "$ENV_NAME" != "" ]]; then
68         # Activate the environment only if it is not already active
69         if [[ ! "$VIRTUAL_ENV" -ef "$WORKON_HOME/$ENV_NAME" ]]; then
70           if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then
71             workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
72           elif [[ -e "$ENV_NAME/bin/activate" ]]; then
73             source $ENV_NAME/bin/activate && export CD_VIRTUAL_ENV="$ENV_NAME"
74           else
75             ENV_NAME=""
76           fi
77         fi
78       fi
79       if [[ "$ENV_NAME" == "" && -n $CD_VIRTUAL_ENV && -n $VIRTUAL_ENV ]]; then
80         # We've just left the repo, deactivate the environment
81         # Note: this only happens if the virtualenv was activated automatically
82         deactivate && unset CD_VIRTUAL_ENV
83       fi
84     fi
85   }
86
87   # Append workon_cwd to the chpwd_functions array, so it will be called on cd
88   # http://zsh.sourceforge.net/Doc/Release/Functions.html
89   autoload -U add-zsh-hook
90   add-zsh-hook chpwd workon_cwd
91 fi