pip.plugin.zsh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. alias pipi="pip install"
  79. alias pipu="pip install --upgrade"
  80. alias pipun="pip uninstall"
  81. alias pipgi="pip freeze | grep"
  82. alias piplo="pip list -o"
  83. # Create requirements file
  84. alias pipreq="pip freeze > requirements.txt"
  85. # Install packages from requirements file
  86. alias pipir="pip install -r requirements.txt"
  87. # Upgrade all installed packages
  88. function pipupall {
  89. # non-GNU xargs does not support nor need `--no-run-if-empty`
  90. local xargs="xargs --no-run-if-empty"
  91. xargs --version 2>/dev/null | grep -q GNU || xargs="xargs"
  92. pip list --outdated | awk 'NR > 2 { print $1 }' | ${=xargs} pip install --upgrade
  93. }
  94. # Uninstall all installed packages
  95. function pipunall {
  96. # non-GNU xargs does not support nor need `--no-run-if-empty`
  97. local xargs="xargs --no-run-if-empty"
  98. xargs --version 2>/dev/null | grep -q GNU || xargs="xargs"
  99. pip list --format freeze | cut -d= -f1 | ${=xargs} pip uninstall
  100. }
  101. # Install from GitHub repository
  102. function pipig {
  103. pip install "git+https://github.com/$1.git"
  104. }
  105. compdef _pip pipig
  106. # Install from GitHub branch
  107. function pipigb {
  108. pip install "git+https://github.com/$1.git@$2"
  109. }
  110. compdef _pip pipigb
  111. # Install from GitHub pull request
  112. function pipigp {
  113. pip install "git+https://github.com/$1.git@refs/pull/$2/head"
  114. }
  115. compdef _pip pipigp