Skip to content

Commit 6674ea3

Browse files
committed
convert more hooks; stop running tests when we see a failure or error
1 parent beb6d4d commit 6674ea3

File tree

8 files changed

+93
-25
lines changed

8 files changed

+93
-25
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ test-loop:
5050
@for test_script in $(wildcard tests/test*.sh) ; do \
5151
echo '********************************************************************************' ; \
5252
echo "Running $$test_script with $(TEST_SHELL)" ; \
53-
SHUNIT_PARENT=$$test_script $(TEST_SHELL) $$test_script ; \
53+
SHUNIT_PARENT=$$test_script $(TEST_SHELL) $$test_script || exit 1 ; \
5454
echo ; \
5555
done
5656

setup.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ def find_package_data(
154154
'virtualenvwrapper.initialize_source': [
155155
'user_scripts = virtualenvwrapper.user_scripts:initialize_source',
156156
],
157+
'virtualenvwrapper.pre_mkvirtualenv': [
158+
'user_scripts = virtualenvwrapper.user_scripts:pre_mkvirtualenv',
159+
'make_hooks = virtualenvwrapper.make_hooks:pre_mkvirtualenv',
160+
],
161+
'virtualenvwrapper.post_mkvirtualenv_source': [
162+
'user_scripts = virtualenvwrapper.user_scripts:post_mkvirtualenv_source',
163+
],
157164
'virtualenvwrapper.initialize': [
158165
'make_hooks = virtualenvwrapper.make_hooks:initialize',
159166
]

tests/test.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ test_virtualenvwrapper_initialize() {
2929
assertTrue "Global $hook is not executable" "[ -x $WORKON_HOME/$hook ]"
3030
done
3131
assertTrue "Log file was not created" "[ -f $WORKON_HOME/hook.log ]"
32+
export pre_test_dir=$(cd "$test_dir"; pwd)
33+
echo "echo GLOBAL initialize >> \"$pre_test_dir/catch_output\"" >> "$WORKON_HOME/initialize"
34+
virtualenvwrapper_initialize
35+
output=$(cat "$test_dir/catch_output")
36+
expected="GLOBAL initialize"
37+
assertSame "$expected" "$output"
3238
}
3339

3440
test_virtualenvwrapper_verify_workon_home() {

virtualenvwrapper.sh

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,12 @@ then
5151
export WORKON_HOME="$HOME/.virtualenvs"
5252
fi
5353

54+
# Locate the global Python where virtualenvwrapper is installed.
55+
VIRTUALENVWRAPPER_PYTHON="$(which python)"
56+
5457
# Normalize the directory name in case it includes
5558
# relative path components.
56-
WORKON_HOME=$(python -c "import os; print os.path.abspath(os.path.expandvars(os.path.expanduser(\"$WORKON_HOME\")))")
59+
WORKON_HOME=$("$VIRTUALENVWRAPPER_PYTHON" -c "import os; print os.path.abspath(os.path.expandvars(os.path.expanduser(\"$WORKON_HOME\")))")
5760
export WORKON_HOME
5861

5962
# Verify that the WORKON_HOME directory exists
@@ -66,17 +69,19 @@ function virtualenvwrapper_verify_workon_home () {
6669
return 0
6770
}
6871

72+
HOOK_VERBOSE_OPTION="-v"
6973

7074
# Run a hook script in the current shell
7175
function virtualenvwrapper_source_hook () {
72-
python -m virtualenvwrapper.hook_loader --source "${1}_source" >>$TMPDIR/$$.hook
76+
"$VIRTUALENVWRAPPER_PYTHON" -m virtualenvwrapper.hook_loader $HOOK_VERBOSE_OPTION \
77+
--source "${1}_source" >>$TMPDIR/$$.hook
7378
source $TMPDIR/$$.hook
7479
rm -f $TMPDIR/$$.hook
7580
}
7681

7782
# Run a hook script in its own shell
7883
function virtualenvwrapper_run_hook () {
79-
python -m virtualenvwrapper.hook_loader "$@"
84+
"$VIRTUALENVWRAPPER_PYTHON" -m virtualenvwrapper.hook_loader $HOOK_VERBOSE_OPTION "$@"
8085
}
8186

8287
# Set up virtualenvwrapper properly
@@ -136,19 +141,16 @@ function mkvirtualenv () {
136141
virtualenvwrapper_verify_virtualenv || return 1
137142
(cd "$WORKON_HOME" &&
138143
virtualenv "$@" &&
139-
virtualenvwrapper_run_hook "./premkvirtualenv" "$envname"
144+
virtualenvwrapper_run_hook "pre_mkvirtualenv" "$envname"
140145
)
141146
# If they passed a help option or got an error from virtualenv,
142147
# the environment won't exist. Use that to tell whether
143148
# we should switch to the environment and run the hook.
144149
[ ! -d "$WORKON_HOME/$envname" ] && return 0
145-
# Create stubs for the environment-specific hook scripts.
146-
virtualenvwrapper_make_hook "$WORKON_HOME/$envname/bin/postactivate" "This hook is sourced after the virtualenv is activated."
147-
virtualenvwrapper_make_hook "$WORKON_HOME/$envname/bin/predeactivate" "This hook is sourced before the virtualenv is deactivated."
148-
virtualenvwrapper_make_hook "$WORKON_HOME/$envname/bin/postdeactivate" "This hook is sourced after the virtualenv is deactivated."
149150
# Now activate the new environment
150151
workon "$envname"
151-
virtualenvwrapper_source_hook "$WORKON_HOME/postmkvirtualenv"
152+
virtualenvwrapper_run_hook "post_mkvirtualenv"
153+
virtualenvwrapper_source_hook "post_mkvirtualenv"
152154
}
153155

154156
# Remove an environment, in the WORKON_HOME.

virtualenvwrapper/hook_loader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def main():
5252
logging.DEBUG,
5353
][options.verbose_level]
5454
console.setLevel(console_level)
55-
formatter = logging.Formatter('%(message)s')
55+
formatter = logging.Formatter('%(name)s %(message)s')
5656
console.setFormatter(formatter)
5757
logging.getLogger('').addHandler(console)
5858

virtualenvwrapper/make_hooks.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
import pkg_resources
1616

17+
PERMISSIONS = stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH | stat.S_IXOTH
18+
1719
GLOBAL_HOOKS = [
1820
# initialize
1921
("initialize",
@@ -42,10 +44,23 @@
4244
"This hook is run before every virtualenv is activated."),
4345
("postactivate",
4446
"This hook is run after every virtualenv is activated."),
45-
4647
]
4748

48-
def make_hook(filename, comment, permissions):
49+
LOCAL_HOOKS = [
50+
# deactivate
51+
("predeactivate",
52+
"This hook is run before the virtualenv is deactivated."),
53+
("postdeactivate",
54+
"This hook is run after the virtualenv is deactivated."),
55+
56+
# activate
57+
("preactivate",
58+
"This hook is run before the virtualenv is activated."),
59+
("postactivate",
60+
"This hook is run after the virtualenv is activated."),
61+
]
62+
63+
def make_hook(filename, comment):
4964
"""Create a hook script.
5065
5166
:param filename: The name of the file to write.
@@ -59,14 +74,18 @@ def make_hook(filename, comment, permissions):
5974
# %s
6075
6176
""" % comment)
62-
os.chmod(filename, permissions)
77+
os.chmod(filename, PERMISSIONS)
6378
return
6479

6580

6681
def initialize(args):
67-
permissions = stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH | stat.S_IXOTH
6882
for filename, comment in GLOBAL_HOOKS:
69-
make_hook(os.path.join('$WORKON_HOME', filename), comment, permissions)
83+
make_hook(os.path.join('$WORKON_HOME', filename), comment)
7084
return
7185

86+
def pre_mkvirtualenv(args):
87+
envname=args[0]
88+
for filename, comment in LOCAL_HOOKS:
89+
make_hook(os.path.join('$WORKON_HOME', envname, 'bin', filename), comment)
90+
return
7291

virtualenvwrapper/user_scripts.py

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,52 @@
77
"""
88

99
import logging
10+
import os
11+
import subprocess
1012

1113
import pkg_resources
1214

1315
log = logging.getLogger(__name__)
1416

15-
def initialize_source(args):
16-
script_name = 'user_scripts_initialize.sh'
17-
fname = pkg_resources.resource_filename(__name__, script_name)
18-
log.debug('Looking for %s in %s', script_name, fname)
17+
def get_script_source(script_name):
18+
"""Retrieve the source code for a script.
19+
"""
20+
script_path = pkg_resources.resource_filename(__name__, script_name)
21+
if not script_path:
22+
raise RuntimeError('Missing script for %s', script_name)
23+
log.debug('Looking for %s in %s', script_name, script_path)
1924
return pkg_resources.resource_string(__name__, script_name)
25+
26+
27+
def run_script(script_path, *args):
28+
"""Execute a script in a subshell.
29+
"""
30+
if os.path.exists(script_path):
31+
log.debug('Running %s', script_path)
32+
subprocess.call([script_path] + list(args), shell=True)
33+
return
34+
35+
# HOOKS
36+
37+
def initialize_source(args):
38+
return """
39+
#
40+
# Run user-provided initialization scripts
41+
#
42+
[ -f "$WORKON_HOME/initialize" ] && source "$WORKON_HOME/initialize"
43+
"""
44+
45+
def pre_mkvirtualenv(args):
46+
log.debug('pre_mkvirtualenv')
47+
script_path = os.path.expandvars(os.path.join('$WORKON_HOME', 'premkvirtualenv'))
48+
run_script(script_path, *args)
49+
return
50+
51+
def post_mkvirtualenv_source(args):
52+
return """
53+
#
54+
# Run user-provided mkvirtualenv scripts
55+
#
56+
[ -f "$WORKON_HOME/postmkvirtualenv" ] && source "$WORKON_HOME/postmkvirtualenv"
57+
[ -f "$VIRTUAL_ENV/bin/postmkvirtualenv" ] && source "$VIRTUAL_ENV/bin/postmkvirtualenv"
58+
"""

virtualenvwrapper/user_scripts_initialize.sh

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)