Parent directory

virtualenvwrapper.plugin.zsh

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