Skip to content

Commit b5a188f

Browse files
vidipsinghkratmanSaransh-cpp
authored
Switch to hatch-vcs for versioning (#4802)
* implement hatch-vcs versioning * integrate hatch-vcs for automated versioning - Move hatch-vcs config to `tool.hatch` in `pyproject.toml` - Switch to `_version.py` for versioning with hatch-vcs - Remove manual `version.py` updates, using git tags instead * remove manual version update - Removed manual version writing in `update_version.py`. - Added `src/pybamm/_version.py` to `.gitignore`. * update release workflow for hatch-vcs * Update version.py to use _version.py and conf.py to use importlib.metadata * Revert update_version.py and use hatch-vcs in version.py * Enable fetch-tags for hatch-vcs * Minor updates to versioning and release workflow. --------- Co-authored-by: Eric G. Kratz <[email protected]> Co-authored-by: Saransh Chopra <[email protected]>
1 parent ef768ca commit b5a188f

File tree

7 files changed

+16
-38
lines changed

7 files changed

+16
-38
lines changed

.github/release_workflow.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ GitHub, PyPI, and conda-forge by the maintainers.
99
and September, updating incrementing the version to `vYY.MM.0` by running
1010
`scripts/update_version.py` in the following files:
1111

12-
- `pybamm/version.py`
13-
- `docs/conf.py`
1412
- `CITATION.cff`
15-
- `pyproject.toml`
1613
- `CHANGELOG.md`
1714

1815
These changes will be automatically pushed to a new branch `vYY.MM`
@@ -34,13 +31,9 @@ If a new release is required after the release of `vYY.MM.{x-1}` -
3431
merged into `develop`. The CHANGELOG entry for such fixes should go under the
3532
`YY.MM.x` heading in `CHANGELOG.md`
3633

37-
3. Run `scripts/update_version.py` manually while setting `VERSION=vYY.MM.x`
38-
in your environment. This will update the version in the following files:
34+
3. Run `scripts/update_version.py` manually to update:
3935

40-
- `pybamm/version.py`
41-
- `docs/conf.py`
4236
- `CITATION.cff`
43-
- `pyproject.toml`
4437
- `CHANGELOG.md`
4538

4639
Commit the changes to your release branch.

.github/workflows/publish_pypi.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
steps:
1616
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
1717
with:
18-
persist-credentials: false
18+
persist-credentials: true
1919
- uses: actions/setup-python@8d9ed9ac5c53483de85588cdf95a591a75ab9f55 # v5.5.0
2020
with:
2121
python-version: 3.12

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
*.cache
3131
.ipynb_checkpoints/
3232

33+
# Version file (auto-generated by hatch-vcs)
34+
src/pybamm/_version.py
35+
3336
# Sphinx build files
3437
docs/_build/
3538
docs/build/

docs/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import os
1515
import sys
1616
import pybamm
17+
import importlib.metadata
1718

1819
# Path for repository root
1920
sys.path.insert(0, os.path.abspath("../"))
@@ -29,7 +30,7 @@
2930
author = "The PyBaMM Team"
3031

3132
# The short X.Y version
32-
version = pybamm.__version__
33+
version = importlib.metadata.version("pybamm")
3334
# The full version, including alpha/beta/rc tags
3435
release = version
3536

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
[build-system]
22
requires = [
33
"hatchling>=1.13.0",
4+
"hatch-vcs>=0.3.0",
45
]
56
build-backend = "hatchling.build"
67

78
[project]
89
name = "pybamm"
9-
version = "25.1.1"
10+
dynamic = ["version"]
1011
license = { file = "LICENSE.txt" }
1112
description = "Python Battery Mathematical Modelling"
1213
authors = [{name = "The PyBaMM Team", email = "[email protected]"}]
@@ -149,10 +150,12 @@ MSMR_Example = "pybamm.input.parameters.lithium_ion.MSMR_example_set:get_paramet
149150
Chayambuka2022 = "pybamm.input.parameters.sodium_ion.Chayambuka2022:get_parameter_values"
150151

151152
[tool.hatch]
153+
version.source = "vcs"
152154
build.targets.sdist.include = [
153155
"src/pybamm",
154156
"CITATION.cff",
155157
]
158+
build.hooks.vcs.version-file = "src/pybamm/_version.py"
156159

157160
[tool.repo-review]
158161
ignore = [

scripts/update_version.py

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,20 @@
11
"""
2-
Automatically update the version number
2+
Automatically update version numbers in various files for releases
33
"""
44

55
import os
66
import re
77
from datetime import date
88
import pybamm
9+
from pybamm._version import __version__ as release_version
910

1011

1112
def update_version():
1213
"""
13-
Opens file and updates the version number
14+
Updates version numbers and release information across project files
1415
"""
15-
release_version = os.getenv("VERSION")[1:]
1616
release_date = date.today()
1717

18-
# pybamm/version.py
19-
with open(
20-
os.path.join(pybamm.root_dir(), "src", "pybamm", "version.py"), "r+"
21-
) as file:
22-
output = file.read()
23-
replace_version = re.sub(
24-
'(?<=__version__ = ")(.+)(?=")', release_version, output
25-
)
26-
file.truncate(0)
27-
file.seek(0)
28-
file.write(replace_version)
29-
30-
# pyproject.toml
31-
with open(os.path.join(pybamm.root_dir(), "pyproject.toml"), "r+") as file:
32-
output = file.read()
33-
replace_version = re.sub(
34-
r'(?<=\bversion = ")(.+)(?=")', release_version, output
35-
)
36-
file.truncate(0)
37-
file.seek(0)
38-
file.write(replace_version)
39-
4018
# CITATION.cff
4119
with open(os.path.join(pybamm.root_dir(), "CITATION.cff"), "r+") as file:
4220
output = file.read()
@@ -45,10 +23,10 @@ def update_version():
4523
file.seek(0)
4624
file.write(replace_version)
4725

26+
# CHANGELOG.md
4827
changelog_line1 = "# [Unreleased](https://github.com/pybamm-team/PyBaMM/)\n"
4928
changelog_line2 = f"# [v{release_version}](https://github.com/pybamm-team/PyBaMM/tree/v{release_version}) - {release_date}\n\n"
5029

51-
# CHANGELOG.md
5230
with open(os.path.join(pybamm.root_dir(), "CHANGELOG.md"), "r+") as file:
5331
output_list = file.readlines()
5432
output_list[0] = changelog_line1

src/pybamm/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "25.1.1"
1+
from pybamm._version import version as __version__ # noqa: F401

0 commit comments

Comments
 (0)