2 # Just add pip to your installed plugins.
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
9 # If you would like to clear your cache, go ahead and do a
10 # "zsh-pip-clear-cache".
12 if [[ -d "${XDG_CACHE_HOME:-$HOME/.cache}/pip" ]]; then
13 ZSH_PIP_CACHE_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/pip/zsh-cache"
15 ZSH_PIP_CACHE_FILE=~/.pip/zsh-cache
17 ZSH_PIP_INDEXES=(https://pypi.org/simple/)
19 zsh-pip-clear-cache() {
20 rm $ZSH_PIP_CACHE_FILE
24 zsh-pip-clean-packages() {
25 sed -n '/<a href/ s/.*>\([^<]\{1,\}\).*/\1/p'
28 zsh-pip-cache-packages() {
29 if [[ ! -d ${ZSH_PIP_CACHE_FILE:h} ]]; then
30 mkdir -p ${ZSH_PIP_CACHE_FILE:h}
33 if [[ ! -f $ZSH_PIP_CACHE_FILE ]]; then
34 echo -n "(...caching package index...)"
35 tmp_cache=/tmp/zsh_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 \
43 sort $tmp_cache | uniq | tr '\n' ' ' > $ZSH_PIP_CACHE_FILE
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
52 zsh-pip-test-clean-packages() {
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)
63 if [[ $actual != $expected ]] ; then
64 echo -e "python's simple index is broken:\n$actual\n !=\n$expected"
66 echo "python's simple index is fine"
69 actual=$(echo -n '<html>
71 <title>Simple Package Index</title>
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)
78 if [[ $actual != $expected ]] ; then
79 echo -e "the djangopypi2 index is broken:\n$actual\n !=\n$expected"
81 echo "the djangopypi2 index is fine"
85 if (( $+commands[pip3] && !$+commands[pip] )); then
86 alias pip="noglob pip3"
88 alias pip="noglob pip"
91 # Create requirements file
92 alias pipreq="pip freeze > requirements.txt"
94 # Install packages from requirements file
95 alias pipir="pip install -r requirements.txt"
97 # Upgrade all installed packages
99 # non-GNU xargs does not support nor need `--no-run-if-empty`
100 local xargs="xargs --no-run-if-empty"
101 xargs --version 2>/dev/null | grep -q GNU || xargs="xargs"
102 pip list --outdated | awk 'NR > 2 { print $1 }' | ${=xargs} pip install --upgrade
105 # Uninstalled all installed packages
107 # non-GNU xargs does not support nor need `--no-run-if-empty`
108 local xargs="xargs --no-run-if-empty"
109 xargs --version 2>/dev/null | grep -q GNU || xargs="xargs"
110 pip list --format freeze | cut -d= -f1 | ${=xargs} pip uninstall