Skip to content

Commit b200811

Browse files
committed
Use VIRTUALENVWRAPPER_HOOK_DIR to control where the hooks are defined.
1 parent 170108f commit b200811

File tree

4 files changed

+64
-9
lines changed

4 files changed

+64
-9
lines changed

tests/test_hook_dir.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/sh
2+
3+
#set -x
4+
5+
test_dir=$(cd $(dirname $0) && pwd)
6+
7+
export WORKON_HOME="$(echo ${TMPDIR:-/tmp}/WORKON_HOME | sed 's|//|/|g')"
8+
9+
oneTimeSetUp() {
10+
rm -rf "$WORKON_HOME"
11+
mkdir -p "$WORKON_HOME"
12+
mkdir -p "$WORKON_HOME/hooks"
13+
}
14+
15+
oneTimeTearDown() {
16+
rm -rf "$WORKON_HOME"
17+
}
18+
19+
setUp () {
20+
echo
21+
rm -f "$test_dir/catch_output"
22+
rm -f "$WORKON_HOME/hooks/*"
23+
}
24+
25+
test_virtualenvwrapper_initialize() {
26+
export VIRTUALENVWRAPPER_HOOK_DIR="$WORKON_HOME/hooks"
27+
source "$test_dir/../virtualenvwrapper.sh"
28+
for hook in premkvirtualenv postmkvirtualenv prermvirtualenv postrmvirtualenv preactivate postactivate predeactivate postdeactivate
29+
do
30+
assertTrue "Global $hook was not created" "[ -f $WORKON_HOME/hooks/$hook ]"
31+
assertTrue "Global $hook is not executable" "[ -x $WORKON_HOME/hooks/$hook ]"
32+
done
33+
}
34+
35+
. "$test_dir/shunit2"

tests/test_run_hook.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ test_virtualenvwrapper_run_hook() {
3232
assertSame "$expected" "$output"
3333
}
3434

35+
test_virtualenvwrapper_run_hook_alternate_dir() {
36+
mkdir "$WORKON_HOME/hooks"
37+
echo "echo WORKON_HOME >> \"$test_dir/catch_output\"" >> "$WORKON_HOME/initialize"
38+
echo "echo WORKON_HOME/hooks >> \"$test_dir/catch_output\"" >> "$WORKON_HOME/hooks/initialize"
39+
chmod +x "$WORKON_HOME/initialize"
40+
chmod +x "$WORKON_HOME/hooks/initialize"
41+
VIRTUALENVWRAPPER_HOOK_DIR="$WORKON_HOME/hooks"
42+
virtualenvwrapper_run_hook "initialize"
43+
output=$(cat "$test_dir/catch_output")
44+
expected="WORKON_HOME/hooks"
45+
assertSame "$expected" "$output"
46+
VIRTUALENVWRAPPER_HOOK_DIR="$WORKON_HOME"
47+
}
48+
3549
test_virtualenvwrapper_source_hook_permissions() {
3650
echo "echo run >> \"$test_dir/catch_output\"" >> "$WORKON_HOME/initialize"
3751
chmod -x "$WORKON_HOME/initialize"

virtualenvwrapper.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ then
5656
VIRTUALENVWRAPPER_VIRTUALENV="virtualenv"
5757
fi
5858

59+
# Set the location of the hook scripts
60+
if [ "$VIRTUALENVWRAPPER_HOOK_DIR" = "" ]
61+
then
62+
export VIRTUALENVWRAPPER_HOOK_DIR="$WORKON_HOME"
63+
fi
64+
5965
virtualenvwrapper_derive_workon_home() {
6066
typeset workon_home_dir="$WORKON_HOME"
6167

virtualenvwrapper/user_scripts.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ def run_script(script_path, *args):
3131

3232

3333
def run_global(script_name, *args):
34-
"""Run a script from $WORKON_HOME.
34+
"""Run a script from $VIRTUALENVWRAPPER_HOOK_DIR.
3535
"""
36-
script_path = os.path.expandvars(os.path.join('$WORKON_HOME', script_name))
36+
script_path = os.path.expandvars(os.path.join('$VIRTUALENVWRAPPER_HOOK_DIR', script_name))
3737
run_script(script_path, *args)
3838
return
3939

@@ -122,7 +122,7 @@ def make_hook(filename, comment):
122122

123123
def initialize(args):
124124
for filename, comment in GLOBAL_HOOKS:
125-
make_hook(os.path.join('$WORKON_HOME', filename), comment)
125+
make_hook(os.path.join('$VIRTUALENVWRAPPER_HOOK_DIR', filename), comment)
126126
return
127127

128128

@@ -131,7 +131,7 @@ def initialize_source(args):
131131
#
132132
# Run user-provided scripts
133133
#
134-
[ -f "$WORKON_HOME/initialize" ] && source "$WORKON_HOME/initialize"
134+
[ -f "$VIRTUALENVWRAPPER_HOOK_DIR/initialize" ] && source "$VIRTUALENVWRAPPER_HOOK_DIR/initialize"
135135
"""
136136

137137
def pre_mkvirtualenv(args):
@@ -148,7 +148,7 @@ def post_mkvirtualenv_source(args):
148148
#
149149
# Run user-provided scripts
150150
#
151-
[ -f "$WORKON_HOME/postmkvirtualenv" ] && source "$WORKON_HOME/postmkvirtualenv"
151+
[ -f "$VIRTUALENVWRAPPER_HOOK_DIR/postmkvirtualenv" ] && source "$VIRTUALENVWRAPPER_HOOK_DIR/postmkvirtualenv"
152152
"""
153153

154154
def pre_cpvirtualenv(args):
@@ -165,7 +165,7 @@ def post_cpvirtualenv_source(args):
165165
#
166166
# Run user-provided scripts
167167
#
168-
[ -f "$WORKON_HOME/postcpvirtualenv" ] && source "$WORKON_HOME/postcpvirtualenv"
168+
[ -f "$VIRTUALENVWRAPPER_HOOK_DIR/postcpvirtualenv" ] && source "$VIRTUALENVWRAPPER_HOOK_DIR/postcpvirtualenv"
169169
"""
170170

171171

@@ -195,7 +195,7 @@ def post_activate_source(args):
195195
#
196196
# Run user-provided scripts
197197
#
198-
[ -f "$WORKON_HOME/postactivate" ] && source "$WORKON_HOME/postactivate"
198+
[ -f "$VIRTUALENVWRAPPER_HOOK_DIR/postactivate" ] && source "$VIRTUALENVWRAPPER_HOOK_DIR/postactivate"
199199
[ -f "$VIRTUAL_ENV/bin/postactivate" ] && source "$VIRTUAL_ENV/bin/postactivate"
200200
"""
201201

@@ -207,7 +207,7 @@ def pre_deactivate_source(args):
207207
# Run user-provided scripts
208208
#
209209
[ -f "$VIRTUAL_ENV/bin/predeactivate" ] && source "$VIRTUAL_ENV/bin/predeactivate"
210-
[ -f "$WORKON_HOME/predeactivate" ] && source "$WORKON_HOME/predeactivate"
210+
[ -f "$VIRTUALENVWRAPPER_HOOK_DIR/predeactivate" ] && source "$VIRTUALENVWRAPPER_HOOK_DIR/predeactivate"
211211
"""
212212

213213

@@ -219,7 +219,7 @@ def post_deactivate_source(args):
219219
#
220220
VIRTUALENVWRAPPER_LAST_VIRTUAL_ENV="$WORKON_HOME/%(env_name)s"
221221
[ -f "$WORKON_HOME/%(env_name)s/bin/postdeactivate" ] && source "$WORKON_HOME/%(env_name)s/bin/postdeactivate"
222-
[ -f "$WORKON_HOME/postdeactivate" ] && source "$WORKON_HOME/postdeactivate"
222+
[ -f "$VIRTUALENVWRAPPER_HOOK_DIR/postdeactivate" ] && source "$VIRTUALENVWRAPPER_HOOK_DIR/postdeactivate"
223223
unset VIRTUALENVWRAPPER_LAST_VIRTUAL_ENV
224224
""" % { 'env_name':args[0] }
225225

0 commit comments

Comments
 (0)