Commits

Doug Hellmann  committed 1e52750

Add VIRTUALENVWRAPPER_LOG_DIR variable.

  • Participants
  • Parent commits b200811

Comments (0)

Files changed (7)

 2011-04-10  Doug Hellmann  <dhellmann@racemi.com>
 
+	* virtualenvwrapper.sh: Initialize VIRTUALENVWRAPPER_LOG_DIR and
+	VIRTUALENVWRAPPER_HOOK_DIR.
+
+	* virtualenvwrapper/hook_loader.py (main): Use
+	VIRTUALENVWRAPPER_LOG_DIR for logs instead of assuming
+	WORKON_HOME.
+
 	* virtualenvwrapper/user_scripts.py (make_hook): Change verbosity
 	of message telling the user we have created a new hook script for
 	them.

File docs/en/history.rst

   - Fixed some tests that were failing under ksh on Ubuntu 10.10.
   - Document the :ref:`VIRTUALENVWRAPPER_VIRTUALENV
     <variable-VIRTUALENVWRAPPER_VIRTUALENV>` variable.
+  - Implement suggestion by Van Lindberg to have
+    :ref:`VIRTUALENVWRAPPER_HOOK_DIR
+    <variable-VIRTUALENVWRAPPER_HOOK_DIR>` and
+    :ref:`VIRTUALENVWRAPPER_LOG_DIR
+    <variable-VIRTUALENVWRAPPER_LOG_DIR>` variables to control the
+    locations of hooks and logs.
 
 2.6.3
 

File docs/en/install.rst

 ===========
 
 The variable ``WORKON_HOME`` tells virtualenvwrapper where to place
-your virtual environments.  The default is ``$HOME/.virtualenvs``.
-This directory must be created before using any virtualenvwrapper
-commands.
+your virtual environments.  The default is ``$HOME/.virtualenvs``. If
+the directory does not exist when virtualenvwrapper is loaded, it will
+be created automatically.
+
+.. _variable-VIRTUALENVWRAPPER_HOOK_DIR:
+
+VIRTUALENVWRAPPER_HOOK_DIR
+==========================
+
+The variable ``VIRTUALENVWRAPPER_HOOK_DIR`` tells virtualenvwrapper
+where the user-defined hooks should be placed. The default is
+``$WORKON_HOME``.
+
+.. _variable-VIRTUALENVWRAPPER_LOG_DIR:
+
+VIRTUALENVWRAPPER_LOG_DIR
+==========================
+
+The variable ``VIRTUALENVWRAPPER_LOG_DIR`` tells virtualenvwrapper
+where the user-defined logs should be written. The default is
+``$WORKON_HOME``.
 
 .. _install-shell-config:
 

File docs/en/scripts.rst

 to modify your shell environment) or *run* as an external program at
 the appropriate trigger time.
 
+The global scripts applied to all environments should be placed in the
+directory named by :ref:`VIRTUALENVWRAPPER_HOOK_DIR
+<variable-VIRTUALENVWRAPPER_HOOK_DIR>`. The local scripts should be
+placed in the ``bin`` directory of the virtualenv.
+
 .. _scripts-get_env_details:
 
 get_env_details
   :Argument(s): env name
   :Sourced/Run: run
 
-``$WORKON_HOME/get_env_details`` is run when ``workon`` is run with no
+``$VIRTUALENVWRAPPER_HOOK_DIR/get_env_details`` is run when ``workon`` is run with no
 arguments and a list of the virtual environments is printed.  The hook
 is run once for each environment, after the name is printed, and can
 print additional information about that environment.
   :Argument(s): None
   :Sourced/Run: sourced
 
-``$WORKON_HOME/initialize`` is sourced when ``virtualenvwrapper.sh``
+``$VIRTUALENVWRAPPER_HOOK_DIR/initialize`` is sourced when ``virtualenvwrapper.sh``
 is loaded into your environment.  Use it to adjust global settings
 when virtualenvwrapper is enabled.
 
   :Argument(s): name of new environment
   :Sourced/Run: run
 
-``$WORKON_HOME/premkvirtualenv`` is run as an external program after
+``$VIRTUALENVWRAPPER_HOOK_DIR/premkvirtualenv`` is run as an external program after
 the virtual environment is created but before the current environment
 is switched to point to the new env. The current working directory for
 the script is ``$WORKON_HOME`` and the name of the new environment is
   :Argument(s): none
   :Sourced/Run: sourced
 
-``$WORKON_HOME/postmkvirtualenv`` is sourced after the new environment
+``$VIRTUALENVWRAPPER_HOOK_DIR/postmkvirtualenv`` is sourced after the new environment
 is created and activated.
 
 .. _scripts-precpvirtualenv:
   :Argument(s): name of original environment, name of new environment
   :Sourced/Run: run
 
-``$WORKON_HOME/precpvirtualenv`` is run as an external program after
+``$VIRTUALENVWRAPPER_HOOK_DIR/precpvirtualenv`` is run as an external program after
 the source environment is duplicated and made relocatable, but before
 the ``premkvirtualenv`` hook is run or the current environment is
 switched to point to the new env. The current working directory for
   :Argument(s): none
   :Sourced/Run: sourced
 
-``$WORKON_HOME/postcpvirtualenv`` is sourced after the new environment
+``$VIRTUALENVWRAPPER_HOOK_DIR/postcpvirtualenv`` is sourced after the new environment
 is created and activated.
 
 .. _scripts-preactivate:
   :Argument(s): environment name
   :Sourced/Run: run
 
-The global ``$WORKON_HOME/preactivate`` script is run before the new
+The global ``$VIRTUALENVWRAPPER_HOOK_DIR/preactivate`` script is run before the new
 environment is enabled.  The environment name is passed as the first
 argument.
 
   :Argument(s): none
   :Sourced/Run: sourced
 
-The global ``$WORKON_HOME/postactivate`` script is sourced after the
+The global ``$VIRTUALENVWRAPPER_HOOK_DIR/postactivate`` script is sourced after the
 new environment is enabled. ``$VIRTUAL_ENV`` refers to the new
 environment at the time the script runs.
 
 clear settings in your environment. ``$VIRTUAL_ENV`` refers to the old
 environment at the time the script runs.
 
-The global ``$WORKON_HOME/predeactivate`` script is sourced before the
+The global ``$VIRTUALENVWRAPPER_HOOK_DIR/predeactivate`` script is sourced before the
 current environment is deactivated.  ``$VIRTUAL_ENV`` refers to the
 old environment at the time the script runs.
 
   :Argument(s): environment name
   :Sourced/Run: run
 
-The ``$WORKON_HOME/prermvirtualenv`` script is run as an external
+The ``$VIRTUALENVWRAPPER_HOOK_DIR/prermvirtualenv`` script is run as an external
 program before the environment is removed. The full path to the
 environment directory is passed as an argument to the script.
 
   :Argument(s): environment name
   :Sourced/Run: run
 
-The ``$WORKON_HOME/postrmvirtualenv`` script is run as an external
+The ``$VIRTUALENVWRAPPER_HOOK_DIR/postrmvirtualenv`` script is run as an external
 program after the environment is removed. The full path to the
 environment directory is passed as an argument to the script.

File tests/test_log_dir.sh

+#!/bin/sh
+
+#set -x
+
+test_dir=$(cd $(dirname $0) && pwd)
+
+export WORKON_HOME="$(echo ${TMPDIR:-/tmp}/WORKON_HOME | sed 's|//|/|g')"
+
+oneTimeSetUp() {
+    rm -rf "$WORKON_HOME"
+    mkdir -p "$WORKON_HOME"
+    mkdir -p "$WORKON_HOME/hooks"
+}
+
+oneTimeTearDown() {
+    rm -rf "$WORKON_HOME"
+}
+
+setUp () {
+    echo
+    rm -f "$test_dir/catch_output"
+    rm -f "$WORKON_HOME/hooks/*"
+}
+
+test_virtualenvwrapper_initialize() {
+    export VIRTUALENVWRAPPER_LOG_DIR="$WORKON_HOME/logs"
+    mkdir -p "$VIRTUALENVWRAPPER_LOG_DIR"
+    source "$test_dir/../virtualenvwrapper.sh"
+    assertTrue "Log file was not created" "[ -f $WORKON_HOME/logs/hook.log ]"
+}
+
+. "$test_dir/shunit2"

File virtualenvwrapper.sh

     export VIRTUALENVWRAPPER_HOOK_DIR="$WORKON_HOME"
 fi
 
+# Set the location of the hook script logs
+if [ "$VIRTUALENVWRAPPER_LOG_DIR" = "" ]
+then
+    export VIRTUALENVWRAPPER_LOG_DIR="$WORKON_HOME"
+fi
+
 virtualenvwrapper_derive_workon_home() {
     typeset workon_home_dir="$WORKON_HOME"
 

File virtualenvwrapper/hook_loader.py

     # Set up logging to a file
     root_logger.setLevel(logging.DEBUG)
     file_handler = logging.handlers.RotatingFileHandler(
-        os.path.expandvars(os.path.join('$WORKON_HOME', 'hook.log')),
+        os.path.expandvars(os.path.join('$VIRTUALENVWRAPPER_LOG_DIR', 'hook.log')),
         maxBytes=10240,
         backupCount=1,
         )