Edited at

pip コマンドでパッケージ名を完全一致で検索(シェルスクリプト)

More than 1 year has passed since last update.

こんにちは

pip コマンドで検索する際に、パッケージ名を完全一致(ただし大文字小文字を無視)で検索するようにシェルスクリプトを作ってみました。なお search_homebrew.sh の方には出力への色付けなどを追加しています(macOS を想定)。

$ ./pip_search.sh gdal

using pip pip3
GDAL (2.2.3) - GDAL: Geospatial Data Abstraction Library
INSTALLED: 2.2.3 (latest)
GDAL (2.2.3) - GDAL: Geospatial Data Abstraction Library
INSTALLED: 2.2.3 (latest)


pip_search.sh

#!/bin/sh

pips="pip pip3"
echo "using "${pips}
for x in "$@"; do
for
pip in ${pips}; do
echo -e "$(${pip} search ${x})" "\n_" | pcregrep -i -M -o "^${x}\s[\s\S]*?^\S" | sed '$d'
done
done
exit $?


pip_search_homebrew.sh

#!/bin/bash

pips="pip2 pip3"
pipcoms=""
for pip in ${pips}; do
which ${pip} >/dev/null
if [ $? = 0 ]; then
pipcoms=${pipcoms}${pip}" "
fi
done
pips=${pipcoms}
echo "using "$'\e[34m'${pips}$'\e[0m'

for x in "$@"; do
for
pip in ${pips}; do
echo -e "$(${pip} search ${x})" "\n_" | pcregrep -i -M -o "^${x}\s[\s\S]*?^\S" | sed '$d' | gsed -E "s/^(${x})/\o033[32m\1\o033[39m/i" | gsed -E "s/(INSTALLED)/\o033[34m\1\o033[39m/"
done
done
exit $?


なお、pip コマンドで、パッケージ名を大文字小文字を無視して検索するには、search.py にパッチを当てた方が良さそうです。

--- /usr/local/lib/python2.7/site-packages/pip/commands/search.py.orig

+++ /usr/local/lib/python2.7/site-packages/pip/commands/search.py
@@
try:
logger.info(line)
- if name in installed_packages:
+ if in_case_insensitive(name, installed_packages):
dist = pkg_resources.get_distribution(name)
with indent_log():
latest = highest_version(hit['versions'])
@@
+def in_case_insensitive(x, list):
+ return x.lower() in [y.lower() for y in list]