pip.plugin.zsh 2.4 KB

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