Commits

Doug Hellmann committed ebfcd01

add path management feature contributed by James Bennett

Comments (0)

Files changed (1)

virtualenvwrapper_bashrc

 
 # Verify that the requested environment exists
 function verify_workon_environment () {
-       typeset env_name="$1"
-       if [ ! -d "$WORKON_HOME/$env_name" ]
+    typeset env_name="$1"
+    if [ ! -d "$WORKON_HOME/$env_name" ]
     then
        echo "ERROR: Environment '$env_name' does not exist. Create it with 'mkvirtualenv $env_name'."
        return 1
     return 0
 }
 
+# Verify that the active environment exists
+function verify_active_environment () {
+    if [ ! -n "${VIRTUAL_ENV}" ] || [ ! -d ${VIRTUAL_ENV} ]
+    then
+        echo "ERROR: no virtualenv active, or active virtualenv is missing"
+        return 1
+    fi
+    return 0
+}
+
 # Create a new environment, in the WORKON_HOME.
 #
 # Usage: mkvirtualenv [options] ENVNAME
 
 complete -o default -o nospace -F _virtualenvs workon
 complete -o default -o nospace -F _virtualenvs rmvirtualenv
+
+# Path management for packages outside of the virtual env.
+# Based on a contribution from James Bennett.
+#
+# add2virtualenv directory1 directory2 ...
+#
+# Adds the specified directories to the Python path for the
+# currently-active virtualenv. This will be done by placing the
+# directory names in a path file named
+# "virtualenv_path_extensions.pth" inside the virtualenv's
+# site-packages directory; if this file does not exist, it will be
+# created first.
+#
+function add2virtualenv () {
+
+    verify_active_environment || return 1
+    
+    site_packages=$VIRTUAL_ENV/lib/python`python -c "import sys; print sys.version[:3]"`/site-packages
+    
+    if [ ! -d "${site_packages}" ]
+    then
+        echo "ERROR: currently-active virtualenv does not appear to have a site-packages directory"
+        return 1
+    fi
+    
+    path_file=$site_packages/virtualenv_path_extensions.pth
+
+    if [ "$*" = "" ]
+    then
+        echo "Usage: add2virtualenv dir [dir ...]"
+        if [ -f "$path_file" ]
+        then
+            echo
+            echo "Existing paths:"
+            cat "$path_file"
+        fi
+        return 1
+    fi
+
+    touch $path_file
+    for pydir in "$@"
+    do
+        echo $pydir >> "$path_file"
+    done
+    return 0
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.