pip.plugin.zsh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. if (( $+commands[pip3] && !$+commands[pip] )); then
  74. alias pip="noglob pip3"
  75. else
  76. alias pip="noglob pip"
  77. fi
  78. # Create requirements file
  79. alias pipreq="pip freeze > requirements.txt"
  80. # Install packages from requirements file
  81. alias pipir="pip install -r requirements.txt"
  82. # Update all installed packages
  83. function pipupall {
  84. # non-GNU xargs does not support nor need `--no-run-if-empty`
  85. local xargs="xargs --no-run-if-empty"
  86. xargs --version 2>/dev/null | grep -q GNU || xargs="xargs"
  87. pip list --outdated --format freeze | cut -d= -f1 | ${=xargs} pip install --upgrade
  88. }
  89. # Uninstalled all installed packages
  90. function pipunall {
  91. # non-GNU xargs does not support nor need `--no-run-if-empty`
  92. local xargs="xargs --no-run-if-empty"
  93. xargs --version 2>/dev/null | grep -q GNU || xargs="xargs"
  94. pip list --format freeze | cut -d= -f1 | ${=xargs} pip uninstall
  95. }