Commits

Doug Hellmann committed 1c09586

convert more hooks; stop running tests when we see a failure or error

Comments (0)

Files changed (8)

 	@for test_script in $(wildcard tests/test*.sh) ; do \
 	 	echo '********************************************************************************' ; \
 		echo "Running $$test_script with $(TEST_SHELL)" ; \
-		SHUNIT_PARENT=$$test_script $(TEST_SHELL) $$test_script ; \
+		SHUNIT_PARENT=$$test_script $(TEST_SHELL) $$test_script || exit 1 ; \
 		echo ; \
 	done
 
         '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',
             ]
         assertTrue "Global $hook is not executable" "[ -x $WORKON_HOME/$hook ]"
     done
     assertTrue "Log file was not created" "[ -f $WORKON_HOME/hook.log ]"
+    export pre_test_dir=$(cd "$test_dir"; pwd)
+    echo "echo GLOBAL initialize >> \"$pre_test_dir/catch_output\"" >> "$WORKON_HOME/initialize"
+    virtualenvwrapper_initialize
+    output=$(cat "$test_dir/catch_output")
+    expected="GLOBAL initialize"
+    assertSame "$expected" "$output"
 }
 
 test_virtualenvwrapper_verify_workon_home() {

virtualenvwrapper.sh

     export WORKON_HOME="$HOME/.virtualenvs"
 fi
 
+# Locate the global Python where virtualenvwrapper is installed.
+VIRTUALENVWRAPPER_PYTHON="$(which python)"
+
 # Normalize the directory name in case it includes 
 # relative path components.
-WORKON_HOME=$(python -c "import os; print os.path.abspath(os.path.expandvars(os.path.expanduser(\"$WORKON_HOME\")))")
+WORKON_HOME=$("$VIRTUALENVWRAPPER_PYTHON" -c "import os; print os.path.abspath(os.path.expandvars(os.path.expanduser(\"$WORKON_HOME\")))")
 export WORKON_HOME
 
 # Verify that the WORKON_HOME directory exists
     return 0
 }
 
+HOOK_VERBOSE_OPTION="-v"
 
 # Run a hook script in the current shell
 function virtualenvwrapper_source_hook () {
-    python -m virtualenvwrapper.hook_loader --source "${1}_source" >>$TMPDIR/$$.hook
+    "$VIRTUALENVWRAPPER_PYTHON" -m virtualenvwrapper.hook_loader $HOOK_VERBOSE_OPTION \
+        --source "${1}_source" >>$TMPDIR/$$.hook
     source $TMPDIR/$$.hook
     rm -f $TMPDIR/$$.hook
 }
 
 # Run a hook script in its own shell
 function virtualenvwrapper_run_hook () {
-    python -m virtualenvwrapper.hook_loader "$@"
+    "$VIRTUALENVWRAPPER_PYTHON" -m virtualenvwrapper.hook_loader $HOOK_VERBOSE_OPTION "$@"
 }
 
 # Set up virtualenvwrapper properly
     virtualenvwrapper_verify_virtualenv || return 1
     (cd "$WORKON_HOME" &&
         virtualenv "$@" &&
-        virtualenvwrapper_run_hook "./premkvirtualenv" "$envname"
+        virtualenvwrapper_run_hook "pre_mkvirtualenv" "$envname"
         )
     # If they passed a help option or got an error from 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_run_hook "post_mkvirtualenv"
+    virtualenvwrapper_source_hook "post_mkvirtualenv"
 }
 
 # Remove an environment, in the WORKON_HOME.

virtualenvwrapper/hook_loader.py

                       logging.DEBUG,
                       ][options.verbose_level]
     console.setLevel(console_level)
-    formatter = logging.Formatter('%(message)s')
+    formatter = logging.Formatter('%(name)s %(message)s')
     console.setFormatter(formatter)
     logging.getLogger('').addHandler(console)
 

virtualenvwrapper/make_hooks.py

 
 import pkg_resources
 
+PERMISSIONS = stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH | stat.S_IXOTH
+
 GLOBAL_HOOKS = [
     # initialize
     ("initialize",
      "This hook is run before every virtualenv is activated."),
     ("postactivate",
      "This hook is run after every virtualenv is activated."),
-    
     ]
 
-def make_hook(filename, comment, permissions):
+LOCAL_HOOKS = [
+    # deactivate
+    ("predeactivate",
+     "This hook is run before the virtualenv is deactivated."),
+    ("postdeactivate",
+     "This hook is run after the virtualenv is deactivated."),
+
+    # activate
+    ("preactivate",
+     "This hook is run before the virtualenv is activated."),
+    ("postactivate",
+     "This hook is run after the virtualenv is activated."),
+    ]
+
+def make_hook(filename, comment):
     """Create a hook script.
     
     :param filename: The name of the file to write.
 # %s
 
 """ % comment)
-        os.chmod(filename, permissions)
+        os.chmod(filename, PERMISSIONS)
     return
 
 
 def initialize(args):
-    permissions = stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH | stat.S_IXOTH
     for filename, comment in GLOBAL_HOOKS:
-        make_hook(os.path.join('$WORKON_HOME', filename), comment, permissions)
+        make_hook(os.path.join('$WORKON_HOME', filename), comment)
     return
 
+def pre_mkvirtualenv(args):
+    envname=args[0]
+    for filename, comment in LOCAL_HOOKS:
+        make_hook(os.path.join('$WORKON_HOME', envname, 'bin', filename), comment)
+    return
 

virtualenvwrapper/user_scripts.py

 """
 
 import logging
+import os
+import subprocess
 
 import pkg_resources
 
 log = logging.getLogger(__name__)
 
+def get_script_source(script_name):
+    """Retrieve the source code for a script.
+    """
+    script_path = pkg_resources.resource_filename(__name__, script_name)
+    if not script_path:
+        raise RuntimeError('Missing script for %s', script_name)
+    log.debug('Looking for %s in %s', script_name, script_path)
+    return pkg_resources.resource_string(__name__, script_name)
+
+
+def run_script(script_path, *args):
+    """Execute a script in a subshell.
+    """
+    if os.path.exists(script_path):
+        log.debug('Running %s', script_path)
+        subprocess.call([script_path] + list(args), shell=True)
+    return
+
+# HOOKS
+
 def initialize_source(args):
-    script_name = 'user_scripts_initialize.sh'
-    fname = pkg_resources.resource_filename(__name__, script_name)
-    log.debug('Looking for %s in %s', script_name, fname)
-    return pkg_resources.resource_string(__name__, script_name)
+    return """
+#
+# Run user-provided initialization 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)
+    return
+
+def post_mkvirtualenv_source(args):
+    return """
+#
+# Run user-provided mkvirtualenv scripts
+#
+[ -f "$WORKON_HOME/postmkvirtualenv" ] && source "$WORKON_HOME/postmkvirtualenv"
+[ -f "$VIRTUAL_ENV/bin/postmkvirtualenv" ] && source "$VIRTUAL_ENV/bin/postmkvirtualenv"
+"""

virtualenvwrapper/user_scripts_initialize.sh

-#
-# Run user-provided initialization scripts
-#
-global_script="$WORKON_HOME/initialize"
-[ -f "$global_script" ] && source "$global_script"
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.