Skip to content

Commit fc6ff16

Browse files
committed
add lazy loader
1 parent af7e3de commit fc6ff16

File tree

7 files changed

+400
-1
lines changed

7 files changed

+400
-1
lines changed

docs/en/history.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
Release History
33
===============
44

5+
3.4
6+
7+
- Add :ref:`install-lazy-loader` option.
8+
59
3.3
610

711
- Clean up file permissions and remove shebangs from scripts not

docs/en/install.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,31 @@ of the script installed with this package::
118118
After editing it, reload the startup file (e.g., run ``source
119119
~/.bashrc``).
120120

121+
.. _install-lazy-loader:
122+
123+
Lazy Loading
124+
------------
125+
126+
An alternative initialization script is provided for loading
127+
virtualenvwrapper lazily. Instead of sourcing ``virtualenvwrapper.sh``
128+
directly, use ``virtualenvwrapper_lazy.sh``. If
129+
``virtualenvwrapper.sh`` is not on your ``$PATH``, set
130+
``VIRTUALENVWRAPPER_SCRIPT`` to point to it.
131+
132+
::
133+
134+
export WORKON_HOME=$HOME/.virtualenvs
135+
export PROJECT_HOME=$HOME/Devel
136+
export VIRTUALENVWRAPPER_SCRIPT=/usr/local/bin/virtualenvwrapper.sh
137+
source /usr/local/bin/virtualenvwrapper_lazy.sh
138+
139+
.. warning::
140+
141+
When the lazy-loading version of the startup script is used,
142+
tab-completion does not work until after the first
143+
virtualenvwrapper command has been run. For example, tab completion
144+
does not work for the first instance of :ref:`command-workon`.
145+
121146
Quick-Start
122147
===========
123148

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ def find_package_data(
139139

140140
platforms = ['Any'],
141141

142-
scripts = ['virtualenvwrapper.sh',
142+
scripts = ['virtualenvwrapper.sh',
143+
'virtualenvwrapper_lazy.sh',
143144
],
144145

145146
provides=['virtualenvwrapper',

tests/test_lazy.sh

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/bin/sh
2+
3+
test_dir=$(cd $(dirname $0) && pwd)
4+
source "$test_dir/setup.sh"
5+
6+
oneTimeSetUp() {
7+
rm -rf "$WORKON_HOME"
8+
mkdir -p "$WORKON_HOME"
9+
source "$test_dir/../virtualenvwrapper_lazy.sh"
10+
}
11+
12+
oneTimeTearDown() {
13+
rm -rf "$WORKON_HOME"
14+
}
15+
16+
setUp () {
17+
echo
18+
rm -f "$test_dir/catch_output"
19+
}
20+
21+
function_defined_lazy() {
22+
name="$1"
23+
assertTrue "$name not defined" "type $name"
24+
assertTrue "$name does not load virtualenvwrapper" "typeset -f $name | grep 'virtualenvwrapper_load'"
25+
}
26+
27+
test_mkvirtualenv_defined_lazy() {
28+
function_defined_lazy mkvirtualenv
29+
}
30+
31+
test_rmvirtualenv_defined_lazy() {
32+
function_defined_lazy rmvirtualenv
33+
}
34+
35+
test_lsvirtualenv_defined_lazy() {
36+
function_defined_lazy lsvirtualenv
37+
}
38+
39+
test_showvirtualenv_defined_lazy() {
40+
function_defined_lazy showvirtualenv
41+
}
42+
43+
test_workon_defined_lazy() {
44+
function_defined_lazy workon
45+
}
46+
47+
test_add2virtualenv_defined_lazy() {
48+
function_defined_lazy add2virtualenv
49+
}
50+
51+
test_cdsitepackages_defined_lazy() {
52+
function_defined_lazy cdsitepackages
53+
}
54+
55+
test_cdvirtualenv_defined_lazy() {
56+
function_defined_lazy cdvirtualenv
57+
}
58+
59+
test_cdvirtualenv_defined_lazy() {
60+
function_defined_lazy cdvirtualenv
61+
}
62+
63+
test_lssitepackages_defined_lazy() {
64+
function_defined_lazy lssitepackages
65+
}
66+
67+
test_toggleglobalsitepackages_defined_lazy() {
68+
function_defined_lazy toggleglobalsitepackages
69+
}
70+
71+
test_cpvirtualenv_defined_lazy() {
72+
function_defined_lazy cpvirtualenv
73+
}
74+
75+
test_setvirtualenvproject_defined_lazy() {
76+
function_defined_lazy setvirtualenvproject
77+
}
78+
79+
test_mkproject_defined_lazy() {
80+
function_defined_lazy mkproject
81+
}
82+
83+
test_cdproject_defined_lazy() {
84+
function_defined_lazy cdproject
85+
}
86+
87+
test_mktmpenv_defined_lazy() {
88+
function_defined_lazy mktmpenv
89+
}
90+
91+
92+
# test_virtualenvwrapper_initialize() {
93+
# assertTrue "Initialized" virtualenvwrapper_initialize
94+
# for hook in premkvirtualenv postmkvirtualenv prermvirtualenv postrmvirtualenv preactivate postactivate predeactivate postdeactivate
95+
# do
96+
# assertTrue "Global $WORKON_HOME/$hook was not created" "[ -f $WORKON_HOME/$hook ]"
97+
# assertTrue "Global $WORKON_HOME/$hook is not executable" "[ -x $WORKON_HOME/$hook ]"
98+
# done
99+
# assertTrue "Log file was not created" "[ -f $WORKON_HOME/hook.log ]"
100+
# export pre_test_dir=$(cd "$test_dir"; pwd)
101+
# echo "echo GLOBAL initialize >> \"$pre_test_dir/catch_output\"" >> "$WORKON_HOME/initialize"
102+
# virtualenvwrapper_initialize
103+
# output=$(cat "$test_dir/catch_output")
104+
# expected="GLOBAL initialize"
105+
# assertSame "$expected" "$output"
106+
# }
107+
108+
. "$test_dir/shunit2"

tests/test_lazy_loaded.sh

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/bin/sh
2+
3+
test_dir=$(cd $(dirname $0) && pwd)
4+
source "$test_dir/setup.sh"
5+
6+
oneTimeSetUp() {
7+
rm -rf "$WORKON_HOME"
8+
mkdir -p "$WORKON_HOME"
9+
source "$test_dir/../virtualenvwrapper_lazy.sh"
10+
virtualenvwrapper_load
11+
}
12+
13+
oneTimeTearDown() {
14+
rm -rf "$WORKON_HOME"
15+
}
16+
17+
setUp () {
18+
echo
19+
rm -f "$test_dir/catch_output"
20+
}
21+
22+
function_defined_normal() {
23+
name="$1"
24+
assertTrue "$name not defined" "type $name"
25+
assertFalse "$name still set to run lazy loader" "typeset -f $name | grep 'virtualenvwrapper_load'"
26+
}
27+
28+
test_mkvirtualenv_defined_normal() {
29+
function_defined_normal mkvirtualenv
30+
}
31+
32+
test_rmvirtualenv_defined_normal() {
33+
function_defined_normal rmvirtualenv
34+
}
35+
36+
test_lsvirtualenv_defined_normal() {
37+
function_defined_normal lsvirtualenv
38+
}
39+
40+
test_showvirtualenv_defined_normal() {
41+
function_defined_normal showvirtualenv
42+
}
43+
44+
test_workon_defined_normal() {
45+
function_defined_normal workon
46+
}
47+
48+
test_add2virtualenv_defined_normal() {
49+
function_defined_normal add2virtualenv
50+
}
51+
52+
test_cdsitepackages_defined_normal() {
53+
function_defined_normal cdsitepackages
54+
}
55+
56+
test_cdvirtualenv_defined_normal() {
57+
function_defined_normal cdvirtualenv
58+
}
59+
60+
test_cdvirtualenv_defined_normal() {
61+
function_defined_normal cdvirtualenv
62+
}
63+
64+
test_lssitepackages_defined_normal() {
65+
function_defined_normal lssitepackages
66+
}
67+
68+
test_toggleglobalsitepackages_defined_normal() {
69+
function_defined_normal toggleglobalsitepackages
70+
}
71+
72+
test_cpvirtualenv_defined_normal() {
73+
function_defined_normal cpvirtualenv
74+
}
75+
76+
test_setvirtualenvproject_defined_normal() {
77+
function_defined_normal setvirtualenvproject
78+
}
79+
80+
test_mkproject_defined_normal() {
81+
function_defined_normal mkproject
82+
}
83+
84+
test_cdproject_defined_normal() {
85+
function_defined_normal cdproject
86+
}
87+
88+
test_mktmpenv_defined_normal() {
89+
function_defined_normal mktmpenv
90+
}
91+
92+
93+
# test_virtualenvwrapper_initialize() {
94+
# assertTrue "Initialized" virtualenvwrapper_initialize
95+
# for hook in premkvirtualenv postmkvirtualenv prermvirtualenv postrmvirtualenv preactivate postactivate predeactivate postdeactivate
96+
# do
97+
# assertTrue "Global $WORKON_HOME/$hook was not created" "[ -f $WORKON_HOME/$hook ]"
98+
# assertTrue "Global $WORKON_HOME/$hook is not executable" "[ -x $WORKON_HOME/$hook ]"
99+
# done
100+
# assertTrue "Log file was not created" "[ -f $WORKON_HOME/hook.log ]"
101+
# export pre_test_dir=$(cd "$test_dir"; pwd)
102+
# echo "echo GLOBAL initialize >> \"$pre_test_dir/catch_output\"" >> "$WORKON_HOME/initialize"
103+
# virtualenvwrapper_initialize
104+
# output=$(cat "$test_dir/catch_output")
105+
# expected="GLOBAL initialize"
106+
# assertSame "$expected" "$output"
107+
# }
108+
109+
. "$test_dir/shunit2"

trace.log

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
_________________________________ [tox sdist] __________________________________
2+
[TOX] ***creating sdist package
3+
[TOX] /Users/dhellmann/Devel/virtualenvwrapper/virtualenvwrapper-startup$ /Users/dhellmann/Envs/virtualenvwrapper/bin/python setup.py sdist --formats=zip --dist-dir .tox/dist >.tox/log/0.log
4+
[TOX] ***copying new sdistfile to '/Users/dhellmann/.tox/distshare/virtualenvwrapper-3.3.zip'
5+
______________________________ [tox testenv:py27] ______________________________
6+
[TOX] ***reusing existing matching virtualenv py27
7+
[TOX] ***upgrade-installing sdist
8+
[TOX] /Users/dhellmann/Devel/virtualenvwrapper/virtualenvwrapper-startup/.tox/py27/log$ ../bin/pip install --download-cache=/Users/dhellmann/Devel/virtualenvwrapper/virtualenvwrapper-startup/.tox/_download /Users/dhellmann/Devel/virtualenvwrapper/virtualenvwrapper-startup/.tox/dist/virtualenvwrapper-3.3.zip -U --no-deps >21.log
9+
[TOX] WARNING:test command found but not installed in testenv
10+
cmd: /bin/bash
11+
env: /Users/dhellmann/Devel/virtualenvwrapper/virtualenvwrapper-startup/.tox/py27
12+
Maybe forgot to specify a dependency?
13+
[TOX] /Users/dhellmann/Devel/virtualenvwrapper/virtualenvwrapper-startup$ /bin/bash ./tests/run_tests /Users/dhellmann/Devel/virtualenvwrapper/virtualenvwrapper-startup/.tox/py27 tests/test.sh
14+
15+
********************************************************************************
16+
Running tests/test.sh
17+
VIRTUAL_ENV=/Users/dhellmann/Devel/virtualenvwrapper/virtualenvwrapper-startup/.tox/py27
18+
VIRTUALENVWRAPPER_PYTHON=/Users/dhellmann/Devel/virtualenvwrapper/virtualenvwrapper-startup/.tox/py27/bin/python
19+
Python 2.7.2
20+
PYTHONPATH=
21+
SHELL=/bin/bash
22+
23+
24+
test_virtualenvwrapper_initialize
25+
26+
test_virtualenvwrapper_run_init_hooks
27+
28+
test_virtualenvwrapper_space_in_workon_home
29+
30+
test_virtualenvwrapper_verify_workon_home
31+
32+
test_virtualenvwrapper_verify_workon_home_missing_dir
33+
34+
test_virtualenvwrapper_verify_workon_home_missing_dir_quiet
35+
36+
test_virtualenvwrapper_verify_workon_home_missing_dir_grep_options
37+
38+
test_python_interpreter_set_incorrectly
39+
Using real prefix '/Library/Frameworks/Python.framework/Versions/2.7'
40+
New python executable in no_wrappers/bin/python
41+
Installing distribute.............................................................................................................................................................................................done.
42+
Installing pip...............done.
43+
44+
Ran 8 tests.
45+
46+
OK
47+
48+
49+
********************************************************************************
50+
Running tests/test.sh
51+
VIRTUAL_ENV=/Users/dhellmann/Devel/virtualenvwrapper/virtualenvwrapper-startup/.tox/py27
52+
VIRTUALENVWRAPPER_PYTHON=/Users/dhellmann/Devel/virtualenvwrapper/virtualenvwrapper-startup/.tox/py27/bin/python
53+
Python 2.7.2
54+
PYTHONPATH=
55+
SHELL=/bin/ksh
56+
57+
58+
test_virtualenvwrapper_initialize
59+
60+
test_virtualenvwrapper_run_init_hooks
61+
62+
test_virtualenvwrapper_space_in_workon_home
63+
64+
test_virtualenvwrapper_verify_workon_home
65+
66+
test_virtualenvwrapper_verify_workon_home_missing_dir
67+
68+
test_virtualenvwrapper_verify_workon_home_missing_dir_quiet
69+
70+
test_virtualenvwrapper_verify_workon_home_missing_dir_grep_options
71+
72+
test_python_interpreter_set_incorrectly
73+
Using real prefix '/Library/Frameworks/Python.framework/Versions/2.7'
74+
New python executable in no_wrappers/bin/python
75+
Installing distribute.............................................................................................................................................................................................done.
76+
Installing pip...............done.
77+
78+
Ran 8 tests.
79+
80+
OK
81+
82+
83+
********************************************************************************
84+
Running tests/test.sh
85+
VIRTUAL_ENV=/Users/dhellmann/Devel/virtualenvwrapper/virtualenvwrapper-startup/.tox/py27
86+
VIRTUALENVWRAPPER_PYTHON=/Users/dhellmann/Devel/virtualenvwrapper/virtualenvwrapper-startup/.tox/py27/bin/python
87+
Python 2.7.2
88+
PYTHONPATH=
89+
SHELL=/bin/zsh
90+
91+
92+
test_virtualenvwrapper_initialize
93+
94+
test_virtualenvwrapper_run_init_hooks
95+
96+
test_virtualenvwrapper_space_in_workon_home
97+
98+
test_virtualenvwrapper_verify_workon_home
99+
100+
test_virtualenvwrapper_verify_workon_home_missing_dir
101+
102+
test_virtualenvwrapper_verify_workon_home_missing_dir_quiet
103+
104+
test_virtualenvwrapper_verify_workon_home_missing_dir_grep_options
105+
106+
test_python_interpreter_set_incorrectly
107+
Using real prefix '/Library/Frameworks/Python.framework/Versions/2.7'
108+
New python executable in no_wrappers/bin/python
109+
Installing distribute.............................................................................................................................................................................................done.
110+
Installing pip...............done.
111+
112+
Ran 8 tests.
113+
114+
OK
115+
116+
________________________________ [tox summary] _________________________________
117+
[TOX] py27: commands succeeded
118+
[TOX] congratulations :)

0 commit comments

Comments
 (0)