Skip to content

Commit 80aab26

Browse files
committed
ENH: Added meson-python metadata provider for dynamic version discovery with meson-python build backend
1 parent 77550d1 commit 80aab26

9 files changed

Lines changed: 187 additions & 22 deletions

File tree

CHANGELOG.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
This file lists all changes to the code
22

3+
v0.4.0 (09Jan26)
4+
----------------
5+
6+
* ENH: Added meson-python metadata provider for dynamic version discovery with meson-python build backend
7+
* MAINT: Renamed internal module `Discovery.py` to `discovery.py` (PEP 8 compliance)
8+
* MAINT: Added keywords and improved classifiers in pyproject.toml for better discoverability
9+
310
v0.3.2 (29Dec25)
411
----------------
512

613
* BUG: Fixed CI workflow to use `DISCOVER_VERSION` environment variable when building
714
* BUG: Build wheel in addition to sdist (wheel has version baked into metadata)
815

16+
v0.3.1 (29Dec25)
17+
----------------
18+
19+
* MAINT: Updated version to 0.3.1
20+
921
v0.3.0 (29Dec25)
1022
----------------
1123

@@ -54,12 +66,12 @@ v0.1.9 (22Mar24)
5466

5567
* MAINT: Changed discovery order
5668

57-
* v0.1.8 (22Mar24)
69+
v0.1.8 (22Mar24)
5870
----------------
5971

6072
* MAINT: Added discovery via PKG-INFO file
6173

62-
* v0.1.7 (22Mar24)
74+
v0.1.7 (22Mar24)
6375
----------------
6476

6577
* BUG: Use __name__ for package name

DiscoverVersion/Discovery.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,7 @@ def get_version(
152152
tried += ", env"
153153
discovered_version = get_version_from_env()
154154

155-
# We need to start with importlib because otherwise all packages will
156-
# have the version of the current git repository
157-
158-
# inspect PKG-INFO file (if it exists)
155+
# Inspect PKG-INFO file (if it exists, e.g. in sdist builds)
159156
if discovered_version is None and use_pkginfo:
160157
tried += ", PKG-INFO"
161158
try:
@@ -175,7 +172,8 @@ def get_version(
175172
except CannotDiscoverVersion:
176173
discovered_version = None
177174

178-
# importlib is present in Python >= 3.8
175+
# importlib is checked last as a fallback for installed packages.
176+
# It is skipped during build isolation (when running under flit_core).
179177
if (
180178
discovered_version is None
181179
and _toplevel_package not in _build_systems

DiscoverVersion/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#
2424

2525
from .version import __version__
26-
from .Discovery import (
26+
from .discovery import (
2727
get_version,
2828
get_version_from_env,
2929
get_version_from_git,

DiscoverVersion/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
import argparse
4646
import sys
4747

48-
from .Discovery import (
48+
from .discovery import (
4949
CannotDiscoverVersion,
5050
get_version_from_env,
5151
get_version_from_git,

DiscoverVersion/meson_python.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#
2+
# Copyright 2024 Lars Pastewka
3+
#
4+
# ### MIT license
5+
#
6+
# Permission is hereby granted, free of charge, to any person obtaining a copy
7+
# of this software and associated documentation files (the "Software"), to deal
8+
# in the Software without restriction, including without limitation the rights
9+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
# copies of the Software, and to permit persons to whom the Software is
11+
# furnished to do so, subject to the following conditions:
12+
#
13+
# The above copyright notice and this permission notice shall be included in
14+
# all copies or substantial portions of the Software.
15+
#
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
# SOFTWARE.
23+
#
24+
25+
"""
26+
Meson-python metadata provider for DiscoverVersion.
27+
28+
This module provides version discovery integration with meson-python build
29+
backend. To use it, add the following to your pyproject.toml:
30+
31+
[tool.meson-python.metadata]
32+
version.provider = "DiscoverVersion.meson_python"
33+
34+
"""
35+
36+
from pathlib import Path
37+
from typing import Any, Mapping
38+
39+
from .discovery import (
40+
CannotDiscoverVersion,
41+
get_version_from_env,
42+
get_version_from_git,
43+
get_version_from_pkginfo,
44+
)
45+
46+
47+
class MesonPythonMetadataProvider:
48+
"""
49+
Metadata provider for meson-python.
50+
51+
This class follows the meson-python metadata provider interface.
52+
"""
53+
54+
def __call__(self, source_dir: Path) -> Mapping[str, Any]:
55+
"""
56+
Return package metadata including version.
57+
58+
Parameters
59+
----------
60+
source_dir : Path
61+
Path to the source directory.
62+
63+
Returns
64+
-------
65+
dict
66+
Dictionary with 'version' key.
67+
"""
68+
version = None
69+
70+
# Check environment variable override first (highest priority)
71+
version = get_version_from_env()
72+
73+
# Try PKG-INFO (for sdist builds)
74+
if version is None:
75+
try:
76+
import os
77+
old_cwd = os.getcwd()
78+
os.chdir(source_dir)
79+
try:
80+
version = get_version_from_pkginfo()
81+
finally:
82+
os.chdir(old_cwd)
83+
except CannotDiscoverVersion:
84+
pass
85+
86+
# Try git
87+
if version is None:
88+
try:
89+
version = get_version_from_git(source_dir)
90+
except CannotDiscoverVersion:
91+
pass
92+
93+
if version is None:
94+
raise CannotDiscoverVersion(
95+
"Could not discover version from environment, PKG-INFO, or git"
96+
)
97+
98+
return {"version": version}
99+
100+
101+
# Module-level callable for meson-python
102+
__call__ = MesonPythonMetadataProvider()

DiscoverVersion/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@
2222
# SOFTWARE.
2323
#
2424

25-
from .Discovery import get_version
25+
from .discovery import get_version
2626

2727
__version__ = get_version('DiscoverVersion', __file__)

README.md

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,37 @@ tries the following options to get the version of the package (in order):
1111
It is intended as a lightweight replacement for
1212
`setuptools_scm`.
1313

14-
## Usage
14+
## Usage with meson-python
1515

16-
To use automatic version discovery with your build system of choice
17-
, here [`flit`](https://flit.pypa.io/), add the following lines to your
16+
For projects using the `meson-python` build backend, add the following to your
1817
`pyproject.toml`:
1918

19+
```toml
20+
[build-system]
21+
requires = ["meson>=1.0.0", "meson-python>=0.13.0", "ninja", "DiscoverVersion>=0.4.0"]
22+
build-backend = "mesonpy"
23+
24+
[project]
25+
dynamic = ["version"]
26+
dependencies = ["DiscoverVersion"]
27+
28+
[tool.meson-python.metadata]
29+
version.provider = "DiscoverVersion.meson_python"
30+
```
31+
32+
Then add the following to your toplevel `__init__.py` for runtime version access:
33+
34+
```python
35+
from DiscoverVersion import get_version
36+
37+
__version__ = get_version('my_package_name', __file__)
38+
```
39+
40+
## Usage with flit
41+
42+
For projects using the [`flit`](https://flit.pypa.io/) build backend, add the
43+
following lines to your `pyproject.toml`:
44+
2045
```toml
2146
[build-system]
2247
requires = ["flit_core>=3.2", "DiscoverVersion"]
@@ -29,14 +54,15 @@ dependencies = ['DiscoverVersion']
2954

3055
Then add the following to your toplevel `__init__.py`:
3156

32-
```python3
57+
```python
3358
from DiscoverVersion import get_version
3459

35-
__version__ = get_version('my_package_name')
60+
__version__ = get_version('my_package_name', __file__)
3661
```
3762

3863
Note that it is important to hard code the name of your package in the call
39-
to `get_version`.
64+
to `get_version`. The `__file__` argument is required for git-based version
65+
discovery to locate the repository.
4066

4167
## Environment Variable Override
4268

@@ -93,7 +119,10 @@ Here's an example of using DiscoverVersion in a GitHub Actions workflow with
93119
## Tests
94120
95121
Before being able to run tests, you need to execute
96-
```python
97-
pip install -e .[test]
122+
```bash
123+
pip install -e .[test]
98124
```
99-
to editably install the code.
125+
to editably install the code. Then run tests with:
126+
```bash
127+
pytest
128+
```

pyproject.toml

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,33 @@ license = { file = "LICENSE.md" }
1010
authors = [
1111
{ name = "Lars Pastewka", email = "lars.pastewka@imtek.uni-freiburg.de" }
1212
]
13+
keywords = [
14+
"version",
15+
"versioning",
16+
"git",
17+
"setuptools_scm",
18+
"setuptools-scm",
19+
"scm",
20+
"meson",
21+
"meson-python",
22+
"flit",
23+
"packaging",
24+
"build"
25+
]
1326
classifiers = [
14-
"Development Status :: 2 - Pre-Alpha",
15-
"Programming Language :: Python"
27+
"Development Status :: 4 - Beta",
28+
"Intended Audience :: Developers",
29+
"License :: OSI Approved :: MIT License",
30+
"Programming Language :: Python",
31+
"Programming Language :: Python :: 3",
32+
"Programming Language :: Python :: 3.8",
33+
"Programming Language :: Python :: 3.9",
34+
"Programming Language :: Python :: 3.10",
35+
"Programming Language :: Python :: 3.11",
36+
"Programming Language :: Python :: 3.12",
37+
"Programming Language :: Python :: 3.13",
38+
"Topic :: Software Development :: Build Tools",
39+
"Topic :: Software Development :: Version Control :: Git"
1640
]
1741
requires-python = ">=3.8.0"
1842
dynamic = [ "version" ]

test/test_version_discovery.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#
2424

2525
"""
26-
Tests the hello world function
26+
Tests version discovery functionality.
2727
"""
2828

2929
import os

0 commit comments

Comments
 (0)