-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpip-extended.sh
90 lines (81 loc) · 2.15 KB
/
pip-extended.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
_update_pip_index() {
INDEX_AGE=$(stat -c %Y ~/.pip_index 2>/dev/null || echo 0)
NOW=$(date +%s)
DIFF_S=$(expr $NOW - $INDEX_AGE)
DIFF_H=$(expr $DIFF_S / 3600)
if [ $DIFF_H -gt 24 ]; then
curl --silent https://pypi.org/simple/ | grep href | sed 's/>/</g' | awk -F'<' '{print $3}' >~/.pip_index
fi
}
_pip_completion() {
# complete subcommands
if [[ $COMP_CWORD == 1 ]]; then
curr_word="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY1=($(compgen -W "info pypi require old" -- ${curr_word}))
COMPREPLY2=($(COMP_WORDS="${COMP_WORDS[*]}" \
COMP_CWORD=$COMP_CWORD \
PIP_AUTO_COMPLETE=1 $1))
# combine
COMPREPLY=($(echo ${COMPREPLY1[*]}) $(echo ${COMPREPLY2[*]}))
return 0
fi
# complete packages
if [[ $COMP_CWORD == 2 && "download info install pypi require search" =~ ${COMP_WORDS[1]} ]]; then
_update_pip_index
curr_word="${COMP_WORDS[COMP_CWORD]}"
packages=$(grep $curr_word ~/.pip_index)
COMPREPLY=($(compgen -W "${packages}" -- ${curr_word}))
return 0
fi
COMPREPLY=($(COMP_WORDS="${COMP_WORDS[*]}" \
COMP_CWORD=$COMP_CWORD \
PIP_AUTO_COMPLETE=1 $1))
}
pip-info() {
curl --silent -L https://pypi.org/project/$1 |
xmllint --xpath '//div[@id="description"]' --html - 2>/dev/null |
html2text | pygmentize -l md
}
pip-require() {
local packages="$*"
local package
local installed
for full_name in $packages; do
# strip version details: eg foo==0.1 becomes just foo
package=$(echo "$full_name" | sed 's/\(^[-_a-zA-Z0-9]\+\).*/\1/')
installed=$(pip freeze | grep -c "^$package=")
if [[ "$installed" == "0" ]]; then
pip install -qq "$full_name" &&
pip freeze | grep -i "^$package=" >>new_requirements.txt
echo "$package installed"
else
echo "$package already installed"
fi
done
}
pip-pypi() {
xdg-open "https://pypi.org/project/$1/"
}
pip() {
case $1 in
info)
pip-info "${@:2}"
;;
require)
pip-require "${@:2}"
;;
pypi)
pip-pypi "$2"
;;
old)
pip-old "${@:2}"
;;
isntall) # common typo
pip install "${@:2}"
;;
*)
command pip "$@"
;;
esac
}
complete -o default -F _pip_completion pip