Commits

Doug Hellmann committed 3d181d1

all existing tests are passing again

Comments (0)

Files changed (7)

 # Testing
 TEST_SCRIPTS=$(wildcard tests/test*.sh)
 
-.PHONY: test test-bash test-sh test-zsh test-loop test-install
-test: test-bash test-sh test-zsh test-install
+.PHONY: develop test test-bash test-sh test-zsh test-loop test-install
+test: develop test-bash test-sh test-zsh test-install
+
+develop:
+	python setup.py develop
 
 test-bash:
 	TEST_SHELL=bash $(MAKE) test-loop
 
     entry_points = {
         #'console_scripts': [ 'venvw_hook = virtualenvwrapper.hook_loader:main' ],
+        'virtualenvwrapper.initialize': [
+            'make_hooks = virtualenvwrapper.make_hooks:initialize',
+            ],
         'virtualenvwrapper.initialize_source': [
             'user_scripts = virtualenvwrapper.user_scripts:initialize_source',
             ],
+
         'virtualenvwrapper.pre_mkvirtualenv': [
             'user_scripts = virtualenvwrapper.user_scripts:pre_mkvirtualenv',
             'make_hooks = virtualenvwrapper.make_hooks:pre_mkvirtualenv',
         'virtualenvwrapper.post_mkvirtualenv_source': [
             'user_scripts = virtualenvwrapper.user_scripts:post_mkvirtualenv_source',
             ],
-        'virtualenvwrapper.initialize': [
-            'make_hooks = virtualenvwrapper.make_hooks:initialize',
-            ]
+
+        'virtualenvwrapper.pre_rmvirtualenv': [
+            'user_scripts = virtualenvwrapper.user_scripts:pre_rmvirtualenv',
+            ],
+        'virtualenvwrapper.post_rmvirtualenv': [
+            'user_scripts = virtualenvwrapper.user_scripts:post_rmvirtualenv',
+            ],
+
+        'virtualenvwrapper.pre_activate': [
+            'user_scripts = virtualenvwrapper.user_scripts:pre_activate',
+            ],
+        'virtualenvwrapper.post_activate_source': [
+            'user_scripts = virtualenvwrapper.user_scripts:post_activate_source',
+            ],
+
+        'virtualenvwrapper.pre_deactivate_source': [
+            'user_scripts = virtualenvwrapper.user_scripts:pre_deactivate_source',
+            ],
+        'virtualenvwrapper.post_deactivate_source': [
+            'user_scripts = virtualenvwrapper.user_scripts:post_deactivate_source',
+            ],
         },
 
     zip_safe=False,

tests/test_run_hook.sh

 
 #set -x
 
+export WORKON_HOME="${TMPDIR:-/tmp}/WORKON_HOME"
+
 test_dir=$(dirname $0)
-source "$test_dir/../virtualenvwrapper.sh"
-
-export WORKON_HOME="${TMPDIR:-/tmp}/WORKON_HOME"
 
 oneTimeSetUp() {
     rm -rf "$WORKON_HOME"
     mkdir -p "$WORKON_HOME"
+    source "$test_dir/../virtualenvwrapper.sh"
 }
 
 oneTimeTearDown() {
 setUp () {
     echo
     rm -f "$test_dir/catch_output"
+    rm -f "$WORKON_HOME/initialize"
+    rm -f "$WORKON_HOME/prermvirtualenv"
 }
 
 test_virtualenvwrapper_run_hook() {
-    echo "echo run >> \"$test_dir/catch_output\"" >> "$WORKON_HOME/test_hook"
-    chmod +x "$WORKON_HOME/test_hook"
-    virtualenvwrapper_run_hook "$WORKON_HOME/test_hook"
+    echo "echo run >> \"$test_dir/catch_output\"" >> "$WORKON_HOME/initialize"
+    chmod +x "$WORKON_HOME/initialize"
+    virtualenvwrapper_run_hook "initialize"
+    output=$(cat "$test_dir/catch_output")
+    expected="run"
+    assertSame "$expected" "$output"
+}
+
+test_virtualenvwrapper_source_hook_permissions() {
+    echo "echo run >> \"$test_dir/catch_output\"" >> "$WORKON_HOME/initialize"
+    chmod -x "$WORKON_HOME/initialize"
+    virtualenvwrapper_run_hook "initialize"
     output=$(cat "$test_dir/catch_output")
     expected="run"
     assertSame "$expected" "$output"
 }
 
 test_virtualenvwrapper_run_hook_permissions() {
-    echo "echo run >> \"$test_dir/catch_output\"" >> "$WORKON_HOME/test_hook"
-    chmod -x "$WORKON_HOME/test_hook"
-    virtualenvwrapper_run_hook "$WORKON_HOME/test_hook"
+    echo "echo run $@ >> \"$test_dir/catch_output\"" >> "$WORKON_HOME/prermvirtualenv"
+    chmod -x "$WORKON_HOME/prermvirtualenv"
+    touch "$test_dir/catch_output"
+    virtualenvwrapper_run_hook "pre_rmvirtualenv" "foo"
     output=$(cat "$test_dir/catch_output")
     expected=""
     assertSame "$expected" "$output"

tests/test_workon.sh

         chmod +x "$WORKON_HOME/env1/bin/${t}activate"
     done
 
-    rm "$test_dir/catch_output"
+    rm -f "$test_dir/catch_output"
+    touch "$test_dir/catch_output"
 
     workon env1
     
         echo "echo ENV ${t}deactivate >> $test_dir/catch_output" > "$WORKON_HOME/env1/bin/${t}deactivate"
     done
 
+    touch "$test_dir/catch_output"
+
     deactivate
 
     output=$(cat "$test_dir/catch_output")

virtualenvwrapper.sh

 
 HOOK_VERBOSE_OPTION="-v"
 
-# Run a hook script in the current shell
-function virtualenvwrapper_source_hook () {
+# Run the hooks
+function virtualenvwrapper_run_hook () {
+    # First anything that runs directly from the plugin
+    "$VIRTUALENVWRAPPER_PYTHON" -m virtualenvwrapper.hook_loader $HOOK_VERBOSE_OPTION "$@"
+    # Now anything that wants to run inside this shell
+    hook_name="$1"
+    shift # get rid of hook name
     "$VIRTUALENVWRAPPER_PYTHON" -m virtualenvwrapper.hook_loader $HOOK_VERBOSE_OPTION \
-        --source "${1}_source" >>$TMPDIR/$$.hook
+        --source "${hook_name}_source" "$@" >>$TMPDIR/$$.hook
     source $TMPDIR/$$.hook
     rm -f $TMPDIR/$$.hook
 }
 
-# Run a hook script in its own shell
-function virtualenvwrapper_run_hook () {
-    "$VIRTUALENVWRAPPER_PYTHON" -m virtualenvwrapper.hook_loader $HOOK_VERBOSE_OPTION "$@"
-}
-
 # Set up virtualenvwrapper properly
 function virtualenvwrapper_initialize () {
     virtualenvwrapper_verify_workon_home -q || return 1
-    virtualenvwrapper_run_hook initialize
-    virtualenvwrapper_source_hook initialize
+    virtualenvwrapper_run_hook "initialize"
 }
 
 virtualenvwrapper_initialize
     # Now activate the new environment
     workon "$envname"
     virtualenvwrapper_run_hook "post_mkvirtualenv"
-    virtualenvwrapper_source_hook "post_mkvirtualenv"
 }
 
 # Remove an environment, in the WORKON_HOME.
         echo "Either switch to another environment, or run 'deactivate'." >&2
         return 1
     fi
-    virtualenvwrapper_run_hook "$WORKON_HOME/prermvirtualenv" "$env_dir"
+    virtualenvwrapper_run_hook "pre_rmvirtualenv" "$env_name"
     rm -rf "$env_dir"
-    virtualenvwrapper_run_hook "$WORKON_HOME/postrmvirtualenv" "$env_dir"
+    virtualenvwrapper_run_hook "post_rmvirtualenv" "$env_name"
 }
 
 # List the available environments.
         deactivate
     fi
 
-    virtualenvwrapper_run_hook "$WORKON_HOME/preactivate"
-    virtualenvwrapper_run_hook "$WORKON_HOME/$env_name/bin/preactivate"
+    virtualenvwrapper_run_hook "pre_activate" "$env_name"
     
     source "$activate"
     
     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"
+        virtualenvwrapper_run_hook "pre_deactivate"
         
         env_postdeactivate_hook="$VIRTUAL_ENV/bin/postdeactivate"
+        old_env=$(basename "$VIRTUAL_ENV")
         
         # 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_run_hook "post_deactivate" "$old_env"
     }'
     
-    virtualenvwrapper_source_hook "$WORKON_HOME/postactivate"
-    virtualenvwrapper_source_hook "$VIRTUAL_ENV/bin/postactivate"    
+    virtualenvwrapper_run_hook "post_activate"
     
 	return 0
 }

virtualenvwrapper/hook_loader.py

     console.setFormatter(formatter)
     logging.getLogger('').addHandler(console)
 
+    logging.getLogger(__name__).debug('cli args %s', args)
+
     # Determine which hook we're running
     if not args:
         parser.error('Please specify the hook to run')

virtualenvwrapper/user_scripts.py

         subprocess.call([script_path] + list(args), shell=True)
     return
 
+def run_global(script_name, *args):
+    script_path = os.path.expandvars(os.path.join('$WORKON_HOME', script_name))
+    run_script(script_path, *args)
+    return
+
+def run_local_from_arg(script_name, *args):
+    script_path = os.path.expandvars(os.path.join('$WORKON_HOME', args[0], 'bin', script_name))
+    run_script(script_path, *args)
+    return
+
+def run_local_from_env(script_name, *args):
+    script_path = os.path.expandvars(os.path.join('$VIRTUAL_ENV', 'bin', script_name))
+    run_script(script_path, *args)
+    return
+
+
 # HOOKS
 
 def initialize_source(args):
     return """
 #
-# Run user-provided initialization scripts
+# Run user-provided scripts
 #
 [ -f "$WORKON_HOME/initialize" ] && source "$WORKON_HOME/initialize"
 """
 
 def pre_mkvirtualenv(args):
     log.debug('pre_mkvirtualenv')
-    script_path = os.path.expandvars(os.path.join('$WORKON_HOME', 'premkvirtualenv'))
-    run_script(script_path, *args)
+    run_global('premkvirtualenv')
     return
 
 def post_mkvirtualenv_source(args):
     return """
 #
-# Run user-provided mkvirtualenv scripts
+# Run user-provided scripts
 #
 [ -f "$WORKON_HOME/postmkvirtualenv" ] && source "$WORKON_HOME/postmkvirtualenv"
 [ -f "$VIRTUAL_ENV/bin/postmkvirtualenv" ] && source "$VIRTUAL_ENV/bin/postmkvirtualenv"
 """
+
+def pre_rmvirtualenv(args):
+    log.debug('pre_rmvirtualenv')
+    run_global('prermvirtualenv', *args)
+    return
+
+def post_rmvirtualenv(args):
+    log.debug('post_rmvirtualenv')
+    run_global('postrmvirtualenv', *args)
+    return
+
+def pre_activate(args):
+    log.debug('pre_activate')
+    run_global('preactivate', *args)
+    run_local_from_arg('preactivate', *args)
+    return
+
+def post_activate_source(args):
+    log.debug('post_activate')
+    return """
+#
+# Run user-provided scripts
+#
+[ -f "$WORKON_HOME/postactivate" ] && source "$WORKON_HOME/postactivate"
+[ -f "$VIRTUAL_ENV/bin/postactivate" ] && source "$VIRTUAL_ENV/bin/postactivate"
+"""
+
+def pre_deactivate_source(args):
+    log.debug('pre_deactivate')
+    return """
+#
+# Run user-provided scripts
+#
+[ -f "$VIRTUAL_ENV/bin/predeactivate" ] && source "$VIRTUAL_ENV/bin/predeactivate"
+[ -f "$WORKON_HOME/predeactivate" ] && source "$WORKON_HOME/predeactivate"
+"""
+
+def post_deactivate_source(args):
+    log.debug('post_deactivate')
+    return """
+#
+# Run user-provided scripts
+#
+[ -f "$WORKON_HOME/%(env_name)s/bin/postdeactivate" ] && source "$WORKON_HOME/%(env_name)s/bin/postdeactivate"
+[ -f "$WORKON_HOME/postdeactivate" ] && source "$WORKON_HOME/postdeactivate"
+""" % { 'env_name':args[0] }
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.