Skip to content

Commit e96ed1a

Browse files
committed
Add allvirtualenv command
Add a new command to run a command across all of the managed virtualenvs. Resolves issue #186. Signed-off-by: Doug Hellmann <[email protected]>
1 parent c91167d commit e96ed1a

File tree

4 files changed

+83
-5
lines changed

4 files changed

+83
-5
lines changed

docs/source/command_ref.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,27 @@ Syntax::
204204
* :ref:`scripts-premkvirtualenv`
205205
* :ref:`scripts-postmkvirtualenv`
206206

207+
.. _command-allvirtualenv:
208+
209+
allvirtualenv
210+
-------------
211+
212+
Run a command in all virtualenvs under WORKON_HOME.
213+
214+
Syntax::
215+
216+
allenvs command with arguments
217+
218+
Each virtualenv is activated, bypassing activation hooks, the current
219+
working directory is changed to the current virtualenv, and then the
220+
command is run. Commands cannot modify the current shell state, but
221+
can modify the virtualenv.
222+
223+
::
224+
225+
$ allenvs pip install -U pip
226+
227+
207228
==================================
208229
Controlling the Active Environment
209230
==================================

docs/source/history.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ dev
1515
:bbissue:`188`.
1616
- Fix detection of ``--python`` option to
1717
:ref:`command-mkvirtualenv`. Resolves :bbissue:`190`.
18+
- Add :ref:`command-allvirtualenv` command to run a command across all
19+
virtualenvs. Suggested by Dave Coutts in :bbissue:`186`.
1820

1921
4.0
2022
===

tests/test_allvirtualenv.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# -*- mode: shell-script -*-
2+
3+
test_dir=$(cd $(dirname $0) && pwd)
4+
source "$test_dir/setup.sh"
5+
6+
oneTimeSetUp() {
7+
rm -rf "$WORKON_HOME"
8+
mkdir -p "$WORKON_HOME"
9+
unset VIRTUAL_ENV
10+
source "$test_dir/../virtualenvwrapper.sh"
11+
mkvirtualenv test1 >/dev/null 2>&1
12+
mkvirtualenv test2 >/dev/null 2>&1
13+
deactivate
14+
}
15+
16+
oneTimeTearDown() {
17+
rm -rf "$WORKON_HOME"
18+
}
19+
20+
setUp () {
21+
echo
22+
rm -f "$test_dir/catch_output"
23+
}
24+
25+
tearDown () {
26+
deactivate >/dev/null 2>&1
27+
}
28+
29+
test_allvirtualenv_all() {
30+
assertTrue "Did not find test1" "allvirtualenv pwd | grep -q 'test1$'"
31+
assertTrue "Did not find test2" "allvirtualenv pwd | grep -q 'test2$'"
32+
}
33+
34+
. "$test_dir/shunit2"

virtualenvwrapper.sh

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -592,10 +592,11 @@ function lsvirtualenv {
592592

593593
if $long_mode
594594
then
595-
for env_name in $(virtualenvwrapper_show_workon_options)
596-
do
597-
showvirtualenv "$env_name"
598-
done
595+
allenvs showvirtualenv "$env_name"
596+
# for env_name in $(virtualenvwrapper_show_workon_options)
597+
# do
598+
599+
# done
599600
else
600601
virtualenvwrapper_show_workon_options
601602
fi
@@ -616,7 +617,6 @@ function showvirtualenv {
616617
env_name=$(basename "$VIRTUAL_ENV")
617618
fi
618619

619-
echo -n "$env_name"
620620
virtualenvwrapper_run_hook "get_env_details" "$env_name"
621621
echo
622622
}
@@ -1157,6 +1157,27 @@ function wipeenv {
11571157
rm -f "$req_file"
11581158
}
11591159

1160+
#
1161+
# Run a command in each virtualenv
1162+
#
1163+
function allvirtualenv {
1164+
virtualenvwrapper_verify_workon_home || return 1
1165+
typeset d
1166+
1167+
virtualenvwrapper_show_workon_options | while read d
1168+
do
1169+
[ ! -d "$WORKON_HOME/$d" ] && continue
1170+
echo "$d"
1171+
echo "$d" | sed 's/./=/g'
1172+
# Activate the environment, but not with workon
1173+
# because we don't want to trigger any hooks.
1174+
(source "$WORKON_HOME/$d/bin/activate";
1175+
cd "$VIRTUAL_ENV";
1176+
$@)
1177+
echo
1178+
done
1179+
}
1180+
11601181
#
11611182
# Invoke the initialization functions
11621183
#

0 commit comments

Comments
 (0)