Commits

Doug Hellmann committed 76598a7

fix #34 by using python's tempfile module instead of a shell command

  • Participants
  • Parent commits e96e532

Comments (0)

Files changed (3)

File tests/test_tempfile.sh

+#!/bin/sh
+
+#set -x
+
+test_dir=$(dirname $0)
+
+export WORKON_HOME="${TMPDIR:-/tmp}/WORKON_HOME"
+
+oneTimeSetUp() {
+    rm -rf "$WORKON_HOME"
+    mkdir -p "$WORKON_HOME"
+    source "$test_dir/../virtualenvwrapper.sh"
+    echo $PYTHONPATH
+}
+
+oneTimeTearDown() {
+    rm -rf "$WORKON_HOME"
+}
+
+setUp () {
+    echo
+    rm -f "$test_dir/catch_output"
+}
+
+test_tempfile () {
+    filename=$(virtualenvwrapper_tempfile)
+    rm -f $filename
+    assertSame "$TMPDIR" "$(dirname $filename)/"
+    assertTrue "echo $filename | grep virtualenvwrapper"
+}
+
+test_no_python () {
+    old=$VIRTUALENVWRAPPER_PYTHON
+    VIRTUALENVWRAPPER_PYTHON=false
+    filename=$(virtualenvwrapper_tempfile)
+    VIRTUALENVWRAPPER_PYTHON=$old
+    rm -f $filename
+    assertSame "$TMPDIR" "$(dirname $filename)/"
+    assertTrue "echo $filename | grep virtualenvwrapper.$$"
+}
+
+. "$test_dir/shunit2"

File tests/test_tmpdir.sh

-#!/bin/sh
-
-#set -x
-
-test_dir=$(dirname $0)
-
-export SCRATCH_DIR="${TMPDIR:-/tmp}/test_scratch_dir"
-
-oneTimeSetUp() {
-    rm -rf "$SCRATCH_DIR"
-    mkdir -p "$SCRATCH_DIR"
-}
-
-oneTimeTearDown() {
-    rm -rf "$SCRATCH_DIR"
-}
-
-setUp () {
-    echo
-    unset VIRTUALENVWRAPPER_TMPDIR
-}
-
-test_unset_tmpdir () {
-    old_tmpdir="$TMPDIR"
-    unset TMPDIR
-    source "$test_dir/../virtualenvwrapper.sh"
-    export TMPDIR="$old_tmpdir"
-    assertSame "$VIRTUALENVWRAPPER_TMPDIR" "/tmp"
-}
-
-test_set_tmpdir () {
-    old_tmpdir="$TMPDIR"
-    export TMPDIR="$SCRATCH_DIR"
-    source "$test_dir/../virtualenvwrapper.sh"
-    export TMPDIR="$old_tmpdir"
-    assertSame "$VIRTUALENVWRAPPER_TMPDIR" "$SCRATCH_DIR"
-}
-
-test_set_virtualenvwrapper_tmpdir () {
-    VIRTUALENVWRAPPER_TMPDIR="$SCRATCH_DIR"
-    source "$test_dir/../virtualenvwrapper.sh"
-    assertSame "$VIRTUALENVWRAPPER_TMPDIR" "$SCRATCH_DIR"
-}
-
-. "$test_dir/shunit2"

File virtualenvwrapper.sh

 WORKON_HOME=$("$VIRTUALENVWRAPPER_PYTHON" -c "import os; print os.path.abspath(os.path.expandvars(os.path.expanduser(\"$WORKON_HOME\")))")
 export WORKON_HOME
 
-# Make sure we have a location for temporary files
-if [ "$VIRTUALENVWRAPPER_TMPDIR" = "" ]
-then
-    VIRTUALENVWRAPPER_TMPDIR="$TMPDIR"
-    if [ "$VIRTUALENVWRAPPER_TMPDIR" = "" ]
-    then
-        VIRTUALENVWRAPPER_TMPDIR="/tmp"
-    fi
-fi
-
 # Verify that the WORKON_HOME directory exists
 virtualenvwrapper_verify_workon_home () {
     if [ ! -d "$WORKON_HOME" ]
 
 #HOOK_VERBOSE_OPTION="-v"
 
+# Use Python's tempfile module to create a temporary file
+# with a unique and not-likely-to-be-predictable name.
+virtualenvwrapper_tempfile () {
+    $VIRTUALENVWRAPPER_PYTHON -c "import tempfile; print tempfile.NamedTemporaryFile(prefix='virtualenvwrapper.').name"
+    if [ $? -ne 0 ]
+    then
+        echo "${TMPDIR:-/tmp}/virtualenvwrapper.$$"
+    fi
+}
+
 # Run the hooks
 virtualenvwrapper_run_hook () {
     # First anything that runs directly from the plugin
     "$VIRTUALENVWRAPPER_PYTHON" -m virtualenvwrapper.hook_loader $HOOK_VERBOSE_OPTION "$@"
     # Now anything that wants to run inside this shell
-    hook_script=$(tempfile --directory "$VIRTUALENVWRAPPER_TMPDIR")
+    hook_script=$(virtualenvwrapper_tempfile)
     "$VIRTUALENVWRAPPER_PYTHON" -m virtualenvwrapper.hook_loader $HOOK_VERBOSE_OPTION \
         --source "$@" >>"$hook_script"
     source "$hook_script"
     virtualenvwrapper_original_deactivate=`typeset -f deactivate | sed 's/deactivate/virtualenv_deactivate/g'`
     eval "$virtualenvwrapper_original_deactivate"
     unset -f deactivate >/dev/null 2>&1
-#     virtualenvwrapper_saved_deactivate=$(tempfile --directory "$VIRTUALENVWRAPPER_TMPDIR")
+#     virtualenvwrapper_saved_deactivate=$(virtualenvwrapper_tempfile)
 #     $(typeset -f deactivate | sed 's/deactivate/original_deactivate/g' > $virtualenvwrapper_saved_deactivate)
 #     echo "original_deactivate" >> $virtualenvwrapper_saved_deactivate
 #     echo "SAVED: \"$virtualenvwrapper_saved_deactivate\""