thirdparty/google_appengine/lib/django/extras/django_bash_completion
changeset 109 620f9b141567
equal deleted inserted replaced
108:261778de26ff 109:620f9b141567
       
     1 # #########################################################################
       
     2 # This bash script adds tab-completion feature to django-admin.py and
       
     3 # manage.py.
       
     4 #
       
     5 # Testing it out without installing
       
     6 # =================================
       
     7 #
       
     8 # To test out the completion without "installing" this, just run this file
       
     9 # directly, like so:
       
    10 #
       
    11 #     . ~/path/to/django_bash_completion
       
    12 #
       
    13 # Note: There's a dot ('.') at the beginning of that command.
       
    14 #
       
    15 # After you do that, tab completion will immediately be made available in your
       
    16 # current Bash shell. But it won't be available next time you log in.
       
    17 #
       
    18 # Installing
       
    19 # ==========
       
    20 #
       
    21 # To install this, point to this file from your .bash_profile, like so:
       
    22 #
       
    23 #     . ~/path/to/django_bash_completion
       
    24 #
       
    25 # Do the same in your .bashrc if .bashrc doesn't invoke .bash_profile.
       
    26 #
       
    27 # Settings will take effect the next time you log in.
       
    28 #
       
    29 # Uninstalling
       
    30 # ============
       
    31 #
       
    32 # To uninstall, just remove the line from your .bash_profile and .bashrc.
       
    33 
       
    34 # Enable extended pattern matching operators.
       
    35 shopt -s extglob
       
    36 
       
    37 _django_completion()
       
    38 {
       
    39     local cur prev opts actions action_shell_opts action_runfcgi_opts
       
    40     COMPREPLY=()
       
    41     cur="${COMP_WORDS[COMP_CWORD]}"
       
    42     prev="${COMP_WORDS[COMP_CWORD-1]}"
       
    43 
       
    44     # Standalone options
       
    45     opts="--help --settings --pythonpath --version"
       
    46     # Actions
       
    47     actions="adminindex createcachetable dbshell diffsettings \
       
    48              inspectdb install reset runfcgi runserver \
       
    49              shell sql sqlall sqlclear sqlindexes sqlinitialdata \
       
    50              sqlreset sqlsequencereset startapp startproject \
       
    51              syncdb validate"
       
    52     # Action's options
       
    53     action_shell_opts="--plain"
       
    54     action_runfcgi_opts="host port socket method maxspare minspare maxchildren daemonize pidfile workdir"
       
    55 
       
    56     if [[ # django-admin.py, ./manage, manage.py
       
    57           ( ${COMP_CWORD} -eq 1 &&
       
    58             ( ${COMP_WORDS[0]} == django-admin.py ||
       
    59               ${COMP_WORDS[0]} == ./manage.py ||
       
    60               ${COMP_WORDS[0]} == manage.py ) )
       
    61           ||
       
    62           # python manage.py, /some/path/python manage.py (if manage.py exists)
       
    63           ( ${COMP_CWORD} -eq 2 &&
       
    64             ( $( basename ${COMP_WORDS[0]} ) == python?([1-9]\.[0-9]) ) &&
       
    65             ( $( basename ${COMP_WORDS[1]} ) == manage.py) &&
       
    66             ( -r ${COMP_WORDS[1]} ) ) 
       
    67           ||
       
    68           ( ${COMP_CWORD} -eq 2 &&
       
    69             ( $( basename ${COMP_WORDS[0]} ) == python?([1-9]\.[0-9]) ) &&
       
    70             ( $( basename ${COMP_WORDS[1]} ) == django-admin.py) &&
       
    71             ( -r ${COMP_WORDS[1]} ) ) ]] ; then
       
    72 
       
    73         case ${cur} in
       
    74             -*)
       
    75                 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
       
    76                 action=$COMPREPLY
       
    77                 return 0
       
    78                 ;;
       
    79             *)
       
    80                 COMPREPLY=( $(compgen -W "${actions}" -- ${cur}) )
       
    81                 action=$COMPREPLY
       
    82                 return 0
       
    83                 ;;
       
    84         esac
       
    85     else
       
    86         case ${prev} in
       
    87             adminindex|install|reset| \
       
    88             sql|sqlall|sqlclear|sqlindexes| \
       
    89             sqlinitialdata|sqlreset|sqlsequencereset)
       
    90             # App completion
       
    91             settings=""
       
    92             # If settings.py in the PWD, use that
       
    93             if [ -e settings.py ] ; then
       
    94                 settings="$PWD/settings.py"
       
    95             else
       
    96                 # Use the ENV variable if it is set
       
    97                 if [ $DJANGO_SETTINGS_MODULE ] ; then
       
    98                     settings=$DJANGO_SETTINGS_MODULE
       
    99                 fi
       
   100             fi
       
   101             # Couldn't find settings so return nothing
       
   102             if [ -z $settings ] ; then
       
   103                 COMPREPLY=()
       
   104             # Otherwise inspect settings.py file
       
   105             else
       
   106                 apps=`sed -n "/INSTALLED_APPS = (/,/)/p" $settings | \
       
   107                       grep -v "django.contrib" | 
       
   108                       sed -n "s/^[ ]*'\(.*\.\)*\(.*\)'.*$/\2 /pg" | \
       
   109                       tr -d "\n"`
       
   110                 COMPREPLY=( $(compgen -W "${apps}" -- ${cur}) )
       
   111             fi
       
   112             return 0
       
   113             ;;
       
   114 
       
   115             createcachetable|dbshell|diffsettings| \
       
   116             inspectdb|runserver|startapp|startproject|syncdb| \
       
   117             validate)
       
   118                 COMPREPLY=()
       
   119                 return 0
       
   120                 ;;
       
   121             shell)
       
   122                 COMPREPLY=( $(compgen -W "$action_shell_opts" -- ${cur}) )
       
   123                 return 0
       
   124                 ;;
       
   125             runfcgi)
       
   126                 COMPREPLY=( $(compgen -W "$action_runfcgi_opts" -- ${cur}) )
       
   127                 return 0
       
   128                 ;;
       
   129             host*|port*|socket*|method*|maxspare*|minspare*|maxchildren*|daemonize*|pidfile*|workdir*)
       
   130                 if [ "$action"  == "runfcgi" ] ; then
       
   131                     COMPREPLY=( $(compgen -W "$action_runfcgi_opts" -- ${cur}) )
       
   132                     return 0
       
   133                 fi
       
   134                 return 0
       
   135                 ;;
       
   136             *)
       
   137                 #COMPREPLY=( $(compgen -W "auth core" -- ${cur}) )
       
   138                 COMPREPLY=()
       
   139                 return 0
       
   140                 ;;
       
   141         esac
       
   142     fi
       
   143 }
       
   144 
       
   145 complete -F _django_completion django-admin.py manage.py
       
   146 
       
   147 # Support for multiple interpreters.
       
   148 unset pythons
       
   149 if command -v whereis &>/dev/null; then
       
   150     python_interpreters=$(whereis python | cut -d " " -f 2-) 
       
   151     for python in $python_interpreters; do
       
   152         pythons="${pythons} $(basename $python)"
       
   153     done
       
   154     pythons=$(echo $pythons | tr " " "\n" | sort -u | tr "\n" " ")
       
   155 else
       
   156     pythons=python    
       
   157 fi
       
   158 
       
   159 complete -F _django_completion -o default $pythons