Skip to content

Commit 4f964c7

Browse files
committed
experimental version of deactivate wrapper
1 parent 98d22ca commit 4f964c7

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

README

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ For more details, refer to the column published in the May 2008 issue of Python
130130
Updates
131131
=======
132132

133+
Upcoming
134+
- Wrap the virtualenv version of deactivate() with one that lets us invoke
135+
the predeactivate hooks.
136+
133137
1.13
134138
- Fix issue #5 by correctly handling symlinks and limiting the list of envs to things
135139
that look like they can be activated.

test.sh

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,21 @@ mkdir -p $WORKON_HOME
77

88
function mk_test_hook () {
99
hookname="$1"
10-
echo "echo \"$hookname\" \$@" > $WORKON_HOME/$hookname
10+
echo "echo GLOBAL \"$hookname\" \$@" > $WORKON_HOME/$hookname
1111
chmod +x $WORKON_HOME/$hookname
1212
}
1313

1414
mk_test_hook premkvirtualenv
1515
mk_test_hook postmkvirtualenv
16+
1617
mk_test_hook prermvirtualenv
1718
mk_test_hook postrmvirtualenv
19+
1820
mk_test_hook postactivate
1921

22+
mk_test_hook predeactivate
23+
mk_test_hook postdeactivate
24+
2025
echo
2126
echo "HOOKS:"
2227
ls -l $WORKON_HOME
@@ -49,15 +54,18 @@ virtualenvwrapper_verify_active_environment && echo "PASS" || echo "FAIL"
4954

5055
echo
5156
echo "POSTACTIVATE HOOK"
52-
echo "echo postactivate" > $WORKON_HOME/env1/bin/postactivate
57+
echo "echo ENV postactivate" > "$WORKON_HOME/env1/bin/postactivate"
5358
workon env1
5459
echo -n "virtualenvwrapper_verify_active_environment: "
5560
virtualenvwrapper_verify_active_environment && echo "PASS" || echo "FAIL"
5661

5762
echo
5863
echo "DEACTIVATING"
64+
echo "before VIRTUAL_ENV: $VIRTUAL_ENV"
65+
echo "echo ENV predeactivate \$@" > "$WORKON_HOME/env1/bin/predeactivate"
66+
echo "echo ENV postdeactivate \$@" > "$WORKON_HOME/env1/bin/postdeactivate"
5967
deactivate
60-
echo "VIRTUAL_ENV: $VIRTUAL_ENV"
68+
echo "after VIRTUAL_ENV: $VIRTUAL_ENV"
6169
echo "virtualenvwrapper_verify_active_environment: "
6270
virtualenvwrapper_verify_active_environment && echo "FAIL" || echo "PASS"
6371

@@ -81,6 +89,10 @@ echo "REMOVING ENVIRONMENTS"
8189
rmvirtualenv "env1"
8290
rmvirtualenv "env2"
8391

92+
echo
93+
echo "REMOVING MISSING ENVIRONMENT"
94+
rmvirtualenv "env1"
95+
8496
rm -rf $WORKON_HOME
8597

8698
echo

virtualenvwrapper_bashrc

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ then
2727
export WORKON_HOME="$HOME/.virtualenvs"
2828
fi
2929

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+
3041
# Verify that the WORKON_HOME directory exists
3142
function virtualenvwrapper_verify_workon_home () {
3243
if [ ! -d "$WORKON_HOME" ]
@@ -140,17 +151,47 @@ function workon () {
140151
return 1
141152
fi
142153

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
144162

145163
source "$activate"
146164

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+
147187
virtualenvwrapper_source_hook "$WORKON_HOME/postactivate"
148188

149189
virtualenvwrapper_source_hook "$VIRTUAL_ENV/bin/postactivate"
150190

151191
return 0
152192
}
153193

194+
154195
#
155196
# Set up tab completion. (Adapted from Arthur Koziel's version at
156197
# http://arthurkoziel.com/2008/10/11/virtualenvwrapper-bash-completion/)

0 commit comments

Comments
 (0)