django_bash_completion 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. _django_completion()
  34. {
  35. COMPREPLY=( $( COMP_WORDS="${COMP_WORDS[*]}" \
  36. COMP_CWORD=$COMP_CWORD \
  37. DJANGO_AUTO_COMPLETE=1 $1 ) )
  38. }
  39. complete -F _django_completion -o default django-admin.py manage.py django-admin
  40. _python_django_completion()
  41. {
  42. if [[ ${COMP_CWORD} -ge 2 ]]; then
  43. PYTHON_EXE=$( basename -- ${COMP_WORDS[0]} )
  44. echo $PYTHON_EXE | egrep "python([2-9]\.[0-9])?" >/dev/null 2>&1
  45. if [[ $? == 0 ]]; then
  46. PYTHON_SCRIPT=$( basename -- ${COMP_WORDS[1]} )
  47. echo $PYTHON_SCRIPT | egrep "manage\.py|django-admin(\.py)?" >/dev/null 2>&1
  48. if [[ $? == 0 ]]; then
  49. COMPREPLY=( $( COMP_WORDS="${COMP_WORDS[*]:1}" \
  50. COMP_CWORD=$(( COMP_CWORD-1 )) \
  51. DJANGO_AUTO_COMPLETE=1 ${COMP_WORDS[*]} ) )
  52. fi
  53. fi
  54. fi
  55. }
  56. # Support for multiple interpreters.
  57. unset pythons
  58. if command -v whereis &>/dev/null; then
  59. python_interpreters=$(whereis python | cut -d " " -f 2-)
  60. for python in $python_interpreters; do
  61. pythons="${pythons} $(basename -- $python)"
  62. done
  63. pythons=$(echo $pythons | tr " " "\n" | sort -u | tr "\n" " ")
  64. else
  65. pythons=python
  66. fi
  67. complete -F _python_django_completion -o default $pythons