pip.plugin.zsh 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Usage:
  2. # Just add pip to your installed plugins.
  3. # If you would like to change the cheeseshops used for autocomplete set
  4. # ZSH_PIP_INDEXES in your zshrc. If one of your indexes are bogus you won't get
  5. # any kind of error message, pip will just not autocomplete from them. Double
  6. # check!
  7. #
  8. # If you would like to clear your cache, go ahead and do a
  9. # "zsh-pip-clear-cache".
  10. if [[ -d "${XDG_CACHE_HOME:-$HOME/.cache}/pip" ]]; then
  11. ZSH_PIP_CACHE_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/pip/zsh-cache"
  12. else
  13. ZSH_PIP_CACHE_FILE=~/.pip/zsh-cache
  14. fi
  15. ZSH_PIP_INDEXES=(https://pypi.org/simple/)
  16. zsh-pip-clear-cache() {
  17. rm $ZSH_PIP_CACHE_FILE
  18. unset piplist
  19. }
  20. zsh-pip-clean-packages() {
  21. sed -n '/<a href/ s/.*>\([^<]\{1,\}\).*/\1/p'
  22. }
  23. zsh-pip-cache-packages() {
  24. if [[ ! -d ${ZSH_PIP_CACHE_FILE:h} ]]; then
  25. mkdir -p ${ZSH_PIP_CACHE_FILE:h}
  26. fi
  27. if [[ ! -f $ZSH_PIP_CACHE_FILE ]]; then
  28. echo -n "(...caching package index...)"
  29. tmp_cache=/tmp/zsh_tmp_cache
  30. touch $tmp_cache
  31. for index in $ZSH_PIP_INDEXES ; do
  32. # well... I've already got two problems
  33. curl -L $index 2>/dev/null | \
  34. zsh-pip-clean-packages \
  35. >> $tmp_cache
  36. done
  37. sort $tmp_cache | uniq | tr '\n' ' ' > $ZSH_PIP_CACHE_FILE
  38. rm $tmp_cache
  39. fi
  40. }
  41. # A test function that validates the regex against known forms of the simple
  42. # index. If you modify the regex to make it work for you, you should add a test
  43. # case in here and make sure that your changes don't break things for someone
  44. # else.
  45. zsh-pip-test-clean-packages() {
  46. local expected
  47. local actual
  48. expected="0x10c-asm
  49. 1009558_nester"
  50. actual=$(echo -n "<html><head><title>Simple Index</title><meta name=\"api-version\" value=\"2\" /></head><body>
  51. <a href='0x10c-asm'>0x10c-asm</a><br/>
  52. <a href='1009558_nester'>1009558_nester</a><br/>
  53. </body></html>" | zsh-pip-clean-packages)
  54. if [[ $actual != $expected ]] ; then
  55. echo -e "python's simple index is broken:\n$actual\n !=\n$expected"
  56. else
  57. echo "python's simple index is fine"
  58. fi
  59. actual=$(echo -n '<html>
  60. <head>
  61. <title>Simple Package Index</title>
  62. </head>
  63. <body>
  64. <a href="0x10c-asm">0x10c-asm</a><br/>
  65. <a href="1009558_nester">1009558_nester</a><br/>
  66. </body></html>' | zsh-pip-clean-packages)
  67. if [[ $actual != $expected ]] ; then
  68. echo -e "the djangopypi2 index is broken:\n$actual\n !=\n$expected"
  69. else
  70. echo "the djangopypi2 index is fine"
  71. fi
  72. }
  73. alias pip="noglob pip" # allows square brackets for pip command invocation