virtualenvwrapper.plugin.zsh 3.69 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
elif [[ -f "/etc/bash_completion.d/virtualenvwrapper" ]]; then
26
27
28
29
30
31
  function {
    setopt local_options
    unsetopt equals
    virtualenvwrapper="/etc/bash_completion.d/virtualenvwrapper"
    source "/etc/bash_completion.d/virtualenvwrapper"
  }
32
else
33
  print "[oh-my-zsh] virtualenvwrapper plugin: Cannot find ${virtualenvwrapper}.\n"\
34
35
36
37
        "Please install with \`pip install virtualenvwrapper\`" >&2
  return
fi
if ! type workon &>/dev/null; then
38
  print "[oh-my-zsh] virtualenvwrapper plugin: shell function 'workon' not defined.\n"\
39
        "Please check ${virtualenvwrapper}" >&2
40
41
  return
fi
42

43
if [[ "$WORKON_HOME" == "" ]]; then
44
  print "[oh-my-zsh] \$WORKON_HOME is not defined so plugin virtualenvwrapper will not work" >&2
45
46
  return
fi
47

48
if [[ ! $DISABLE_VENV_CD -eq 1 ]]; then
49
  # Automatically activate Git projects or other customized virtualenvwrapper projects based on the
50
  # directory name of the project. Virtual environment name can be overridden
51
  # by placing a .venv file in the project root with a virtualenv name in it.
52
  function workon_cwd {
53
54
    if [[ -z "$WORKON_CWD" ]]; then
      local WORKON_CWD=1
55
      # Check if this is a Git repo
56
      local GIT_REPO_ROOT=""
57
      local GIT_TOPLEVEL="$(git rev-parse --show-toplevel 2> /dev/null)"
58
59
60
      if [[ $? == 0 ]]; then
        GIT_REPO_ROOT="$GIT_TOPLEVEL"
      fi
61
      # Get absolute path, resolving symlinks
62
63
64
      local PROJECT_ROOT="${PWD:A}"
      while [[ "$PROJECT_ROOT" != "/" && ! -e "$PROJECT_ROOT/.venv" \
               && ! -d "$PROJECT_ROOT/.git"  && "$PROJECT_ROOT" != "$GIT_REPO_ROOT" ]]; do
65
        PROJECT_ROOT="${PROJECT_ROOT:h}"
66
67
      done
      if [[ "$PROJECT_ROOT" == "/" ]]; then
68
69
70
71
        PROJECT_ROOT="."
      fi
      # Check for virtualenv name override
      if [[ -f "$PROJECT_ROOT/.venv" ]]; then
72
        ENV_NAME="$(cat "$PROJECT_ROOT/.venv")"
73
74
75
      elif [[ -f "$PROJECT_ROOT/.venv/bin/activate" ]];then
        ENV_NAME="$PROJECT_ROOT/.venv"
      elif [[ "$PROJECT_ROOT" != "." ]]; then
76
        ENV_NAME="${PROJECT_ROOT:t}"
77
      else
78
79
        ENV_NAME=""
      fi
80
81
82
83
84
85
      
      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
86
87
88
89
90
91
92
93
94
      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
95
      fi
96
    fi
97
98
99
100
  }

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