Skip to content

Commit c24fc82

Browse files
committed
add path management feature contributed by James Bennett
--HG-- extra : convert_revision : svn%3A98f53aa3-d424-0410-b225-a548b0275c4d/Projects/virtualenvwrapper/trunk%401767
1 parent d7f357c commit c24fc82

File tree

1 file changed

+58
-2
lines changed

1 file changed

+58
-2
lines changed

virtualenvwrapper_bashrc

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,25 @@ function verify_workon_home () {
4141

4242
# Verify that the requested environment exists
4343
function verify_workon_environment () {
44-
typeset env_name="$1"
45-
if [ ! -d "$WORKON_HOME/$env_name" ]
44+
typeset env_name="$1"
45+
if [ ! -d "$WORKON_HOME/$env_name" ]
4646
then
4747
echo "ERROR: Environment '$env_name' does not exist. Create it with 'mkvirtualenv $env_name'."
4848
return 1
4949
fi
5050
return 0
5151
}
5252

53+
# Verify that the active environment exists
54+
function verify_active_environment () {
55+
if [ ! -n "${VIRTUAL_ENV}" ] || [ ! -d ${VIRTUAL_ENV} ]
56+
then
57+
echo "ERROR: no virtualenv active, or active virtualenv is missing"
58+
return 1
59+
fi
60+
return 0
61+
}
62+
5363
# Create a new environment, in the WORKON_HOME.
5464
#
5565
# Usage: mkvirtualenv [options] ENVNAME
@@ -134,3 +144,49 @@ _virtualenvs ()
134144

135145
complete -o default -o nospace -F _virtualenvs workon
136146
complete -o default -o nospace -F _virtualenvs rmvirtualenv
147+
148+
# Path management for packages outside of the virtual env.
149+
# Based on a contribution from James Bennett.
150+
#
151+
# add2virtualenv directory1 directory2 ...
152+
#
153+
# Adds the specified directories to the Python path for the
154+
# currently-active virtualenv. This will be done by placing the
155+
# directory names in a path file named
156+
# "virtualenv_path_extensions.pth" inside the virtualenv's
157+
# site-packages directory; if this file does not exist, it will be
158+
# created first.
159+
#
160+
function add2virtualenv () {
161+
162+
verify_active_environment || return 1
163+
164+
site_packages=$VIRTUAL_ENV/lib/python`python -c "import sys; print sys.version[:3]"`/site-packages
165+
166+
if [ ! -d "${site_packages}" ]
167+
then
168+
echo "ERROR: currently-active virtualenv does not appear to have a site-packages directory"
169+
return 1
170+
fi
171+
172+
path_file=$site_packages/virtualenv_path_extensions.pth
173+
174+
if [ "$*" = "" ]
175+
then
176+
echo "Usage: add2virtualenv dir [dir ...]"
177+
if [ -f "$path_file" ]
178+
then
179+
echo
180+
echo "Existing paths:"
181+
cat "$path_file"
182+
fi
183+
return 1
184+
fi
185+
186+
touch $path_file
187+
for pydir in "$@"
188+
do
189+
echo $pydir >> "$path_file"
190+
done
191+
return 0
192+
}

0 commit comments

Comments
 (0)