Skip to content

Instantly share code, notes, and snippets.

@jjorissen52
Created December 23, 2020 22:59
Show Gist options
  • Save jjorissen52/e5eb5caad8b92f549160f09aaea76eff to your computer and use it in GitHub Desktop.
Save jjorissen52/e5eb5caad8b92f549160f09aaea76eff to your computer and use it in GitHub Desktop.
Thin wrapper around pip that manages your requirements file.
# 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