06021ccad72d626bae4f5883f0cbb5d8278cc007

Author
Robby Russell <robby@planetargon.com>
Committer
Robby Russell <robby@planetargon.com>
Date

Message

Merge pull request #2209 from quodlibetor/pip-caching-fixes

pip: successfully cache all the packages

Diff

 1diff --git a/plugins/pip/_pip b/plugins/pip/_pip
 2index fb8765c7e5d488adde84481d260e06b18279f7be..967da48cad713ffd9bb15bb34968626dd0c072bd 100644
 3--- a/plugins/pip/_pip
 4+++ b/plugins/pip/_pip
 5@@ -6,8 +6,8 @@
 6 _pip_all() {
 7   # we cache the list of packages (originally from the macports plugin)
 8   if (( ! $+piplist )); then
 9-      echo -n " (caching package index...)"
10-      piplist=($(pip search * | cut -d ' ' -f 1 | tr '[A-Z]' '[a-z]'))
11+      zsh-pip-cache-packages
12+      piplist=($(cat $ZSH_PIP_CACHE_FILE))
13   fi
14 }
15 
16diff --git a/plugins/pip/pip.plugin.zsh b/plugins/pip/pip.plugin.zsh
17new file mode 100644
18index 0000000000000000000000000000000000000000..71f977bbfcc810306adf7e04438424e191b3c9e1
19--- /dev/null
20+++ b/plugins/pip/pip.plugin.zsh
21@@ -0,0 +1,78 @@
22+# Usage:
23+# Just add pip to your installed plugins.
24+
25+# If you would like to change the cheeseshops used for autocomplete set
26+# ZSH_PIP_INDEXES in your zshrc. If one of your indexes are bogus you won't get
27+# any kind of error message, pip will just not autocomplete from them. Double
28+# check!
29+#
30+# If you would like to clear your cache, go ahead and do a
31+# "zsh-pip-clear-cache".
32+
33+ZSH_PIP_CACHE_FILE=~/.pip/zsh-cache
34+ZSH_PIP_INDEXES=(https://pypi.python.org/simple/)
35+
36+zsh-pip-clear-cache() {
37+  rm $ZSH_PIP_CACHE_FILE
38+  unset piplist
39+}
40+
41+zsh-pip-clean-packages() {
42+    sed -nr '/<a href/ s/.*>([^<]+).*/\1/p'
43+}
44+
45+zsh-pip-cache-packages() {
46+  if [[ ! -d ${PIP_CACHE_FILE:h} ]]; then
47+      mkdir -p ${PIP_CACHE_FILE:h}
48+  fi
49+
50+  if [[ ! -f $ZSH_PIP_CACHE_FILE ]]; then
51+      echo -n "(...caching package index...)"
52+      tmp_cache=/tmp/zsh_tmp_cache
53+      for index in $ZSH_PIP_INDEXES ; do
54+          # well... I've already got two problems
55+          curl $index 2>/dev/null | \
56+              zsh-pip-clean-packages \
57+               >> $tmp_cache
58+      done
59+      sort $tmp_cache | uniq | tr '\n' ' ' > $ZSH_PIP_CACHE_FILE
60+      rm $tmp_cache
61+  fi
62+}
63+
64+# A test function that validates the regex against known forms of the simple
65+# index. If you modify the regex to make it work for you, you should add a test
66+# case in here and make sure that your changes don't break things for someone
67+# else.
68+zsh-pip-test-clean-packages() {
69+    local expected
70+    local actual
71+    expected="0x10c-asm
72+1009558_nester"
73+
74+    actual=$(echo -n "<html><head><title>Simple Index</title><meta name=\"api-version\" value=\"2\" /></head><body>
75+<a href='0x10c-asm'>0x10c-asm</a><br/>
76+<a href='1009558_nester'>1009558_nester</a><br/>
77+</body></html>" | zsh-pip-clean-packages)
78+
79+    if [[ $actual != $expected ]] ; then
80+        echo -e "python's simple index is broken:\n$actual\n  !=\n$expected"
81+    else
82+        echo "python's simple index is fine"
83+    fi
84+
85+    actual=$(echo -n '<html>
86+  <head>
87+    <title>Simple Package Index</title>
88+  </head>
89+  <body>
90+    <a href="0x10c-asm">0x10c-asm</a><br/>
91+    <a href="1009558_nester">1009558_nester</a><br/>
92+</body></html>' | zsh-pip-clean-packages)
93+
94+    if [[ $actual != $expected ]] ; then
95+        echo -e "the djangopypi2 index is broken:\n$actual\n  !=\n$expected"
96+    else
97+        echo "the djangopypi2 index is fine"
98+    fi
99+}