Created
December 23, 2020 22:59
-
-
Save jjorissen52/e5eb5caad8b92f549160f09aaea76eff to your computer and use it in GitHub Desktop.
Thin wrapper around pip that manages your requirements file.
This file contains hidden or 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
# licensed under the UNLICENSE https://unlicense.org/ | |
mpip() { | |
if [[ "$1" == "install" ]]; then | |
touch requirements.txt | |
local old_req="$(< requirements.txt)" | |
local new_req="$old_req" | |
shift 1 | |
local install_list=($@) | |
python -m pip install "$@" | |
if [[ "$?" == "1" ]]; then | |
return 1 | |
fi | |
local currently_installed="$(pip freeze)" | |
# remove newly installed from the requirements list | |
for req in "${install_list[@]}" | |
do | |
local req_base_name=$(python -c "print(\"$req\".split(\"==\")[0])") | |
local req_full_name=$(echo "$currently_installed" | grep -pie "^$req_base_name==") | |
new_req="$(echo "$new_req" | grep -vpie "^$req_base_name\$" | grep -vpie "^$req_full_name\$")" | |
done | |
# add updated versions to the requirements list | |
for req in "${install_list[@]}" | |
do | |
local req_base_name=$(python -c "print(\"$req\".split(\"==\")[0])") | |
local req_full_name=$(echo "$currently_installed" | grep -pie "^$req_base_name==") | |
new_req="$(echo "$new_req" | grep -vpie "^$req_base_name\$" | grep -vpie "^$req_full_name\$")\n$req_full_name" | |
done | |
echo -e "$new_req" | sort > requirements.txt | |
return | |
fi | |
python -m pip $@ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment