Skip to content

Commit 0530f67

Browse files
committed
Add -r option to mkvirtualenv to install base requirements after the environment is created. Fix argument processing in mkproject so the correct template names are preserved.
1 parent 3c306e1 commit 0530f67

File tree

2 files changed

+98
-2
lines changed

2 files changed

+98
-2
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/sh
2+
3+
#set -x
4+
5+
test_dir=$(cd $(dirname $0) && pwd)
6+
7+
export WORKON_HOME="$(echo ${TMPDIR:-/tmp}/WORKON_HOME | sed 's|//|/|g')"
8+
9+
oneTimeSetUp() {
10+
rm -rf "$WORKON_HOME"
11+
mkdir -p "$WORKON_HOME"
12+
source "$test_dir/../virtualenvwrapper.sh"
13+
}
14+
15+
oneTimeTearDown() {
16+
rm -rf "$WORKON_HOME"
17+
rm -f "$test_dir/requirements.txt"
18+
}
19+
20+
setUp () {
21+
echo
22+
rm -f "$test_dir/catch_output"
23+
}
24+
25+
test_requirements_file () {
26+
echo "commandlineapp" > "$test_dir/requirements.txt"
27+
mkvirtualenv -r "$test_dir/requirements.txt" "env3" >/dev/null 2>&1
28+
installed=$(pip freeze)
29+
assertTrue "CommandLineApp not found in $installed" "echo $installed | grep CommandLineApp"
30+
}
31+
32+
. "$test_dir/shunit2"

virtualenvwrapper.sh

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,70 @@ function virtualenvwrapper_verify_active_environment {
269269
return 0
270270
}
271271

272+
# Help text for mkvirtualenv
273+
function mkvirtualenv_help {
274+
echo "Usage: mkvirtualenv [-r requirements_file] [virtualenv options] env_name"
275+
echo
276+
echo " -r requirements_file"
277+
echo
278+
echo " Provide a pip requirements file to install a base set of packages"
279+
echo " into the new environment."
280+
}
281+
272282
# Create a new environment, in the WORKON_HOME.
273283
#
274284
# Usage: mkvirtualenv [options] ENVNAME
275285
# (where the options are passed directly to virtualenv)
276286
#
277287
function mkvirtualenv {
288+
typeset -a in_args
289+
typeset -a out_args
290+
typeset -i i
291+
typeset tst
292+
typeset a
293+
typeset envname
294+
typeset requirements
295+
296+
in_args=( "$@" )
297+
298+
if [ -n "$ZSH_VERSION" ]
299+
then
300+
i=1
301+
tst="-le"
302+
else
303+
i=0
304+
tst="-lt"
305+
fi
306+
while [ $i $tst $# ]
307+
do
308+
a="${in_args[$i]}"
309+
# echo "arg $i : $a"
310+
case "$a" in
311+
-h)
312+
echo 'mkvirtualenv help:';
313+
echo;
314+
mkvirtualenv_help;
315+
echo;
316+
echo 'virtualenv help:';
317+
echo;
318+
virtualenv -h;
319+
return;;
320+
-r)
321+
i=$(( $i + 1 ));
322+
requirements="${in_args[$i]}";;
323+
*)
324+
if [ ${#out_args} -gt 0 ]
325+
then
326+
out_args=( "${out_args[@]-}" "$a" )
327+
else
328+
out_args=( "$a" )
329+
fi;;
330+
esac
331+
i=$(( $i + 1 ))
332+
done
333+
334+
set -- "${out_args[@]}"
335+
278336
eval "envname=\$$#"
279337
virtualenvwrapper_verify_workon_home || return 1
280338
virtualenvwrapper_verify_virtualenv || return 1
@@ -294,6 +352,12 @@ function mkvirtualenv {
294352
[ ! -d "$WORKON_HOME/$envname" ] && return 0
295353
# Now activate the new environment
296354
workon "$envname"
355+
356+
if [ ! -z "$requirements" ]
357+
then
358+
pip install -r "$requirements"
359+
fi
360+
297361
virtualenvwrapper_run_hook "post_mkvirtualenv"
298362
}
299363

@@ -738,8 +802,8 @@ function mkproject {
738802
mkvirtualenv -h;
739803
return;;
740804
-t)
741-
templates="$templates $a";
742-
i=$(( $i + 1 ));;
805+
i=$(( $i + 1 ));
806+
templates="$templates ${in_args[$i]}";;
743807
*)
744808
if [ ${#out_args} -gt 0 ]
745809
then

0 commit comments

Comments
 (0)