Skip to content

Commit 71aa555

Browse files
committed
Turn off logging by default
Fix race condition in hook loader by disabling logging by default. Fixes bug #152 --HG-- rename : tests/test_log_dir.sh => tests/test_log_file.sh
1 parent 2187fa6 commit 71aa555

File tree

7 files changed

+27
-41
lines changed

7 files changed

+27
-41
lines changed

docs/source/history.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ dev
88
mode. (:bbuser:`upsuper`)
99
- Add ``--help`` option to ``mkproject``.
1010
- Add ``--help`` option to ``workon``.
11+
- Turn off logging from the hook loader by default, and replace
12+
``VIRTUALENVWRAPPER_LOG_DIR`` environment variable with
13+
``VIRTUALENVWRAPPER_LOG_FILE``. The rotating log behavior remains
14+
the same. The motivation for this change is the race condition
15+
caused by that rotating behavior, especially when the wrappers are
16+
being used by users with different permissions and
17+
umasks. (:bbissue:`152`)
1118

1219
3.6.1
1320

docs/source/install.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,14 @@ default is ``$WORKON_HOME``.
220220

221221
* :ref:`scripts`
222222

223-
.. _variable-VIRTUALENVWRAPPER_LOG_DIR:
223+
.. _variable-VIRTUALENVWRAPPER_LOG_FILE:
224224

225225
Location of Hook Logs
226226
---------------------
227227

228-
The variable ``VIRTUALENVWRAPPER_LOG_DIR`` tells virtualenvwrapper
228+
The variable ``VIRTUALENVWRAPPER_LOG_FILE`` tells virtualenvwrapper
229229
where the logs for the hook loader should be written. The default is
230-
``$WORKON_HOME``.
230+
to not log from the hooks.
231231

232232
.. _variable-VIRTUALENVWRAPPER_VIRTUALENV:
233233

tests/test.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ test_virtualenvwrapper_initialize() {
2626
assertTrue "Global $WORKON_HOME/$hook was not created" "[ -f $WORKON_HOME/$hook ]"
2727
assertTrue "Global $WORKON_HOME/$hook is not executable" "[ -x $WORKON_HOME/$hook ]"
2828
done
29-
assertTrue "Log file was not created" "[ -f $WORKON_HOME/hook.log ]"
3029
export pre_test_dir=$(cd "$test_dir"; pwd)
3130
echo "echo GLOBAL initialize >> \"$pre_test_dir/catch_output\"" >> "$WORKON_HOME/initialize"
3231
virtualenvwrapper_initialize

tests/test_log_dir.sh renamed to tests/test_log_file.sh

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,25 @@ setUp () {
88
}
99

1010
test_set_by_user() {
11-
export VIRTUALENVWRAPPER_LOG_DIR="$WORKON_HOME/logs"
12-
mkdir -p "$VIRTUALENVWRAPPER_LOG_DIR"
11+
export VIRTUALENVWRAPPER_LOG_FILE="$WORKON_HOME/hooks.log"
1312
source "$test_dir/../virtualenvwrapper.sh"
14-
assertTrue "Log file was not created" "[ -f $WORKON_HOME/logs/hook.log ]"
13+
assertTrue "Log file was not created" "[ -f $VIRTUALENVWRAPPER_LOG_FILE ]"
1514
}
1615

1716
test_file_permissions() {
18-
export VIRTUALENVWRAPPER_LOG_DIR="$WORKON_HOME/logs"
19-
mkdir -p "$VIRTUALENVWRAPPER_LOG_DIR"
17+
export VIRTUALENVWRAPPER_LOG_FILE="$WORKON_HOME/hooks.log"
2018
source "$test_dir/../virtualenvwrapper.sh"
21-
perms=$(ls -l "$WORKON_HOME/logs/hook.log" | cut -f1 -d' ')
19+
perms=$(ls -l "$VIRTUALENVWRAPPER_LOG_FILE" | cut -f1 -d' ')
2220
#echo $perms
2321
assertTrue "Log file permissions are wrong: $perms" "echo $perms | grep '^-rw-rw'"
2422
}
2523

2624
test_not_set_by_user() {
2725
unset WORKON_HOME
28-
unset VIRTUALENVWRAPPER_LOG_DIR
26+
unset VIRTUALENVWRAPPER_LOG_FILE
2927
unset VIRTUALENVWRAPPER_HOOK_DIR
3028
source "$test_dir/../virtualenvwrapper.sh"
31-
assertSame "$WORKON_HOME" "$VIRTUALENVWRAPPER_LOG_DIR"
29+
assertSame "" "$VIRTUALENVWRAPPER_LOG_FILE"
3230
}
3331

3432
. "$test_dir/shunit2"

tests/test_run_hook.sh

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,4 @@ test_virtualenvwrapper_run_hook_permissions() {
6464
assertSame "Errno 13] Permission denied" "$error"
6565
}
6666

67-
test_virtualenvwrapper_run_hook_without_log_dir() {
68-
old_log_dir="$VIRTUALENVWRAPPER_LOG_DIR"
69-
unset VIRTUALENVWRAPPER_LOG_DIR
70-
assertFalse "virtualenvwrapper_run_hook initialize"
71-
export VIRTUALENVWRAPPER_LOG_DIR="$old_log_dir"
72-
}
73-
7467
. "$test_dir/shunit2"

virtualenvwrapper.sh

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,6 @@ function virtualenvwrapper_run_hook {
202202

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

205-
if [ -z "$VIRTUALENVWRAPPER_LOG_DIR" ]
206-
then
207-
echo "ERROR: VIRTUALENVWRAPPER_LOG_DIR is not set." 1>&2
208-
command \rm -f "$hook_script"
209-
return 1
210-
fi
211205
"$VIRTUALENVWRAPPER_PYTHON" -c 'from virtualenvwrapper.hook_loader import main; main()' $HOOK_VERBOSE_OPTION --script "$hook_script" "$@"
212206
result=$?
213207

@@ -286,12 +280,6 @@ function virtualenvwrapper_initialize {
286280
export VIRTUALENVWRAPPER_HOOK_DIR="$WORKON_HOME"
287281
fi
288282

289-
# Set the location of the hook script logs
290-
if [ "$VIRTUALENVWRAPPER_LOG_DIR" = "" ]
291-
then
292-
export VIRTUALENVWRAPPER_LOG_DIR="$WORKON_HOME"
293-
fi
294-
295283
virtualenvwrapper_run_hook "initialize"
296284

297285
virtualenvwrapper_setup_tab_completion

virtualenvwrapper/hook_loader.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,17 @@ def main():
8585
root_logger = logging.getLogger('')
8686

8787
# Set up logging to a file
88-
root_logger.setLevel(logging.DEBUG)
89-
file_handler = GroupWriteRotatingFileHandler(
90-
os.path.expandvars(os.path.join('$VIRTUALENVWRAPPER_LOG_DIR',
91-
'hook.log')),
92-
maxBytes=10240,
93-
backupCount=1,
94-
)
95-
formatter = logging.Formatter(LOG_FORMAT)
96-
file_handler.setFormatter(formatter)
97-
root_logger.addHandler(file_handler)
88+
logfile = os.environ.get('VIRTUALENVWRAPPER_LOG_FILE')
89+
if logfile:
90+
root_logger.setLevel(logging.DEBUG)
91+
file_handler = GroupWriteRotatingFileHandler(
92+
logfile,
93+
maxBytes=10240,
94+
backupCount=1,
95+
)
96+
formatter = logging.Formatter(LOG_FORMAT)
97+
file_handler.setFormatter(formatter)
98+
root_logger.addHandler(file_handler)
9899

99100
# Send higher-level messages to the console, too
100101
console = logging.StreamHandler()

0 commit comments

Comments
 (0)