virtualenvwrapper-win icon indicating copy to clipboard operation
virtualenvwrapper-win copied to clipboard

Support hooks?

Open adamc55 opened this issue 11 years ago • 9 comments

Am I correct in thinking this does not support hook scripts? (I looked at workon.bat and didn't see where they would be activated.)

If not, a feature request would be to support them.

Thanks for this project, by the way -- it makes my time in Windows a lot less painful.

adamc55 avatar Jun 13 '14 13:06 adamc55

Agreed! I'm willing to take a look at this, time permitting.

MCGallaspy avatar Sep 09 '15 16:09 MCGallaspy

In particular I'm looking for support of postactivate.

MCGallaspy avatar Sep 09 '15 17:09 MCGallaspy

Same here, following this tutorial and was scratching my head trying to figure out if I was missing something obvious

mkane848 avatar Jan 18 '16 17:01 mkane848

I see hooks support for mkvirtualenv.bat, but what about postactivate? Voting for it as well

hellt avatar Feb 16 '16 05:02 hellt

Hello, I was able to enable the "postactivate" hook by adding the following to the end of PYTHON\scripts\workon.bat (similarly to mkvirtualenv.bat)

if defined VIRTUALENVWRAPPER_HOOK_DIR (
    if exist "%VIRTUALENVWRAPPER_HOOK_DIR%\postactivate.bat" (
        call "%VIRTUALENVWRAPPER_HOOK_DIR%\postactivate.bat"
    )
)

I am not sure enabling postactivate is so easy or I am missing something - if this solution is correct I can provide a PR.

spapas avatar Mar 10 '16 07:03 spapas

@spapas your solution is probably useful as-is, so go ahead with the PR.

I see that the *nix virtualenvwrapper does a bit more. It is called like this:

VIRTUALENVWRAPPER_PROJECT_CD=$cd_after_activate virtualenvwrapper_run_hook "post_activate"

i.e. it sets up the VIRTUALENVWRAPPER_PROJECT_CD environment variable before calling virtualenvwrapper_run_hook, which looks complex (although I'm not sure it does anything relevant to windows). For reference:

function virtualenvwrapper_run_hook {
    typeset hook_script
    typeset result

    hook_script="$(virtualenvwrapper_tempfile ${1}-hook)" || return 1

    # 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.
    ( \
        virtualenvwrapper_cd "$WORKON_HOME" &&
        "$VIRTUALENVWRAPPER_PYTHON" -m 'virtualenvwrapper.hook_loader' \
            $HOOK_VERBOSE_OPTION --script "$hook_script" "$@" \
    )
    result=$?

    if [ $result -eq 0 ]
    then
        if [ ! -f "$hook_script" ]
        then
            echo "ERROR: virtualenvwrapper_run_hook could not find temporary file $hook_script" 1>&2
            command \rm -f "$hook_script"
            return 2
        fi
        # cat "$hook_script"
        source "$hook_script"
    elif [ "${1}" = "initialize" ]
    then
        cat - 1>&2 <<EOF
virtualenvwrapper.sh: There was a problem running the initialization hooks.

If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=$VIRTUALENVWRAPPER_PYTHON and that PATH is
set properly.
EOF
    fi
    command \rm -f "$hook_script"
    return $result
}

thebjorn avatar Mar 10 '16 10:03 thebjorn

Hello @thebjorn, from what I see, the unix version runs the same code also on post_mkvirtualenv -- but I don't see it implemented in the windows versionm, so probably the virtualenvwrapper_run_hook is not needed.

Also, at least in my case (doing a set DJANGO_SETTINGS_MODULE=%VENV%.settings.dev when activating a virtualenv) it works great!

spapas avatar Mar 10 '16 10:03 spapas

I've added a slightly different version of the pull request, which also supports postdeactivate. This means you can clean up whatever you did in postactivate.

davidfraser avatar Dec 20 '16 14:12 davidfraser

Mother docs: http://virtualenvwrapper.readthedocs.io/en/latest/scripts.html List of hooks (we don't need to distinguish between source/run on windows).

global/local/both hook-name(parameters)

  • [ ] both get_env_details(env-name)
  • [ ] global initialize()
  • [ ] global premkvirtualenv(name-of-new-environment)
  • [ ] global postmkvirtualenv()
  • [ ] global precpvirtualenv(original-env-name, new-env-name)
  • [ ] global postcpvirtualenv()
  • [ ] both preactivate(env-name)
  • [ ] both postactivate()
  • [ ] both predeactivate()
  • [ ] both postdeactivate()
  • [ ] global prermvirtualenv(env-name)
  • [ ] global postrmvirtualenv(env-name)
  • [ ] global premkproject(name-of-new-project)
  • [ ] global postmkproject()

thebjorn avatar Aug 04 '17 12:08 thebjorn