1 # #########################################################################
2 # This bash script adds tab-completion feature to django-admin.py and
5 # Testing it out without installing
6 # =================================
8 # To test out the completion without "installing" this, just run this file
11 # . ~/path/to/django_bash_completion
13 # Note: There's a dot ('.') at the beginning of that command.
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.
21 # To install this, point to this file from your .bash_profile, like so:
23 # . ~/path/to/django_bash_completion
25 # Do the same in your .bashrc if .bashrc doesn't invoke .bash_profile.
27 # Settings will take effect the next time you log in.
32 # To uninstall, just remove the line from your .bash_profile and .bashrc.
34 # Enable extended pattern matching operators.
39 local cur prev opts actions action_shell_opts action_runfcgi_opts
41 cur="${COMP_WORDS[COMP_CWORD]}"
42 prev="${COMP_WORDS[COMP_CWORD-1]}"
45 opts="--help --settings --pythonpath --noinput --noreload --format --indent --verbosity --adminmedia --version --locale --domain"
47 actions="createcachetable createsuperuser compilemessages \
48 dbshell diffsettings dumpdata flush inspectdb loaddata \
49 makemessages reset runfcgi runserver shell sql sqlall sqlclear \
50 sqlcustom sqlflush sqlindexes sqlreset sqlsequencereset startapp \
51 startproject syncdb test validate"
53 action_shell_opts="--plain"
54 action_runfcgi_opts="host port socket method maxspare minspare maxchildren daemonize pidfile workdir"
56 if [[ # django-admin.py, django-admin, ./manage, manage.py
57 ( ${COMP_CWORD} -eq 1 &&
58 ( ${COMP_WORDS[0]} == django-admin.py ||
59 ${COMP_WORDS[0]} == django-admin ||
60 ${COMP_WORDS[0]} == ./manage.py ||
61 ${COMP_WORDS[0]} == manage.py ) )
63 # python manage.py, /some/path/python manage.py (if manage.py exists)
64 ( ${COMP_CWORD} -eq 2 &&
65 ( $( basename -- ${COMP_WORDS[0]} ) == python?([1-9]\.[0-9]) ) &&
66 ( $( basename -- ${COMP_WORDS[1]} ) == manage.py) &&
67 ( -r ${COMP_WORDS[1]} ) )
69 ( ${COMP_CWORD} -eq 2 &&
70 ( $( basename -- ${COMP_WORDS[0]} ) == python?([1-9]\.[0-9]) ) &&
71 ( $( basename -- ${COMP_WORDS[1]} ) == django-admin.py) &&
72 ( -r ${COMP_WORDS[1]} ) )
74 ( ${COMP_CWORD} -eq 2 &&
75 ( $( basename -- ${COMP_WORDS[0]} ) == python?([1-9]\.[0-9]) ) &&
76 ( $( basename -- ${COMP_WORDS[1]} ) == django-admin) &&
77 ( -r ${COMP_WORDS[1]} ) ) ]] ; then
81 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
86 COMPREPLY=( $(compgen -W "${actions}" -- ${cur}) )
94 sql|sqlall|sqlclear|sqlcustom|sqlindexes| \
95 sqlreset|sqlsequencereset|test)
98 # If settings.py in the PWD, use that
99 if [ -e settings.py ] ; then
100 settings="$PWD/settings.py"
102 # Use the ENV variable if it is set
103 if [ $DJANGO_SETTINGS_MODULE ] ; then
104 settings=$DJANGO_SETTINGS_MODULE
107 # Couldn't find settings so return nothing
108 if [ -z $settings ] ; then
110 # Otherwise inspect settings.py file
112 apps=`sed -n "/INSTALLED_APPS = (/,/)/p" $settings | \
113 grep -v "django.contrib" |
114 sed -n "s/^[ ]*'\(.*\.\)*\(.*\)'.*$/\2 /pg" | \
116 COMPREPLY=( $(compgen -W "${apps}" -- ${cur}) )
121 createcachetable|cleanup|compilemessages|dbshell| \
122 diffsettings|inspectdb|makemessages| \
123 runserver|startapp|startproject|syncdb| \
129 COMPREPLY=( $(compgen -W "$action_shell_opts" -- ${cur}) )
133 COMPREPLY=( $(compgen -W "$action_runfcgi_opts" -- ${cur}) )
136 host*|port*|socket*|method*|maxspare*|minspare*|maxchildren*|daemonize*|pidfile*|workdir*)
137 if [ "$action" == "runfcgi" ] ; then
138 COMPREPLY=( $(compgen -W "$action_runfcgi_opts" -- ${cur}) )
144 #COMPREPLY=( $(compgen -W "auth core" -- ${cur}) )
152 complete -F _django_completion django-admin.py manage.py django-admin
154 # Support for multiple interpreters.
156 if command -v whereis &>/dev/null; then
157 python_interpreters=$(whereis python | cut -d " " -f 2-)
158 for python in $python_interpreters; do
159 pythons="${pythons} $(basename -- $python)"
161 pythons=$(echo $pythons | tr " " "\n" | sort -u | tr "\n" " ")
166 complete -F _django_completion -o default $pythons