django_bash_completion 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 createsuperuser 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, django-admin, ./manage, manage.py
  53. ( ${COMP_CWORD} -eq 1 &&
  54. ( ${COMP_WORDS[0]} == django-admin.py ||
  55. ${COMP_WORDS[0]} == django-admin ||
  56. ${COMP_WORDS[0]} == ./manage.py ||
  57. ${COMP_WORDS[0]} == manage.py ) )
  58. ||
  59. # python manage.py, /some/path/python manage.py (if manage.py exists)
  60. ( ${COMP_CWORD} -eq 2 &&
  61. ( $( basename -- ${COMP_WORDS[0]} ) == python?([1-9]\.[0-9]) ) &&
  62. ( $( basename -- ${COMP_WORDS[1]} ) == manage.py) &&
  63. ( -r ${COMP_WORDS[1]} ) )
  64. ||
  65. ( ${COMP_CWORD} -eq 2 &&
  66. ( $( basename -- ${COMP_WORDS[0]} ) == python?([1-9]\.[0-9]) ) &&
  67. ( $( basename -- ${COMP_WORDS[1]} ) == django-admin.py) &&
  68. ( -r ${COMP_WORDS[1]} ) )
  69. ||
  70. ( ${COMP_CWORD} -eq 2 &&
  71. ( $( basename -- ${COMP_WORDS[0]} ) == python?([1-9]\.[0-9]) ) &&
  72. ( $( basename -- ${COMP_WORDS[1]} ) == django-admin) &&
  73. ( -r ${COMP_WORDS[1]} ) ) ]] ; then
  74. case ${cur} in
  75. -*)
  76. COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  77. action=$COMPREPLY
  78. return 0
  79. ;;
  80. *)
  81. COMPREPLY=( $(compgen -W "${actions}" -- ${cur}) )
  82. action=$COMPREPLY
  83. return 0
  84. ;;
  85. esac
  86. else
  87. case ${prev} in
  88. adminindex|dumpdata|reset| \
  89. sql|sqlall|sqlclear|sqlcustom|sqlindexes| \
  90. sqlreset|sqlsequencereset|test)
  91. # App completion
  92. settings=""
  93. # If settings.py in the PWD, use that
  94. if [ -e settings.py ] ; then
  95. settings="$PWD/settings.py"
  96. else
  97. # Use the ENV variable if it is set
  98. if [ $DJANGO_SETTINGS_MODULE ] ; then
  99. settings=$DJANGO_SETTINGS_MODULE
  100. fi
  101. fi
  102. # Couldn't find settings so return nothing
  103. if [ -z $settings ] ; then
  104. COMPREPLY=()
  105. # Otherwise inspect settings.py file
  106. else
  107. apps=`sed -n "/INSTALLED_APPS = (/,/)/p" $settings | \
  108. grep -v "django.contrib" |
  109. sed -n "s/^[ ]*'\(.*\.\)*\(.*\)'.*$/\2 /pg" | \
  110. tr -d "\n"`
  111. COMPREPLY=( $(compgen -W "${apps}" -- ${cur}) )
  112. fi
  113. return 0
  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. complete -F _django_completion django-admin.py manage.py django-admin
  145. # Support for multiple interpreters.
  146. unset pythons
  147. if command -v whereis &>/dev/null; then
  148. python_interpreters=$(whereis python | cut -d " " -f 2-)
  149. for python in $python_interpreters; do
  150. pythons="${pythons} $(basename -- $python)"
  151. done
  152. pythons=$(echo $pythons | tr " " "\n" | sort -u | tr "\n" " ")
  153. else
  154. pythons=python
  155. fi
  156. complete -F _django_completion -o default $pythons