Commits

Doug Hellmann committed 48376fe

Use VIRTUALENVWRAPPER_HOOK_DIR to control where the hooks are defined.

  • Participants
  • Parent commits 59e8a7f

Comments (0)

Files changed (4)

tests/test_hook_dir.sh

+#!/bin/sh
+
+#set -x
+
+test_dir=$(cd $(dirname $0) && pwd)
+
+export WORKON_HOME="$(echo ${TMPDIR:-/tmp}/WORKON_HOME | sed 's|//|/|g')"
+
+oneTimeSetUp() {
+    rm -rf "$WORKON_HOME"
+    mkdir -p "$WORKON_HOME"
+    mkdir -p "$WORKON_HOME/hooks"
+}
+
+oneTimeTearDown() {
+    rm -rf "$WORKON_HOME"
+}
+
+setUp () {
+    echo
+    rm -f "$test_dir/catch_output"
+    rm -f "$WORKON_HOME/hooks/*"
+}
+
+test_virtualenvwrapper_initialize() {
+    export VIRTUALENVWRAPPER_HOOK_DIR="$WORKON_HOME/hooks"
+    source "$test_dir/../virtualenvwrapper.sh"
+    for hook in premkvirtualenv postmkvirtualenv prermvirtualenv postrmvirtualenv preactivate postactivate predeactivate postdeactivate
+    do
+        assertTrue "Global $hook was not created" "[ -f $WORKON_HOME/hooks/$hook ]"
+        assertTrue "Global $hook is not executable" "[ -x $WORKON_HOME/hooks/$hook ]"
+    done
+}
+
+. "$test_dir/shunit2"

tests/test_run_hook.sh

     assertSame "$expected" "$output"
 }
 
+test_virtualenvwrapper_run_hook_alternate_dir() {
+    mkdir "$WORKON_HOME/hooks"
+    echo "echo WORKON_HOME >> \"$test_dir/catch_output\"" >> "$WORKON_HOME/initialize"
+    echo "echo WORKON_HOME/hooks >> \"$test_dir/catch_output\"" >> "$WORKON_HOME/hooks/initialize"
+    chmod +x "$WORKON_HOME/initialize"
+    chmod +x "$WORKON_HOME/hooks/initialize"
+    VIRTUALENVWRAPPER_HOOK_DIR="$WORKON_HOME/hooks"
+    virtualenvwrapper_run_hook "initialize"
+    output=$(cat "$test_dir/catch_output")
+    expected="WORKON_HOME/hooks"
+    assertSame "$expected" "$output"
+    VIRTUALENVWRAPPER_HOOK_DIR="$WORKON_HOME"
+}
+
 test_virtualenvwrapper_source_hook_permissions() {
     echo "echo run >> \"$test_dir/catch_output\"" >> "$WORKON_HOME/initialize"
     chmod -x "$WORKON_HOME/initialize"

virtualenvwrapper.sh

     VIRTUALENVWRAPPER_VIRTUALENV="virtualenv"
 fi
 
+# Set the location of the hook scripts
+if [ "$VIRTUALENVWRAPPER_HOOK_DIR" = "" ]
+then
+    export VIRTUALENVWRAPPER_HOOK_DIR="$WORKON_HOME"
+fi
+
 virtualenvwrapper_derive_workon_home() {
     typeset workon_home_dir="$WORKON_HOME"
 

virtualenvwrapper/user_scripts.py

 
 
 def run_global(script_name, *args):
-    """Run a script from $WORKON_HOME.
+    """Run a script from $VIRTUALENVWRAPPER_HOOK_DIR.
     """
-    script_path = os.path.expandvars(os.path.join('$WORKON_HOME', script_name))
+    script_path = os.path.expandvars(os.path.join('$VIRTUALENVWRAPPER_HOOK_DIR', script_name))
     run_script(script_path, *args)
     return
 
 
 def initialize(args):
     for filename, comment in GLOBAL_HOOKS:
-        make_hook(os.path.join('$WORKON_HOME', filename), comment)
+        make_hook(os.path.join('$VIRTUALENVWRAPPER_HOOK_DIR', filename), comment)
     return
 
 
 #
 # Run user-provided scripts
 #
-[ -f "$WORKON_HOME/initialize" ] && source "$WORKON_HOME/initialize"
+[ -f "$VIRTUALENVWRAPPER_HOOK_DIR/initialize" ] && source "$VIRTUALENVWRAPPER_HOOK_DIR/initialize"
 """
 
 def pre_mkvirtualenv(args):
 #
 # Run user-provided scripts
 #
-[ -f "$WORKON_HOME/postmkvirtualenv" ] && source "$WORKON_HOME/postmkvirtualenv"
+[ -f "$VIRTUALENVWRAPPER_HOOK_DIR/postmkvirtualenv" ] && source "$VIRTUALENVWRAPPER_HOOK_DIR/postmkvirtualenv"
 """
 
 def pre_cpvirtualenv(args):
 #
 # Run user-provided scripts
 #
-[ -f "$WORKON_HOME/postcpvirtualenv" ] && source "$WORKON_HOME/postcpvirtualenv"
+[ -f "$VIRTUALENVWRAPPER_HOOK_DIR/postcpvirtualenv" ] && source "$VIRTUALENVWRAPPER_HOOK_DIR/postcpvirtualenv"
 """
 
 
 #
 # Run user-provided scripts
 #
-[ -f "$WORKON_HOME/postactivate" ] && source "$WORKON_HOME/postactivate"
+[ -f "$VIRTUALENVWRAPPER_HOOK_DIR/postactivate" ] && source "$VIRTUALENVWRAPPER_HOOK_DIR/postactivate"
 [ -f "$VIRTUAL_ENV/bin/postactivate" ] && source "$VIRTUAL_ENV/bin/postactivate"
 """
 
 # Run user-provided scripts
 #
 [ -f "$VIRTUAL_ENV/bin/predeactivate" ] && source "$VIRTUAL_ENV/bin/predeactivate"
-[ -f "$WORKON_HOME/predeactivate" ] && source "$WORKON_HOME/predeactivate"
+[ -f "$VIRTUALENVWRAPPER_HOOK_DIR/predeactivate" ] && source "$VIRTUALENVWRAPPER_HOOK_DIR/predeactivate"
 """
 
 
 #
 VIRTUALENVWRAPPER_LAST_VIRTUAL_ENV="$WORKON_HOME/%(env_name)s"
 [ -f "$WORKON_HOME/%(env_name)s/bin/postdeactivate" ] && source "$WORKON_HOME/%(env_name)s/bin/postdeactivate"
-[ -f "$WORKON_HOME/postdeactivate" ] && source "$WORKON_HOME/postdeactivate"
+[ -f "$VIRTUALENVWRAPPER_HOOK_DIR/postdeactivate" ] && source "$VIRTUALENVWRAPPER_HOOK_DIR/postdeactivate"
 unset VIRTUALENVWRAPPER_LAST_VIRTUAL_ENV
 """ % { 'env_name':args[0] }