|
27 | 27 | export WORKON_HOME="$HOME/.virtualenvs" |
28 | 28 | fi |
29 | 29 |
|
| 30 | +# Normalize the directory name in case it includes |
| 31 | +# relative path components. |
| 32 | +WORKON_HOME=$(cd "$WORKON_HOME"; pwd) |
| 33 | +export WORKON_HOME |
| 34 | + |
| 35 | +VIRTUALENV_WRAPPER_BIN=$(dirname "$0") |
| 36 | +if [ "$VIRTUALENV_WRAPPER_BIN" = "." ] |
| 37 | +then |
| 38 | + VIRTUALENV_WRAPPER_BIN=$(pwd) |
| 39 | +fi |
| 40 | + |
30 | 41 | # Verify that the WORKON_HOME directory exists |
31 | 42 | function virtualenvwrapper_verify_workon_home () { |
32 | 43 | if [ ! -d "$WORKON_HOME" ] |
@@ -140,17 +151,47 @@ function workon () { |
140 | 151 | return 1 |
141 | 152 | fi |
142 | 153 |
|
143 | | - virtualenvwrapper_source_hook "$VIRTUAL_ENV/bin/predeactivate" |
| 154 | + # Deactivate any current environment "destructively" |
| 155 | + # before switching so we use our override function, |
| 156 | + # if it exists. |
| 157 | + type deactivate >/dev/null 2>&1 |
| 158 | + if [ $? -eq 0 ] |
| 159 | + then |
| 160 | + deactivate |
| 161 | + fi |
144 | 162 |
|
145 | 163 | source "$activate" |
146 | 164 |
|
| 165 | + # Save the deactivate function from virtualenv |
| 166 | + virtualenvwrapper_saved_deactivate=$(typeset -f deactivate) |
| 167 | + |
| 168 | + # Replace the deactivate() function with a wrapper. |
| 169 | + eval 'function deactivate () { |
| 170 | + # Call the local hook before the global so we can undo |
| 171 | + # any settings made by the local postactivate first. |
| 172 | + virtualenvwrapper_source_hook "$VIRTUAL_ENV/bin/predeactivate" |
| 173 | + virtualenvwrapper_source_hook "$WORKON_HOME/predeactivate" |
| 174 | + |
| 175 | + env_postdeactivate_hook="$VIRTUAL_ENV/bin/postdeactivate" |
| 176 | + |
| 177 | + # Restore the original definition of deactivate |
| 178 | + eval "$virtualenvwrapper_saved_deactivate" |
| 179 | + |
| 180 | + # Instead of recursing, this calls the now restored original function. |
| 181 | + deactivate |
| 182 | + |
| 183 | + virtualenvwrapper_source_hook "$env_postdeactivate_hook" |
| 184 | + virtualenvwrapper_source_hook "$WORKON_HOME/postdeactivate" |
| 185 | + }' |
| 186 | + |
147 | 187 | virtualenvwrapper_source_hook "$WORKON_HOME/postactivate" |
148 | 188 |
|
149 | 189 | virtualenvwrapper_source_hook "$VIRTUAL_ENV/bin/postactivate" |
150 | 190 |
|
151 | 191 | return 0 |
152 | 192 | } |
153 | 193 |
|
| 194 | + |
154 | 195 | # |
155 | 196 | # Set up tab completion. (Adapted from Arthur Koziel's version at |
156 | 197 | # http://arthurkoziel.com/2008/10/11/virtualenvwrapper-bash-completion/) |
|
0 commit comments