Commits

Doug Hellmann  committed 8a94c97

add mktmpenv command from virtualenvwrapper.tmpenv

  • Participants
  • Parent commits 8a9a11c

Comments (0)

Files changed (4)

File docs/en/command_ref.rst

 
 .. _requirements file format: http://www.pip-installer.org/en/latest/requirement-format.html
 
+.. _command-mktmpenv:
+
+mktmpenv
+--------
+
+Create a new virtualenv in the ``WORKON_HOME`` directory.
+
+Syntax::
+
+    mktmpenv [ENVNAME]
+
+If no environment name is given, a temporary unique name is generated.
+
+::
+
+    $ mktmpenv
+    Using real prefix '/Library/Frameworks/Python.framework/Versions/2.7'
+    New python executable in 1e513ac6-616e-4d56-9aa5-9d0a3b305e20/bin/python
+    Overwriting 1e513ac6-616e-4d56-9aa5-9d0a3b305e20/lib/python2.7/distutils/__init__.py 
+    with new content
+    Installing distribute...............................................
+    ....................................................................
+    .................................................................done.
+    This is a temporary environment. It will be deleted when deactivated.
+    (1e513ac6-616e-4d56-9aa5-9d0a3b305e20) $
+
 .. _command-lsvirtualenv:
 
 lsvirtualenv

File docs/en/history.rst

   - Incorporated patch to add ``-d`` option to
     :ref:`command-add2virtualenv`, contributed by :bbuser:`miracle2k`.
   - Add ``-i`` option to :ref:`command-mkvirtualenv`.
+  - Add :ref:`command-mktmpenv` command for creating temporary
+    environments that are automatically removed when they are
+    deactivated.
 
 2.9
 

File tests/test_mktmpenv.sh

+#!/bin/sh
+
+test_dir=$(cd $(dirname $0) && pwd)
+
+export WORKON_HOME="$(echo ${TMPDIR:-/tmp}/WORKON_HOME | sed 's|//|/|g')"
+
+
+oneTimeSetUp() {
+    rm -rf "$WORKON_HOME"
+    mkdir -p "$WORKON_HOME"
+    source "$test_dir/../virtualenvwrapper.sh"
+}
+
+oneTimeTearDown() {
+    rm -rf "$WORKON_HOME"
+}
+
+setUp () {
+    echo
+    rm -f "$test_dir/catch_output"
+}
+
+test_mktmpenv_no_name() {
+    before=$(lsvirtualenv -b)
+    mktmpenv >/dev/null 2>&1
+    after=$(lsvirtualenv -b)
+    assertFalse "Environment was not created" "[ \"$before\" = \"$after\" ]"
+}
+
+test_mktmpenv_name() {
+    assertFalse "Environment already exists" "[ -d \"$WORKON_HOME/name-given-by-user\" ]"
+    mktmpenv name-given-by-user >/dev/null 2>&1
+    assertTrue "Environment was not created" "[ -d \"$WORKON_HOME/name-given-by-user\" ]"
+    assertSame $(basename "$VIRTUAL_ENV") "name-given-by-user"
+}
+
+test_deactivate() {
+    assertFalse "Environment already exists" "[ -d \"$WORKON_HOME/automatically-deleted\" ]"
+    mktmpenv automatically-deleted >/dev/null 2>&1
+    assertSame $(basename "$VIRTUAL_ENV") "automatically-deleted"
+    assertTrue "Environment was not created" "[ -d \"$WORKON_HOME/automatically-deleted\" ]"
+    deactivate >/dev/null 2>&1
+    assertFalse "Environment still exists" "[ -d \"$WORKON_HOME/automatically-deleted\" ]"
+}
+
+. "$test_dir/shunit2"

File virtualenvwrapper.sh

     return 0
 }
 
+#
+# Temporary virtualenv
+#
+# Originally part of virtualenvwrapper.tmpenv plugin
+#
+mktmpenv() {
+    typeset tmpenvname
+
+    # Generate a unique temporary name, if one is not given.
+    if [ $# -eq 0 ]
+    then
+        tmpenvname=$("$VIRTUALENVWRAPPER_PYTHON" -c 'import uuid; print uuid.uuid4()')
+        mkvirtualenv "$tmpenvname"
+    else
+        mkvirtualenv "$@"
+    fi
+
+    # Create the environment
+    RC=$?
+    if [ $RC -ne 0 ]
+    then
+        return $RC
+    fi
+
+    # Change working directory
+    cdvirtualenv
+
+    # Create the tmpenv marker file
+    echo "This is a temporary environment. It will be deleted when you run 'deactivate'." | tee "$VIRTUAL_ENV/README.tmpenv"
+
+    # Update the postdeactivate script
+    cat - >> "$VIRTUAL_ENV/bin/postdeactivate" <<EOF
+if [ -f "$VIRTUAL_ENV/README.tmpenv" ]
+then
+    echo "Removing temporary environment:" $(basename "$VIRTUAL_ENV")
+    rmvirtualenv $(basename "$VIRTUAL_ENV")
+fi
+EOF
+}
+
 
 #
 # Invoke the initialization hooks