Skip to content

Commit 3525bc7

Browse files
committed
fix #34 by using python's tempfile module instead of a shell command
1 parent eb8948d commit 3525bc7

File tree

3 files changed

+54
-57
lines changed

3 files changed

+54
-57
lines changed

tests/test_tempfile.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/sh
2+
3+
#set -x
4+
5+
test_dir=$(dirname $0)
6+
7+
export WORKON_HOME="${TMPDIR:-/tmp}/WORKON_HOME"
8+
9+
oneTimeSetUp() {
10+
rm -rf "$WORKON_HOME"
11+
mkdir -p "$WORKON_HOME"
12+
source "$test_dir/../virtualenvwrapper.sh"
13+
echo $PYTHONPATH
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+
test_tempfile () {
26+
filename=$(virtualenvwrapper_tempfile)
27+
rm -f $filename
28+
assertSame "$TMPDIR" "$(dirname $filename)/"
29+
assertTrue "echo $filename | grep virtualenvwrapper"
30+
}
31+
32+
test_no_python () {
33+
old=$VIRTUALENVWRAPPER_PYTHON
34+
VIRTUALENVWRAPPER_PYTHON=false
35+
filename=$(virtualenvwrapper_tempfile)
36+
VIRTUALENVWRAPPER_PYTHON=$old
37+
rm -f $filename
38+
assertSame "$TMPDIR" "$(dirname $filename)/"
39+
assertTrue "echo $filename | grep virtualenvwrapper.$$"
40+
}
41+
42+
. "$test_dir/shunit2"

tests/test_tmpdir.sh

Lines changed: 0 additions & 45 deletions
This file was deleted.

virtualenvwrapper.sh

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,6 @@ fi
6262
WORKON_HOME=$("$VIRTUALENVWRAPPER_PYTHON" -c "import os; print os.path.abspath(os.path.expandvars(os.path.expanduser(\"$WORKON_HOME\")))")
6363
export WORKON_HOME
6464

65-
# Make sure we have a location for temporary files
66-
if [ "$VIRTUALENVWRAPPER_TMPDIR" = "" ]
67-
then
68-
VIRTUALENVWRAPPER_TMPDIR="$TMPDIR"
69-
if [ "$VIRTUALENVWRAPPER_TMPDIR" = "" ]
70-
then
71-
VIRTUALENVWRAPPER_TMPDIR="/tmp"
72-
fi
73-
fi
74-
7565
# Verify that the WORKON_HOME directory exists
7666
virtualenvwrapper_verify_workon_home () {
7767
if [ ! -d "$WORKON_HOME" ]
@@ -84,12 +74,22 @@ virtualenvwrapper_verify_workon_home () {
8474

8575
#HOOK_VERBOSE_OPTION="-v"
8676

77+
# Use Python's tempfile module to create a temporary file
78+
# with a unique and not-likely-to-be-predictable name.
79+
virtualenvwrapper_tempfile () {
80+
$VIRTUALENVWRAPPER_PYTHON -c "import tempfile; print tempfile.NamedTemporaryFile(prefix='virtualenvwrapper.').name"
81+
if [ $? -ne 0 ]
82+
then
83+
echo "${TMPDIR:-/tmp}/virtualenvwrapper.$$"
84+
fi
85+
}
86+
8787
# Run the hooks
8888
virtualenvwrapper_run_hook () {
8989
# First anything that runs directly from the plugin
9090
"$VIRTUALENVWRAPPER_PYTHON" -m virtualenvwrapper.hook_loader $HOOK_VERBOSE_OPTION "$@"
9191
# Now anything that wants to run inside this shell
92-
hook_script=$(tempfile --directory "$VIRTUALENVWRAPPER_TMPDIR")
92+
hook_script=$(virtualenvwrapper_tempfile)
9393
"$VIRTUALENVWRAPPER_PYTHON" -m virtualenvwrapper.hook_loader $HOOK_VERBOSE_OPTION \
9494
--source "$@" >>"$hook_script"
9595
source "$hook_script"
@@ -240,7 +240,7 @@ workon () {
240240
virtualenvwrapper_original_deactivate=`typeset -f deactivate | sed 's/deactivate/virtualenv_deactivate/g'`
241241
eval "$virtualenvwrapper_original_deactivate"
242242
unset -f deactivate >/dev/null 2>&1
243-
# virtualenvwrapper_saved_deactivate=$(tempfile --directory "$VIRTUALENVWRAPPER_TMPDIR")
243+
# virtualenvwrapper_saved_deactivate=$(virtualenvwrapper_tempfile)
244244
# $(typeset -f deactivate | sed 's/deactivate/original_deactivate/g' > $virtualenvwrapper_saved_deactivate)
245245
# echo "original_deactivate" >> $virtualenvwrapper_saved_deactivate
246246
# echo "SAVED: \"$virtualenvwrapper_saved_deactivate\""

0 commit comments

Comments
 (0)