pip.plugin.zsh 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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
  74. # Create requirements file
  75. alias pipreq="pip freeze > requirements.txt"
  76. # Update all installed packages
  77. alias pipupall="pipreq && sed -i 's/==/>=/g' requirements.txt && pip install -r requirements.txt --upgrade && rm -rf requirements.txt"
  78. # Install packages from requirements file
  79. alias pipir="pip install -r requirements.txt"
  80. # Uninstalled all installed packages
  81. alias pipunall="pipreq && pip uninstall -r requirements.txt -y && rm -rf requirements.txt"