Parent directory

pip.plugin.zsh

3898 bytes
  1# Usage:
  2# Just add pip to your installed plugins.
  3
  4# If you would like to change the cheeseshops used for autocomplete set
  5# ZSH_PIP_INDEXES in your zshrc. If one of your indexes are bogus you won't get
  6# any kind of error message, pip will just not autocomplete from them. Double
  7# check!
  8#
  9# If you would like to clear your cache, go ahead and do a
 10# "zsh-pip-clear-cache".
 11
 12if [[ -d "${XDG_CACHE_HOME:-$HOME/.cache}/pip" ]]; then
 13  ZSH_PIP_CACHE_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/pip/zsh-cache"
 14else
 15  ZSH_PIP_CACHE_FILE=~/.pip/zsh-cache
 16fi
 17ZSH_PIP_INDEXES=(https://pypi.org/simple/)
 18
 19zsh-pip-clear-cache() {
 20  rm $ZSH_PIP_CACHE_FILE
 21  unset piplist
 22}
 23
 24zsh-pip-clean-packages() {
 25    sed -n '/<a href/ s/.*>\([^<]\{1,\}\).*/\1/p'
 26}
 27
 28zsh-pip-cache-packages() {
 29  if [[ ! -d ${ZSH_PIP_CACHE_FILE:h} ]]; then
 30      mkdir -p ${ZSH_PIP_CACHE_FILE:h}
 31  fi
 32
 33  if [[ ! -f $ZSH_PIP_CACHE_FILE ]]; then
 34      echo -n "(...caching package index...)"
 35      tmp_cache=/tmp/zsh_tmp_cache
 36      touch $tmp_cache
 37      for index in $ZSH_PIP_INDEXES ; do
 38          # well... I've already got two problems
 39          curl -L $index 2>/dev/null | \
 40              zsh-pip-clean-packages \
 41               >> $tmp_cache
 42      done
 43      sort $tmp_cache | uniq | tr '\n' ' ' > $ZSH_PIP_CACHE_FILE
 44      rm $tmp_cache
 45  fi
 46}
 47
 48# A test function that validates the regex against known forms of the simple
 49# index. If you modify the regex to make it work for you, you should add a test
 50# case in here and make sure that your changes don't break things for someone
 51# else.
 52zsh-pip-test-clean-packages() {
 53    local expected
 54    local actual
 55    expected="0x10c-asm
 561009558_nester"
 57
 58    actual=$(echo -n "<html><head><title>Simple Index</title><meta name=\"api-version\" value=\"2\" /></head><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
 63    if [[ $actual != $expected ]] ; then
 64        echo -e "python's simple index is broken:\n$actual\n  !=\n$expected"
 65    else
 66        echo "python's simple index is fine"
 67    fi
 68
 69    actual=$(echo -n '<html>
 70  <head>
 71    <title>Simple Package Index</title>
 72  </head>
 73  <body>
 74    <a href="0x10c-asm">0x10c-asm</a><br/>
 75    <a href="1009558_nester">1009558_nester</a><br/>
 76</body></html>' | zsh-pip-clean-packages)
 77
 78    if [[ $actual != $expected ]] ; then
 79        echo -e "the djangopypi2 index is broken:\n$actual\n  !=\n$expected"
 80    else
 81        echo "the djangopypi2 index is fine"
 82    fi
 83}
 84
 85if (( $+commands[pip3] && !$+commands[pip] )); then
 86  alias pip="noglob pip3"
 87else
 88  alias pip="noglob pip"
 89fi
 90
 91alias pipi="pip install"
 92alias pipu="pip install --upgrade"
 93alias pipun="pip uninstall"
 94alias pipgi="pip freeze | grep"
 95alias piplo="pip list -o"
 96
 97# Create requirements file
 98alias pipreq="pip freeze > requirements.txt"
 99
100# Install packages from requirements file
101alias pipir="pip install -r requirements.txt"
102
103# Upgrade all installed packages
104function pipupall {
105  # non-GNU xargs does not support nor need `--no-run-if-empty`
106  local xargs="xargs --no-run-if-empty"
107  xargs --version 2>/dev/null | grep -q GNU || xargs="xargs"
108  pip list --outdated | awk 'NR > 2 { print $1 }' | ${=xargs} pip install --upgrade
109}
110
111# Uninstall all installed packages
112function pipunall {
113  # non-GNU xargs does not support nor need `--no-run-if-empty`
114  local xargs="xargs --no-run-if-empty"
115  xargs --version 2>/dev/null | grep -q GNU || xargs="xargs"
116  pip list --format freeze | cut -d= -f1 | ${=xargs} pip uninstall
117}
118
119# Install from GitHub repository
120function pipig {
121  pip install "git+https://github.com/$1.git"
122}
123compdef _pip pipig
124
125# Install from GitHub branch
126function pipigb {
127  pip install "git+https://github.com/$1.git@$2"
128}
129compdef _pip pipigb
130
131# Install from GitHub pull request
132function pipigp {
133  pip install "git+https://github.com/$1.git@refs/pull/$2/head"
134}
135compdef _pip pipigp