Skip to content

Commit ecdbfa2

Browse files
committed
Merged in ciberglo/virtualenvwrapper (pull request #13)
2 parents 5545127 + 0e9d170 commit ecdbfa2

File tree

2 files changed

+61
-43
lines changed

2 files changed

+61
-43
lines changed

tests/test_rmvirtualenv.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ test_remove () {
2929
assertFalse "[ -d $WORKON_HOME/deleteme ]"
3030
}
3131

32+
test_remove_several_envs () {
33+
mkvirtualenv "deleteme" >/dev/null 2>&1
34+
assertTrue "[ -d $WORKON_HOME/deleteme ]"
35+
deactivate
36+
mkvirtualenv "deleteme2" >/dev/null 2>&1
37+
assertTrue "[ -d $WORKON_HOME/deleteme2 ]"
38+
deactivate
39+
rmvirtualenv "deleteme deleteme2"
40+
assertFalse "[ -d $WORKON_HOME/deleteme ]"
41+
assertFalse "[ -d $WORKON_HOME/deleteme2 ]"
42+
}
43+
3244
test_within_virtualenv () {
3345
mkvirtualenv "deleteme" >/dev/null 2>&1
3446
assertTrue "[ -d $WORKON_HOME/deleteme ]"

virtualenvwrapper.sh

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ VIRTUALENVWRAPPER_ENV_BIN_DIR="bin"
6161
if [ "$OS" = "Windows_NT" ] && [ "$MSYSTEM" = "MINGW32" ]
6262
then
6363
# Only assign this for msys, cygwin use standard Unix paths
64-
# and its own python installation
64+
# and its own python installation
6565
VIRTUALENVWRAPPER_ENV_BIN_DIR="Scripts"
6666
fi
6767

@@ -156,7 +156,7 @@ function virtualenvwrapper_run_hook {
156156
fi
157157
"$VIRTUALENVWRAPPER_PYTHON" -c 'from virtualenvwrapper.hook_loader import main; main()' $HOOK_VERBOSE_OPTION --script "$hook_script" "$@"
158158
result=$?
159-
159+
160160
if [ $result -eq 0 ]
161161
then
162162
if [ ! -f "$hook_script" ]
@@ -171,7 +171,7 @@ function virtualenvwrapper_run_hook {
171171
return $result
172172
}
173173

174-
# Set up tab completion. (Adapted from Arthur Koziel's version at
174+
# Set up tab completion. (Adapted from Arthur Koziel's version at
175175
# http://arthurkoziel.com/2008/10/11/virtualenvwrapper-bash-completion/)
176176
function virtualenvwrapper_setup_tab_completion {
177177
if [ -n "$BASH" ] ; then
@@ -203,7 +203,7 @@ function virtualenvwrapper_setup_tab_completion {
203203
_cdsitepackages_complete () {
204204
reply=( $(cdsitepackages && ls -d ${1}*) )
205205
}
206-
compctl -K _virtualenvs workon rmvirtualenv cpvirtualenv showvirtualenv
206+
compctl -K _virtualenvs workon rmvirtualenv cpvirtualenv showvirtualenv
207207
compctl -K _cdvirtualenv_complete cdvirtualenv
208208
compctl -K _cdsitepackages_complete cdsitepackages
209209
fi
@@ -399,35 +399,41 @@ function mkvirtualenv {
399399

400400
# Remove an environment, in the WORKON_HOME.
401401
function rmvirtualenv {
402-
typeset env_name="$1"
403402
virtualenvwrapper_verify_workon_home || return 1
404-
if [ "$env_name" = "" ]
403+
if [ ${#@} = 0 ]
405404
then
406405
echo "Please specify an enviroment." >&2
407406
return 1
408407
fi
409-
env_dir="$WORKON_HOME/$env_name"
410-
if [ "$VIRTUAL_ENV" = "$env_dir" ]
411-
then
412-
echo "ERROR: You cannot remove the active environment ('$env_name')." >&2
413-
echo "Either switch to another environment, or run 'deactivate'." >&2
414-
return 1
415-
fi
416408

417-
# Move out of the current directory to one known to be
418-
# safe, in case we are inside the environment somewhere.
419-
typeset prior_dir="$(pwd)"
420-
\cd "$WORKON_HOME"
409+
# support to remove several environments
410+
typeset env_name
411+
for env_name in $@
412+
do
413+
echo "Removing $env_name..."
414+
typeset env_dir="$WORKON_HOME/$env_name"
415+
if [ "$VIRTUAL_ENV" = "$env_dir" ]
416+
then
417+
echo "ERROR: You cannot remove the active environment ('$env_name')." >&2
418+
echo "Either switch to another environment, or run 'deactivate'." >&2
419+
return 1
420+
fi
421421

422-
virtualenvwrapper_run_hook "pre_rmvirtualenv" "$env_name"
423-
\rm -rf "$env_dir"
424-
virtualenvwrapper_run_hook "post_rmvirtualenv" "$env_name"
422+
# Move out of the current directory to one known to be
423+
# safe, in case we are inside the environment somewhere.
424+
typeset prior_dir="$(pwd)"
425+
\cd "$WORKON_HOME"
425426

426-
# If the directory we used to be in still exists, move back to it.
427-
if [ -d "$prior_dir" ]
428-
then
429-
\cd "$prior_dir"
430-
fi
427+
virtualenvwrapper_run_hook "pre_rmvirtualenv" "$env_name"
428+
\rm -rf "$env_dir"
429+
virtualenvwrapper_run_hook "post_rmvirtualenv" "$env_name"
430+
431+
# If the directory we used to be in still exists, move back to it.
432+
if [ -d "$prior_dir" ]
433+
then
434+
\cd "$prior_dir"
435+
fi
436+
done
431437
}
432438

433439
# List the available environments.
@@ -452,9 +458,9 @@ function _lsvirtualenv_usage {
452458
#
453459
# Usage: lsvirtualenv [-l]
454460
function lsvirtualenv {
455-
461+
456462
typeset long_mode=true
457-
if command -v "getopts" &> /dev/null
463+
if command -v "getopts" &> /dev/null
458464
then
459465
# Use getopts when possible
460466
OPTIND=1
@@ -535,14 +541,14 @@ function workon {
535541

536542
virtualenvwrapper_verify_workon_home || return 1
537543
virtualenvwrapper_verify_workon_environment $env_name || return 1
538-
544+
539545
activate="$WORKON_HOME/$env_name/$VIRTUALENVWRAPPER_ENV_BIN_DIR/activate"
540546
if [ ! -f "$activate" ]
541547
then
542548
echo "ERROR: Environment '$WORKON_HOME/$env_name' does not contain an activate script." >&2
543549
return 1
544550
fi
545-
551+
546552
# Deactivate any current environment "destructively"
547553
# before switching so we use our override function,
548554
# if it exists.
@@ -554,9 +560,9 @@ function workon {
554560
fi
555561

556562
virtualenvwrapper_run_hook "pre_activate" "$env_name"
557-
563+
558564
source "$activate"
559-
565+
560566
# Save the deactivate function from virtualenv under a different name
561567
virtualenvwrapper_original_deactivate=`typeset -f deactivate | sed 's/deactivate/virtualenv_deactivate/g'`
562568
eval "$virtualenvwrapper_original_deactivate"
@@ -568,10 +574,10 @@ function workon {
568574
# Call the local hook before the global so we can undo
569575
# any settings made by the local postactivate first.
570576
virtualenvwrapper_run_hook "pre_deactivate"
571-
577+
572578
env_postdeactivate_hook="$VIRTUAL_ENV/$VIRTUALENVWRAPPER_ENV_BIN_DIR/postdeactivate"
573579
old_env=$(basename "$VIRTUAL_ENV")
574-
580+
575581
# Call the original function.
576582
virtualenv_deactivate $1
577583
@@ -585,9 +591,9 @@ function workon {
585591
fi
586592
587593
}'
588-
594+
589595
virtualenvwrapper_run_hook "post_activate"
590-
596+
591597
return 0
592598
}
593599

@@ -603,7 +609,7 @@ function virtualenvwrapper_get_python_version {
603609

604610
# Prints the path to the site-packages directory for the current environment.
605611
function virtualenvwrapper_get_site_packages_dir {
606-
echo "$VIRTUAL_ENV/lib/python`virtualenvwrapper_get_python_version`/site-packages"
612+
echo "$VIRTUAL_ENV/lib/python`virtualenvwrapper_get_python_version`/site-packages"
607613
}
608614

609615
# Path management for packages outside of the virtual env.
@@ -620,15 +626,15 @@ function virtualenvwrapper_get_site_packages_dir {
620626
function add2virtualenv {
621627
virtualenvwrapper_verify_workon_home || return 1
622628
virtualenvwrapper_verify_active_environment || return 1
623-
629+
624630
site_packages="`virtualenvwrapper_get_site_packages_dir`"
625-
631+
626632
if [ ! -d "${site_packages}" ]
627633
then
628634
echo "ERROR: currently-active virtualenv does not appear to have a site-packages directory" >&2
629635
return 1
630636
fi
631-
637+
632638
# Prefix with _ to ensure we are loaded as early as possible,
633639
# and at least before easy_install.pth.
634640
path_file="$site_packages/_virtualenv_path_extensions.pth"
@@ -702,7 +708,7 @@ function lssitepackages {
702708
virtualenvwrapper_verify_active_environment || return 1
703709
typeset site_packages="`virtualenvwrapper_get_site_packages_dir`"
704710
ls $@ $site_packages
705-
711+
706712
path_file="$site_packages/_virtualenv_path_extensions.pth"
707713
if [ -f "$path_file" ]
708714
then
@@ -749,7 +755,7 @@ function cpvirtualenv {
749755
fi
750756
typeset source_env="$env_home$env_name"
751757
typeset target_env="$env_home$new_env"
752-
758+
753759
if [ ! -e "$source_env" ]
754760
then
755761
echo "$env_name virtualenv doesn't exist"
@@ -768,7 +774,7 @@ function cpvirtualenv {
768774
"$VIRTUALENVWRAPPER_VIRTUALENV" "$target_env" --relocatable
769775
\sed "s/VIRTUAL_ENV\(.*\)$env_name/VIRTUAL_ENV\1$new_env/g" < "$source_env/$VIRTUALENVWRAPPER_ENV_BIN_DIR/activate" > "$target_env/$VIRTUALENVWRAPPER_ENV_BIN_DIR/activate"
770776

771-
(\cd "$WORKON_HOME" && (
777+
(\cd "$WORKON_HOME" && (
772778
virtualenvwrapper_run_hook "pre_cpvirtualenv" "$env_name" "$new_env";
773779
virtualenvwrapper_run_hook "pre_mkvirtualenv" "$new_env"
774780
))
@@ -797,7 +803,7 @@ function virtualenvwrapper_verify_project_home {
797803
}
798804

799805
# Given a virtualenv directory and a project directory,
800-
# set the virtualenv up to be associated with the
806+
# set the virtualenv up to be associated with the
801807
# project
802808
function setvirtualenvproject {
803809
typeset venv="$1"

0 commit comments

Comments
 (0)