Skip to content

Commit 488d794

Browse files
committed
add more hooks based on suggestion from Chris Hasenpflug; add documentation
1 parent 9b64b67 commit 488d794

File tree

5 files changed

+143
-40
lines changed

5 files changed

+143
-40
lines changed

.hgignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ README.html
22
virtualenvwrapper.egg-info
33
paver-minilib.zip
44
setup.py
5+
trace.txt

README

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,59 @@ Sometimes it is desirable to share installed packages that are not in the system
2828
3. Run: ``add2virtualenv``.
2929
4. A usage message and list of current "extra" paths is printed.
3030

31-
==================
32-
Activation Scripts
33-
==================
34-
35-
virtualenvwrapper adds two separate hook scripts you can use to change your settings when moving between environments. They are *sourced* by ``workon`` at the appropriate trigger time, allowing them to modify your shell environment.
31+
============
32+
Hook Scripts
33+
============
3634

37-
Both scripts are bash shell scripts and need to be saved in ``$VIRTUAL_ENV/bin/``.
35+
virtualenvwrapper adds several hook points you can use to change your settings when creating,
36+
deleting, or moving between environments. They are either *sourced* (allowing them to modify
37+
your shell environment) or run as an external program at the appropriate trigger time.
3838

39-
postactivate
40-
============
39+
$VIRTUAL_ENV/bin/postactivate
40+
=============================
4141

42-
The ``postactivate`` script is run after the new environment is enabled. ``$VIRTUAL_ENV`` refers to the new environment at the time the script runs.
42+
The ``postactivate`` script is sourced after the new environment is enabled. ``$VIRTUAL_ENV``
43+
refers to the new environment at the time the script runs.
4344

44-
This example script for the PyMOTW environment changes the current working directory and the PATH variable to refer to the source tree containing the PyMOTW source.
45+
This example script for the PyMOTW environment changes the current working directory and the
46+
PATH variable to refer to the source tree containing the PyMOTW source.
4547

4648
::
4749

48-
pymotw_root=/Users/dhellmann/Documents/PyMOTW
49-
cd $pymotw_root
50-
PATH=$pymotw_root/bin:$PATH
50+
pymotw_root=/Users/dhellmann/Documents/PyMOTW
51+
cd $pymotw_root
52+
PATH=$pymotw_root/bin:$PATH
53+
54+
$VIRTUAL_ENV/bin/predeactivate
55+
==============================
56+
57+
The ``predeactivate`` script is source before the current environment is deactivated, and can
58+
be used to disable or clear settings in your environment. ``$VIRTUAL_ENV`` refers to the old
59+
environment at the time the script runs.
60+
61+
$WORKON_HOME/premkvirtualenv
62+
=============================
5163

52-
predeactivate
53-
=============
64+
The ``premkvirtualenv`` script is run as an external program after the virtual environment is
65+
created but before the current environment is switched to point to the new env. The current
66+
working directory for the script is ``$WORKON_HOME`` and the name of the new environment is
67+
passed as an argument to the script.
5468

55-
The ``predeactivate`` script is run before the current environment is deactivated, and can be used to disable or clear settings in your environment. ``$VIRTUAL_ENV`` refers to the old environment at the time the script runs.
69+
$WORKON_HOME/postmkvirtualenv
70+
=============================
71+
72+
The ``postmkvirtualenv`` script is sourced after the new environment is created and
73+
activated.
74+
75+
$WORKON_HOME/prermvirtualenv
76+
============================
77+
78+
The ``prermvirtualenv`` script is run as an external program before the environment is removed. The full path to the environment directory is passed as an argument to the script.
79+
80+
$WORKON_HOME/postrmvirtualenv
81+
=============================
82+
83+
The ``postrmvirtualenv`` script is run as an external program after the environment is removed. The full path to the environment directory is passed as an argument to the script.
5684

5785
===============
5886
Path Management
@@ -70,6 +98,9 @@ For more details, refer to the column I wrote for the May 2008 issue of Python M
7098
Updates
7199
=======
72100

101+
1.9
102+
- Add more hooks for operations to run before and after creating or deleting environments based on changes from Chris Hasenpflug.
103+
73104
1.8.1
74105
- Corrected a problem with change to mkvirtualenv that lead to release 1.8 by using an alternate fix proposed by James in comments on release 1.4.
75106

pavement.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
# What project are we building?
2424
PROJECT = 'virtualenvwrapper'
25-
VERSION = '1.8.1'
25+
VERSION = '1.9'
2626

2727
# Read the long description to give to setup
2828
README_FILE = 'README'

test.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/bin/sh
2+
3+
#set -x
4+
5+
export WORKON_HOME="./WORKON_HOME"
6+
mkdir -p $WORKON_HOME
7+
8+
function mk_test_hook () {
9+
hookname="$1"
10+
echo "echo \"$hookname\" \$@" > $WORKON_HOME/$hookname
11+
chmod +x $WORKON_HOME/$hookname
12+
}
13+
14+
mk_test_hook premkvirtualenv
15+
mk_test_hook postmkvirtualenv
16+
mk_test_hook prermvirtualenv
17+
mk_test_hook postrmvirtualenv
18+
19+
echo
20+
echo "HOOKS:"
21+
ls -l $WORKON_HOME
22+
23+
bindir=$(dirname $0)
24+
source "$bindir/virtualenvwrapper_bashrc"
25+
26+
echo
27+
echo "CREATING AND ACTIVATING"
28+
mkvirtualenv "env1"
29+
echo "Current environment: $VIRTUAL_ENV"
30+
31+
echo
32+
echo "CREATING AND SWITCHING"
33+
mkvirtualenv "env2"
34+
echo "Current environment: $VIRTUAL_ENV"
35+
36+
echo
37+
echo "POSTACTIVATE HOOK"
38+
echo "echo postactivate" > $WORKON_HOME/env1/bin/postactivate
39+
workon env1
40+
41+
echo
42+
echo "DEACTIVATING"
43+
deactivate
44+
45+
echo
46+
echo "LISTING ENVIRONMENTS"
47+
workon
48+
49+
echo
50+
echo "REMOVING ENVIRONMENTS"
51+
rmvirtualenv "env1"
52+
rmvirtualenv "env2"
53+
54+
rm -rf $WORKON_HOME

virtualenvwrapper_bashrc

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ then
2828
fi
2929

3030
# Verify that the WORKON_HOME directory exists
31-
function verify_workon_home () {
31+
function virtualenvwrapper_verify_workon_home () {
3232
if [ ! -d "$WORKON_HOME" ]
3333
then
3434
echo "ERROR: Virtual environments directory '$WORKON_HOME' does not exist."
@@ -38,7 +38,7 @@ function verify_workon_home () {
3838
}
3939

4040
# Verify that the requested environment exists
41-
function verify_workon_environment () {
41+
function virtualenvwrapper_verify_workon_environment () {
4242
typeset env_name="$1"
4343
if [ ! -d "$WORKON_HOME/$env_name" ]
4444
then
@@ -49,7 +49,7 @@ function verify_workon_environment () {
4949
}
5050

5151
# Verify that the active environment exists
52-
function verify_active_environment () {
52+
function virtualenvwrapper_verify_active_environment () {
5353
if [ ! -n "${VIRTUAL_ENV}" ] || [ ! -d "${VIRTUAL_ENV}" ]
5454
then
5555
echo "ERROR: no virtualenv active, or active virtualenv is missing"
@@ -58,22 +58,43 @@ function verify_active_environment () {
5858
return 0
5959
}
6060

61+
function virtualenvwrapper_source_hook () {
62+
scriptname="$1"
63+
if [ -f "$scriptname" ]
64+
then
65+
source "$scriptname"
66+
fi
67+
}
68+
69+
function virtualenvwrapper_run_hook () {
70+
scriptname="$1"
71+
shift
72+
if [ -x "$scriptname" ]
73+
then
74+
"$scriptname" "$@"
75+
fi
76+
}
77+
6178
# Create a new environment, in the WORKON_HOME.
6279
#
6380
# Usage: mkvirtualenv [options] ENVNAME
6481
# (where the options are passed directly to virtualenv)
6582
#
6683
function mkvirtualenv () {
6784
eval "envname=\$$#"
68-
verify_workon_home
69-
(cd "$WORKON_HOME"; virtualenv "$@")
85+
virtualenvwrapper_verify_workon_home
86+
(cd "$WORKON_HOME";
87+
virtualenv "$@";
88+
virtualenvwrapper_run_hook "./premkvirtualenv" "$envname"
89+
)
7090
workon "$envname"
91+
virtualenvwrapper_source_hook "$WORKON_HOME/postmkvirtualenv"
7192
}
7293

7394
# Remove an environment, in the WORKON_HOME.
7495
function rmvirtualenv () {
7596
typeset env_name="$1"
76-
verify_workon_home
97+
virtualenvwrapper_verify_workon_home
7798
if [ "$env_name" = "" ]
7899
then
79100
echo "Please specify an enviroment."
@@ -86,13 +107,15 @@ function rmvirtualenv () {
86107
echo "Either switch to another environment, or run 'deactivate'."
87108
return 1
88109
fi
110+
virtualenvwrapper_run_hook "$WORKON_HOME/prermvirtualenv" "$env_dir"
89111
rm -rf "$env_dir"
112+
virtualenvwrapper_run_hook "$WORKON_HOME/postrmvirtualenv" "$env_dir"
90113
}
91114

92115
# List the available environments.
93-
function show_workon_options () {
94-
verify_workon_home
95-
ls "$WORKON_HOME" | egrep -v '*.egg' | sort
116+
function virtualenvwrapper_show_workon_options () {
117+
virtualenvwrapper_verify_workon_home
118+
ls "$WORKON_HOME" | egrep -v '*.egg' | egrep -v '(pre|post)(rm|mk)virtualenv' | sort
96119
}
97120

98121
# List or change working virtual environments
@@ -103,12 +126,12 @@ function workon () {
103126
typeset env_name="$1"
104127
if [ "$env_name" = "" ]
105128
then
106-
show_workon_options
129+
virtualenvwrapper_show_workon_options
107130
return 1
108131
fi
109132

110-
verify_workon_home || return 1
111-
verify_workon_environment $env_name || return 1
133+
virtualenvwrapper_verify_workon_home || return 1
134+
virtualenvwrapper_verify_workon_environment $env_name || return 1
112135

113136
activate="$WORKON_HOME/$env_name/bin/activate"
114137
if [ ! -f "$activate" ]
@@ -117,17 +140,11 @@ function workon () {
117140
return 1
118141
fi
119142

120-
if [ -f "$VIRTUAL_ENV/bin/predeactivate" ]
121-
then
122-
source "$VIRTUAL_ENV/bin/predeactivate"
123-
fi
143+
virtualenvwrapper_source_hook "$VIRTUAL_ENV/bin/predeactivate"
124144

125145
source "$activate"
126146

127-
if [ -f "$VIRTUAL_ENV/bin/postactivate" ]
128-
then
129-
source "$VIRTUAL_ENV/bin/postactivate"
130-
fi
147+
virtualenvwrapper_source_hook "$VIRTUAL_ENV/bin/postactivate"
131148
return 0
132149
}
133150

@@ -140,13 +157,13 @@ if [ -n "$BASH" ] ; then
140157
_virtualenvs ()
141158
{
142159
local cur="${COMP_WORDS[COMP_CWORD]}"
143-
COMPREPLY=( $(compgen -W "`show_workon_options`" -- ${cur}) )
160+
COMPREPLY=( $(compgen -W "`virtualenvwrapper_show_workon_options`" -- ${cur}) )
144161
}
145162

146163
complete -o default -o nospace -F _virtualenvs workon
147164
complete -o default -o nospace -F _virtualenvs rmvirtualenv
148165
elif [ -n "$ZSH_VERSION" ] ; then
149-
compctl -g "`show_workon_options`" workon rmvirtualenv
166+
compctl -g "`virtualenvwrapper_show_workon_options`" workon rmvirtualenv
150167
fi
151168

152169
# Path management for packages outside of the virtual env.
@@ -163,7 +180,7 @@ fi
163180
#
164181
function add2virtualenv () {
165182

166-
verify_active_environment || return 1
183+
virtualenvwrapper_verify_active_environment || return 1
167184

168185
pyvers="`python -c 'import sys; print sys.version[:3]'`"
169186
site_packages="$VIRTUAL_ENV/lib/python${pyvers}/site-packages"

0 commit comments

Comments
 (0)