@@ -41,15 +41,25 @@ function verify_workon_home () {
41
41
42
42
# Verify that the requested environment exists
43
43
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" ]
46
46
then
47
47
echo "ERROR: Environment '$env_name' does not exist. Create it with 'mkvirtualenv $env_name'."
48
48
return 1
49
49
fi
50
50
return 0
51
51
}
52
52
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
+
53
63
# Create a new environment, in the WORKON_HOME.
54
64
#
55
65
# Usage: mkvirtualenv [options] ENVNAME
@@ -134,3 +144,49 @@ _virtualenvs ()
134
144
135
145
complete -o default -o nospace -F _virtualenvs workon
136
146
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