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"
47 actions="adminindex createcachetable dbshell diffsettings \
48 dumpdata flush inspectdb loaddata reset runfcgi runserver \
49 shell sql sqlall sqlclear sqlcustom sqlflush sqlindexes \
50 sqlreset sqlsequencereset startapp startproject \
53 action_shell_opts="--plain"
54 action_runfcgi_opts="host port socket method maxspare minspare maxchildren daemonize pidfile workdir"
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 ) )
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]} ) )
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
75 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
80 COMPREPLY=( $(compgen -W "${actions}" -- ${cur}) )
87 adminindex|dumpdata|reset| \
88 sql|sqlall|sqlclear|sqlcustom|sqlindexes| \
89 sqlreset|sqlsequencereset|test)
92 # If settings.py in the PWD, use that
93 if [ -e settings.py ] ; then
94 settings="$PWD/settings.py"
96 # Use the ENV variable if it is set
97 if [ $DJANGO_SETTINGS_MODULE ] ; then
98 settings=$DJANGO_SETTINGS_MODULE
101 # Couldn't find settings so return nothing
102 if [ -z $settings ] ; then
104 # Otherwise inspect settings.py file
106 apps=`sed -n "/INSTALLED_APPS = (/,/)/p" $settings | \
107 grep -v "django.contrib" |
108 sed -n "s/^[ ]*'\(.*\.\)*\(.*\)'.*$/\2 /pg" | \
110 COMPREPLY=( $(compgen -W "${apps}" -- ${cur}) )
115 createcachetable|dbshell|diffsettings| \
116 inspectdb|runserver|startapp|startproject|syncdb| \
122 COMPREPLY=( $(compgen -W "$action_shell_opts" -- ${cur}) )
126 COMPREPLY=( $(compgen -W "$action_runfcgi_opts" -- ${cur}) )
129 host*|port*|socket*|method*|maxspare*|minspare*|maxchildren*|daemonize*|pidfile*|workdir*)
130 if [ "$action" == "runfcgi" ] ; then
131 COMPREPLY=( $(compgen -W "$action_runfcgi_opts" -- ${cur}) )
137 #COMPREPLY=( $(compgen -W "auth core" -- ${cur}) )
145 complete -F _django_completion django-admin.py manage.py
147 # Support for multiple interpreters.
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)"
154 pythons=$(echo $pythons | tr " " "\n" | sort -u | tr "\n" " ")
159 complete -F _django_completion -o default $pythons