Skip to content

Instantly share code, notes, and snippets.

@renxida
Last active September 25, 2024 05:14
Show Gist options
  • Save renxida/785d595098b0fb9e236c6a83bb9b7e1a to your computer and use it in GitHub Desktop.
Save renxida/785d595098b0fb9e236c6a83bb9b7e1a to your computer and use it in GitHub Desktop.
python_version_scan_docker.sh
#!/bin/bash
# Define the range of Python versions to test
versions=("3.12" "3.11" "3.10" "3.9" "3.8" "3.7")
# Function to update pyproject.toml
update_pyproject() {
sed -i 's/python = "^[0-9.]*"/python = "^'"$1"'"/' pyproject.toml
}
# Store original Python version
original_version=$(grep '^python = ' pyproject.toml | cut -d'"' -f2)
# Create a temporary Dockerfile
cat << EOF > Dockerfile.temp
FROM python:VERSION-slim
RUN pip install poetry
WORKDIR /app
COPY . /app
RUN poetry config virtualenvs.create false \
&& poetry install --no-interaction --no-ansi
CMD poetry run pytest
EOF
# Test versions
for version in "${versions[@]}"; do
echo "Testing Python $version"
update_pyproject $version
# Create a version-specific Dockerfile
sed "s/VERSION/$version/" Dockerfile.temp > Dockerfile
# Build and run Docker container
if docker build -t python-test:$version . && docker run --rm python-test:$version; then
echo "Python $version is compatible"
lowest_compatible=$version
else
echo "Python $version is not compatible"
break
fi
done
# Clean up
rm Dockerfile Dockerfile.temp
if [ -n "$lowest_compatible" ]; then
echo "Lowest compatible Python version: $lowest_compatible"
update_pyproject $lowest_compatible
echo "Updated pyproject.toml to use Python ^$lowest_compatible"
else
echo "No compatible Python version found"
echo "Restoring original Python version constraint"
update_pyproject ${original_version#^}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment