Skip to content

Commit 74eb3e4

Browse files
committed
Merged in miracle2k/virtualenvwrapper (pull request #6)
2 parents dc96568 + 7b08805 commit 74eb3e4

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

tests/test_add2virtualenv.sh

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,17 @@ setUp () {
2323

2424
test_add2virtualenv () {
2525
mkvirtualenv "pathtest"
26-
add2virtualenv "/full/path"
26+
full_path=$(pwd)
27+
add2virtualenv "$full_path"
2728
cdsitepackages
28-
path_file="./virtualenv_path_extensions.pth"
29-
assertTrue "No /full/path in `cat $path_file`" "grep /full/path $path_file"
29+
# Check contents of path file
30+
path_file="./_virtualenv_path_extensions.pth"
31+
assertTrue "No $full_path in `cat $path_file`" "grep $full_path $path_file"
32+
assertTrue "No path insert code in `cat $path_file`" "grep sys.__egginsert $path_file"
33+
# Check the path we inserted is actually at the top
34+
expected=$full_path
35+
actual=$($WORKON_HOME/pathtest/bin/python -c "import sys; print sys.path[1]")
36+
assertSame "$expected" "$actual"
3037
cd -
3138
}
3239

@@ -36,10 +43,21 @@ test_add2virtualenv_relative () {
3643
base_dir=$(basename $(pwd))
3744
add2virtualenv "../$base_dir"
3845
cdsitepackages
39-
path_file="./virtualenv_path_extensions.pth"
46+
path_file="./_virtualenv_path_extensions.pth"
4047
assertTrue "No $parent_dir/$base_dir in \"`cat $path_file`\"" "grep \"$parent_dir/$base_dir\" $path_file"
4148
cd - >/dev/null 2>&1
4249
}
4350

51+
test_add2virtualenv_delete () {
52+
mkvirtualenv "pathtest"
53+
add2virtualenv "/full/path"
54+
add2virtualenv -d "/full/path"
55+
cdsitepackages
56+
# Check contents of path file
57+
path_file="./_virtualenv_path_extensions.pth"
58+
assertFalse "/full/path in `cat $path_file`" "grep /full/path $path_file"
59+
cd -
60+
}
61+
4462

4563
. "$test_dir/shunit2"

virtualenvwrapper.sh

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,9 @@ function add2virtualenv {
591591
return 1
592592
fi
593593

594-
path_file="$site_packages/virtualenv_path_extensions.pth"
594+
# Prefix with _ to ensure we are loaded as early as possible,
595+
# and at least before easy_install.pth.
596+
path_file="$site_packages/_virtualenv_path_extensions.pth"
595597

596598
if [ "$*" = "" ]
597599
then
@@ -600,20 +602,38 @@ function add2virtualenv {
600602
then
601603
echo
602604
echo "Existing paths:"
603-
cat "$path_file"
605+
cat "$path_file" | grep -v "^import"
604606
fi
605607
return 1
606608
fi
607609

608-
touch "$path_file"
610+
remove=0
611+
if [ "$1" = "-d" ]
612+
then
613+
remove=1
614+
shift
615+
fi
616+
617+
if [ ! -f "$path_file" ]
618+
then
619+
echo "import sys; sys.__plen = len(sys.path)" >> "$path_file"
620+
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"
621+
fi
622+
609623
for pydir in "$@"
610624
do
611625
absolute_path=$("$VIRTUALENVWRAPPER_PYTHON" -c "import os; print os.path.abspath(\"$pydir\")")
612626
if [ "$absolute_path" != "$pydir" ]
613627
then
614628
echo "Warning: Converting \"$pydir\" to \"$absolute_path\"" 1>&2
615629
fi
616-
echo "$absolute_path" >> "$path_file"
630+
631+
if [ $remove -eq 1 ]
632+
then
633+
sed -i "\:^$absolute_path$: d" "$path_file"
634+
else
635+
sed -i "1a $absolute_path" "$path_file"
636+
fi
617637
done
618638
return 0
619639
}
@@ -642,11 +662,11 @@ function lssitepackages {
642662
typeset site_packages="`virtualenvwrapper_get_site_packages_dir`"
643663
ls $@ $site_packages
644664

645-
path_file="$site_packages/virtualenv_path_extensions.pth"
665+
path_file="$site_packages/_virtualenv_path_extensions.pth"
646666
if [ -f "$path_file" ]
647667
then
648668
echo
649-
echo "virtualenv_path_extensions.pth:"
669+
echo "_virtualenv_path_extensions.pth:"
650670
cat "$path_file"
651671
fi
652672
}

0 commit comments

Comments
 (0)