Commits

Doug Hellmann committed e96ed1a

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 <doug.hellmann@gmail.com>

  • Participants
  • Parent commits c91167d

Comments (0)

Files changed (4)

File docs/source/command_ref.rst

    * :ref:`scripts-premkvirtualenv`
    * :ref:`scripts-postmkvirtualenv`
 
+.. _command-allvirtualenv:
+
+allvirtualenv
+-------------
+
+Run a command in all virtualenvs under WORKON_HOME.
+
+Syntax::
+
+    allenvs command with arguments
+
+Each virtualenv is activated, bypassing activation hooks, the current
+working directory is changed to the current virtualenv, and then the
+command is run. Commands cannot modify the current shell state, but
+can modify the virtualenv.
+
+::
+
+  $ allenvs pip install -U pip
+
+
 ==================================
 Controlling the Active Environment
 ==================================

File docs/source/history.rst

   :bbissue:`188`.
 - Fix detection of ``--python`` option to
   :ref:`command-mkvirtualenv`. Resolves :bbissue:`190`.
+- Add :ref:`command-allvirtualenv` command to run a command across all
+  virtualenvs. Suggested by Dave Coutts in :bbissue:`186`.
 
 4.0
 ===

File tests/test_allvirtualenv.sh

+# -*- mode: shell-script -*-
+
+test_dir=$(cd $(dirname $0) && pwd)
+source "$test_dir/setup.sh"
+
+oneTimeSetUp() {
+    rm -rf "$WORKON_HOME"
+    mkdir -p "$WORKON_HOME"
+    unset VIRTUAL_ENV
+    source "$test_dir/../virtualenvwrapper.sh"
+    mkvirtualenv test1 >/dev/null 2>&1
+    mkvirtualenv test2 >/dev/null 2>&1
+    deactivate
+}
+
+oneTimeTearDown() {
+    rm -rf "$WORKON_HOME"
+}
+
+setUp () {
+    echo
+    rm -f "$test_dir/catch_output"
+}
+
+tearDown () {
+    deactivate >/dev/null 2>&1
+}
+
+test_allvirtualenv_all() {
+    assertTrue "Did not find test1" "allvirtualenv pwd | grep -q 'test1$'"
+    assertTrue "Did not find test2" "allvirtualenv pwd | grep -q 'test2$'"
+}
+
+. "$test_dir/shunit2"

File virtualenvwrapper.sh

 
     if $long_mode
     then
-        for env_name in $(virtualenvwrapper_show_workon_options)
-        do
-            showvirtualenv "$env_name"
-        done
+        allenvs showvirtualenv "$env_name"
+        # for env_name in $(virtualenvwrapper_show_workon_options)
+        # do
+            
+        # done
     else
         virtualenvwrapper_show_workon_options
     fi
         env_name=$(basename "$VIRTUAL_ENV")
     fi
 
-    echo -n "$env_name"
     virtualenvwrapper_run_hook "get_env_details" "$env_name"
     echo
 }
 }
 
 #
+# Run a command in each virtualenv
+#
+function allvirtualenv {
+    virtualenvwrapper_verify_workon_home || return 1
+    typeset d
+
+    virtualenvwrapper_show_workon_options | while read d
+    do
+        [ ! -d "$WORKON_HOME/$d" ] && continue
+        echo "$d"
+        echo "$d" | sed 's/./=/g'
+        # Activate the environment, but not with workon
+        # because we don't want to trigger any hooks.
+        (source "$WORKON_HOME/$d/bin/activate";
+            cd "$VIRTUAL_ENV";
+            $@)
+        echo
+    done
+}
+
+#
 # Invoke the initialization functions
 #
 virtualenvwrapper_initialize