Source

virtualenvwrapper / virtualenvwrapper_bashrc

Diff from to

File virtualenvwrapper_bashrc

 fi
 
 # Verify that the WORKON_HOME directory exists
-function verify_workon_home () {
+function virtualenvwrapper_verify_workon_home () {
     if [ ! -d "$WORKON_HOME" ]
     then
         echo "ERROR: Virtual environments directory '$WORKON_HOME' does not exist."
 }
 
 # Verify that the requested environment exists
-function verify_workon_environment () {
+function virtualenvwrapper_verify_workon_environment () {
     typeset env_name="$1"
     if [ ! -d "$WORKON_HOME/$env_name" ]
     then
 }
 
 # Verify that the active environment exists
-function verify_active_environment () {
+function virtualenvwrapper_verify_active_environment () {
     if [ ! -n "${VIRTUAL_ENV}" ] || [ ! -d "${VIRTUAL_ENV}" ]
     then
         echo "ERROR: no virtualenv active, or active virtualenv is missing"
     return 0
 }
 
+function virtualenvwrapper_source_hook () {
+    scriptname="$1"
+    if [ -f "$scriptname" ]
+    then
+        source "$scriptname"
+    fi
+}
+
+function virtualenvwrapper_run_hook () {
+    scriptname="$1"
+    shift
+    if [ -x "$scriptname" ]
+    then
+        "$scriptname" "$@"
+    fi
+}
+
 # Create a new environment, in the WORKON_HOME.
 #
 # Usage: mkvirtualenv [options] ENVNAME
 #
 function mkvirtualenv () {
     eval "envname=\$$#"
-    verify_workon_home
-    (cd "$WORKON_HOME"; virtualenv "$@")
+    virtualenvwrapper_verify_workon_home
+    (cd "$WORKON_HOME"; 
+        virtualenv "$@"; 
+        virtualenvwrapper_run_hook "./premkvirtualenv" "$envname"
+        )
     workon "$envname"
+    virtualenvwrapper_source_hook "$WORKON_HOME/postmkvirtualenv"
 }
 
 # Remove an environment, in the WORKON_HOME.
 function rmvirtualenv () {
     typeset env_name="$1"
-    verify_workon_home
+    virtualenvwrapper_verify_workon_home
     if [ "$env_name" = "" ]
     then
         echo "Please specify an enviroment."
         echo "Either switch to another environment, or run 'deactivate'."
         return 1
     fi
+    virtualenvwrapper_run_hook "$WORKON_HOME/prermvirtualenv" "$env_dir"
     rm -rf "$env_dir"
+    virtualenvwrapper_run_hook "$WORKON_HOME/postrmvirtualenv" "$env_dir"
 }
 
 # List the available environments.
-function show_workon_options () {
-    verify_workon_home
-	ls "$WORKON_HOME" | egrep -v '*.egg' | sort
+function virtualenvwrapper_show_workon_options () {
+    virtualenvwrapper_verify_workon_home
+	ls "$WORKON_HOME" | egrep -v '*.egg' | egrep -v '(pre|post)(rm|mk)virtualenv' | sort
 }
 
 # List or change working virtual environments
 	typeset env_name="$1"
 	if [ "$env_name" = "" ]
     then
-        show_workon_options
+        virtualenvwrapper_show_workon_options
         return 1
     fi
 
-    verify_workon_home || return 1
-    verify_workon_environment $env_name || return 1
+    virtualenvwrapper_verify_workon_home || return 1
+    virtualenvwrapper_verify_workon_environment $env_name || return 1
     
     activate="$WORKON_HOME/$env_name/bin/activate"
     if [ ! -f "$activate" ]
         return 1
     fi
     
-    if [ -f "$VIRTUAL_ENV/bin/predeactivate" ]
-    then
-        source "$VIRTUAL_ENV/bin/predeactivate"
-    fi
+    virtualenvwrapper_source_hook "$VIRTUAL_ENV/bin/predeactivate"
     
     source "$activate"
     
-    if [ -f "$VIRTUAL_ENV/bin/postactivate" ]
-    then
-        source "$VIRTUAL_ENV/bin/postactivate"
-    fi
+    virtualenvwrapper_source_hook "$VIRTUAL_ENV/bin/postactivate"
 	return 0
 }
 
     _virtualenvs ()
     {
         local cur="${COMP_WORDS[COMP_CWORD]}"
-        COMPREPLY=( $(compgen -W "`show_workon_options`" -- ${cur}) )
+        COMPREPLY=( $(compgen -W "`virtualenvwrapper_show_workon_options`" -- ${cur}) )
     }
 
     complete -o default -o nospace -F _virtualenvs workon
     complete -o default -o nospace -F _virtualenvs rmvirtualenv
 elif [ -n "$ZSH_VERSION" ] ; then
-    compctl -g "`show_workon_options`" workon rmvirtualenv
+    compctl -g "`virtualenvwrapper_show_workon_options`" workon rmvirtualenv
 fi
 
 # Path management for packages outside of the virtual env.
 #
 function add2virtualenv () {
 
-    verify_active_environment || return 1
+    virtualenvwrapper_verify_active_environment || return 1
     
     pyvers="`python -c 'import sys; print sys.version[:3]'`"
     site_packages="$VIRTUAL_ENV/lib/python${pyvers}/site-packages"