Source

django-environments / scripts / initenv_generic

Full commit
#removeorphanpycs 
# django-environments environment initialization script
#
# It's best to set the following variables in your own initenv
# script or virtualenv bin/postactivate, and then source this file.
#
# The root of the project, i.e. the directory containing your Django project
#PROJECT_ROOT=/Users/joe/projects/myproject
#
# Set this to the name of the Django project in PROJECT_ROOT
#DJANGO_PROJECT=mysite
#
# 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
    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

# 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

# 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

# Set the PYTHONPATH to include PROJECT_ROOT; assume first argument are additional directories
_OLD_PYTHONPATH=$PYTHONPATH
PYTHONPATH=$PROJECT_ROOT:$1:$PYTHONPATH

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

# Some useful functions

# Get a django setting from the current settings module
# Experimental.
#
# Example:
# LC=`get_django_setting LANGUAGE_CODE` && echo $LC
# 
# Returns nothing if setting cannot be found
function get_django_setting() {
    [ -z "$1" ] && echo "Usage: get_django_setting <setting-name>" 1>&2 && return 1

    python << EOF
import $DJANGO_SETTINGS_MODULE as settings

try:
    print settings.__dict__['$1']
except KeyError:
    pass
EOF
}

# Import django settings into the shell environment
# Experimental.
#
# When using, set $IFS to empty (this is needed because we eval
# the output of the python-generated shell script code):
# IFS=''
#
# Then use the function as follows:
# eval `import_django_settings` # all settings
# or
# eval `import_django_settings ADMIN` # all settings starting with 'ADMIN'
#
# Tuples and lists items are separated with newlines, so set $IFS
# to newline to get to those:
# IFS='
# '
# Note that all variables are prefixed with value of $prefix, 'DJANGO_'.
function import_django_settings() {
    prefix=DJANGO_
    python << EOF
import $DJANGO_SETTINGS_MODULE as settings
from types import TupleType, ListType, DictType

def escape(value):
    return str(value).replace('"', '\\\\"')

settings = settings.__dict__.items()
settings.sort()
for name, value in settings:
    if name.find('__') == -1 and name.find('$1') == 0:
        if type(value) in (TupleType, ListType):
            print '$prefix%s="' % name
            for item in value:
                print escape(item)
            print '"'
        elif type(value) == DictType:
            print '$prefix%s="' % name
            for name, value in value.items():
                print '%s:%s' % (name, escape(value))
            print '"'
        else:
            print '$prefix%s="%s"' % (name, escape(value))
EOF
}

# Change directory to project root
function cdroot() {
    cd $PROJECT_ROOT/$1
}

# Change directory to django project
function cdjango () {
    cd $PROJECT_ROOT/$DJANGO_PROJECT/$1
}

# Install dependencies for an environment (first argument); the file
# with the dependencies should be in the requirements directory and
# should have the format 'libs-<environment>.txt'
function pipup() {
    [ -z "$1" ] && echo "Usage: pipup <requirements-identifier>" 1>&2 && return 1

    pip install --requirement=$PROJECT_ROOT/requirements/libs-$1.txt
}

# Use compileall to compile all .py files - handy for web server
# environments where the server user often has no write access to the
# .pyc files / directories
function pycompile() {
    python -c "import compileall; compileall.compile_dir('$PROJECT_ROOT')"

    removeorphanpycs
}

# Remove .pyc files without a corresponding .py
function removeorphanpycs() {
    for pyc in `find . -name \*.pyc`; do
        [ -f  `echo $pyc | sed 's/c$//'` ] || rm -v $pyc
    done
}

# Forget manage.py, django-admin.py respects our settings!
function djadmin() {
    django-admin.py $*
}

# Run development server on settings.LOCAL_SERVER_PORT
function runserver() {
    django-admin.py runserver `get_django_setting LOCAL_SERVER_PORT`
}

# 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