django_bash_completion 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. # Enable extended pattern matching operators.
  34. shopt -s extglob
  35. _django_completion()
  36. {
  37. local cur prev opts actions action_shell_opts action_runfcgi_opts
  38. COMPREPLY=()
  39. cur="${COMP_WORDS[COMP_CWORD]}"
  40. prev="${COMP_WORDS[COMP_CWORD-1]}"
  41. # Standalone options
  42. opts="--help --settings --pythonpath --noinput --noreload --format --indent --verbosity --adminmedia --version"
  43. # Actions
  44. actions="adminindex createcachetable dbshell diffsettings \
  45. dumpdata flush inspectdb loaddata reset runfcgi runserver \
  46. shell sql sqlall sqlclear sqlcustom sqlflush sqlindexes \
  47. sqlreset sqlsequencereset startapp startproject \
  48. syncdb test validate"
  49. # Action's options
  50. action_shell_opts="--plain"
  51. action_runfcgi_opts="host port socket method maxspare minspare maxchildren daemonize pidfile workdir"
  52. if [[ # django-admin.py, ./manage, manage.py
  53. ( ${COMP_CWORD} -eq 1 &&
  54. ( ${COMP_WORDS[0]} == django-admin.py ||
  55. ${COMP_WORDS[0]} == ./manage.py ||
  56. ${COMP_WORDS[0]} == manage.py ) )
  57. ||
  58. # python manage.py, /some/path/python manage.py (if manage.py exists)
  59. ( ${COMP_CWORD} -eq 2 &&
  60. ( $( basename -- ${COMP_WORDS[0]} ) == python?([1-9]\.[0-9]) ) &&
  61. ( $( basename -- ${COMP_WORDS[1]} ) == manage.py) &&
  62. ( -r ${COMP_WORDS[1]} ) )
  63. ||
  64. ( ${COMP_CWORD} -eq 2 &&
  65. ( $( basename -- ${COMP_WORDS[0]} ) == python?([1-9]\.[0-9]) ) &&
  66. ( $( basename -- ${COMP_WORDS[1]} ) == django-admin.py) &&
  67. ( -r ${COMP_WORDS[1]} ) ) ]] ; then
  68. case ${cur} in
  69. -*)
  70. COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  71. action=$COMPREPLY
  72. return 0
  73. ;;
  74. *)
  75. COMPREPLY=( $(compgen -W "${actions}" -- ${cur}) )
  76. action=$COMPREPLY
  77. return 0
  78. ;;
  79. esac
  80. else
  81. case ${prev} in
  82. adminindex|dumpdata|reset| \
  83. sql|sqlall|sqlclear|sqlcustom|sqlindexes| \
  84. sqlreset|sqlsequencereset|test)
  85. # App completion
  86. settings=""
  87. # If settings.py in the PWD, use that
  88. if [ -e settings.py ] ; then
  89. settings="$PWD/settings.py"
  90. else
  91. # Use the ENV variable if it is set
  92. if [ $DJANGO_SETTINGS_MODULE ] ; then
  93. settings=$DJANGO_SETTINGS_MODULE
  94. fi
  95. fi
  96. # Couldn't find settings so return nothing
  97. if [ -z $settings ] ; then
  98. COMPREPLY=()
  99. # Otherwise inspect settings.py file
  100. else
  101. apps=`sed -n "/INSTALLED_APPS = (/,/)/p" $settings | \
  102. grep -v "django.contrib" |
  103. sed -n "s/^[ ]*'\(.*\.\)*\(.*\)'.*$/\2 /pg" | \
  104. tr -d "\n"`
  105. COMPREPLY=( $(compgen -W "${apps}" -- ${cur}) )
  106. fi
  107. return 0
  108. ;;
  109. createcachetable|dbshell|diffsettings| \
  110. inspectdb|runserver|startapp|startproject|syncdb| \
  111. validate)
  112. COMPREPLY=()
  113. return 0
  114. ;;
  115. shell)
  116. COMPREPLY=( $(compgen -W "$action_shell_opts" -- ${cur}) )
  117. return 0
  118. ;;
  119. runfcgi)
  120. COMPREPLY=( $(compgen -W "$action_runfcgi_opts" -- ${cur}) )
  121. return 0
  122. ;;
  123. host*|port*|socket*|method*|maxspare*|minspare*|maxchildren*|daemonize*|pidfile*|workdir*)
  124. if [ "$action" == "runfcgi" ] ; then
  125. COMPREPLY=( $(compgen -W "$action_runfcgi_opts" -- ${cur}) )
  126. return 0
  127. fi
  128. return 0
  129. ;;
  130. *)
  131. #COMPREPLY=( $(compgen -W "auth core" -- ${cur}) )
  132. COMPREPLY=()
  133. return 0
  134. ;;
  135. esac
  136. fi
  137. }
  138. complete -F _django_completion django-admin.py manage.py
  139. # Support for multiple interpreters.
  140. unset pythons
  141. if command -v whereis &>/dev/null; then
  142. python_interpreters=$(whereis python | cut -d " " -f 2-)
  143. for python in $python_interpreters; do
  144. pythons="${pythons} $(basename -- $python)"
  145. done
  146. pythons=$(echo $pythons | tr " " "\n" | sort -u | tr "\n" " ")
  147. else
  148. pythons=python
  149. fi
  150. complete -F _django_completion -o default $pythons