diff --git a/.github/workflows/python-publish.yml b/.github/workflows/python-publish.yml index 0a0c303..f4bf887 100644 --- a/.github/workflows/python-publish.yml +++ b/.github/workflows/python-publish.yml @@ -26,6 +26,9 @@ jobs: steps: - uses: actions/checkout@v3 + with: + # The version is taken from the release tag, see tools/version.py + fetch-depth: 0 - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v6 with: @@ -35,12 +38,14 @@ jobs: python -m pip install --upgrade pip pip install build cibuildwheel - name: Build package - # if: github.event.inputs.build == 'true' - # run: python -m build --wheel --sdist --outdir dist/ env: # Also builds the free-threaded (cp3XXt) wheels for versions that support them CIBW_PROJECT_REQUIRES_PYTHON: ==${{ matrix.python-version }}.* - run: python -m cibuildwheel --output-dir dist/ + # The wheels are built from the sdist, which carries the version, + # since the build containers don't have the git history + run: | + python -m build --sdist --outdir sdist/ + python -m cibuildwheel --output-dir dist/ sdist/*.tar.gz - name: Upload Artifact uses: actions/upload-artifact@v6 with: @@ -57,6 +62,9 @@ jobs: steps: - uses: actions/checkout@v3 + with: + # The version is taken from the release tag, see tools/version.py + fetch-depth: 0 - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v6 with: @@ -87,6 +95,9 @@ jobs: steps: - uses: actions/checkout@v3 + with: + # The version is taken from the release tag, see tools/version.py + fetch-depth: 0 - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v6 with: @@ -116,6 +127,15 @@ jobs: merge-multiple: true - name: Dist Directory Tree run: ls dist/ -R + - name: Check the wheels match the release version + run: | + version="${GITHUB_REF_NAME#v}" + for wheel in dist/*.whl; do + case "$(basename "$wheel")" in + "extype-$version-"*) ;; + *) echo "::error::$wheel doesn't match the version of $GITHUB_REF_NAME"; exit 1 ;; + esac + done - name: Publish package uses: pypa/gh-action-pypi-publish@v1.8.3 with: diff --git a/README.md b/README.md index 1fdc22a..0b8ce65 100644 --- a/README.md +++ b/README.md @@ -173,6 +173,14 @@ hatch run +py=39 dist:test Both commands will build, install the package into an isolated environment, and run the tests in it. +### Releasing + +The version is taken from the latest `vX.Y.Z` git tag (see `tools/version.py`), so there's no version to bump in the code. +Builds between releases get a development version, such as `1.3.1.dev4+gabc1234`. + +To release, publish a GitHub release with a new `vX.Y.Z` tag. This builds the wheels for all platforms +and Python versions and uploads them to PyPI. The upload is refused if the wheels' version doesn't match the tag. + ### Built-in Extensions diff --git a/meson.build b/meson.build index b585cb8..13a7acb 100644 --- a/meson.build +++ b/meson.build @@ -1,6 +1,7 @@ project('extype', 'cpp', - version: '1.2.0', + # The version comes from the latest `vX.Y.Z` git tag, see tools/version.py + version: run_command('tools/version.py', check: true).stdout().strip(), default_options: [ 'buildtype=release', 'c_std=c17', @@ -8,4 +9,7 @@ project('extype', ] ) +# sdists have no git history, so the version is stored in them when they're made +meson.add_dist_script('tools/version.py', '--write', meson.project_version()) + subdir('src/extype') diff --git a/src/extype/__init__.py b/src/extype/__init__.py index 1198eec..203eba3 100644 --- a/src/extype/__init__.py +++ b/src/extype/__init__.py @@ -1,3 +1,3 @@ from .extension_utils import extension, extend_type_with -__version__ = "1.2.0" \ No newline at end of file +from ._version import __version__ # noqa: F401 \ No newline at end of file diff --git a/src/extype/_version.py.in b/src/extype/_version.py.in new file mode 100644 index 0000000..d156f7e --- /dev/null +++ b/src/extype/_version.py.in @@ -0,0 +1,2 @@ +# Generated at build time from the git tag, see tools/version.py +__version__ = "@VERSION@" diff --git a/src/extype/meson.build b/src/extype/meson.build index d217fff..27ce4ff 100644 --- a/src/extype/meson.build +++ b/src/extype/meson.build @@ -9,8 +9,15 @@ python_sources = [ 'extension_utils.py' ] +version_file = configure_file( + input: '_version.py.in', + output: '_version.py', + configuration: {'VERSION': meson.project_version()}, +) + python.install_sources( python_sources, + version_file, subdir: 'extype' ) diff --git a/tools/version.py b/tools/version.py new file mode 100644 index 0000000..2e80eef --- /dev/null +++ b/tools/version.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 +""" +Computes the package version from git tags, so releasing only takes a `vX.Y.Z` tag. + +- On a tagged commit: `X.Y.Z` +- N commits after the tag `vX.Y.Z`: `X.Y.(Z+1).devN+g` +- In an sdist, where there's no git: the version written to `.version` when the sdist was made +- Without git or tags (e.g. a shallow clone): `0.0.0.dev0` + +Usage: + version.py prints the version + version.py --write V writes V to $MESON_DIST_ROOT/.version (used as a meson dist script) +""" + +import os +import re +import subprocess +import sys +from pathlib import Path + + +ROOT = Path(__file__).resolve().parent.parent +VERSION_FILE = ".version" +FALLBACK_VERSION = "0.0.0.dev0" + + +def version_from_file(): + path = ROOT / VERSION_FILE + if not path.is_file(): + return None + return path.read_text().strip() + + +def version_from_git(): + try: + description = subprocess.run( + ["git", "describe", "--tags", "--long", "--match", "v[0-9]*"], + cwd=str(ROOT), + stdout=subprocess.PIPE, + stderr=subprocess.DEVNULL, + universal_newlines=True, + check=True, + ).stdout.strip() + except (OSError, subprocess.CalledProcessError): + return None + + # `git describe --long` gives `--g` + match = re.fullmatch(r"v(.+)-(\d+)-g([0-9a-f]+)", description) + if match is None: + return None + tag, distance, commit = match.groups() + + if distance == "0": + return tag + + release = re.fullmatch(r"(\d+)\.(\d+)\.(\d+)", tag) + if release is None: + # A pre-release tag, such as `v1.3.0rc1` + return f"{tag}.post{distance}.dev0+g{commit}" + + major, minor, patch = release.groups() + return f"{major}.{minor}.{int(patch) + 1}.dev{distance}+g{commit}" + + +def main(): + if len(sys.argv) == 3 and sys.argv[1] == "--write": + Path(os.environ["MESON_DIST_ROOT"], VERSION_FILE).write_text(sys.argv[2] + "\n") + return + + print(version_from_file() or version_from_git() or FALLBACK_VERSION) + + +if __name__ == "__main__": + main()