Skip to content

Commit c2aebe5

Browse files
committed
start implementing hook loader and a couple of sample hooks
1 parent 8843454 commit c2aebe5

File tree

8 files changed

+118
-5
lines changed

8 files changed

+118
-5
lines changed

setup.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ def find_package_data(
150150
),
151151

152152
entry_points = {
153-
# 'console_scripts': [ 'ical2org = ical2org.app:main' ],
153+
#'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',
157+
],
154158
},
155159

156160
zip_safe=False,

virtualenvwrapper.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
# (mkdir $HOME/.virtualenvs).
3333
# 2. Add a line like "export WORKON_HOME=$HOME/.virtualenvs"
3434
# to your .bashrc.
35-
# 3. Add a line like "source /path/to/this/file/virtualenvwrapper_bashrc"
35+
# 3. Add a line like "source /path/to/this/file/virtualenvwrapper.sh"
3636
# to your .bashrc.
3737
# 4. Run: source ~/.bashrc
3838
# 5. Run: workon

virtualenvwrapper/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
"""virtualenvwrapper module
22
"""
33

4-
import os, warnings
5-
docs_root = os.path.join(os.path.dirname(__file__), 'docs', 'index.html')
6-
warnings.warn('Use virtualenvwrapper_bashrc to set up your shell environment. See %s for details.' % docs_root)
4+
__import__('pkg_resources').declare_namespace(__name__)

virtualenvwrapper/hook_loader.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
#
4+
# Copyright (c) 2010 Doug Hellmann. All rights reserved.
5+
#
6+
"""Load hooks for virtualenvwrapper.
7+
"""
8+
9+
import optparse
10+
11+
import pkg_resources
12+
13+
def main():
14+
parser = optparse.OptionParser(
15+
usage='usage: %prog [options] <hook> [options]',
16+
prog='virtualenvwrapper.hook_loader',
17+
description='Manage hooks for virtualenvwrapper',
18+
)
19+
options, args = parser.parse_args()
20+
21+
hook = args[0]
22+
print hook
23+
24+
for ep in pkg_resources.iter_entry_points('virtualenvwrapper.%s' % hook):
25+
plugin = ep.load()
26+
print plugin()
27+
return 0
28+
29+
if __name__ == '__main__':
30+
main()

virtualenvwrapper/make_hooks.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
#
4+
# Copyright (c) 2010 Doug Hellmann. All rights reserved.
5+
#
6+
"""Plugin to create hooks during initialization phase.
7+
"""
8+
9+
import pkg_resources
10+
11+
def pre_initialize_source():
12+
return pkg_resources.resource_string(__name__, 'make_hooks_pre_initialize.sh')
13+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#
2+
# Create the default hooks, if they do not exist on startup.
3+
#
4+
5+
# Create a hook script
6+
#
7+
# Usage: virtualenvwrapper_make_hook filename comment
8+
#
9+
function virtualenvwrapper_make_hook () {
10+
filename="$1"
11+
comment="$2"
12+
if [ ! -f "$filename" ]
13+
then
14+
#echo "Creating $filename"
15+
cat - > "$filename" <<EOF
16+
#!/bin/sh
17+
# $comment
18+
19+
EOF
20+
fi
21+
if [ ! -x "$filename" ]
22+
then
23+
chmod +x "$filename"
24+
fi
25+
}
26+
27+
# initialize
28+
virtualenvwrapper_make_hook "$WORKON_HOME/preinitialize" \
29+
"This hook is run early in the startup phase when loading virtualenvwrapper.sh."
30+
virtualenvwrapper_make_hook "$WORKON_HOME/postinitialize" \
31+
"This hook is run later in the startup phase when loading virtualenvwrapper.sh."
32+
# mkvirtualenv
33+
virtualenvwrapper_make_hook "$WORKON_HOME/premkvirtualenv" \
34+
"This hook is run after a new virtualenv is created and before it is activated."
35+
virtualenvwrapper_make_hook "$WORKON_HOME/postmkvirtualenv" \
36+
"This hook is run after a new virtualenv is activated."
37+
# rmvirtualenv
38+
virtualenvwrapper_make_hook "$WORKON_HOME/prermvirtualenv" \
39+
"This hook is run before a virtualenv is deleted."
40+
virtualenvwrapper_make_hook "$WORKON_HOME/postrmvirtualenv" \
41+
"This hook is run after a virtualenv is deleted."
42+
# deactivate
43+
virtualenvwrapper_make_hook "$WORKON_HOME/predeactivate" \
44+
"This hook is run before every virtualenv is deactivated."
45+
virtualenvwrapper_make_hook "$WORKON_HOME/postdeactivate" \
46+
"This hook is run after every virtualenv is deactivated."
47+
# activate
48+
virtualenvwrapper_make_hook "$WORKON_HOME/preactivate" \
49+
"This hook is run before every virtualenv is activated."
50+
virtualenvwrapper_make_hook "$WORKON_HOME/postactivate" \
51+
"This hook is run after every virtualenv is activated."

virtualenvwrapper/user_scripts.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
#
4+
# Copyright (c) 2010 Doug Hellmann. All rights reserved.
5+
#
6+
"""Plugin to handle hooks in user-defined scripts.
7+
"""
8+
9+
import pkg_resources
10+
11+
def pre_initialize_source():
12+
return pkg_resources.resource_string(__name__, 'user_scripts_pre_initialize.sh')
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#
2+
# Run user-provided initialization scripts
3+
#
4+
global_script="$WORKON_HOME/preinitialize"
5+
[ -f "$global_script" ] && source "$global_script"

0 commit comments

Comments
 (0)