Commits

Anonymous committed 9806c05

Consider this version 2.0

  • Participants
  • Parent commits cb6198f

Comments (0)

Files changed (4)

scripts/deinitenv_generic

 # to bin/postdeactivate. Alternatively, you may also symlink
 # bin/postdeactivate to (a custom version of) this file.
 
-if [ ! -z "$_OLD_DJANGO_SETTINGS_MODULE" ]; then
-    DJANGO_SETTINGS_MODULE=$_OLD_DJANGO_SETTINGS_MODULE
-else
-    unset DJANGO_SETTINGS_MODULE
-fi
-
-if [ ! -z "$_OLD_PYTHONPATH" ]; then
-    PYTHONPATH=$_OLD_PYTHONPATH
-else
-    unset PYTHONPATH
-fi
-
-unset _OLD_DJANGO_SETTINGS_MODULE _OLD_PYTHONPATH PROJECT_ROOT \
-    DJANGO_PROJECT DJANGO_SETTINGS SHOW_DJANGO_ENVIRONMENT
+deinitenv

scripts/initenv_example

 # lines to bin/postactivate. Alternatively, you may also symlink
 # bin/postactivate to a custom version of this file.
 
-#DOMAIN=local.mysite.org # Domain for virtual host configuration - optional
+#export DOMAIN=local.mysite.org # Domain for virtual host configuration - optional
 PROJECT_ROOT=/Users/grone/repos/django-environments
 DJANGO_PROJECT=mysite
 DJANGO_SETTINGS=settings.env.development
-SHOW_DJANGO_ENVIRONMENT=yes
+SHOW_DJANGO_ENVIRONMENT=yes # 'yes' shows basic info, 'settings' a full settings dump
+
+source $PROJECT_ROOT/scripts/initenv_generic
+[ ! $? -eq 0 ] && return 1
+djenv $DJANGO_PROJECT $DJANGO_SETTINGS
+cdjango
 
 # These two lines help greatly when using pip with virtualenv, see
 # http://pip.openplans.org/
 export PIP_REQUIRE_VIRTUALENV=true PIP_RESPECT_VIRTUALENV=true
-
-# Pass additional directories for sys.path as arguments to
-# initenv_generic, e.g.
-# source $PROJECT_ROOT/scripts/initenv_generic $PROJECT_ROOT/apps
-source $PROJECT_ROOT/scripts/initenv_generic
-[ ! $? -eq 0 ] && return 1

scripts/initenv_generic

 # Set this to something that can be imported within DJANGO_PROJECT, like 'settings'
 #DJANGO_SETTINGS=settings
 
-# In case we are used within a virtualenv setting
 _deactivate() {
-    [ ! -z "$VIRTUAL_ENV" ] && deactivate
+    [ "$DEACTIVATE_VIRTUAL_ENV_ON_ERROR" = "yes" -a ! -z "$VIRTUAL_ENV" ] && deactivate
     return 0
 }
 
-# The root of the project should exist, of course
-[ -z "$PROJECT_ROOT" ] && \
-    echo "Variable \$PROJECT_ROOT not set or empty" 1>&2 && _deactivate && return 1
-[ ! -d "$PROJECT_ROOT" ] && \
-     echo "Variable \$PROJECT_ROOT does not point to a readable directory" 1>&2 && _deactivate && return 1
+function initenv() {
+    DJANGO_PROJECT=$1
+    DJANGO_SETTINGS=$2
 
-# Check Django project as well
-[ -z "$DJANGO_PROJECT" ] && \
-    echo "Variable \$DJANGO_PROJECT not set or empty" 1>&2 && _deactivate && return 1
-[ ! -d "$PROJECT_ROOT/$DJANGO_PROJECT" ] && \
-    echo "Variable \$DJANGO_PROJECT does not identify a readable directory within $PROJECT_ROOT" 1>&2 && \
-    _deactivate && return 1
+    # The root of the project should exist, of course
+    [ -z "$PROJECT_ROOT" ] && \
+        echo "Variable \$PROJECT_ROOT not set or empty" 1>&2 && _deactivate && return 1
+    [ ! -d "$PROJECT_ROOT" ] && \
+         echo "Variable \$PROJECT_ROOT does not point to a readable directory" 1>&2 && _deactivate && return 1
 
-# If no particular settings are defined, just use the "base" settings
-_OLD_DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
-if [ -n "$DJANGO_SETTINGS" ]; then
-    DJANGO_SETTINGS_MODULE=$DJANGO_PROJECT.$DJANGO_SETTINGS
-else
-    DJANGO_SETTINGS_MODULE=$DJANGO_PROJECT.settings
-fi
+    # Check Django project as well
+    [ -z "$DJANGO_PROJECT" ] && \
+        echo "Variable \$DJANGO_PROJECT not set or empty" 1>&2 && _deactivate && return 1
+    [ ! -d "$PROJECT_ROOT/$DJANGO_PROJECT" ] && \
+        echo "Variable \$DJANGO_PROJECT does not identify a readable directory within $PROJECT_ROOT" 1>&2 && \
+        _deactivate && return 1
 
-# Set the PYTHONPATH to include PROJECT_ROOT; assume first argument are additional directories
-_OLD_PYTHONPATH=$PYTHONPATH
-PYTHONPATH=$PROJECT_ROOT:$1:$PYTHONPATH
+    # If no particular settings are defined, just use the "base" settings
+    [ ! -z "$_OLD_DJANGO_SETTINGS_MODULE" ] && DJANGO_SETTINGS_MODULE=$_OLD_DJANGO_SETTINGS_MODULE
+    _OLD_DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
+    if [ -n "$DJANGO_SETTINGS" ]; then
+        DJANGO_SETTINGS_MODULE=$DJANGO_PROJECT.$DJANGO_SETTINGS
+    else
+        DJANGO_SETTINGS_MODULE=$DJANGO_PROJECT.settings
+    fi
 
-export PROJECT_ROOT DJANGO_PROJECT PYTHONPATH DJANGO_SETTINGS_MODULE
+    # Set the PYTHONPATH to include PROJECT_ROOT
+    [ ! -z "$_OLD_PYTHONPATH" ] && PYTHONPATH=$_OLD_PYTHONPATH
+    _OLD_PYTHONPATH=$PYTHONPATH
+    PYTHONPATH=$PROJECT_ROOT:$PYTHONPATH
 
-# Test settings import
-python -c "import $DJANGO_SETTINGS_MODULE" > /dev/null 2>&1
-if [ ! "$?" -eq 0 ]; then
-    echo "Error importing settings $DJANGO_SETTINGS_MODULE (PYTHONPATH: $PYTHONPATH)" 1>&2
-    python -c "import $DJANGO_SETTINGS_MODULE"
-    _deactivate
-    return 1
-fi
+    export PROJECT_ROOT DJANGO_PROJECT PYTHONPATH DJANGO_SETTINGS_MODULE
+
+    # Test settings import
+    python -c "import $DJANGO_SETTINGS_MODULE" > /dev/null 2>&1
+    if [ ! "$?" -eq 0 ]; then
+        echo "Error importing settings $DJANGO_SETTINGS_MODULE (PYTHONPATH: $PYTHONPATH)" 1>&2
+        python -c "import $DJANGO_SETTINGS_MODULE"
+        _deactivate
+        return 1
+    fi
+
+    # We're good - let's set the prompt
+    [ ! -z "$_OLD_DJANGO_ENV_PS1" ] && PS1=$_OLD_DJANGO_ENV_PS1
+    _OLD_DJANGO_ENV_PS1=$PS1
+    PS1=[$DJANGO_PROJECT]$PS1
+
+    # Test if the definitions in the settings match ours
+    [ "$PROJECT_ROOT" != "`get_django_setting PROJECT_ROOT`" ] && \
+        echo "\$PROJECT_ROOT in Django settings is different from shell" 1>&2 && \
+        _deactivate && return 1
+    [ "$DJANGO_PROJECT" != "`get_django_setting DJANGO_PROJECT`" ] && \
+        echo "\$DJANGO_PROJECT in Django settings is different from shell" 1>&2 && \
+        _deactivate && return 1
+    [ "$PROJECT_ROOT/$DJANGO_PROJECT" != "`get_django_setting DJANGO_PROJECT_DIR`" ] && \
+        echo "\$DJANGO_PROJECT_DIR in Django settings is different from \$PROJECT_ROOT/\$DJANGO_PROJECT" 1>&2 && 
+        _deactivate && return 1
+
+    # Show environment info
+    if [ ! -z "$SHOW_DJANGO_ENVIRONMENT" ]; then
+        if [ "$SHOW_DJANGO_ENVIRONMENT" = "yes" ]; then
+            echo Welcome to $DJANGO_PROJECT. Environment info:
+            djenv
+            echo
+            if [ "$SHOW_DJANGO_ENVIRONMENT" = "settings" ]; then
+                echo Django settings:
+                PAGER=cat python -c "import $DJANGO_SETTINGS_MODULE; help($DJANGO_SETTINGS_MODULE)" | grep -v "^$"
+                echo
+            fi
+        fi
+    fi
+
+    # Check settings
+    for dirs in TEMPLATE_DIRS FIXTURE_DIRS; do
+        eval `import_django_settings $dirs`
+        envname=DJANGO_$dirs
+        for dir in ${!envname}; do
+            [ ! -d $dir ] && echo "Warning: \"$dir\" in settings.$dirs is not a valid directory"
+        done
+    done
+}
+
+function deinitenv() {
+    [ ! -z "$VIRTUAL_ENV" ] && PS1=$_OLD_DJANGO_ENV_PS1
+
+    if [ ! -z "$_OLD_DJANGO_SETTINGS_MODULE" ]; then
+        DJANGO_SETTINGS_MODULE=$_OLD_DJANGO_SETTINGS_MODULE
+    else
+        unset DJANGO_SETTINGS_MODULE
+    fi
+
+    if [ ! -z "$_OLD_PYTHONPATH" ]; then
+        PYTHONPATH=$_OLD_PYTHONPATH
+    else
+        unset PYTHONPATH
+    fi
+
+    unset _OLD_DJANGO_SETTINGS_MODULE _OLD_PYTHONPATH PROJECT_ROOT \
+        DJANGO_PROJECT DJANGO_SETTINGS SHOW_DJANGO_ENVIRONMENT
+}
 
 # Some useful functions
 
+# Change django project
+# Example:
+# djenv # Print current environment settings
+# or
+# djenv mysite # Use default settings
+# or
+# djenv mysite settings.env.local # Use specific settins
+function djenv() {
+    # Environment info
+    if [ -z "$1" ]; then
+        echo PROJECT_ROOT: \'$PROJECT_ROOT\'
+        echo DJANGO_PROJECT: \'$DJANGO_PROJECT\'
+        echo DJANGO_SETTINGS_MODULE: \'$DJANGO_SETTINGS_MODULE\'
+        echo PYTHONPATH: \'$PYTHONPATH\'
+        return
+    fi
+
+    # Help
+    if [ "$1" = "-h" -o "$1" = "--help" ]; then
+        echo "Usage: djenv [ django-project [django-settings] ]"
+        return
+    fi
+
+    # Check $PROJECT_ROOT
+    if [ -z "$PROJECT_ROOT" ]; then
+        echo "Variable \$PROJECT_ROOT not set or empty" 1>&2
+        return 1
+    fi
+
+    initenv $*
+}
+
 # Get a django setting from the current settings module
 # Experimental.
 #
 
 # Export functions that are not solely intended for interactive use
 export -f get_django_setting import_django_settings
-
-# Test if the definitions in the settings match ours
-[ "$PROJECT_ROOT" != "`get_django_setting PROJECT_ROOT`" ] && \
-    echo "\$PROJECT_ROOT in Django settings is different from shell" 1>&2 && \
-    _deactivate && return 1
-[ "$DJANGO_PROJECT" != "`get_django_setting DJANGO_PROJECT`" ] && \
-    echo "\$DJANGO_PROJECT in Django settings is different from shell" 1>&2 && \
-    _deactivate && return 1
-[ "$PROJECT_ROOT/$DJANGO_PROJECT" != "`get_django_setting DJANGO_PROJECT_DIR`" ] && \
-    echo "\$DJANGO_PROJECT_DIR in Django settings is different from \$PROJECT_ROOT/\$DJANGO_PROJECT" 1>&2 && 
-    _deactivate && return 1
-
-# Show environment info
-if [ "$SHOW_DJANGO_ENVIRONMENT" = "yes" ]; then
-    echo Welcome to $DJANGO_PROJECT. Environment info:
-    echo PROJECT_ROOT: \'$PROJECT_ROOT\'
-    echo DJANGO_PROJECT: \'$DJANGO_PROJECT\'
-    echo DJANGO_SETTINGS_MODULE: \'$DJANGO_SETTINGS_MODULE\'
-    echo PYTHONPATH: \'$PYTHONPATH\'
-    echo
-    echo Django settings:
-    PAGER=cat python -c "import $DJANGO_SETTINGS_MODULE; help($DJANGO_SETTINGS_MODULE)" | grep -v "^$"
-    echo
-fi
-
-# Check settings
-for dirs in TEMPLATE_DIRS FIXTURE_DIRS; do
-    eval `import_django_settings $dirs`
-    envname=DJANGO_$dirs
-    for dir in ${!envname}; do
-        [ ! -d $dir ] && echo "Warning: \"$dir\" in settings.$dirs is not a valid directory"
-    done
-done
-
-# Change working directory - time to party
-cdjango

scripts/run_in_env_wrapper.sh

+#!/bin/sh
 #
 # Standard environment wrapper for other scripts
 #
 # Update PATH
 PATH=$APP_HOME:$PATH
 
+# Update PYTHONPATH
+PYTHONPATH=$PROJECT_ROOT/apps:$PYTHONPATH
+
 # Check for help request
 if [ "$1" = "-h" -o "$1" = "--help" ]; then
     echo "Usage: `basename $0` <command> [args...]"
 
 # Set environment
 [ -f $VIRTUAL_ENV/bin/activate ] && source $VIRTUAL_ENV/bin/activate
-source $PROJECT_ROOT/scripts/initenv_generic $PROJECT_ROOT/apps
-[ ! $? -eq 0 ] && exit 1
+source $PROJECT_ROOT/scripts/initenv_generic
+djenv $DJANGO_PROJECT $DJANGO_SETTINGS
 
 # Execute command
 $*