]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/pip/pip.plugin.zsh
initial
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / pip / pip.plugin.zsh
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
12 if [[ -d "${XDG_CACHE_HOME:-$HOME/.cache}/pip" ]]; then
13   ZSH_PIP_CACHE_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/pip/zsh-cache"
14 else
15   ZSH_PIP_CACHE_FILE=~/.pip/zsh-cache
16 fi
17 ZSH_PIP_INDEXES=(https://pypi.org/simple/)
18
19 zsh-pip-clear-cache() {
20   rm $ZSH_PIP_CACHE_FILE
21   unset piplist
22 }
23
24 zsh-pip-clean-packages() {
25     sed -n '/<a href/ s/.*>\([^<]\{1,\}\).*/\1/p'
26 }
27
28 zsh-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.
52 zsh-pip-test-clean-packages() {
53     local expected
54     local actual
55     expected="0x10c-asm
56 1009558_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
85 if (( $+commands[pip3] && !$+commands[pip] )); then
86   alias pip="noglob pip3"
87 else
88   alias pip="noglob pip"
89 fi
90
91 # Create requirements file
92 alias pipreq="pip freeze > requirements.txt"
93
94 # Install packages from requirements file
95 alias pipir="pip install -r requirements.txt"
96
97 # Upgrade all installed packages
98 function pipupall {
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
103 }
104
105 # Uninstalled all installed packages
106 function pipunall {
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
111 }