Commits

Anonymous committed fadef2e

Added cleanup hooks

  • Participants
  • Parent commits f9e97f9

Comments (0)

Files changed (2)

File scripts/djenv.mercurial

 # Mercurial utilities for django-environments
 # 
 
-export hgpidfile=$PROJECT_ROOT/tmp/hgserve.pid
-export hgport=7100
+hgbaseport=7100
+hgpidfile=$PROJECT_ROOT/tmp/hgserve.pid
 
 # Start hg server in daemon mode and open first argument in browser
 function hgserve() {
+    [ -z "$PROJECT_ROOT" ] && echo "No \$PROJECT_ROOT" 1>&2 && return 1
+
+    # Determine port number
+    local numhash=`echo $PROJECT_ROOT | md5 | sed 's/[^0-9]//g'`
+    hgport=`expr $hgbaseport + ${numhash:0:2}`
+
+    # See if the server is alive - start it if not
     kill -0 `cat $hgpidfile 2>&1` > /dev/null 2>&1
     if [ ! $? -eq 0 ]; then
         cwd=`pwd`
-        cdjango
+        cdroot
         hg serve --port $hgport --daemon \
             --pid-file $PROJECT_ROOT/tmp/hgserve.pid
         sleep 0.5
     [ ! "$1" = "--no-open" ] && open http://localhost:$hgport/$1
 }
 
+# Browse files in tip
 function hgbrowse() {
+    [ -z "$PROJECT_ROOT" ] && echo "No \$PROJECT_ROOT" 1>&2 && return 1
     hgserve --no-open
 
     path=file/tip`echo \`pwd\` | sed "s#$PROJECT_ROOT##"`
     open http://localhost:$hgport/$path
 }
 
-# Shutdown hg server
+# Shut down hg server
 function hgkill() {
     kill -9 `cat $hgpidfile 2>&1` > /dev/null 2>&1
     if [ ! $? -eq 0 ]; then
 
 # Runs an hg command on all hg repositories in the externals directory,
 # e.g. 'hgexternals pull -u'
-function hgexternals {
+function hgexternals() {
+    [ -z "$PROJECT_ROOT" ] && echo "No \$PROJECT_ROOT" 1>&2 && return 1
     for external in $PROJECT_ROOT/externals/*; do
         if [ -d $external/.hg ]; then
             echo `basename $external`:
+            _IFS=$IFS
             IFS='' # magic
             hg -R $external $*
+            IFS=$_IFS
             echo
         fi
     done
 
 # Pull and update the project and all Mercurial externals
 function hgfetchall() {
+    [ -z "$PROJECT_ROOT" ] && echo "No \$PROJECT_ROOT" 1>&2 && return 1
     hg -R $PROJECT_ROOT pull -u
     echo
     hgexternals pull -u
 
 # List all .orig files
 function hgfindorig() {
+    [ -z "$PROJECT_ROOT" ] && echo "No \$PROJECT_ROOT" 1>&2 && return 1
     find $PROJECT_ROOT -name \*.orig -l
 }
 
 # List all .orig files
-function hgremoveorig {
+function hgremoveorig() {
+    [ -z "$PROJECT_ROOT" ] && echo "No \$PROJECT_ROOT" 1>&2 && return 1
     find $PROJECT_ROOT -name \*.orig -print -delete
 }
+
+# Clean up
+function _djenv_mercurial_cleanup () {
+    # Stop server
+    hgkill 2>&1 > /dev/null
+    # Clean environment
+    unset hgport
+}
+
+_djenv_register_cleanup _djenv_mercurial_cleanup 

File scripts/djenvlib

 # PROJECT_ROOT=/Users/joe/projects/myproject
 
 # Not to be called directly, see djenv below
-function initenv() {
+function _djenv_init() {
     DJANGO_PROJECT=$1
     DJANGO_SETTINGS=$2
 
         return 1
 
     # We're good - let's set the prompt
-    _OLD_DJANGO_ENV_PS1=$PS1
+    _OLD_DJENV_PS1=$PS1
     PS1=[$DJANGO_PROJECT]$PS1
 
     # Show environment info
     fi
 
     # Check settings
+    local dirs dir
     for dirs in TEMPLATE_DIRS FIXTURE_DIRS; do
         eval `import_django_settings $dirs`
         envname=DJANGO_$dirs
     return 0
 }
 
+# Register cleanup hook
+function _djenv_register_cleanup() {
+    local func
+    for func in $*; do
+        if [ -z "$DJENV_CLEANUP_FUNCTIONS" ]; then
+            DJENV_CLEANUP_FUNCTIONS=$func
+        else
+            DJENV_CLEANUP_FUNCTIONS=$DJENV_CLEANUP_FUNCTIONS:$func
+        fi
+    done
+}
+
 # Some useful functions
 
 # Exit current project
 function djexit() {
     [ -z "$DJANGO_PROJECT" ] && return 1
 
-    PS1=$_OLD_DJANGO_ENV_PS1
+    # Restore prompt, django settings and python path
+    PS1=$_OLD_DJENV_PS1
 
     if [ ! -z "$_OLD_DJANGO_SETTINGS_MODULE" ]; then
         DJANGO_SETTINGS_MODULE=$_OLD_DJANGO_SETTINGS_MODULE
         unset PYTHONPATH
     fi
 
-    unset _OLD_DJANGO_SETTINGS_MODULE _OLD_PYTHONPATH \
-        DJANGO_PROJECT DJANGO_SETTINGS
+    # Call the registerd cleanup functions
+    _IFS=$IFS
+    IFS=:
+    local func
+    for func in $DJENV_CLEANUP_FUNCTIONS; do
+        $func
+    done
+    IFS=$_IFS
 }
 
 # Change django project
 
     djexit
 
-    initenv $*
+    _djenv_init $*
     [ $? -eq 1 ] && djexit && return 1
 
     cdroot
 # Remove .pyc files without a corresponding .py
 function removeorphanpycs() {
     [ -z "$PROJECT_ROOT" ] && echo "No \$PROJECT_ROOT" 1>&2 && return 1
+    local pyc
     for pyc in `find $PROJECT_ROOT -name \*.pyc`; do
         [ -f  `echo $pyc | sed 's/c$//'` ] || rm -v $pyc
     done
 
 # Export functions that are not solely intended for interactive use
 export -f get_django_setting import_django_settings
+
+# Clean up the environment
+function _djenv_cleanup () {
+    unset DJANGO_PROJECT DJANGO_SETTINGS \
+        _OLD_DJENV_PS1 _OLD_DJANGO_SETTINGS_MODULE _OLD_PYTHONPATH \
+        DJANGO_TEMPLATE_DIRS DJANGO_FIXTURE_DIRS
+}
+
+# Use _djenv_register_cleanup to register your own cleanup functions
+_djenv_register_cleanup _djenv_cleanup