pip.plugin.zsh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. ZSH_PIP_CACHE_FILE=~/.pip/zsh-cache
  11. ZSH_PIP_INDEXES=(https://pypi.python.org/simple/)
  12. zsh-pip-clear-cache() {
  13. rm $ZSH_PIP_CACHE_FILE
  14. unset piplist
  15. }
  16. zsh-pip-clean-packages() {
  17. sed -nr '/<a href/ s/.*>([^<]+).*/\1/p'
  18. }
  19. zsh-pip-cache-packages() {
  20. if [[ ! -d ${PIP_CACHE_FILE:h} ]]; then
  21. mkdir -p ${PIP_CACHE_FILE:h}
  22. fi
  23. if [[ ! -f $ZSH_PIP_CACHE_FILE ]]; then
  24. echo -n "(...caching package index...)"
  25. tmp_cache=/tmp/zsh_tmp_cache
  26. for index in $ZSH_PIP_INDEXES ; do
  27. # well... I've already got two problems
  28. curl $index 2>/dev/null | \
  29. zsh-pip-clean-packages \
  30. >> $tmp_cache
  31. done
  32. sort $tmp_cache | uniq | tr '\n' ' ' > $ZSH_PIP_CACHE_FILE
  33. rm $tmp_cache
  34. fi
  35. }
  36. # A test function that validates the regex against known forms of the simple
  37. # index. If you modify the regex to make it work for you, you should add a test
  38. # case in here and make sure that your changes don't break things for someone
  39. # else.
  40. zsh-pip-test-clean-packages() {
  41. local expected
  42. local actual
  43. expected="0x10c-asm
  44. 1009558_nester"
  45. actual=$(echo -n "<html><head><title>Simple Index</title><meta name=\"api-version\" value=\"2\" /></head><body>
  46. <a href='0x10c-asm'>0x10c-asm</a><br/>
  47. <a href='1009558_nester'>1009558_nester</a><br/>
  48. </body></html>" | zsh-pip-clean-packages)
  49. if [[ $actual != $expected ]] ; then
  50. echo -e "python's simple index is broken:\n$actual\n !=\n$expected"
  51. else
  52. echo "python's simple index is fine"
  53. fi
  54. actual=$(echo -n '<html>
  55. <head>
  56. <title>Simple Package Index</title>
  57. </head>
  58. <body>
  59. <a href="0x10c-asm">0x10c-asm</a><br/>
  60. <a href="1009558_nester">1009558_nester</a><br/>
  61. </body></html>' | zsh-pip-clean-packages)
  62. if [[ $actual != $expected ]] ; then
  63. echo -e "the djangopypi2 index is broken:\n$actual\n !=\n$expected"
  64. else
  65. echo "the djangopypi2 index is fine"
  66. fi
  67. }