Skip to content

Commit 8a94c97

Browse files
committed
add mktmpenv command from virtualenvwrapper.tmpenv
1 parent 8a9a11c commit 8a94c97

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

docs/en/command_ref.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,32 @@ installed.
5656

5757
.. _requirements file format: http://www.pip-installer.org/en/latest/requirement-format.html
5858

59+
.. _command-mktmpenv:
60+
61+
mktmpenv
62+
--------
63+
64+
Create a new virtualenv in the ``WORKON_HOME`` directory.
65+
66+
Syntax::
67+
68+
mktmpenv [ENVNAME]
69+
70+
If no environment name is given, a temporary unique name is generated.
71+
72+
::
73+
74+
$ mktmpenv
75+
Using real prefix '/Library/Frameworks/Python.framework/Versions/2.7'
76+
New python executable in 1e513ac6-616e-4d56-9aa5-9d0a3b305e20/bin/python
77+
Overwriting 1e513ac6-616e-4d56-9aa5-9d0a3b305e20/lib/python2.7/distutils/__init__.py
78+
with new content
79+
Installing distribute...............................................
80+
....................................................................
81+
.................................................................done.
82+
This is a temporary environment. It will be deleted when deactivated.
83+
(1e513ac6-616e-4d56-9aa5-9d0a3b305e20) $
84+
5985
.. _command-lsvirtualenv:
6086

6187
lsvirtualenv

docs/en/history.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ dev
77
- Incorporated patch to add ``-d`` option to
88
:ref:`command-add2virtualenv`, contributed by :bbuser:`miracle2k`.
99
- Add ``-i`` option to :ref:`command-mkvirtualenv`.
10+
- Add :ref:`command-mktmpenv` command for creating temporary
11+
environments that are automatically removed when they are
12+
deactivated.
1013

1114
2.9
1215

tests/test_mktmpenv.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/sh
2+
3+
test_dir=$(cd $(dirname $0) && pwd)
4+
5+
export WORKON_HOME="$(echo ${TMPDIR:-/tmp}/WORKON_HOME | sed 's|//|/|g')"
6+
7+
8+
oneTimeSetUp() {
9+
rm -rf "$WORKON_HOME"
10+
mkdir -p "$WORKON_HOME"
11+
source "$test_dir/../virtualenvwrapper.sh"
12+
}
13+
14+
oneTimeTearDown() {
15+
rm -rf "$WORKON_HOME"
16+
}
17+
18+
setUp () {
19+
echo
20+
rm -f "$test_dir/catch_output"
21+
}
22+
23+
test_mktmpenv_no_name() {
24+
before=$(lsvirtualenv -b)
25+
mktmpenv >/dev/null 2>&1
26+
after=$(lsvirtualenv -b)
27+
assertFalse "Environment was not created" "[ \"$before\" = \"$after\" ]"
28+
}
29+
30+
test_mktmpenv_name() {
31+
assertFalse "Environment already exists" "[ -d \"$WORKON_HOME/name-given-by-user\" ]"
32+
mktmpenv name-given-by-user >/dev/null 2>&1
33+
assertTrue "Environment was not created" "[ -d \"$WORKON_HOME/name-given-by-user\" ]"
34+
assertSame $(basename "$VIRTUAL_ENV") "name-given-by-user"
35+
}
36+
37+
test_deactivate() {
38+
assertFalse "Environment already exists" "[ -d \"$WORKON_HOME/automatically-deleted\" ]"
39+
mktmpenv automatically-deleted >/dev/null 2>&1
40+
assertSame $(basename "$VIRTUAL_ENV") "automatically-deleted"
41+
assertTrue "Environment was not created" "[ -d \"$WORKON_HOME/automatically-deleted\" ]"
42+
deactivate >/dev/null 2>&1
43+
assertFalse "Environment still exists" "[ -d \"$WORKON_HOME/automatically-deleted\" ]"
44+
}
45+
46+
. "$test_dir/shunit2"

virtualenvwrapper.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,46 @@ function cdproject {
905905
return 0
906906
}
907907

908+
#
909+
# Temporary virtualenv
910+
#
911+
# Originally part of virtualenvwrapper.tmpenv plugin
912+
#
913+
mktmpenv() {
914+
typeset tmpenvname
915+
916+
# Generate a unique temporary name, if one is not given.
917+
if [ $# -eq 0 ]
918+
then
919+
tmpenvname=$("$VIRTUALENVWRAPPER_PYTHON" -c 'import uuid; print uuid.uuid4()')
920+
mkvirtualenv "$tmpenvname"
921+
else
922+
mkvirtualenv "$@"
923+
fi
924+
925+
# Create the environment
926+
RC=$?
927+
if [ $RC -ne 0 ]
928+
then
929+
return $RC
930+
fi
931+
932+
# Change working directory
933+
cdvirtualenv
934+
935+
# Create the tmpenv marker file
936+
echo "This is a temporary environment. It will be deleted when you run 'deactivate'." | tee "$VIRTUAL_ENV/README.tmpenv"
937+
938+
# Update the postdeactivate script
939+
cat - >> "$VIRTUAL_ENV/bin/postdeactivate" <<EOF
940+
if [ -f "$VIRTUAL_ENV/README.tmpenv" ]
941+
then
942+
echo "Removing temporary environment:" $(basename "$VIRTUAL_ENV")
943+
rmvirtualenv $(basename "$VIRTUAL_ENV")
944+
fi
945+
EOF
946+
}
947+
908948

909949
#
910950
# Invoke the initialization hooks

0 commit comments

Comments
 (0)