Skip to content

Instantly share code, notes, and snippets.

@engineervix
Created August 9, 2024 12:05
Show Gist options
  • Select an option

  • Save engineervix/d428d835a43d3ff31a2cfc9bbc67e4ab to your computer and use it in GitHub Desktop.

Select an option

Save engineervix/d428d835a43d3ff31a2cfc9bbc67e4ab to your computer and use it in GitHub Desktop.
Automate updating Poetry dependencies with individual commits for each package, including version changes.
#!/bin/bash
# =================================================================================================
# Description: This script updates all outdated Poetry dependencies for a Python project, ensuring
# each package is kept up-to-date with the latest compatible versions available.
# It processes each outdated package individually, updating them one by one.
# If an update is successful, the script commits these changes to git
# with a clear message indicating the package name and the version change.
# This granular approach to updating and committing allows for
# better tracking of changes and simplifies rollback procedures if an update
# introduces issues.
#
# ❓ Why this script?
# The main advantage of this script lies in its approach to handling updates
# with individual commits. This method provides several benefits:
# - **Improved Traceability**: Each commit corresponds to a single package update,
# making it easy to identify when and how each dependency was updated.
# - **Simplified Debugging**: If a particular update causes problems, developers
# can quickly pinpoint the offending update based on the commit history.
# - **Better Control**: Developers can choose to revert a single package update
# without affecting others, offering finer control over the project's dependencies.
# - **Streamlined Collaboration**: When working in a team, clear and concise commits
# make it easier for all team members to understand the changes made to the
# project's dependencies.
#
# Utilizing this script ensures that your project's dependencies are not only
# current but also that each change is documented and manageable.
#
# ⚠️ Warning:
# While this script automates the update process, it does not differentiate between
# patch, minor and major version updates. Major version updates can introduce breaking
# changes that may require manual intervention to resolve compatibility issues.
# It is your responsibility to
# - pay attention to how dependencies are specified in `pyproject.toml`
# - review the release notes of major version updates before applying them
# - ensure that your project has a comprehensive test suite to catch any issues
# arising from dependency updates.
#
# license: BSD-3-Clause
# author: Victor Miti <https://github.com/engineervix>
# =================================================================================================
# Navigate to the project directory if not already there
# cd /path/to/your/python/project
echo "Checking for outdated packages..."
# Checking outdated packages with Poetry
outdated_packages=$(poetry show --outdated | awk '{print $1, $2, $3}')
echo "Starting the update process..."
# Read the outdated packages list line by line
echo "$outdated_packages" | while read -r line; do
# Splitting the line into package name, current version, and latest version
read -ra ADDR <<<"$line"
package_name=${ADDR[0]}
current_version=${ADDR[1]}
latest_version=${ADDR[2]}
# Update package
echo "Attempting to update $package_name from $current_version to $latest_version..."
if poetry update "$package_name"; then
# If update succeeded, commit the changes
git add poetry.lock
git commit -m "Update $package_name ($current_version -> $latest_version)"
echo "Successfully updated and committed $package_name"
else
# If update failed, continue to the next package
echo "Failed to update $package_name. Continuing to next package..."
fi
done
echo "Dependency update process completed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment