Doug Hellmann avatar Doug Hellmann committed 0a86a4f

experimental version of deactivate wrapper

Comments (0)

Files changed (3)

 Updates
 =======
 
+Upcoming
+  - Wrap the virtualenv version of deactivate() with one that lets us invoke
+    the predeactivate hooks.
+
 1.13
   - Fix issue #5 by correctly handling symlinks and limiting the list of envs to things 
     that look like they can be activated.
 
 function mk_test_hook () {
     hookname="$1"
-    echo "echo \"$hookname\" \$@" > $WORKON_HOME/$hookname
+    echo "echo GLOBAL \"$hookname\" \$@" > $WORKON_HOME/$hookname
     chmod +x $WORKON_HOME/$hookname
 }
 
 mk_test_hook premkvirtualenv
 mk_test_hook postmkvirtualenv
+
 mk_test_hook prermvirtualenv
 mk_test_hook postrmvirtualenv
+
 mk_test_hook postactivate
 
+mk_test_hook predeactivate
+mk_test_hook postdeactivate
+
 echo
 echo "HOOKS:"
 ls -l $WORKON_HOME
 
 echo
 echo "POSTACTIVATE HOOK"
-echo "echo postactivate" > $WORKON_HOME/env1/bin/postactivate
+echo "echo ENV postactivate" > "$WORKON_HOME/env1/bin/postactivate"
 workon env1
 echo -n "virtualenvwrapper_verify_active_environment: "
 virtualenvwrapper_verify_active_environment && echo "PASS" || echo "FAIL"
 
 echo
 echo "DEACTIVATING"
+echo "before VIRTUAL_ENV: $VIRTUAL_ENV"
+echo "echo ENV predeactivate \$@" > "$WORKON_HOME/env1/bin/predeactivate"
+echo "echo ENV postdeactivate \$@" > "$WORKON_HOME/env1/bin/postdeactivate"
 deactivate
-echo "VIRTUAL_ENV: $VIRTUAL_ENV"
+echo "after VIRTUAL_ENV: $VIRTUAL_ENV"
 echo "virtualenvwrapper_verify_active_environment: "
 virtualenvwrapper_verify_active_environment && echo "FAIL" || echo "PASS"
 
 rmvirtualenv "env1"
 rmvirtualenv "env2"
 
+echo
+echo "REMOVING MISSING ENVIRONMENT"
+rmvirtualenv "env1"
+
 rm -rf $WORKON_HOME
 
 echo

virtualenvwrapper_bashrc

     export WORKON_HOME="$HOME/.virtualenvs"
 fi
 
+# Normalize the directory name in case it includes 
+# relative path components.
+WORKON_HOME=$(cd "$WORKON_HOME"; pwd)
+export WORKON_HOME
+
+VIRTUALENV_WRAPPER_BIN=$(dirname "$0")
+if [ "$VIRTUALENV_WRAPPER_BIN" = "." ]
+then
+    VIRTUALENV_WRAPPER_BIN=$(pwd)
+fi
+
 # Verify that the WORKON_HOME directory exists
 function virtualenvwrapper_verify_workon_home () {
     if [ ! -d "$WORKON_HOME" ]
         return 1
     fi
     
-    virtualenvwrapper_source_hook "$VIRTUAL_ENV/bin/predeactivate"
+    # Deactivate any current environment "destructively"
+    # before switching so we use our override function,
+    # if it exists.
+    type deactivate >/dev/null 2>&1
+    if [ $? -eq 0 ]
+    then
+        deactivate
+    fi
     
     source "$activate"
     
+    # Save the deactivate function from virtualenv
+    virtualenvwrapper_saved_deactivate=$(typeset -f deactivate)
+
+    # Replace the deactivate() function with a wrapper.
+    eval 'function deactivate () {
+        # Call the local hook before the global so we can undo
+        # any settings made by the local postactivate first.
+        virtualenvwrapper_source_hook "$VIRTUAL_ENV/bin/predeactivate"
+        virtualenvwrapper_source_hook "$WORKON_HOME/predeactivate"
+        
+        env_postdeactivate_hook="$VIRTUAL_ENV/bin/postdeactivate"
+        
+        # Restore the original definition of deactivate
+        eval "$virtualenvwrapper_saved_deactivate"
+
+        # Instead of recursing, this calls the now restored original function.
+        deactivate
+
+        virtualenvwrapper_source_hook "$env_postdeactivate_hook"
+        virtualenvwrapper_source_hook "$WORKON_HOME/postdeactivate"
+    }'
+    
     virtualenvwrapper_source_hook "$WORKON_HOME/postactivate"
 
     virtualenvwrapper_source_hook "$VIRTUAL_ENV/bin/postactivate"    
 	return 0
 }
 
+
 #
 # Set up tab completion.  (Adapted from Arthur Koziel's version at 
 # http://arthurkoziel.com/2008/10/11/virtualenvwrapper-bash-completion/)
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.