virtualenvwrapper.plugin.zsh 3.95 KB
Newer Older
1
virtualenvwrapper='virtualenvwrapper.sh'
2
virtualenvwrapper_lazy='virtualenvwrapper_lazy.sh'
3

4
5
6
7
8
9
if (( $+commands[$virtualenvwrapper_lazy] )); then
  function {
    setopt local_options
    unsetopt equals
    virtualenvwrapper=${${virtualenvwrapper_lazy}:c}
    source ${${virtualenvwrapper_lazy}:c}
10
    [[ -z "$WORKON_HOME" ]] && WORKON_HOME="$HOME/.virtualenvs"
11
12
  }
elif (( $+commands[$virtualenvwrapper] )); then
13
14
15
16
17
  function {
    setopt local_options
    unsetopt equals
    source ${${virtualenvwrapper}:c}
  }
18
19
20
21
22
23
24
elif [[ -f "/usr/local/bin/virtualenvwrapper.sh" ]]; then
  function {
    setopt local_options
    unsetopt equals
    virtualenvwrapper="/usr/local/bin/virtualenvwrapper.sh"
    source "/usr/local/bin/virtualenvwrapper.sh"
  }
25
26
27
28
29
30
31
elif [[ -f "/usr/share/virtualenvwrapper/virtualenvwrapper.sh" ]]; then
  function {
    setopt local_options
    unsetopt equals
    virtualenvwrapper="/usr/share/virtualenvwrapper/virtualenvwrapper.sh"
    source "/usr/share/virtualenvwrapper/virtualenvwrapper.sh"
  }
32
elif [[ -f "/etc/bash_completion.d/virtualenvwrapper" ]]; then
33
34
35
36
37
38
  function {
    setopt local_options
    unsetopt equals
    virtualenvwrapper="/etc/bash_completion.d/virtualenvwrapper"
    source "/etc/bash_completion.d/virtualenvwrapper"
  }
39
else
40
  print "[oh-my-zsh] virtualenvwrapper plugin: Cannot find ${virtualenvwrapper}.\n"\
41
42
43
44
        "Please install with \`pip install virtualenvwrapper\`" >&2
  return
fi
if ! type workon &>/dev/null; then
45
  print "[oh-my-zsh] virtualenvwrapper plugin: shell function 'workon' not defined.\n"\
46
        "Please check ${virtualenvwrapper}" >&2
47
48
  return
fi
49

50
if [[ "$WORKON_HOME" == "" ]]; then
51
  print "[oh-my-zsh] \$WORKON_HOME is not defined so plugin virtualenvwrapper will not work" >&2
52
53
  return
fi
54

55
if [[ ! $DISABLE_VENV_CD -eq 1 ]]; then
56
  # Automatically activate Git projects or other customized virtualenvwrapper projects based on the
57
  # directory name of the project. Virtual environment name can be overridden
58
  # by placing a .venv file in the project root with a virtualenv name in it.
59
  function workon_cwd {
60
61
    if [[ -z "$WORKON_CWD" ]]; then
      local WORKON_CWD=1
62
      # Check if this is a Git repo
63
      local GIT_REPO_ROOT=""
64
      local GIT_TOPLEVEL="$(git rev-parse --show-toplevel 2> /dev/null)"
65
66
67
      if [[ $? == 0 ]]; then
        GIT_REPO_ROOT="$GIT_TOPLEVEL"
      fi
68
      # Get absolute path, resolving symlinks
69
70
71
      local PROJECT_ROOT="${PWD:A}"
      while [[ "$PROJECT_ROOT" != "/" && ! -e "$PROJECT_ROOT/.venv" \
               && ! -d "$PROJECT_ROOT/.git"  && "$PROJECT_ROOT" != "$GIT_REPO_ROOT" ]]; do
72
        PROJECT_ROOT="${PROJECT_ROOT:h}"
73
74
      done
      if [[ "$PROJECT_ROOT" == "/" ]]; then
75
76
77
78
        PROJECT_ROOT="."
      fi
      # Check for virtualenv name override
      if [[ -f "$PROJECT_ROOT/.venv" ]]; then
79
        ENV_NAME="$(cat "$PROJECT_ROOT/.venv")"
80
81
82
      elif [[ -f "$PROJECT_ROOT/.venv/bin/activate" ]];then
        ENV_NAME="$PROJECT_ROOT/.venv"
      elif [[ "$PROJECT_ROOT" != "." ]]; then
83
        ENV_NAME="${PROJECT_ROOT:t}"
84
      else
85
86
        ENV_NAME=""
      fi
87
88
89
90
91
92
      
      if [[ -n $CD_VIRTUAL_ENV && "$ENV_NAME" != "$CD_VIRTUAL_ENV" ]]; then
        # We've just left the repo, deactivate the environment
        # Note: this only happens if the virtualenv was activated automatically
        deactivate && unset CD_VIRTUAL_ENV
      fi
93
94
95
96
97
98
99
100
101
      if [[ "$ENV_NAME" != "" ]]; then
        # Activate the environment only if it is not already active
        if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then
          if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then
            workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
          elif [[ -e "$ENV_NAME/bin/activate" ]]; then
            source $ENV_NAME/bin/activate && export CD_VIRTUAL_ENV="$ENV_NAME"
          fi
        fi
102
      fi
103
    fi
104
105
106
107
  }

  # Append workon_cwd to the chpwd_functions array, so it will be called on cd
  # http://zsh.sourceforge.net/Doc/Release/Functions.html
108
109
  autoload -U add-zsh-hook
  add-zsh-hook chpwd workon_cwd
110
fi