Skip to content

Conversation

@davidfraser
Copy link

This adds the ability to create postactivate.bat and postdeactivate.bat hooks in each virtual environment, analogous to the postactivate and postdeactivate supported by virtualenvwrapper

This is equivalent to #64 but also supports postdeactivate - see #43


if defined VIRTUAL_ENV (
call "%VIRTUAL_ENV%\Scripts\deactivate.bat"
if exist "%VIRTUAL_ENV%\Scripts\postdeactivate.bat" (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deactivate.bat clears %VIRTUAL_ENV%, so this works by taking advantage of DOS's rather special variable expansion rules. It might be clearer if the postdeactivate.bat path was saved before the call ... deactivate.bat?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, done

)
)

if exist "%WORKON_HOME%\%VENV%\Scripts\postactivate.bat" (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mother-project implements both local and global post-activate/deactivate hooks. This code implements the "local" post-activate/deactivate hooks. #64 implements the global postactivate hook (only). I would prefer a more comprehensive hook implementation PR...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added the global versions of these hooks, following #64 but including a global post-deactivate

David Fraser added 5 commits December 27, 2017 16:52
* upstream/master:
  upversion
  debug leftover
  Typo fix in CHANGELOG.rst
  Getting ready for next release..
  Publish the vwenv.bat script.
  It's starting to be a lot of setup that needs to be done, and it's copied into most scripts with varying levels of fidelity.  This is an attempt to collect these into one file, and also to make the work in davidmarble#95 easier to implement.
  Push 1.2.4 to PyPI.
  Fix workon, rmvirtualenv, and mkvirtualenv -a when virtualenv and/or project contain spaces. This fixes davidmarble#89.
  Remove debug echo.
  Fix 3 failing tests (test-code issues).
  "Number" headers in CHANGELOG similar to README (the files are concatenated and must match).
  Updates to add2virtualenv to fix davidmarble#93.
  Tests for add2virtualenv and setprojectdir.
  Convert tabs to spaces, fix som issues with spaces in paths, and exit with a distinct error level for each error.
  Make sure we call cleanup, even on a successful run, and set ERRORLEVEL to zero.
  Missing return from error_msg.
  Add support for -h, --help switches, and fix davidmarble#92.
  Add support for -h --help options, and fix davidmarble#92.
  tests for mkproject
  Add publishing step that checks syntax of the reStructuredText in README.rst.
  updated mkproject to follow coding style of mkvirtualenv.  updated readme and changelog as requested.
  New commands need to be installed..
  Add virtualenvwrapper.bat command: Print a list of commands and their descriptions as basic help output. (The linux version has it: https://virtualenvwrapper.readthedocs.io/en/latest/command_ref.html#virtualenvwrapper)
  Added top-comment and usage block to whereis.bat Changed some awkward verbiage in README.rst
  Missed a goto:eof..
  Fix a number of issues with spaces in virtualenv names in rmvirtualenv.bat and simplified the removal algorithm.
  Allow spaces in virtualenv names.
  Start next version.
  Added mkproject script (with better default PROJECT_HOME) to setup.py
  upversion
  Added invoke tasks.py file.
  ignore PyCharm files
  Grab fix for path with env-var issue from @nakedmin2017 (nakedmind2017@de4ada2)
  virtualenv doesn't need an equal sign for parameters with double dashes, fixes davidmarble#86.
  Fix for davidmarble#85.
  Fix other potential dir-with-spaces problems.
  Test for davidmarble#85
  Add -k option to run_tests.bat for running single tests.
  Fix (?) PyPI barfing on readme.
  Bump Version
  Release notes for v1.2.2
  Implement -a, -i, and -r options to mkvirtualenv.
  This is a little bit of a hefty rewrite, but it's needed to be able to handle some options, and let other options through to virtualenv.
  Start of test-suite.
@thebjorn
Copy link
Collaborator

Hi @davidfraser thanks for looking into this problem. I've started working on complete support of all hooks, in the hookimpl branch. The implementation mirrors the linux hook implementation in that the hooks are called from a common virtualenvwrapper_run_hook script (see https://github.com/davidmarble/virtualenvwrapper-win/blob/hookimpl/scripts/virtualenvwrapper_run_hook.bat and usage e.g. for workon in https://github.com/davidmarble/virtualenvwrapper-win/blob/hookimpl/scripts/workon.bat).

One of the problems I've ran into is that the postdeactivate hook is difficult to add as a wrapper on the calling site (it's called directly from mkvirtualenv, rmvirtualenv, and workon), since the user can legitimately call deactivate themselves -- and that is not our file.

The linux version of virtualenvwrapper solves this by stashing away the original deactivate function and overriding it with a wrapper (https://bitbucket.org/virtualenvwrapper/virtualenvwrapper/src/36b8050a90192a087d1060f32083249d13d8a215/virtualenvwrapper.sh?at=master&fileviewer=file-view-default#virtualenvwrapper.sh-770).

The solution for us is probably going to be similarly invasive, either we inject our code into the existing deactivate.bat file (we do this already to restore PYTHONPATH, https://github.com/davidmarble/virtualenvwrapper-win/blob/hookimpl/scripts/mkvirtualenv.bat#L204), or we move the existing deactivate.bat (to e.g. _virtualenv_original_deactivate.bat) and install our own deactivate.bat which calls this file (between calls to predeactivate and postdeactivate).

Besides the problem of messing with files that don't belong to us, we also need to come up with a solution for existing virtualenvs, since we don't want to require that users re-create their virtualenvs after upgrading virtualenvwrapper-win.

Maybe requiring a call to a new function/command (e.g. virtualenvwrapper enable-hooks) that would change the activate.bat/deactivate.bat per the above..? (and maybe add hook-status to lsvirtualenv/workon output..?)

@davidfraser
Copy link
Author

That looks a lot more comprehensive, nice to have all the hooks implemented!

I think your solution of needing to run enable-hooks to update the activate/deactivate is a good one. I wouldn't think hook-status is needed on every lsvirtualenv - but maybe a virtualenvwrapper hook-status command would be good (since we'd need to be able to determine whether enable-hooks should make changes anyway)

@davidfraser
Copy link
Author

Closing this as your branch supercedes it

@natehawkboss
Copy link

I am confused, is this pull request not being accepted? Am I supposed to pull from the other repo instead? Seems like features that should be added.

@thebjorn
Copy link
Collaborator

@natehawkboss check this branch for the current work on this https://github.com/davidmarble/virtualenvwrapper-win/tree/hookimpl

Read my comment about deactivate.bat above for the current issue (rewriting files that other packages install are generally not a good idea, and this package is used a lot so any problems would be severe)...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants