Doug Hellmann avatar Doug Hellmann committed eddb292

automatically create hook scripts

Comments (0)

Files changed (3)

docsource/history.rst

 Release History
 ===============
 
+1.22
+
+  - Automatically create any missing hook scripts as stubs with
+    comments to expose the feature in case users are not aware of it.
+
 1.21
 
   - Better protection of ``$WORKON_HOME`` does not exist when the wrapper script is sourced.
 test_mkvirtualenv() {
     mkvirtualenv "env1"
     assertTrue "Environment directory was not created" "[ -d $WORKON_HOME/env1 ]"
+    for hook in postactivate predeactivate postdeactivate
+    do
+        assertTrue "env1 $hook was not created" "[ -f $WORKON_HOME/env1/bin/$hook ]"
+        assertTrue "env1 $hook is not executable" "[ -x $WORKON_HOME/env1/bin/$hook ]"
+    done
+}
+
+test_virtualenvwrapper_initialize() {
+    virtualenvwrapper_initialize
+    for hook in premkvirtualenv postmkvirtualenv prermvirtualenv postrmvirtualenv preactivate postactivate predeactivate postdeactivate
+    do
+        assertTrue "Global $hook was not created" "[ -f $WORKON_HOME/$hook ]"
+        assertTrue "Global $hook is not executable" "[ -x $WORKON_HOME/$hook ]"
+    done
 }
 
 test_get_python_version() {

virtualenvwrapper_bashrc

     return 0
 }
 
-virtualenvwrapper_verify_workon_home
+# Create a hook script
+#
+# Usage: virtualenvwrapper_make_hook filename comment
+#
+function virtualenvwrapper_make_hook () {
+    filename="$1"
+    comment="$2"
+    if [ ! -f "$filename" ]
+    then
+        #echo "Creating $filename"
+        cat - > "$filename" <<EOF
+#!/bin/sh
+# $comment
+
+EOF
+    fi
+    if [ ! -x "$filename" ]
+    then
+        chmod +x "$filename"
+    fi
+}
+
+# Set up virtualenvwrapper properly
+function virtualenvwrapper_initialize () {
+    virtualenvwrapper_verify_workon_home || return 1
+    # mkvirtualenv
+    virtualenvwrapper_make_hook "$WORKON_HOME/premkvirtualenv" \
+        "This hook is run after a new virtualenv is created and before it is activated."
+    virtualenvwrapper_make_hook "$WORKON_HOME/postmkvirtualenv" \
+        "This hook is run after a new virtualenv is activated."
+    # rmvirtualenv
+    virtualenvwrapper_make_hook "$WORKON_HOME/prermvirtualenv" \
+        "This hook is run before a virtualenv is deleted."
+    virtualenvwrapper_make_hook "$WORKON_HOME/postrmvirtualenv" \
+        "This hook is run after a virtualenv is deleted."
+    # deactivate
+    virtualenvwrapper_make_hook "$WORKON_HOME/predeactivate" \
+        "This hook is run before every virtualenv is deactivated."
+    virtualenvwrapper_make_hook "$WORKON_HOME/postdeactivate" \
+        "This hook is run after every virtualenv is deactivated."
+    # activate
+    virtualenvwrapper_make_hook "$WORKON_HOME/preactivate" \
+        "This hook is run before every virtualenv is activated."
+    virtualenvwrapper_make_hook "$WORKON_HOME/postactivate" \
+        "This hook is run after every virtualenv is activated."
+}
+
+virtualenvwrapper_initialize
 
 # Verify that virtualenv is installed and visible
 function virtualenvwrapper_verify_virtualenv () {
     # the environment won't exist.  Use that to tell whether
     # we should switch to the environment and run the hook.
     [ ! -d "$WORKON_HOME/$envname" ] && return 0
+    # Create stubs for the environment-specific hook scripts.
+    virtualenvwrapper_make_hook "$WORKON_HOME/$envname/bin/postactivate" "This hook is sourced after the virtualenv is activated."
+    virtualenvwrapper_make_hook "$WORKON_HOME/$envname/bin/predeactivate" "This hook is sourced before the virtualenv is deactivated."
+    virtualenvwrapper_make_hook "$WORKON_HOME/$envname/bin/postdeactivate" "This hook is sourced after the virtualenv is deactivated."
+    # Now activate the new environment
     workon "$envname"
-    virtualenvwrapper_source_hook "$WORKON_HOME/postmkvirtualenv"
+    virtualenvwrapper_source_hook "$WORKON_HOME/$envname/postmkvirtualenv"
 }
 
 # Remove an environment, in the WORKON_HOME.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.