Last active
June 10, 2022 12:36
-
-
Save aphilas/6bf28a7bb71a66f2a974d27e1ca3ff30 to your computer and use it in GitHub Desktop.
Pip install and save to requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Pip install and add to requirements.txt and sort requirements.txt | |
# Flags: -d Use -d at the beginning! to add dev-requirements.txt instead | |
# TODO: Use extras from pip show, see https://github.com/pypa/pip/issues/4824 | |
# Uses perl with \Q to perform a literal substitution | |
function pips () | |
{ | |
local REQS_PATH='requirements.txt' | |
local OPTIND=1 # Reset index | |
while getopts ":d" opt; do | |
case $opt in | |
d) | |
local REQS_PATH='dev-requirements.txt' | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
exit 1 | |
;; | |
esac | |
done | |
shift "$((OPTIND-1))" | |
for p in "$@" | |
do | |
pip install $p | |
local EXTRAS=$(echo $p | grep -o "\[\w*\]") | |
pip show $(echo $p | perl -pe "s|\Q$EXTRAS||") | awk 'NR==1,NR==2 {print $2}' | sed -z "s/\n/$EXTRAS==/" >> $REQS_PATH | |
done | |
sort -u $REQS_PATH -o $REQS_PATH | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment