Commits

Doug Hellmann committed 8db6e76 Draft

Update Python 3 compatibility.

Run the hook loader (and all Python-based hooks) within the WORKON_HOME directory, instead of wherever the shell happened to be before. In particular, this effects projects templates, which used to be run in the project directory. That path is now passed as an argument to the template, instead.

Update tests to account for differences in output when exceptions are reported between different Python versions.

Use typeset to properly scope variables used in deactivate().

Fix syntax for declaration of mktmpenv.

Comments (0)

Files changed (5)

docs/source/history.rst

 ===============
 
 dev
+===
 
-  - Fix the name of the script in an error message produced
-    by ``virtualenvwrapper_lazy.sh``. (Contributed by
-    :bbuser:`scottstvnsn`.)
+**Warning:** This release includes some potentially incompatible
+changes for extensions. The python modules for extensions are now
+*always* run with ``PWD=$WORKON_HOME`` (previously the value of PWD
+varied depending on the hook). The *shell* portion of any hook
+(anything sourced by the user's shell when the hook is run) is still
+run in the same place as before.
+
+- All tests pass under Python 3.2 and 3.3.
+- Fix the name of the script in an error message produced
+  by ``virtualenvwrapper_lazy.sh``. (Contributed by
+  :bbuser:`scottstvnsn`.)
 
 3.7.1
+=====
 
   - Rename functions for generating help so they do not pollute the
     global namespace, and especially so they do not interfere with tab
     the option. (:bbissue:`178`)
 
 3.7
+===
 
   - Improve tab-completion support for users of the lazy-loading
     mode. (:bbuser:`upsuper`)
 .. _flake8: https://pypi.python.org/pypi/flake8
 
 3.6.1
+=====
 
   - Replace realpath with a more portable way of converting a relative
     path to an absolute path, used with the ``--python`` option to
     issue. (:bbissue:`171` and :bbissue:`172`)
 
 3.6
+===
 
   - Switch to stevedore_ for plugin management
   - mkvirtualenv_help should use ``$VIRTUALENVWRAPPER_PYTHON`` instead
 .. _stevedore: http://pypi.python.org/pypi/stevedore
 
 3.5
+===
 
   - Rewrite :ref:`command-cpvirtualenv` to use `virtualenv-clone`_
     instead of making the new environment relocatable. Contributed by
 
 
 3.4
+===
 
   - Add :ref:`install-lazy-loader` option.
 
 3.3
+===
 
   - Clean up file permissions and remove shebangs from scripts not
     intended to be executed on the command line. (contributed by
   - Make whitespace consistent. (:bbuser:`agriffis`)
 
 3.2
+===
 
   - Make ``project_dir`` a local variable so that
     :ref:`command-cdproject` does not interfere with other variables
     contributions toward the fix.
 
 3.1
+===
 
   - Fix a problem with activation hooks when associating a new
     virtualenv with an existing project directory. (:bbissue:`122`)
     containing "special" characters such as ``&``. (:bbissue:`132`)
 
 3.0.1
+=====
 
   - Fix some packaging issues that made it more difficult to run the
     tests directly from the sdist package. (:bbissue:`126`)
 
 3.0
+===
 
   - Add Python 3 support, thanks in large part to the efforts of
     Daniel Kraus (:bbuser:`dakra`). Tested under Python 2.6, 2.7, and
     3.2.
 
 2.11.1
+======
 
   - Remove the initialization shortcut because it breaks tab
     completion in sub-shell environments like screen and
     tmux. (:bbissue:`121`)
 
 2.11
+====
 
   - Add ``-a`` option to :ref:`command-mkvirtualenv` to associate a
     new virtualenv with an existing project directory. Contributed by
     reported by :bbuser:`arthuralvim`)
 
 2.10.1
+======
 
   - Changed arguments to :ref:`command-mktmpenv` so it always creates
     an environment name for you. (:bbissue:`114` reported by
     :bbuser:`alex_gaynor`)
 
 2.10
+====
 
   - Incorporated patch to add ``-d`` option to
     :ref:`command-add2virtualenv`, contributed by :bbuser:`miracle2k`.
     zsh. (:bbissue:`111`)
 
 2.9
+===
 
   - Change the shell function shell definition syntax so that ksh will
     treat typeset-declared variables as local. No kidding.
     dependencies using a pip requirements file.
 
 2.8
+===
 
   - Use VIRTUALENVWRAPPER_VIRTUALENV in `cpvirtualenv` (:bbissue:`104`).
   - Add support for `MSYS <http://www.mingw.org/wiki/MSYS>`_
     H. (:bbuser:`noirbizarre`).
 
 2.7.2
+=====
 
   - Move setup code for tab completion later in the startup code so
     all of the needed variables are configured. (:bbissue:`97`)
   - Expand tab completion for zsh to work for all commands.
 
 2.7.1
+=====
 
   - When testing for WORKON_HOME during startup, dereference any
     symlink to make sure it is a directory.
     outside of a virtualenv).
 
 2.7
+===
 
   - Fix problem with space in WORKON_HOME path (:bbissue:`79`).
   - Fix problem with argument processing in lsvirtualenv under zsh
     (:bbissue:`85`).
 
 2.6.3
+=====
 
   - Hard-code the version information in the setup.py and conf.py
     scripts. This still doesn't work for http://readthedocs.org, since
     will make it easier to transition the docs to another site later.
 
 2.6.2
+=====
 
   - Attempted to make the doc build work with http://readthedocs.org.
   - Merged in `Japanese translation of the documentation
     (:ref:`VIRTUALENVWRAPPER_VIRTUALENV <variable-VIRTUALENVWRAPPER_VIRTUALENV>`).
 
 2.6.1
+=====
 
   - Fixed virtualenvwrapper_get_python_version (:bbissue:`73`).
 
 2.6
+===
 
   - Fixed a problem with hook script line endings under Cygwin
     (:bbissue:`68`).
     scripts in the Makefile.
 
 2.5.3
+=====
 
   - Point release uploaded to PyPI during outage on doughellmann.com.
 
 2.5.2
+=====
 
   - Apply patch from Zach Voase to fix :ref:`command-lsvirtualenv`
     under zsh. Resolves :bbissue:`64`.
 
 2.5.1
+=====
 
   - Make :ref:`command-workon` list brief environment details when run
     without argument, instead of full details.
 
 2.5
+===
 
   - Add :ref:`command-showvirtualenv` command.  Modify
     :ref:`command-lsvirtualenv` to make verbose output the default.
 
 2.4
+===
 
   - Add :ref:`command-lsvirtualenv` command with ``-l`` option to run
     :ref:`scripts-get_env_details` hook instead of always running it
     when :ref:`command-workon` has no arguments.
 
 2.3
+===
 
   - Added ``get_env_details`` hook.
 
 2.2.2
+=====
 
   - Integrate Fred Palmer's patch to escape more shell commands to
     avoid aliases.  Resolves :bbissue:`57`.
   - Fix a problem with running mkvirtualenv without arguments (:bbissue:`56`).
 
 2.2.1
+=====
 
   - Escape ``which`` calls to avoid aliases. Resolves :bbissue:`46`.
   - Integrate Manuel Kaufmann's patch to unset GREP_OPTIONS before
     :bbissue:`50`.
 
 2.2
+===
 
   - Switched hook loader execution to a form that works with Python
     2.4 to resolve :bbissue:`43`.
   - Added some debug instrumentation for :bbissue:`35`.
 
 2.1.1
+=====
 
   - Added `Spanish translation for the documentation
     <http://www.doughellmann.com/docs/virtualenvwrapper/es/>`__ via
     virtualenv under zsh.  See :bbissue:`42`.
 
 2.1
+===
 
   - Add support for ksh.  Thanks to Doug Latornell for doing the
     research on what needed to be changed.
     10KiB.
 
 2.0.2
+=====
 
   - Fixed :bbissue:`32`, making virtualenvwrapper.user_scripts compatible
     with Python 2.5 again.
 
 2.0.1
+=====
 
   - Fixed :bbissue:`29`, to use a default value for ``TMPDIR`` if it
     is not set in the user's shell environment.
 
 2.0
+===
 
   - Rewrote hook management using Distribute_ entry points to make it
     easier to share extensions.
 .. _Distribute: http://packages.python.org/distribute/
 
 1.27
+====
   
   - Added cpvirtualenv command [Thomas Desvenain]
 
 1.26
+====
 
   - Fix a problem with error messages showing up during init for users
     with the wrappers installed site-wide but who are not actually
   - Run all tests with all supported shells.
 
 1.25
+====
 
   - Merged in changes to cdsitepackages from William McVey.  It now
     takes an argument and supports tab-completion for directories
     within site-packages.
 
 1.24.2
+======
 
   - Add user provided :ref:`tips-and-tricks` section.
   - Add link to Rich Leland's screencast to :ref:`references` section.
 
 1.24.1
+======
 
   - Add license text to the header of the script.
 
 1.24
+====
 
   - Resolve a bug with the preactivate hook not being run properly.
     Refer to :bbissue:`21` for complete details.
 
 1.23
+====
 
   - Resolve a bug with the postmkvirtualenv hook not being run
     properly.  Refer to :bbissue:`19` and :bbissue:`20` for complete
     details.
 
 1.22
+====
 
   - Automatically create any missing hook scripts as stubs with
     comments to expose the feature in case users are not aware of it.
 
 1.21
+====
 
   - Better protection of ``$WORKON_HOME`` does not exist when the
     wrapper script is sourced.
 
 1.20
+====
 
   - Incorporate lssitepackages feature from Sander Smits.
   - Refactor some of the functions that were using copy-and-paste code
   - Add a few tests.
 
 1.19
+====
 
   - Fix problem with add2virtualenv and relative paths. Thanks to Doug
     Latornell for the bug report James Bennett for the suggested fix.
 
 1.18.1
+======
 
   - Incorporate patch from Sascha Brossmann to fix a
     :bbissue:`15`. Directory normalization was causing ``WORKON_HOME``
     characters in the output of ``pwd``.
 
 1.18
+====
 
   - Remove warning during installation if sphinxcontrib.paverutils is
     not installed. (:bbissue:`10`)
   - Added documentation for deactivate command.
 
 1.17
+====
 
   - Added documentation updates provided by Steve Steiner.
 
 1.16
+====
 
   - Merged in changes to ``cdvirtualenv`` from wam and added tests and
     docs.
     provided by wam.
 
 1.15
+====
+
   - Better error handling in mkvirtualenv.
   - Remove bogus VIRTUALENV_WRAPPER_BIN variable.
 
 1.14
+====
+
   - Wrap the virtualenv version of deactivate() with one that lets us
     invoke the predeactivate hooks.
   - Fix virtualenvwrapper_show_workon_options for colorized versions
     <http://shunit2.googlecode.com/>`_
 
 1.13
+====
 
   - Fix :bbissue:`5` by correctly handling symlinks and limiting the
     list of envs to things that look like they can be activated.
 
 1.12
+====
 
   - Check return value of virtualenvwrapper_verify_workon_home
     everywhere, thanks to Jeff Forcier for pointing out the errors.
   - Enhance test.sh.
 
 1.11
+====
 
   - Optimize virtualenvwrapper_show_workon_options.
   - Add global postactivate hook.
 
 1.10
+====
 
   - Pull in fix for colorized ls from Jeff Forcier
     (:bbchangeset:`b42a25f7b74a`).
 
 1.9
+===
 
   - Add more hooks for operations to run before and after creating or
     deleting environments based on changes from Chris Hasenpflug.
 
 1.8.1
+=====
 
   - Corrected a problem with change to mkvirtualenv that lead to
     release 1.8 by using an alternate fix proposed by James in
     comments on release 1.4.
 
 1.8
+===
 
   - Fix for processing the argument list in mkvirtualenv from
     jorgevargas (:bbissue:`1`)
 
 1.7
+===
 
   - Move to bitbucket.org for hosting
   - clean up TODO list and svn keywords
   - add license section below
 
 1.6.1
+=====
 
   - More zsh support (fixes to rmvirtualenv) from Byron Clark.
 
 1.6
+===
 
   - Add completion support for zsh, courtesy of Ted Leung.
 
 1.5
+===
 
   - Fix some issues with spaces in directory or env names.  They still
     don't really work with virtualenv, though.
   - Added documentation for the postactivate and predeactivate scripts.
 
 1.4
+===
 
   - Includes a new .pth management function based on work contributed
     by James Bennett and Jannis Leidel.
 
 1.3.x
+=====
 
   - Includes a fix for a nasty bug in rmvirtualenv identified by John Shimek.
     mkvirtualenv no_wrappers >/dev/null 2>&1
 	RC=$?
 	assertEquals "mkvirtualenv return code wrong" "0" "$RC"
-    expected="ImportError: No module named virtualenvwrapper.hook_loader"
+    expected="No module named virtualenvwrapper"
     # test_shell is set by tests/run_tests
     if [ "$test_shell" = "" ]
     then
         export test_shell=$SHELL
     fi
-    subshell_output=$(VIRTUALENVWRAPPER_PYTHON="$WORKON_HOME/no_wrappers/bin/python" $test_shell $return_to/virtualenvwrapper.sh 2>&1)
+    outfilename="$WORKON_HOME/test_out.$$"
+    subshell_output=$(VIRTUALENVWRAPPER_PYTHON="$WORKON_HOME/no_wrappers/bin/python" $test_shell $return_to/virtualenvwrapper.sh >"$outfilename" 2>&1)
     #echo "$subshell_output"
-    echo "$subshell_output" | grep -q "$expected" 2>&1
+    cat "$outfilename" | sed "s/'//g" | grep -q "$expected" 2>&1
     found_it=$?
     #echo "$found_it"
-    assertTrue "Expected \'$expected\', got: \'$subshell_output\'" "[ $found_it -eq 0 ]"
+    assertTrue "Expected \'$expected\', got: \'$(cat "$outfilename")\'" "[ $found_it -eq 0 ]"
     assertFalse "Failed to detect invalid Python location" "VIRTUALENVWRAPPER_PYTHON=$VIRTUAL_ENV/bin/python virtualenvwrapper_run_hook initialize >/dev/null 2>&1"
     cd "$return_to"
     deactivate

tests/test_project_mk.sh

 
     output=$(cat "$WORKON_HOME/catch_output")
 
-    expected="GLOBAL premkproject $PROJECT_HOME myproject3
+    expected="GLOBAL premkproject $WORKON_HOME myproject3
 GLOBAL postmkproject $PROJECT_HOME/myproject3"
     assertSame "$expected" "$output"
 

tests/testtemplate/mytemplates/main.py

 
 log = logging.getLogger(__name__)
 
+
 def template(args):
     """Creates a test file containing the args passed to us
     """
-    log.info('Running test template with args %r', args)
-    project = args[0]
-    filename = 'TEST_FILE'
-    log.info('Writing to %s', filename)
+    print('Running test template with args %r' % args)
+    project, project_dir = args
+    filename = os.path.join(project_dir, 'TEST_FILE')
+    print('Writing to %s' % filename)
     output = open(filename, 'w')
     try:
         output.write('\n'.join(args))

virtualenvwrapper.sh

 
     hook_script="$(virtualenvwrapper_tempfile ${1}-hook)" || return 1
 
-    "$VIRTUALENVWRAPPER_PYTHON" -c 'from virtualenvwrapper.hook_loader import main; main()' $HOOK_VERBOSE_OPTION --script "$hook_script" "$@"
+    # Use a subshell to run the python interpreter with hook_loader so
+    # we can change the working directory. This avoids having the
+    # Python 3 interpreter decide that its "prefix" is the virtualenv
+    # if we happen to be inside the virtualenv when we start.
+    ( \
+        cd "$WORKON_HOME" &&
+        "$VIRTUALENVWRAPPER_PYTHON" -m 'virtualenvwrapper.hook_loader' \
+            $HOOK_VERBOSE_OPTION --script "$hook_script" "$@" \
+    )
     result=$?
 
     if [ $result -eq 0 ]
 
     # Replace the deactivate() function with a wrapper.
     eval 'deactivate () {
+        typeset env_postdeactivate_hook
+        typeset old_env
 
         # Call the local hook before the global so we can undo
         # any settings made by the local postactivate first.
         # For some reason zsh insists on prefixing the template
         # names with a space, so strip them out before passing
         # the value to the hook loader.
-        virtualenvwrapper_run_hook --name $(echo $t | sed 's/^ //') "project.template" $envname
+        virtualenvwrapper_run_hook --name $(echo $t | sed 's/^ //') "project.template" "$envname" "$PROJECT_HOME/$envname"
     done
 
     virtualenvwrapper_run_hook "project.post_mkproject"
 #
 # Originally part of virtualenvwrapper.tmpenv plugin
 #
-mktmpenv() {
+function mktmpenv {
     typeset tmpenvname
     typeset RC