Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
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',
'cpp_std=c++17'
]
)

# 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')
2 changes: 1 addition & 1 deletion src/extype/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .extension_utils import extension, extend_type_with

__version__ = "1.2.0"
from ._version import __version__ # noqa: F401
2 changes: 2 additions & 0 deletions src/extype/_version.py.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Generated at build time from the git tag, see tools/version.py
__version__ = "@VERSION@"
7 changes: 7 additions & 0 deletions src/extype/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)

Expand Down
74 changes: 74 additions & 0 deletions tools/version.py
Original file line number Diff line number Diff line change
@@ -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<hash>`
- 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 `<tag>-<commits since tag>-g<hash>`
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()
Loading