Skip to content

Commit 1e52750

Browse files
committed
Add VIRTUALENVWRAPPER_LOG_DIR variable.
1 parent b200811 commit 1e52750

File tree

7 files changed

+89
-15
lines changed

7 files changed

+89
-15
lines changed

ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
2011-04-10 Doug Hellmann <[email protected]>
22

3+
* virtualenvwrapper.sh: Initialize VIRTUALENVWRAPPER_LOG_DIR and
4+
VIRTUALENVWRAPPER_HOOK_DIR.
5+
6+
* virtualenvwrapper/hook_loader.py (main): Use
7+
VIRTUALENVWRAPPER_LOG_DIR for logs instead of assuming
8+
WORKON_HOME.
9+
310
* virtualenvwrapper/user_scripts.py (make_hook): Change verbosity
411
of message telling the user we have created a new hook script for
512
them.

docs/en/history.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ dev
2121
- Fixed some tests that were failing under ksh on Ubuntu 10.10.
2222
- Document the :ref:`VIRTUALENVWRAPPER_VIRTUALENV
2323
<variable-VIRTUALENVWRAPPER_VIRTUALENV>` variable.
24+
- Implement suggestion by Van Lindberg to have
25+
:ref:`VIRTUALENVWRAPPER_HOOK_DIR
26+
<variable-VIRTUALENVWRAPPER_HOOK_DIR>` and
27+
:ref:`VIRTUALENVWRAPPER_LOG_DIR
28+
<variable-VIRTUALENVWRAPPER_LOG_DIR>` variables to control the
29+
locations of hooks and logs.
2430

2531
2.6.3
2632

docs/en/install.rst

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,27 @@ WORKON_HOME
4545
===========
4646

4747
The variable ``WORKON_HOME`` tells virtualenvwrapper where to place
48-
your virtual environments. The default is ``$HOME/.virtualenvs``.
49-
This directory must be created before using any virtualenvwrapper
50-
commands.
48+
your virtual environments. The default is ``$HOME/.virtualenvs``. If
49+
the directory does not exist when virtualenvwrapper is loaded, it will
50+
be created automatically.
51+
52+
.. _variable-VIRTUALENVWRAPPER_HOOK_DIR:
53+
54+
VIRTUALENVWRAPPER_HOOK_DIR
55+
==========================
56+
57+
The variable ``VIRTUALENVWRAPPER_HOOK_DIR`` tells virtualenvwrapper
58+
where the user-defined hooks should be placed. The default is
59+
``$WORKON_HOME``.
60+
61+
.. _variable-VIRTUALENVWRAPPER_LOG_DIR:
62+
63+
VIRTUALENVWRAPPER_LOG_DIR
64+
==========================
65+
66+
The variable ``VIRTUALENVWRAPPER_LOG_DIR`` tells virtualenvwrapper
67+
where the user-defined logs should be written. The default is
68+
``$WORKON_HOME``.
5169

5270
.. _install-shell-config:
5371

docs/en/scripts.rst

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ The end-user customization scripts are either *sourced* (allowing them
88
to modify your shell environment) or *run* as an external program at
99
the appropriate trigger time.
1010

11+
The global scripts applied to all environments should be placed in the
12+
directory named by :ref:`VIRTUALENVWRAPPER_HOOK_DIR
13+
<variable-VIRTUALENVWRAPPER_HOOK_DIR>`. The local scripts should be
14+
placed in the ``bin`` directory of the virtualenv.
15+
1116
.. _scripts-get_env_details:
1217

1318
get_env_details
@@ -17,7 +22,7 @@ get_env_details
1722
:Argument(s): env name
1823
:Sourced/Run: run
1924

20-
``$WORKON_HOME/get_env_details`` is run when ``workon`` is run with no
25+
``$VIRTUALENVWRAPPER_HOOK_DIR/get_env_details`` is run when ``workon`` is run with no
2126
arguments and a list of the virtual environments is printed. The hook
2227
is run once for each environment, after the name is printed, and can
2328
print additional information about that environment.
@@ -31,7 +36,7 @@ initialize
3136
:Argument(s): None
3237
:Sourced/Run: sourced
3338

34-
``$WORKON_HOME/initialize`` is sourced when ``virtualenvwrapper.sh``
39+
``$VIRTUALENVWRAPPER_HOOK_DIR/initialize`` is sourced when ``virtualenvwrapper.sh``
3540
is loaded into your environment. Use it to adjust global settings
3641
when virtualenvwrapper is enabled.
3742

@@ -44,7 +49,7 @@ premkvirtualenv
4449
:Argument(s): name of new environment
4550
:Sourced/Run: run
4651

47-
``$WORKON_HOME/premkvirtualenv`` is run as an external program after
52+
``$VIRTUALENVWRAPPER_HOOK_DIR/premkvirtualenv`` is run as an external program after
4853
the virtual environment is created but before the current environment
4954
is switched to point to the new env. The current working directory for
5055
the script is ``$WORKON_HOME`` and the name of the new environment is
@@ -59,7 +64,7 @@ postmkvirtualenv
5964
:Argument(s): none
6065
:Sourced/Run: sourced
6166

62-
``$WORKON_HOME/postmkvirtualenv`` is sourced after the new environment
67+
``$VIRTUALENVWRAPPER_HOOK_DIR/postmkvirtualenv`` is sourced after the new environment
6368
is created and activated.
6469

6570
.. _scripts-precpvirtualenv:
@@ -71,7 +76,7 @@ precpvirtualenv
7176
:Argument(s): name of original environment, name of new environment
7277
:Sourced/Run: run
7378

74-
``$WORKON_HOME/precpvirtualenv`` is run as an external program after
79+
``$VIRTUALENVWRAPPER_HOOK_DIR/precpvirtualenv`` is run as an external program after
7580
the source environment is duplicated and made relocatable, but before
7681
the ``premkvirtualenv`` hook is run or the current environment is
7782
switched to point to the new env. The current working directory for
@@ -87,7 +92,7 @@ postcpvirtualenv
8792
:Argument(s): none
8893
:Sourced/Run: sourced
8994

90-
``$WORKON_HOME/postcpvirtualenv`` is sourced after the new environment
95+
``$VIRTUALENVWRAPPER_HOOK_DIR/postcpvirtualenv`` is sourced after the new environment
9196
is created and activated.
9297

9398
.. _scripts-preactivate:
@@ -99,7 +104,7 @@ preactivate
99104
:Argument(s): environment name
100105
:Sourced/Run: run
101106

102-
The global ``$WORKON_HOME/preactivate`` script is run before the new
107+
The global ``$VIRTUALENVWRAPPER_HOOK_DIR/preactivate`` script is run before the new
103108
environment is enabled. The environment name is passed as the first
104109
argument.
105110

@@ -116,7 +121,7 @@ postactivate
116121
:Argument(s): none
117122
:Sourced/Run: sourced
118123

119-
The global ``$WORKON_HOME/postactivate`` script is sourced after the
124+
The global ``$VIRTUALENVWRAPPER_HOOK_DIR/postactivate`` script is sourced after the
120125
new environment is enabled. ``$VIRTUAL_ENV`` refers to the new
121126
environment at the time the script runs.
122127

@@ -155,7 +160,7 @@ current environment is deactivated, and can be used to disable or
155160
clear settings in your environment. ``$VIRTUAL_ENV`` refers to the old
156161
environment at the time the script runs.
157162

158-
The global ``$WORKON_HOME/predeactivate`` script is sourced before the
163+
The global ``$VIRTUALENVWRAPPER_HOOK_DIR/predeactivate`` script is sourced before the
159164
current environment is deactivated. ``$VIRTUAL_ENV`` refers to the
160165
old environment at the time the script runs.
161166

@@ -182,7 +187,7 @@ prermvirtualenv
182187
:Argument(s): environment name
183188
:Sourced/Run: run
184189

185-
The ``$WORKON_HOME/prermvirtualenv`` script is run as an external
190+
The ``$VIRTUALENVWRAPPER_HOOK_DIR/prermvirtualenv`` script is run as an external
186191
program before the environment is removed. The full path to the
187192
environment directory is passed as an argument to the script.
188193

@@ -195,6 +200,6 @@ postrmvirtualenv
195200
:Argument(s): environment name
196201
:Sourced/Run: run
197202

198-
The ``$WORKON_HOME/postrmvirtualenv`` script is run as an external
203+
The ``$VIRTUALENVWRAPPER_HOOK_DIR/postrmvirtualenv`` script is run as an external
199204
program after the environment is removed. The full path to the
200205
environment directory is passed as an argument to the script.

tests/test_log_dir.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/sh
2+
3+
#set -x
4+
5+
test_dir=$(cd $(dirname $0) && pwd)
6+
7+
export WORKON_HOME="$(echo ${TMPDIR:-/tmp}/WORKON_HOME | sed 's|//|/|g')"
8+
9+
oneTimeSetUp() {
10+
rm -rf "$WORKON_HOME"
11+
mkdir -p "$WORKON_HOME"
12+
mkdir -p "$WORKON_HOME/hooks"
13+
}
14+
15+
oneTimeTearDown() {
16+
rm -rf "$WORKON_HOME"
17+
}
18+
19+
setUp () {
20+
echo
21+
rm -f "$test_dir/catch_output"
22+
rm -f "$WORKON_HOME/hooks/*"
23+
}
24+
25+
test_virtualenvwrapper_initialize() {
26+
export VIRTUALENVWRAPPER_LOG_DIR="$WORKON_HOME/logs"
27+
mkdir -p "$VIRTUALENVWRAPPER_LOG_DIR"
28+
source "$test_dir/../virtualenvwrapper.sh"
29+
assertTrue "Log file was not created" "[ -f $WORKON_HOME/logs/hook.log ]"
30+
}
31+
32+
. "$test_dir/shunit2"

virtualenvwrapper.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ then
6262
export VIRTUALENVWRAPPER_HOOK_DIR="$WORKON_HOME"
6363
fi
6464

65+
# Set the location of the hook script logs
66+
if [ "$VIRTUALENVWRAPPER_LOG_DIR" = "" ]
67+
then
68+
export VIRTUALENVWRAPPER_LOG_DIR="$WORKON_HOME"
69+
fi
70+
6571
virtualenvwrapper_derive_workon_home() {
6672
typeset workon_home_dir="$WORKON_HOME"
6773

virtualenvwrapper/hook_loader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def main():
6767
# Set up logging to a file
6868
root_logger.setLevel(logging.DEBUG)
6969
file_handler = logging.handlers.RotatingFileHandler(
70-
os.path.expandvars(os.path.join('$WORKON_HOME', 'hook.log')),
70+
os.path.expandvars(os.path.join('$VIRTUALENVWRAPPER_LOG_DIR', 'hook.log')),
7171
maxBytes=10240,
7272
backupCount=1,
7373
)

0 commit comments

Comments
 (0)