Commits

Doug Hellmann  committed bb51351

Add wipeenv command

Add the wipeenv command.

Signed-off-by: Doug Hellmann <doug.hellmann@gmail.com>

  • Participants
  • Parent commits 4af182f

Comments (0)

Files changed (4)

File docs/source/command_ref.rst

 
   cdproject
 
+===========================
+Managing Installed Packages
+===========================
+
+.. _command-wipeenv:
+
+wipeenv
+-------
+
+Remove all of the installed third-party packages in the current
+virtualenv.
+
+Syntax::
+
+  wipeenv

File docs/source/history.rst

 
 - Ensure that all $() style commands that produce paths are
   quoted. Addresses :bbissue:`164`.
+- Add :ref:`command-wipeenv` command for removing all packages
+  installed in the virtualenv.
 
 4.0
 ===

File tests/test_wipeenv.sh

+# -*- mode: shell-script -*-
+
+test_dir=$(cd $(dirname $0) && pwd)
+source "$test_dir/setup.sh"
+
+setUp () {
+    rm -rf "$WORKON_HOME"
+    mkdir -p "$WORKON_HOME"
+    source "$test_dir/../virtualenvwrapper.sh"
+    rm -f "$test_dir/catch_output"
+    echo
+}
+
+tearDown() {
+    if type deactivate >/dev/null 2>&1
+    then 
+        deactivate
+    fi
+    rm -rf "$WORKON_HOME"
+}
+
+test_wipeenv () {
+    mkvirtualenv "wipetest" >/dev/null 2>&1
+    (cd tests/testpackage && python setup.py install) >/dev/null 2>&1
+    before="$(pip freeze)"
+    assertTrue "testpackage not installed" "pip freeze | grep testpackage"
+    wipeenv >/dev/null 2>&1
+    after="$(pip freeze)"
+    assertFalse "testpackage still installed" "pip freeze | grep testpackage"
+}
+
+test_empty_env () {
+    mkvirtualenv "wipetest" >/dev/null 2>&1
+    before="$(pip freeze)"
+    assertFalse "testpackage still installed" "pip freeze | grep testpackage"
+    wipeenv >/dev/null 2>&1
+    after="$(pip freeze)"
+    assertFalse "testpackage still installed" "pip freeze | grep testpackage"
+}
+
+test_not_active_env () {
+    mkvirtualenv "wipetest" >/dev/null 2>&1
+    deactivate
+    assertFalse "wipenv did not report an error" "wipeenv >/dev/null 2>&1"
+}
+
+. "$test_dir/shunit2"
+

File virtualenvwrapper.sh

 EOF
 }
 
+#
+# Remove all installed packages from the env
+#
+function wipeenv {
+    virtualenvwrapper_verify_workon_home || return 1
+    virtualenvwrapper_verify_active_environment || return 1
+
+    typeset req_file="$(virtualenvwrapper_tempfile "requirements.txt")"
+    pip freeze | egrep -v '(distribute|wsgiref)' > "$req_file"
+    if [ -n "$(cat "$req_file")" ]
+    then
+        echo "Uninstalling packages:"
+        cat "$req_file"
+        echo
+        pip uninstall -y $(cat "$req_file" | sed 's/>/=/g' | cut -f1 -d=)
+    else
+        echo "Nothing to remove."
+    fi
+    rm -f "$req_file"
+}
 
 #
 # Invoke the initialization functions