Skip to content

Commit beb6d4d

Browse files
committed
implement initialize hooks
--HG-- rename : virtualenvwrapper/user_scripts_pre_initialize.sh => virtualenvwrapper/user_scripts_initialize.sh
1 parent c2aebe5 commit beb6d4d

File tree

8 files changed

+141
-125
lines changed

8 files changed

+141
-125
lines changed

setup.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,12 @@ def find_package_data(
151151

152152
entry_points = {
153153
#'console_scripts': [ 'venvw_hook = virtualenvwrapper.hook_loader:main' ],
154-
'virtualenvwrapper.pre_initialize_source': [
155-
'user_scripts = virtualenvwrapper.user_scripts:pre_initialize_source',
156-
'make_hooks = virtualenvwrapper.make_hooks:pre_initialize_source',
154+
'virtualenvwrapper.initialize_source': [
155+
'user_scripts = virtualenvwrapper.user_scripts:initialize_source',
157156
],
157+
'virtualenvwrapper.initialize': [
158+
'make_hooks = virtualenvwrapper.make_hooks:initialize',
159+
]
158160
},
159161

160162
zip_safe=False,

tests/test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ test_virtualenvwrapper_initialize() {
2828
assertTrue "Global $hook was not created" "[ -f $WORKON_HOME/$hook ]"
2929
assertTrue "Global $hook is not executable" "[ -x $WORKON_HOME/$hook ]"
3030
done
31+
assertTrue "Log file was not created" "[ -f $WORKON_HOME/hook.log ]"
3132
}
3233

3334
test_virtualenvwrapper_verify_workon_home() {

virtualenvwrapper.sh

Lines changed: 12 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -66,51 +66,24 @@ function virtualenvwrapper_verify_workon_home () {
6666
return 0
6767
}
6868

69-
# Create a hook script
70-
#
71-
# Usage: virtualenvwrapper_make_hook filename comment
72-
#
73-
function virtualenvwrapper_make_hook () {
74-
filename="$1"
75-
comment="$2"
76-
if [ ! -f "$filename" ]
77-
then
78-
#echo "Creating $filename"
79-
cat - > "$filename" <<EOF
80-
#!/bin/sh
81-
# $comment
8269

83-
EOF
84-
fi
85-
if [ ! -x "$filename" ]
86-
then
87-
chmod +x "$filename"
88-
fi
70+
# Run a hook script in the current shell
71+
function virtualenvwrapper_source_hook () {
72+
python -m virtualenvwrapper.hook_loader --source "${1}_source" >>$TMPDIR/$$.hook
73+
source $TMPDIR/$$.hook
74+
rm -f $TMPDIR/$$.hook
75+
}
76+
77+
# Run a hook script in its own shell
78+
function virtualenvwrapper_run_hook () {
79+
python -m virtualenvwrapper.hook_loader "$@"
8980
}
9081

9182
# Set up virtualenvwrapper properly
9283
function virtualenvwrapper_initialize () {
9384
virtualenvwrapper_verify_workon_home -q || return 1
94-
# mkvirtualenv
95-
virtualenvwrapper_make_hook "$WORKON_HOME/premkvirtualenv" \
96-
"This hook is run after a new virtualenv is created and before it is activated."
97-
virtualenvwrapper_make_hook "$WORKON_HOME/postmkvirtualenv" \
98-
"This hook is run after a new virtualenv is activated."
99-
# rmvirtualenv
100-
virtualenvwrapper_make_hook "$WORKON_HOME/prermvirtualenv" \
101-
"This hook is run before a virtualenv is deleted."
102-
virtualenvwrapper_make_hook "$WORKON_HOME/postrmvirtualenv" \
103-
"This hook is run after a virtualenv is deleted."
104-
# deactivate
105-
virtualenvwrapper_make_hook "$WORKON_HOME/predeactivate" \
106-
"This hook is run before every virtualenv is deactivated."
107-
virtualenvwrapper_make_hook "$WORKON_HOME/postdeactivate" \
108-
"This hook is run after every virtualenv is deactivated."
109-
# activate
110-
virtualenvwrapper_make_hook "$WORKON_HOME/preactivate" \
111-
"This hook is run before every virtualenv is activated."
112-
virtualenvwrapper_make_hook "$WORKON_HOME/postactivate" \
113-
"This hook is run after every virtualenv is activated."
85+
virtualenvwrapper_run_hook initialize
86+
virtualenvwrapper_source_hook initialize
11487
}
11588

11689
virtualenvwrapper_initialize
@@ -152,30 +125,6 @@ function virtualenvwrapper_verify_active_environment () {
152125
return 0
153126
}
154127

155-
# Run a hook script in the current shell
156-
function virtualenvwrapper_source_hook () {
157-
scriptname="$1"
158-
#echo "Looking for hook $scriptname"
159-
if [ -f "$scriptname" ]
160-
then
161-
source "$scriptname"
162-
fi
163-
}
164-
165-
# Run a hook script in its own shell
166-
function virtualenvwrapper_run_hook () {
167-
scriptname="$1"
168-
shift
169-
#echo "Looking for hook $scriptname"
170-
if [ -x "$scriptname" ]
171-
then
172-
"$scriptname" "$@"
173-
elif [ -e "$scriptname" ]
174-
then
175-
echo "Warning: Found \"$scriptname\" but it is not executable." 1>&2
176-
fi
177-
}
178-
179128
# Create a new environment, in the WORKON_HOME.
180129
#
181130
# Usage: mkvirtualenv [options] ENVNAME

virtualenvwrapper/hook_loader.py

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,73 @@
66
"""Load hooks for virtualenvwrapper.
77
"""
88

9+
import logging
910
import optparse
11+
import os
1012

1113
import pkg_resources
1214

1315
def main():
1416
parser = optparse.OptionParser(
15-
usage='usage: %prog [options] <hook> [options]',
17+
usage='usage: %prog [options] <hook> [<arguments>]',
1618
prog='virtualenvwrapper.hook_loader',
1719
description='Manage hooks for virtualenvwrapper',
1820
)
21+
parser.add_option('-s', '--source',
22+
help='Print the shell commands to be run in the current shell',
23+
action='store_true',
24+
dest='sourcing',
25+
default=False,
26+
)
27+
parser.add_option('-v', '--verbose',
28+
help='Show more information on the console',
29+
action='store_const',
30+
const=2,
31+
default=1,
32+
dest='verbose_level',
33+
)
34+
parser.add_option('-q', '--quiet',
35+
help='Show less information on the console',
36+
action='store_const',
37+
const=0,
38+
dest='verbose_level',
39+
)
40+
parser.disable_interspersed_args() # stop when we hit an option without an '-'
1941
options, args = parser.parse_args()
2042

43+
# Set up logging to a file and to the console
44+
logging.basicConfig(
45+
filename=os.path.expandvars(os.path.join('$WORKON_HOME', 'hook.log')),
46+
level=logging.DEBUG,
47+
format='%(asctime)s %(levelname)s %(name)s %(message)s',
48+
)
49+
console = logging.StreamHandler()
50+
console_level = [ logging.WARNING,
51+
logging.INFO,
52+
logging.DEBUG,
53+
][options.verbose_level]
54+
console.setLevel(console_level)
55+
formatter = logging.Formatter('%(message)s')
56+
console.setFormatter(formatter)
57+
logging.getLogger('').addHandler(console)
58+
59+
# Determine which hook we're running
60+
if not args:
61+
parser.error('Please specify the hook to run')
2162
hook = args[0]
22-
print hook
2363

2464
for ep in pkg_resources.iter_entry_points('virtualenvwrapper.%s' % hook):
2565
plugin = ep.load()
26-
print plugin()
66+
if options.sourcing:
67+
# Show the shell commands so they can
68+
# be run in the calling shell.
69+
contents = (plugin(args[1:]) or '').strip()
70+
if contents:
71+
print contents
72+
print
73+
else:
74+
# Just run the plugin ourselves
75+
plugin(args[1:])
2776
return 0
2877

2978
if __name__ == '__main__':

virtualenvwrapper/make_hooks.py

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,67 @@
66
"""Plugin to create hooks during initialization phase.
77
"""
88

9+
import logging
10+
import os
11+
import stat
12+
13+
log = logging.getLogger(__name__)
14+
915
import pkg_resources
1016

11-
def pre_initialize_source():
12-
return pkg_resources.resource_string(__name__, 'make_hooks_pre_initialize.sh')
17+
GLOBAL_HOOKS = [
18+
# initialize
19+
("initialize",
20+
"This hook is run during the startup phase when loading virtualenvwrapper.sh."),
21+
22+
# mkvirtualenv
23+
("premkvirtualenv",
24+
"This hook is run after a new virtualenv is created and before it is activated."),
25+
("postmkvirtualenv",
26+
"This hook is run after a new virtualenv is activated."),
27+
28+
# rmvirtualenv
29+
("prermvirtualenv",
30+
"This hook is run before a virtualenv is deleted."),
31+
("postrmvirtualenv",
32+
"This hook is run after a virtualenv is deleted."),
33+
34+
# deactivate
35+
("predeactivate",
36+
"This hook is run before every virtualenv is deactivated."),
37+
("postdeactivate",
38+
"This hook is run after every virtualenv is deactivated."),
39+
40+
# activate
41+
("preactivate",
42+
"This hook is run before every virtualenv is activated."),
43+
("postactivate",
44+
"This hook is run after every virtualenv is activated."),
45+
46+
]
47+
48+
def make_hook(filename, comment, permissions):
49+
"""Create a hook script.
50+
51+
:param filename: The name of the file to write.
52+
:param comment: The comment to insert into the file.
53+
"""
54+
filename = os.path.expanduser(os.path.expandvars(filename))
55+
if not os.path.exists(filename):
56+
log.info('Creating %s', filename)
57+
with open(filename, 'wt') as f:
58+
f.write("""#!/bin/sh
59+
# %s
60+
61+
""" % comment)
62+
os.chmod(filename, permissions)
63+
return
64+
65+
66+
def initialize(args):
67+
permissions = stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH | stat.S_IXOTH
68+
for filename, comment in GLOBAL_HOOKS:
69+
make_hook(os.path.join('$WORKON_HOME', filename), comment, permissions)
70+
return
71+
1372

virtualenvwrapper/make_hooks_pre_initialize.sh

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

virtualenvwrapper/user_scripts.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66
"""Plugin to handle hooks in user-defined scripts.
77
"""
88

9+
import logging
10+
911
import pkg_resources
1012

11-
def pre_initialize_source():
12-
return pkg_resources.resource_string(__name__, 'user_scripts_pre_initialize.sh')
13+
log = logging.getLogger(__name__)
14+
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)
19+
return pkg_resources.resource_string(__name__, script_name)
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
22
# Run user-provided initialization scripts
33
#
4-
global_script="$WORKON_HOME/preinitialize"
4+
global_script="$WORKON_HOME/initialize"
55
[ -f "$global_script" ] && source "$global_script"

0 commit comments

Comments
 (0)