78df6e96f5ad2d6df77af39f70f4906a09700849

Author
Brandon W Maister <quodlibetor@gmail.com>
Committer
Brandon W Maister <quodlibetor@gmail.com>
Date

Message

pip: successfully cache all the packages

Switch to using curl and regular expressions to generate a local cache
file so that we don't need to hit pypi.python.org every time.
This (obviously) results in a massive speed improvement, especially if
you spawn new shells frequently.

This also makes the autocompletion work for me, it didn't before. (pip
would always time out.)

And, also, for fun: This allows you to explicitly set which pip indexes
to use. Technically the old version of the plugin should have had this
behavior automatically -- without having to do more than configure pip
-- but the install completion never worked for me so this is a net gain
in functionality.

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..78532a6d3ca9513d6c4334ceda1265825ffd4094
19--- /dev/null
20+++ b/plugins/pip/pip.plugin.zsh
21@@ -0,0 +1,37 @@
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-cache-packages() {
42+  if [[ ! -d ${PIP_CACHE_FILE:h} ]]; then
43+      mkdir -p ${PIP_CACHE_FILE:h}
44+  fi
45+
46+  if [[ ! -f $ZSH_PIP_CACHE_FILE ]]; then
47+      echo -n "(...caching package index...)"
48+      tmp_cache=/tmp/zsh_tmp_cache
49+      for index in $ZSH_PIP_INDEXES ; do
50+          # well... I've already got two problems
51+          curl $index 2>/dev/null | \
52+              sed -nr '/^<a href/ s/.*>([^<]+).*/\1/p' \
53+               >> $tmp_cache
54+      done
55+      sort $tmp_cache | uniq | tr '\n' ' ' > $ZSH_PIP_CACHE_FILE
56+      rm $tmp_cache
57+  fi
58+}