Skip to content

Commit 8fa939f

Browse files
committed
automatically create hook scripts
1 parent e35688f commit 8fa939f

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

docsource/history.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
Release History
33
===============
44

5+
1.22
6+
7+
- Automatically create any missing hook scripts as stubs with
8+
comments to expose the feature in case users are not aware of it.
9+
510
1.21
611

712
- Better protection of ``$WORKON_HOME`` does not exist when the wrapper script is sourced.

tests/test.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ setUp () {
2424
test_mkvirtualenv() {
2525
mkvirtualenv "env1"
2626
assertTrue "Environment directory was not created" "[ -d $WORKON_HOME/env1 ]"
27+
for hook in postactivate predeactivate postdeactivate
28+
do
29+
assertTrue "env1 $hook was not created" "[ -f $WORKON_HOME/env1/bin/$hook ]"
30+
assertTrue "env1 $hook is not executable" "[ -x $WORKON_HOME/env1/bin/$hook ]"
31+
done
32+
}
33+
34+
test_virtualenvwrapper_initialize() {
35+
virtualenvwrapper_initialize
36+
for hook in premkvirtualenv postmkvirtualenv prermvirtualenv postrmvirtualenv preactivate postactivate predeactivate postdeactivate
37+
do
38+
assertTrue "Global $hook was not created" "[ -f $WORKON_HOME/$hook ]"
39+
assertTrue "Global $hook is not executable" "[ -x $WORKON_HOME/$hook ]"
40+
done
2741
}
2842

2943
test_get_python_version() {

virtualenvwrapper_bashrc

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,54 @@ function virtualenvwrapper_verify_workon_home () {
4545
return 0
4646
}
4747

48-
virtualenvwrapper_verify_workon_home
48+
# Create a hook script
49+
#
50+
# Usage: virtualenvwrapper_make_hook filename comment
51+
#
52+
function virtualenvwrapper_make_hook () {
53+
filename="$1"
54+
comment="$2"
55+
if [ ! -f "$filename" ]
56+
then
57+
#echo "Creating $filename"
58+
cat - > "$filename" <<EOF
59+
#!/bin/sh
60+
# $comment
61+
62+
EOF
63+
fi
64+
if [ ! -x "$filename" ]
65+
then
66+
chmod +x "$filename"
67+
fi
68+
}
69+
70+
# Set up virtualenvwrapper properly
71+
function virtualenvwrapper_initialize () {
72+
virtualenvwrapper_verify_workon_home || return 1
73+
# mkvirtualenv
74+
virtualenvwrapper_make_hook "$WORKON_HOME/premkvirtualenv" \
75+
"This hook is run after a new virtualenv is created and before it is activated."
76+
virtualenvwrapper_make_hook "$WORKON_HOME/postmkvirtualenv" \
77+
"This hook is run after a new virtualenv is activated."
78+
# rmvirtualenv
79+
virtualenvwrapper_make_hook "$WORKON_HOME/prermvirtualenv" \
80+
"This hook is run before a virtualenv is deleted."
81+
virtualenvwrapper_make_hook "$WORKON_HOME/postrmvirtualenv" \
82+
"This hook is run after a virtualenv is deleted."
83+
# deactivate
84+
virtualenvwrapper_make_hook "$WORKON_HOME/predeactivate" \
85+
"This hook is run before every virtualenv is deactivated."
86+
virtualenvwrapper_make_hook "$WORKON_HOME/postdeactivate" \
87+
"This hook is run after every virtualenv is deactivated."
88+
# activate
89+
virtualenvwrapper_make_hook "$WORKON_HOME/preactivate" \
90+
"This hook is run before every virtualenv is activated."
91+
virtualenvwrapper_make_hook "$WORKON_HOME/postactivate" \
92+
"This hook is run after every virtualenv is activated."
93+
}
94+
95+
virtualenvwrapper_initialize
4996

5097
# Verify that virtualenv is installed and visible
5198
function virtualenvwrapper_verify_virtualenv () {
@@ -120,8 +167,13 @@ function mkvirtualenv () {
120167
# the environment won't exist. Use that to tell whether
121168
# we should switch to the environment and run the hook.
122169
[ ! -d "$WORKON_HOME/$envname" ] && return 0
170+
# Create stubs for the environment-specific hook scripts.
171+
virtualenvwrapper_make_hook "$WORKON_HOME/$envname/bin/postactivate" "This hook is sourced after the virtualenv is activated."
172+
virtualenvwrapper_make_hook "$WORKON_HOME/$envname/bin/predeactivate" "This hook is sourced before the virtualenv is deactivated."
173+
virtualenvwrapper_make_hook "$WORKON_HOME/$envname/bin/postdeactivate" "This hook is sourced after the virtualenv is deactivated."
174+
# Now activate the new environment
123175
workon "$envname"
124-
virtualenvwrapper_source_hook "$WORKON_HOME/postmkvirtualenv"
176+
virtualenvwrapper_source_hook "$WORKON_HOME/$envname/postmkvirtualenv"
125177
}
126178

127179
# Remove an environment, in the WORKON_HOME.

0 commit comments

Comments
 (0)