@@ -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
5198function 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