-
Notifications
You must be signed in to change notification settings - Fork 1
/
pypi-simple-search
executable file
·95 lines (80 loc) · 2.43 KB
/
pypi-simple-search
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
91
92
93
94
95
#!/bin/bash
#from https://github.com/jeffmm/pypi-simple-search
PSS_DIR=~/.local/share/pypi-simple-search
PSS_CACHE=${PSS_DIR}/simple.txt
curl_pypi() {
echo "Updating cache of PyPi packages"
curl -s https://pypi.org/simple/ > "${PSS_CACHE}.tmp"
# Remove header text
tail -n +7 "${PSS_CACHE}.tmp" > "$PSS_CACHE" && rm "${PSS_CACHE}.tmp"
# Remove html tags and whitespace
perl -pi -e 's/[\t ]+|<.+?>//g' "$PSS_CACHE"
}
print_help() {
echo "pypi-simple-search: a stop-gap replacement for \"pip search\""
echo " Usage:"
echo " $0 [-hu] [query]"
echo " Options:"
echo " -h show this menu"
echo " -u update cache of pypi packages"
echo " Arguments:"
echo " query package name to search"
echo " Environment:"
echo " \$PYPI_SIMPLE_SEARCH search command to use, defaults to \"grep\""
}
update=0
OPTIND=1 # Reset in case getopts has been used previously in the shell
while getopts "h?u" opt; do
case "$opt" in
h|\?)
print_help
exit 0
;;
u)
# Update the pypi package cache
update=1
;;
esac
done
shift $((OPTIND-1))
[ "${1:-}" = "--" ] && shift
if [ ! -d "$PSS_DIR" ]; then
mkdir -p "$PSS_DIR"
curl_pypi
elif [ ! -f "$PSS_CACHE" ]; then
curl_pypi
fi
# Thanks to @lfom for the Linux portability fix
cache_time=0
case "$(uname)" in
Darwin* | FreeBSD*) cache_time=$(stat -f%c ${PSS_CACHE});;
Linux*) cache_time=$(stat -c%Y ${PSS_CACHE});;
*)
echo "Sorry, unsuported or unknown system: $(uname)!"
echo "Please submit an issue here: https://github.com/jeffmm/pypi-simple-search/issues"
exit
esac
elapsed_time=$(( $(date +%s) - ${cache_time} ))
if [ $elapsed_time -gt 604800 ]; then
# Update automatically if the cache is over 1 week old
echo "It's been over a week since the package cache was updated"
curl_pypi
fi
# If the user sets their own search preference (e.g. ag, rg, etc), use it
if [ -z "${PYPI_SIMPLE_SEARCH}" ]; then
# Otherwise just use grep, because it's universal
pss_func="grep"
else
pss_func="${PYPI_SIMPLE_SEARCH}"
fi
# Allow just updating the cache ie: $0 -u
if [ $update -eq 1 ]; then
curl_pypi
fi
if [ -n "$1" ]; then
"$pss_func" "$1" "$PSS_CACHE"
elif [ $update -eq 0 ]; then
# If no arg given and we are not updating the cache, educate the user
print_help
exit 1
fi