Created
February 9, 2024 21:55
-
-
Save Wind010/f9679252bb5d7ba88987946ced224fa3 to your computer and use it in GitHub Desktop.
Compare actually installed vs what the requirements.txt or setup.py references
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
#!/bin/bash | |
# Work in progerss. Currently the diff is not accurate. | |
# TODO: Take in setup.py and requirements_*.txt. | |
# Exit immediately if a command exits with a non-zero status | |
set -e | |
# Uncomment for debugging | |
#set -x | |
REQUIREMENTS_CURRENT="requirements_current.txt" | |
REQUIREMENTS_ACTUAL="requirements_actual.txt" | |
# Function to show script usage | |
show_usage() { | |
echo "Usage: $0 <MANIFEST_PATH>" | |
echo " <MANIFEST_PATH> Path to the source file (setup.py or requirements_*.txt)" | |
exit 1 | |
} | |
# Parse command line options | |
if [ "$#" -ne 1 ]; then | |
show_usage | |
fi | |
MANIFEST_PATH=$1 | |
# Check if the source file exists | |
if [ ! -f "$MANIFEST_PATH" ]; then | |
echo "Source file not found at $MANIFEST_PATH" | |
exit 1 | |
fi | |
# Check if pipdeptree is installed and prompt for installation since it's required. | |
if ! command -v pipdeptree &> /dev/null; then | |
read -p "The pipdeptree package is required Do you want to install it? " -n 1 -r | |
if [ $REPLY =~ ^[Yy]$ ]; then | |
pip install pipdeptree | |
fi | |
fi | |
# Determine whether to extract dependencies from setup.py or compare with requirements.txt | |
if [ "${MANIFEST_PATH##*.}" = "py" ]; then | |
INSTALL_REQUIRES=$(grep -oP '"[^"]+"' $MANIFEST_PATH \ | |
| grep -oP '"[^"]+"' \ | |
| grep '==' \ | |
| tr -d '"') \ | |
|| { echo "Failed to extract dependencies from setup.py"; exit 1; } | |
if [ -z "$INSTALL_REQUIRES" ]; then | |
echo "Unable to extract the dependences from '$MANIFEST_PATH' !" | |
exit 1 | |
fi | |
echo "$INSTALL_REQUIRES" > $REQUIREMENTS_CURRENT | |
echo "Dependencies from setup.py added to requirements_current.txt." | |
elif [ "${MANIFEST_PATH##*.}" = "txt" ]; then | |
cp "$MANIFEST_PATH" "$REQUIREMENTS_CURRENT" | |
echo "Using $MANIFEST_PATH as requirements_current.txt." | |
else | |
echo "Invalid dependency manifest file. Use a setup.py or a requirements_*.txt file." | |
show_usage | |
fi | |
# Generate a list of installed dependencies using pipdeptree | |
pipdeptree --warn silence | sed -E -n 's/^(\S+)\s*==\s*([^=]+).*$/\1==\2/p' > $REQUIREMENTS_ACTUAL | |
# Compare requirements_current.txt and requirements_actual.txt | |
differences=$(comm -23 <(sort $REQUIREMENTS_CURRENT) <(sort $REQUIREMENTS_ACTUAL)) | |
# Display the differences | |
if [ -z "$differences" ]; then | |
echo "✔ All dependencies in $REQUIREMENTS_CURRENT are satisfied. 🖖" | |
else | |
echo "⚠ Dependencies in $REQUIREMENTS_CURRENT not actually installed:" | |
echo "$differences" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment