Commits

Doug Hellmann  committed fc6ff16

add lazy loader

  • Participants
  • Parent commits af7e3de

Comments (0)

Files changed (7)

File docs/en/history.rst

 Release History
 ===============
 
+3.4
+
+  - Add :ref:`install-lazy-loader` option.
+
 3.3
 
   - Clean up file permissions and remove shebangs from scripts not

File docs/en/install.rst

 After editing it, reload the startup file (e.g., run ``source
 ~/.bashrc``).
 
+.. _install-lazy-loader:
+
+Lazy Loading
+------------
+
+An alternative initialization script is provided for loading
+virtualenvwrapper lazily. Instead of sourcing ``virtualenvwrapper.sh``
+directly, use ``virtualenvwrapper_lazy.sh``. If
+``virtualenvwrapper.sh`` is not on your ``$PATH``, set
+``VIRTUALENVWRAPPER_SCRIPT`` to point to it.
+
+::
+
+    export WORKON_HOME=$HOME/.virtualenvs
+    export PROJECT_HOME=$HOME/Devel
+    export VIRTUALENVWRAPPER_SCRIPT=/usr/local/bin/virtualenvwrapper.sh
+    source /usr/local/bin/virtualenvwrapper_lazy.sh
+
+.. warning::
+
+   When the lazy-loading version of the startup script is used,
+   tab-completion does not work until after the first
+   virtualenvwrapper command has been run. For example, tab completion
+   does not work for the first instance of :ref:`command-workon`.
+
 Quick-Start
 ===========
 
 
     platforms = ['Any'],
 
-    scripts = ['virtualenvwrapper.sh', 
+    scripts = ['virtualenvwrapper.sh',
+               'virtualenvwrapper_lazy.sh',
                ],
 
     provides=['virtualenvwrapper',

File tests/test_lazy.sh

+#!/bin/sh
+
+test_dir=$(cd $(dirname $0) && pwd)
+source "$test_dir/setup.sh"
+
+oneTimeSetUp() {
+    rm -rf "$WORKON_HOME"
+    mkdir -p "$WORKON_HOME"
+    source "$test_dir/../virtualenvwrapper_lazy.sh"
+}
+
+oneTimeTearDown() {
+    rm -rf "$WORKON_HOME"
+}
+
+setUp () {
+    echo
+    rm -f "$test_dir/catch_output"
+}
+
+function_defined_lazy() {
+    name="$1"
+    assertTrue "$name not defined" "type $name"
+	assertTrue "$name does not load virtualenvwrapper" "typeset -f $name | grep 'virtualenvwrapper_load'"
+}
+
+test_mkvirtualenv_defined_lazy() {
+    function_defined_lazy mkvirtualenv
+}
+
+test_rmvirtualenv_defined_lazy() {
+    function_defined_lazy rmvirtualenv
+}
+
+test_lsvirtualenv_defined_lazy() {
+    function_defined_lazy lsvirtualenv
+}
+
+test_showvirtualenv_defined_lazy() {
+    function_defined_lazy showvirtualenv
+}
+
+test_workon_defined_lazy() {
+    function_defined_lazy workon
+}
+
+test_add2virtualenv_defined_lazy() {
+    function_defined_lazy add2virtualenv
+}
+
+test_cdsitepackages_defined_lazy() {
+    function_defined_lazy cdsitepackages
+}
+
+test_cdvirtualenv_defined_lazy() {
+    function_defined_lazy cdvirtualenv
+}
+
+test_cdvirtualenv_defined_lazy() {
+    function_defined_lazy cdvirtualenv
+}
+
+test_lssitepackages_defined_lazy() {
+    function_defined_lazy lssitepackages
+}
+
+test_toggleglobalsitepackages_defined_lazy() {
+    function_defined_lazy toggleglobalsitepackages
+}
+
+test_cpvirtualenv_defined_lazy() {
+    function_defined_lazy cpvirtualenv
+}
+
+test_setvirtualenvproject_defined_lazy() {
+    function_defined_lazy setvirtualenvproject
+}
+
+test_mkproject_defined_lazy() {
+    function_defined_lazy mkproject
+}
+
+test_cdproject_defined_lazy() {
+    function_defined_lazy cdproject
+}
+
+test_mktmpenv_defined_lazy() {
+    function_defined_lazy mktmpenv
+}
+
+
+# test_virtualenvwrapper_initialize() {
+#     assertTrue "Initialized" virtualenvwrapper_initialize
+#     for hook in premkvirtualenv postmkvirtualenv prermvirtualenv postrmvirtualenv preactivate postactivate predeactivate postdeactivate
+#     do
+#         assertTrue "Global $WORKON_HOME/$hook was not created" "[ -f $WORKON_HOME/$hook ]"
+#         assertTrue "Global $WORKON_HOME/$hook is not executable" "[ -x $WORKON_HOME/$hook ]"
+#     done
+#     assertTrue "Log file was not created" "[ -f $WORKON_HOME/hook.log ]"
+#     export pre_test_dir=$(cd "$test_dir"; pwd)
+#     echo "echo GLOBAL initialize >> \"$pre_test_dir/catch_output\"" >> "$WORKON_HOME/initialize"
+#     virtualenvwrapper_initialize
+#     output=$(cat "$test_dir/catch_output")
+#     expected="GLOBAL initialize"
+#     assertSame "$expected" "$output"
+# }
+
+. "$test_dir/shunit2"

File tests/test_lazy_loaded.sh

+#!/bin/sh
+
+test_dir=$(cd $(dirname $0) && pwd)
+source "$test_dir/setup.sh"
+
+oneTimeSetUp() {
+    rm -rf "$WORKON_HOME"
+    mkdir -p "$WORKON_HOME"
+    source "$test_dir/../virtualenvwrapper_lazy.sh"
+	virtualenvwrapper_load
+}
+
+oneTimeTearDown() {
+    rm -rf "$WORKON_HOME"
+}
+
+setUp () {
+    echo
+    rm -f "$test_dir/catch_output"
+}
+
+function_defined_normal() {
+    name="$1"
+    assertTrue "$name not defined" "type $name"
+	assertFalse "$name still set to run lazy loader" "typeset -f $name | grep 'virtualenvwrapper_load'"
+}
+
+test_mkvirtualenv_defined_normal() {
+    function_defined_normal mkvirtualenv
+}
+
+test_rmvirtualenv_defined_normal() {
+    function_defined_normal rmvirtualenv
+}
+
+test_lsvirtualenv_defined_normal() {
+    function_defined_normal lsvirtualenv
+}
+
+test_showvirtualenv_defined_normal() {
+    function_defined_normal showvirtualenv
+}
+
+test_workon_defined_normal() {
+    function_defined_normal workon
+}
+
+test_add2virtualenv_defined_normal() {
+    function_defined_normal add2virtualenv
+}
+
+test_cdsitepackages_defined_normal() {
+    function_defined_normal cdsitepackages
+}
+
+test_cdvirtualenv_defined_normal() {
+    function_defined_normal cdvirtualenv
+}
+
+test_cdvirtualenv_defined_normal() {
+    function_defined_normal cdvirtualenv
+}
+
+test_lssitepackages_defined_normal() {
+    function_defined_normal lssitepackages
+}
+
+test_toggleglobalsitepackages_defined_normal() {
+    function_defined_normal toggleglobalsitepackages
+}
+
+test_cpvirtualenv_defined_normal() {
+    function_defined_normal cpvirtualenv
+}
+
+test_setvirtualenvproject_defined_normal() {
+    function_defined_normal setvirtualenvproject
+}
+
+test_mkproject_defined_normal() {
+    function_defined_normal mkproject
+}
+
+test_cdproject_defined_normal() {
+    function_defined_normal cdproject
+}
+
+test_mktmpenv_defined_normal() {
+    function_defined_normal mktmpenv
+}
+
+
+# test_virtualenvwrapper_initialize() {
+#     assertTrue "Initialized" virtualenvwrapper_initialize
+#     for hook in premkvirtualenv postmkvirtualenv prermvirtualenv postrmvirtualenv preactivate postactivate predeactivate postdeactivate
+#     do
+#         assertTrue "Global $WORKON_HOME/$hook was not created" "[ -f $WORKON_HOME/$hook ]"
+#         assertTrue "Global $WORKON_HOME/$hook is not executable" "[ -x $WORKON_HOME/$hook ]"
+#     done
+#     assertTrue "Log file was not created" "[ -f $WORKON_HOME/hook.log ]"
+#     export pre_test_dir=$(cd "$test_dir"; pwd)
+#     echo "echo GLOBAL initialize >> \"$pre_test_dir/catch_output\"" >> "$WORKON_HOME/initialize"
+#     virtualenvwrapper_initialize
+#     output=$(cat "$test_dir/catch_output")
+#     expected="GLOBAL initialize"
+#     assertSame "$expected" "$output"
+# }
+
+. "$test_dir/shunit2"
+_________________________________ [tox sdist] __________________________________
+[TOX] ***creating sdist package
+[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
+[TOX] ***copying new sdistfile to '/Users/dhellmann/.tox/distshare/virtualenvwrapper-3.3.zip'
+______________________________ [tox testenv:py27] ______________________________
+[TOX] ***reusing existing matching virtualenv py27
+[TOX] ***upgrade-installing sdist
+[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
+[TOX] WARNING:test command found but not installed in testenv
+  cmd: /bin/bash
+  env: /Users/dhellmann/Devel/virtualenvwrapper/virtualenvwrapper-startup/.tox/py27
+Maybe forgot to specify a dependency?
+[TOX] /Users/dhellmann/Devel/virtualenvwrapper/virtualenvwrapper-startup$ /bin/bash ./tests/run_tests /Users/dhellmann/Devel/virtualenvwrapper/virtualenvwrapper-startup/.tox/py27 tests/test.sh
+
+********************************************************************************
+Running tests/test.sh
+  VIRTUAL_ENV=/Users/dhellmann/Devel/virtualenvwrapper/virtualenvwrapper-startup/.tox/py27
+  VIRTUALENVWRAPPER_PYTHON=/Users/dhellmann/Devel/virtualenvwrapper/virtualenvwrapper-startup/.tox/py27/bin/python
+    Python 2.7.2
+  PYTHONPATH=
+  SHELL=/bin/bash
+
+
+test_virtualenvwrapper_initialize
+
+test_virtualenvwrapper_run_init_hooks
+
+test_virtualenvwrapper_space_in_workon_home
+
+test_virtualenvwrapper_verify_workon_home
+
+test_virtualenvwrapper_verify_workon_home_missing_dir
+
+test_virtualenvwrapper_verify_workon_home_missing_dir_quiet
+
+test_virtualenvwrapper_verify_workon_home_missing_dir_grep_options
+
+test_python_interpreter_set_incorrectly
+Using real prefix '/Library/Frameworks/Python.framework/Versions/2.7'
+New python executable in no_wrappers/bin/python
+Installing distribute.............................................................................................................................................................................................done.
+Installing pip...............done.
+
+Ran 8 tests.
+
+OK
+
+
+********************************************************************************
+Running tests/test.sh
+  VIRTUAL_ENV=/Users/dhellmann/Devel/virtualenvwrapper/virtualenvwrapper-startup/.tox/py27
+  VIRTUALENVWRAPPER_PYTHON=/Users/dhellmann/Devel/virtualenvwrapper/virtualenvwrapper-startup/.tox/py27/bin/python
+    Python 2.7.2
+  PYTHONPATH=
+  SHELL=/bin/ksh
+
+
+test_virtualenvwrapper_initialize
+
+test_virtualenvwrapper_run_init_hooks
+
+test_virtualenvwrapper_space_in_workon_home
+
+test_virtualenvwrapper_verify_workon_home
+
+test_virtualenvwrapper_verify_workon_home_missing_dir
+
+test_virtualenvwrapper_verify_workon_home_missing_dir_quiet
+
+test_virtualenvwrapper_verify_workon_home_missing_dir_grep_options
+
+test_python_interpreter_set_incorrectly
+Using real prefix '/Library/Frameworks/Python.framework/Versions/2.7'
+New python executable in no_wrappers/bin/python
+Installing distribute.............................................................................................................................................................................................done.
+Installing pip...............done.
+
+Ran 8 tests.
+
+OK
+
+
+********************************************************************************
+Running tests/test.sh
+  VIRTUAL_ENV=/Users/dhellmann/Devel/virtualenvwrapper/virtualenvwrapper-startup/.tox/py27
+  VIRTUALENVWRAPPER_PYTHON=/Users/dhellmann/Devel/virtualenvwrapper/virtualenvwrapper-startup/.tox/py27/bin/python
+    Python 2.7.2
+  PYTHONPATH=
+  SHELL=/bin/zsh
+
+
+test_virtualenvwrapper_initialize
+
+test_virtualenvwrapper_run_init_hooks
+
+test_virtualenvwrapper_space_in_workon_home
+
+test_virtualenvwrapper_verify_workon_home
+
+test_virtualenvwrapper_verify_workon_home_missing_dir
+
+test_virtualenvwrapper_verify_workon_home_missing_dir_quiet
+
+test_virtualenvwrapper_verify_workon_home_missing_dir_grep_options
+
+test_python_interpreter_set_incorrectly
+Using real prefix '/Library/Frameworks/Python.framework/Versions/2.7'
+New python executable in no_wrappers/bin/python
+Installing distribute.............................................................................................................................................................................................done.
+Installing pip...............done.
+
+Ran 8 tests.
+
+OK
+
+________________________________ [tox summary] _________________________________
+[TOX] py27: commands succeeded
+[TOX] congratulations :)

File virtualenvwrapper_lazy.sh

+#!/bin/sh
+# Alternative startup script for faster login times.
+
+export _VIRTUALENVWRAPPER_API="mkvirtualenv rmvirtualenv lsvirtualenv showvirtualenv workon add2virtualenv cdsitepackages cdvirtualenv lssitepackages toggleglobalsitepackages cpvirtualenv setvirtualenvproject mkproject cdproject mktmpenv"
+
+if [ -z "$VIRTUALENVWRAPPER_SCRIPT" ]
+then
+	export VIRTUALENVWRAPPER_SCRIPT="$(which virtualenvwrapper.sh)"
+fi
+if [ -z "$VIRTUALENVWRAPPER_SCRIPT" ]
+then
+	echo "ERROR: virtualenvwrapper_quick.sh: Could not find virtualenvwrapper.sh" 1>&2
+fi
+
+# Load the real implementation of the API from virtualenvwrapper.sh
+function virtualenvwrapper_load {
+    source "$VIRTUALENVWRAPPER_SCRIPT"
+}
+
+# Set up "alias" functions based on the API definition.
+function virtualenvwrapper_setup_lazy_loader {
+    typeset venvw_name
+	for venvw_name in $_VIRTUALENVWRAPPER_API
+	do
+		eval "
+function $venvw_name {
+	virtualenvwrapper_load
+	$venvw_name \"\$@\"
+}
+"
+	done
+}
+
+virtualenvwrapper_setup_lazy_loader