Skip to content

Commit 14a67fb

Browse files
authored
Merge pull request #93 from dhellmann/project-dir-hook-files
add project hook scripts
2 parents 7c84b76 + e07db6a commit 14a67fb

File tree

6 files changed

+62
-1
lines changed

6 files changed

+62
-1
lines changed

docs/source/history.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGES
22
=======
33

4+
Next
5+
----
6+
7+
* Look for `./.virtualenvwrapper/postactivate` and
8+
`./.virtualenvwrapper/predeactivate` hook scripts.
9+
410
6.0.0.0a5
511
---------
612

docs/source/projects.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ site, combine the :ref:`templates-bitbucket` and
3131

3232
$ mkproject -t bitbucket -t django my_site
3333

34+
Project Hook Files
35+
==================
36+
37+
The project directory can include additional hook files for the
38+
`postactivate` and `predeactivate` hooks. Placing hook scripts in the
39+
project hook directory, `.virtualenvwrapper`, allows them to be
40+
checked into version control and shared more easily.
41+
42+
When the :ref:`scripts-postactivate` hook runs, it looks for
43+
`.virtualenvwrapper/postactivate` within the project directory and if
44+
it is found it sources the file.
45+
46+
When the :ref:`scripts-predeactivate` hook runs, it looks for
47+
`.virtualenvwrapper/predeactivate` within the project directory and if
48+
it is found it sources the file.
49+
3450
.. seealso::
3551

3652
* :ref:`extensions-templates`

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ project = "virtualenvwrapper.project:post_activate_source"
9393
user_scripts = "virtualenvwrapper.user_scripts:post_activate_source"
9494

9595
[project.entry-points."virtualenvwrapper.pre_deactivate_source"]
96+
project = "virtualenvwrapper.project:pre_deactivate_source"
9697
user_scripts = "virtualenvwrapper.user_scripts:pre_deactivate_source"
9798

9899
[project.entry-points."virtualenvwrapper.post_deactivate_source"]

tests/test_project.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,24 @@ test_virtualenvwrapper_verify_project_home_missing_dir() {
5252
PROJECT_HOME="$old_home"
5353
}
5454

55+
test_virtualenvwrapper_postactivate_hook() {
56+
load_wrappers
57+
mkproject "test_project_hook"
58+
mkdir .virtualenvwrapper
59+
echo "export TEST_PROJECT_HOOK_VAR=true" > .virtualenvwrapper/postactivate
60+
echo "unset TEST_PROJECT_HOOK_VAR" > .virtualenvwrapper/predeactivate
61+
deactivate
62+
63+
# Variable should not be set to start
64+
assertSame "${TEST_PROJECT_HOOK_VAR}" ""
65+
66+
# Activating the env should set it
67+
workon "test_project_hook"
68+
assertSame "true" "${TEST_PROJECT_HOOK_VAR}"
69+
70+
# Deactivating should unset it
71+
deactivate
72+
assertSame "" "${TEST_PROJECT_HOOK_VAR}"
73+
}
74+
5575
. "$test_dir/shunit2"

tox.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ setenv =
2323
deps = .[linter]
2424
commands = flake8 virtualenvwrapper docs/source/conf.py
2525

26+
[flake8]
27+
max-line-length = 200
28+
2629
[testenv:zsh]
2730
setenv =
2831
USING_TOX = 1

virtualenvwrapper/project.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import logging
99
import os
1010

11-
from virtualenvwrapper.user_scripts import make_hook, run_global, PERMISSIONS
11+
from virtualenvwrapper.user_scripts import PERMISSIONS, make_hook, run_global
1212

1313
log = logging.getLogger(__name__)
1414

@@ -58,4 +58,19 @@ def post_activate_source(args):
5858
-a "$VIRTUALENVWRAPPER_PROJECT_CD" = 1 ] && \
5959
virtualenvwrapper_cd \
6060
"$(cat \"$VIRTUAL_ENV/$VIRTUALENVWRAPPER_PROJECT_FILENAME\")"
61+
if [ -f "$VIRTUAL_ENV/$VIRTUALENVWRAPPER_PROJECT_FILENAME" ]; then
62+
if [ -f "$(cat \"$VIRTUAL_ENV/$VIRTUALENVWRAPPER_PROJECT_FILENAME\")/.virtualenvwrapper/postactivate" ]; then
63+
source "$(cat \"$VIRTUAL_ENV/$VIRTUALENVWRAPPER_PROJECT_FILENAME\")/.virtualenvwrapper/postactivate"
64+
fi
65+
fi
66+
"""
67+
68+
69+
def pre_deactivate_source(args):
70+
return """
71+
if [ -f "$VIRTUAL_ENV/$VIRTUALENVWRAPPER_PROJECT_FILENAME" ]; then
72+
if [ -f "$(cat \"$VIRTUAL_ENV/$VIRTUALENVWRAPPER_PROJECT_FILENAME\")/.virtualenvwrapper/predeactivate" ]; then
73+
source "$(cat \"$VIRTUAL_ENV/$VIRTUALENVWRAPPER_PROJECT_FILENAME\")/.virtualenvwrapper/predeactivate"
74+
fi
75+
fi
6176
"""

0 commit comments

Comments
 (0)