Skip to content

Instantly share code, notes, and snippets.

@Wind010
Last active February 9, 2024 22:03
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 Wind010/6cb0b1e3d9b711460a5bdd17c8f08f13 to your computer and use it in GitHub Desktop.
Save Wind010/6cb0b1e3d9b711460a5bdd17c8f08f13 to your computer and use it in GitHub Desktop.
Script to pull the existing packages from a setup.py, pip install latest versions of those packages, pip freeze those versions and repopulate the setup.py
#!/bin/bash
# Work in Progress
# The pip freeze will bring in a lot more packages explicitly than expected.
# Still need to populate/paste into setup.py correctly.
SETUP_PY_PATH="./setup.py"
# Extract install_requires values without versioning
INSTALL_REQUIRES=$(grep -oP '"[^"]+"' $SETUP_PY_PATH \
| grep -oP '"[^"]+"' \
| grep '==' \
| tr -d '"')
REQUIREMENTS_TMP='requirements_tmp.txt'
# Create or overwrite requirements_tmp.txt
echo "$INSTALL_REQUIRES" > $REQUIREMENTS_TMP
# Display a message
echo "Requirements have been extracted and written to requirements.txt:"
cat $REQUIREMENTS_TMP
pip install -r $REQUIREMENTS_TMP
pip freeze > $REQUIREMENTS_TMP
# Pause for debugging
#read -n 1 -s -r -p "Press any key to continue..."
echo "Freezing exact versions"
cat $REQUIREMENTS_TMP
echo "Updating setup.py with entries from requirements.txt..."
sed -i "s/\(install_requires\s*=\s*\)[^,]*\(,\)/\1['$(paste -s -d, requirements.txt)']\2/" "$SETUP_PY_PATH"
echo "setup.py has been updated:"
cat "$SETUP_PY_PATH"
rm $REQUIREMENTS_TMP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment