William Thompson MilesCranmer commited on
Commit
a90c881
1 Parent(s): acee094

Refactor update_backend.yml workflow (#562)

Browse files

* Moves backend update code to python script

* Account for move from pysr/version.py to pyproject.toml

* Remove need for installing pysr during backend update

---------

Co-authored-by: MilesCranmer <[email protected]>

.github/workflows/update_backend.yml CHANGED
@@ -10,16 +10,13 @@ jobs:
10
  - uses: actions/checkout@v4
11
  - uses: actions/setup-python@v5
12
  with:
13
- python-version: 3.9
14
  cache: pip
15
 
16
- - name: "Install PySR"
17
  run: |
18
  python -m pip install --upgrade pip
19
- pip install -r requirements.txt
20
- python setup.py install
21
- # Not needed:
22
- # python -c 'import pysr; pysr.install()'
23
 
24
  - name: "Get SymbolicRegression.jl latest version"
25
  id: get-latest
@@ -27,32 +24,20 @@ jobs:
27
  cd $(mktemp -d)
28
  git clone https://github.com/MilesCranmer/SymbolicRegression.jl
29
  cd SymbolicRegression.jl
30
- echo "version=$(git describe --tags --abbrev=0 | sed 's/^v//')" >> $GITHUB_OUTPUT
31
 
32
- - name: "Get SymbolicRegression.jl version used in PySR"
33
- id: get-current
34
  run: |
35
- echo "version=$(python -c 'import pysr; print(pysr.version.__symbolic_regression_jl_version__)' 2>/dev/null)" >> $GITHUB_OUTPUT
36
 
37
- # If versions are different, we want to take our checked-out version,
38
- # create a new branch called "update_compat_{...}", where the "..."
39
- # is a timestamp. We then want to
40
- # go to pysr/version.py, bump the patch version of PySR (__version__),
41
- # set the version of __symbolic_regression_jl_version__ to the latest
42
- # version of SymbolicRegression.jl, and then commit and push.
43
- # Finally, we will open a PR from this branch to master.
44
- - name: "Update versions"
45
- if: ${{ steps.get-latest.outputs.version != steps.get-current.outputs.version }}
46
  run: |
47
- # Bump PySR patch number:
48
- CURRENT_PYSR_PATCH_VERSION=$(python -c 'import pysr; print(pysr.version.__version__.split(".")[-1], end="")' 2>/dev/null)
49
- NEW_PYSR_PATCH_VERSION=$((CURRENT_PYSR_PATCH_VERSION + 1))
50
- 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
51
 
52
- # Set SymbolicRegression.jl version:
53
- sed -i "s/^__symbolic_regression_jl_version__ = .*/__symbolic_regression_jl_version__ = \"${{ steps.get-latest.outputs.version }}\"/" pysr/version.py
54
-
55
- - name: "Create PR"
56
  uses: peter-evans/create-pull-request@v6
57
  with:
58
  token: ${{ secrets.REPO_SCOPED_TOKEN }}
@@ -63,4 +48,6 @@ jobs:
63
  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 }}).
64
  delete-branch: true
65
  commit-message: "Update backend version to v${{ steps.get-latest.outputs.version }}"
66
- add-paths: pysr/version.py
 
 
 
10
  - uses: actions/checkout@v4
11
  - uses: actions/setup-python@v5
12
  with:
13
+ python-version: 3.12
14
  cache: pip
15
 
16
+ - name: "Install dependencies"
17
  run: |
18
  python -m pip install --upgrade pip
19
+ pip install tomlkit
 
 
 
20
 
21
  - name: "Get SymbolicRegression.jl latest version"
22
  id: get-latest
 
24
  cd $(mktemp -d)
25
  git clone https://github.com/MilesCranmer/SymbolicRegression.jl
26
  cd SymbolicRegression.jl
27
+ echo "version=$(git describe --tags --match='v*' --abbrev=0 | sed 's/^v//')" >> $GITHUB_OUTPUT
28
 
29
+ - name: "Update SymbolicRegression.jl version in PySR"
 
30
  run: |
31
+ python .github/workflows/update_backend_version.py ${{ steps.get-latest.outputs.version }}
32
 
33
+ - name: "Restore changes if no diff to `pysr/juliapkg.json`"
 
 
 
 
 
 
 
 
34
  run: |
35
+ if git diff --quiet pysr/juliapkg.json; then
36
+ echo "No changes to pysr/juliapkg.json. Restoring changes."
37
+ git restore pyproject.toml
38
+ fi
39
 
40
+ - name: "Create PR if necessary"
 
 
 
41
  uses: peter-evans/create-pull-request@v6
42
  with:
43
  token: ${{ secrets.REPO_SCOPED_TOKEN }}
 
48
  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 }}).
49
  delete-branch: true
50
  commit-message: "Update backend version to v${{ steps.get-latest.outputs.version }}"
51
+ add-paths: |
52
+ pyproject.toml
53
+ pysr/juliapkg.json
.github/workflows/update_backend_version.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import sys
3
+ from pathlib import Path
4
+
5
+ import tomlkit
6
+
7
+ new_backend_version = sys.argv[1]
8
+
9
+ assert not new_backend_version.startswith("v"), "Version should not start with 'v'"
10
+
11
+ pyproject_toml = Path(__file__).parent / ".." / ".." / "pyproject.toml"
12
+ juliapkg_json = Path(__file__).parent / ".." / ".." / "pysr" / "juliapkg.json"
13
+
14
+ with open(pyproject_toml) as toml_file:
15
+ pyproject_data = tomlkit.parse(toml_file.read())
16
+
17
+ with open(juliapkg_json) as f:
18
+ juliapkg_data = json.load(f)
19
+
20
+ major, minor, patch, *dev = pyproject_data["project"]["version"].split(".")
21
+ pyproject_data["project"]["version"] = f"{major}.{minor}.{int(patch)+1}"
22
+
23
+ juliapkg_data["packages"]["SymbolicRegression"]["version"] = f"={new_backend_version}"
24
+
25
+ with open(pyproject_toml, "w") as toml_file:
26
+ toml_file.write(tomlkit.dumps(pyproject_data))
27
+
28
+ with open(juliapkg_json, "w") as f:
29
+ json.dump(juliapkg_data, f, indent=4)