Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor update_backend.yml workflow #562

Merged
merged 10 commits into from
Mar 9, 2024
43 changes: 15 additions & 28 deletions .github/workflows/update_backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,34 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.9
python-version: 3.12
cache: pip

- name: "Install PySR"
- name: "Install dependencies"
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
python setup.py install
# Not needed:
# python -c 'import pysr; pysr.install()'
pip install tomlkit

- name: "Get SymbolicRegression.jl latest version"
id: get-latest
run: |
cd $(mktemp -d)
git clone https://github.com/MilesCranmer/SymbolicRegression.jl
cd SymbolicRegression.jl
echo "version=$(git describe --tags --abbrev=0 | sed 's/^v//')" >> $GITHUB_OUTPUT
echo "version=$(git describe --tags --match='v*' --abbrev=0 | sed 's/^v//')" >> $GITHUB_OUTPUT

- name: "Get SymbolicRegression.jl version used in PySR"
id: get-current
- name: "Update SymbolicRegression.jl version in PySR"
run: |
echo "version=$(python -c 'import pysr; print(pysr.version.__symbolic_regression_jl_version__)' 2>/dev/null)" >> $GITHUB_OUTPUT
python .github/workflows/update_backend_version.py ${{ steps.get-latest.outputs.version }}

# If versions are different, we want to take our checked-out version,
# create a new branch called "update_compat_{...}", where the "..."
# is a timestamp. We then want to
# go to pysr/version.py, bump the patch version of PySR (__version__),
# set the version of __symbolic_regression_jl_version__ to the latest
# version of SymbolicRegression.jl, and then commit and push.
# Finally, we will open a PR from this branch to master.
- name: "Update versions"
if: ${{ steps.get-latest.outputs.version != steps.get-current.outputs.version }}
- name: "Restore changes if no diff to `pysr/juliapkg.json`"
run: |
# Bump PySR patch number:
CURRENT_PYSR_PATCH_VERSION=$(python -c 'import pysr; print(pysr.version.__version__.split(".")[-1], end="")' 2>/dev/null)
NEW_PYSR_PATCH_VERSION=$((CURRENT_PYSR_PATCH_VERSION + 1))
sed -i "s/^__version__ = .*/__version__ = \"$(python -c 'import pysr; print(".".join(pysr.version.__version__.split(".")[:-1]), end="")' 2>/dev/null).${NEW_PYSR_PATCH_VERSION}\"/" pysr/version.py
if git diff --quiet pysr/juliapkg.json; then
echo "No changes to pysr/juliapkg.json. Restoring changes."
git restore pyproject.toml
fi

# Set SymbolicRegression.jl version:
sed -i "s/^__symbolic_regression_jl_version__ = .*/__symbolic_regression_jl_version__ = \"${{ steps.get-latest.outputs.version }}\"/" pysr/version.py

- name: "Create PR"
- name: "Create PR if necessary"
uses: peter-evans/create-pull-request@v6
with:
token: ${{ secrets.REPO_SCOPED_TOKEN }}
Expand All @@ -63,4 +48,6 @@ jobs:
It updates the backend version to v${{ steps.get-latest.outputs.version }}. For a full description of the changes, see the backend changelog: [v${{ steps.get-latest.outputs.version }}](https://github.com/MilesCranmer/SymbolicRegression.jl/releases/tag/v${{ steps.get-latest.outputs.version }}).
delete-branch: true
commit-message: "Update backend version to v${{ steps.get-latest.outputs.version }}"
add-paths: pysr/version.py
add-paths: |
pyproject.toml
pysr/juliapkg.json
29 changes: 29 additions & 0 deletions .github/workflows/update_backend_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import json
import sys
from pathlib import Path

import tomlkit

new_backend_version = sys.argv[1]

assert not new_backend_version.startswith("v"), "Version should not start with 'v'"

pyproject_toml = Path(__file__).parent / ".." / ".." / "pyproject.toml"
juliapkg_json = Path(__file__).parent / ".." / ".." / "pysr" / "juliapkg.json"

with open(pyproject_toml) as toml_file:
pyproject_data = tomlkit.parse(toml_file.read())

with open(juliapkg_json) as f:
juliapkg_data = json.load(f)

major, minor, patch, *dev = pyproject_data["project"]["version"].split(".")
pyproject_data["project"]["version"] = f"{major}.{minor}.{int(patch)+1}"

juliapkg_data["packages"]["SymbolicRegression"]["version"] = f"={new_backend_version}"

with open(pyproject_toml, "w") as toml_file:
toml_file.write(tomlkit.dumps(pyproject_data))

with open(juliapkg_json, "w") as f:
json.dump(juliapkg_data, f, indent=4)