Created
April 18, 2023 05:26
-
-
Save jakkaj/ff8203dfff6676b6ae34a194d08f9960 to your computer and use it in GitHub Desktop.
Pins the versions of pip entries in the requirements.txt file. Unlike freeze, it only does the 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
#!/bin/bash | |
# Check if requirements.txt exists | |
if [ ! -f "requirements.txt" ]; then | |
echo "Error: requirements.txt not found." | |
exit 1 | |
fi | |
# Remove existing requirements2.txt if it exists | |
if [ -f "requirements2.txt" ]; then | |
rm -f requirements2.txt | |
fi | |
# Ensure the input file ends with a newline | |
sed -i -e '$a\' requirements.txt | |
# Iterate through each line in requirements.txt | |
while IFS= read -r package; do | |
# Check if the package is installed | |
version=$(pip show "$package" | grep -i "^version" | awk '{print $2}') | |
if [ -n "$version" ]; then | |
# Append the package name and version to requirements2.txt | |
echo "${package}==${version}" >> requirements2.txt | |
else | |
echo "Warning: Package $package not found. Skipping." | |
fi | |
done < requirements.txt | |
echo "Requirements updated in requirements2.txt" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment