Skip to content

Instantly share code, notes, and snippets.

@eduardocardoso
Last active October 21, 2016 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eduardocardoso/04835256f79e42e84b57c8932fbac03c to your computer and use it in GitHub Desktop.
Save eduardocardoso/04835256f79e42e84b57c8932fbac03c to your computer and use it in GitHub Desktop.
Shell script to install pip packages and add them to requirements.txt
pipi() {
REQ_FILE=requirements.txt
TMP_FILE=/tmp/tmp_req.txt
pip install "$@"
if [ $? -eq 0 ] && [ -f "${REQ_FILE}" ]
then
FREEZE="$(pip freeze)"
for arg in "$@"
do
if [[ $arg != -* ]]; then
# sed -i "/^$arg[=<>]/dI" $REQ_FILE # Should work on newer sed versions
grep -vi "$arg[=<>]" $REQ_FILE > $TMP_FILE
mv $TMP_FILE $REQ_FILE
echo "$FREEZE" | grep -i "^$arg[=<>]" >> $REQ_FILE
fi
done
fi
}
pipi() {
REQ_FILE=requirements.txt
pip install "$@"
if [ $? -eq 0 ] && [ -f "${REQ_FILE}" ]
then
FREEZE="$(pip freeze)"
for arg in "$@"
do
if [[ $arg != -* ]]; then
SED_ARG=$(cat $REQ_FILE | grep -i "^$arg[=<>]")
TEMP=$(sed -e "/^${SED_ARG}$/d" $REQ_FILE)
echo "$TEMP" > $REQ_FILE
echo "$FREEZE" | grep -i "^$arg[=<>]" >> $REQ_FILE
fi
done
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment