Commits

Doug Hellmann committed f66d903 Merge

Merged in miracle2k/virtualenvwrapper (pull request #6)

Comments (0)

Files changed (2)

tests/test_add2virtualenv.sh

 
 test_add2virtualenv () {
     mkvirtualenv "pathtest"
-    add2virtualenv "/full/path"
+    full_path=$(pwd)
+    add2virtualenv "$full_path"
     cdsitepackages
-    path_file="./virtualenv_path_extensions.pth"
-    assertTrue "No /full/path in `cat $path_file`" "grep /full/path $path_file"
+    # Check contents of path file
+    path_file="./_virtualenv_path_extensions.pth"
+    assertTrue "No $full_path in `cat $path_file`" "grep $full_path $path_file"
+    assertTrue "No path insert code in `cat $path_file`" "grep sys.__egginsert $path_file"
+    # Check the path we inserted is actually at the top
+    expected=$full_path
+    actual=$($WORKON_HOME/pathtest/bin/python -c "import sys; print sys.path[1]")
+    assertSame "$expected" "$actual"
     cd -
 }
 
     base_dir=$(basename $(pwd))
     add2virtualenv "../$base_dir"
     cdsitepackages
-    path_file="./virtualenv_path_extensions.pth"
+    path_file="./_virtualenv_path_extensions.pth"
     assertTrue "No $parent_dir/$base_dir in \"`cat $path_file`\"" "grep \"$parent_dir/$base_dir\" $path_file"
     cd - >/dev/null 2>&1
 }
 
+test_add2virtualenv_delete () {
+    mkvirtualenv "pathtest"
+    add2virtualenv "/full/path"
+    add2virtualenv -d "/full/path"
+    cdsitepackages
+    # Check contents of path file
+    path_file="./_virtualenv_path_extensions.pth"
+    assertFalse "/full/path in `cat $path_file`" "grep /full/path $path_file"
+    cd -
+}
+
 
 . "$test_dir/shunit2"

virtualenvwrapper.sh

         return 1
     fi
     
-    path_file="$site_packages/virtualenv_path_extensions.pth"
+    # Prefix with _ to ensure we are loaded as early as possible,
+    # and at least before easy_install.pth.
+    path_file="$site_packages/_virtualenv_path_extensions.pth"
 
     if [ "$*" = "" ]
     then
         then
             echo
             echo "Existing paths:"
-            cat "$path_file"
+            cat "$path_file" | grep -v "^import"
         fi
         return 1
     fi
 
-    touch "$path_file"
+    remove=0
+    if [ "$1" = "-d" ]
+    then
+        remove=1
+        shift
+    fi
+
+    if [ ! -f "$path_file" ]
+    then
+        echo "import sys; sys.__plen = len(sys.path)" >> "$path_file"
+        echo "import sys; new=sys.path[sys.__plen:]; del sys.path[sys.__plen:]; p=getattr(sys,'__egginsert',0); sys.path[p:p]=new; sys.__egginsert = p+len(new)" >> "$path_file"
+    fi
+
     for pydir in "$@"
     do
         absolute_path=$("$VIRTUALENVWRAPPER_PYTHON" -c "import os; print os.path.abspath(\"$pydir\")")
         then
             echo "Warning: Converting \"$pydir\" to \"$absolute_path\"" 1>&2
         fi
-        echo "$absolute_path" >> "$path_file"
+
+        if [ $remove -eq 1 ]
+        then
+            sed -i "\:^$absolute_path$: d" "$path_file"
+        else
+            sed -i "1a $absolute_path" "$path_file"
+        fi
     done
     return 0
 }
     typeset site_packages="`virtualenvwrapper_get_site_packages_dir`"
     ls $@ $site_packages
     
-    path_file="$site_packages/virtualenv_path_extensions.pth"
+    path_file="$site_packages/_virtualenv_path_extensions.pth"
     if [ -f "$path_file" ]
     then
         echo
-        echo "virtualenv_path_extensions.pth:"
+        echo "_virtualenv_path_extensions.pth:"
         cat "$path_file"
     fi
 }