Skip to content

Commit d8b5f29

Browse files
committed
ENH: add support for licence and license-files dynamic fields
Fixes #270.
1 parent 010b92c commit d8b5f29

File tree

6 files changed

+153
-13
lines changed

6 files changed

+153
-13
lines changed

docs/reference/meson-compatibility.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ versions.
3939
Meson 1.3.0 or later is required for compiling extension modules
4040
targeting the Python limited API.
4141

42+
.. option:: 1.6.0
43+
44+
Meson 1.6.0 or later is required to support ``license`` and
45+
``license-files`` dynamic fields in ``pyproject.toml`` and to
46+
populate the package license and license files from the ones
47+
declared via the ``project()`` call in ``meson.build``. This also
48+
requires ``pyproject-metadata`` version 0.9.0 or later.
49+
4250
Build front-ends by default build packages in an isolated Python
4351
environment where build dependencies are installed. Most often, unless
4452
a package or its build dependencies declare explicitly a version

mesonpy/__init__.py

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ def canonicalize_license_expression(s: str) -> str:
7878

7979
__version__ = '0.17.0.dev0'
8080

81+
_PYPROJECT_METADATA_VERSION = tuple(map(int, pyproject_metadata.__version__.split('.')[:2]))
82+
_SUPPORTED_DYNAMIC_FIELDS = {'version', } if _PYPROJECT_METADATA_VERSION < (0, 9) else {'version', 'license', 'license-files'}
8183

8284
_NINJA_REQUIRED_VERSION = '1.8.2'
8385
_MESON_REQUIRED_VERSION = '0.63.3' # keep in sync with the version requirement in pyproject.toml
@@ -272,7 +274,7 @@ def from_pyproject(
272274
'Required "project.version" field is missing and not declared as dynamic')
273275

274276
# Check for unsupported dynamic fields.
275-
unsupported_dynamic = set(metadata.dynamic) - {'version', }
277+
unsupported_dynamic = set(metadata.dynamic) - _SUPPORTED_DYNAMIC_FIELDS
276278
if unsupported_dynamic:
277279
fields = ', '.join(f'"{x}"' for x in unsupported_dynamic)
278280
raise pyproject_metadata.ConfigurationError(f'Unsupported dynamic fields: {fields}')
@@ -754,13 +756,30 @@ def __init__(
754756
raise pyproject_metadata.ConfigurationError(
755757
'Field "version" declared as dynamic but version is not defined in meson.build')
756758
self._metadata.version = packaging.version.Version(version)
759+
if 'license' in self._metadata.dynamic:
760+
license = self._meson_license
761+
if license is None:
762+
raise pyproject_metadata.ConfigurationError(
763+
'Field "license" declared as dynamic but license is not specified in meson.build')
764+
# mypy is not happy when analyzing typing based on
765+
# pyproject-metadata < 0.9 where license needs to be of
766+
# License type. However, this code is not executed if
767+
# pyproject-metadata is older than 0.9 because then dynamic
768+
# license is not allowed.
769+
self._metadata.license = license # type: ignore[assignment]
770+
if 'license-files' in self._metadata.dynamic:
771+
self._metadata.license_files = self._meson_license_files
757772
else:
758773
# if project section is missing, use minimal metdata from meson.build
759774
name, version = self._meson_name, self._meson_version
760775
if version is None:
761776
raise pyproject_metadata.ConfigurationError(
762777
'Section "project" missing in pyproject.toml and version is not defined in meson.build')
763-
self._metadata = Metadata(name=name, version=packaging.version.Version(version))
778+
kwargs = {
779+
'license': self._meson_license,
780+
'license_files': self._meson_license_files
781+
} if _PYPROJECT_METADATA_VERSION >= (0, 9) else {}
782+
self._metadata = Metadata(name=name, version=packaging.version.Version(version), **kwargs)
764783

765784
# verify that we are running on a supported interpreter
766785
if self._metadata.requires_python:
@@ -885,6 +904,31 @@ def _meson_version(self) -> Optional[str]:
885904
return None
886905
return value
887906

907+
@property
908+
def _meson_license(self) -> Optional[str]:
909+
"""The license specified with the ``license`` argument to ``project()`` in meson.build."""
910+
value = self._info('intro-projectinfo').get('license', None)
911+
if value is None:
912+
return None
913+
assert isinstance(value, list)
914+
if len(value) > 1:
915+
raise pyproject_metadata.ConfigurationError(
916+
'using a list of strings for the license declared in meson.build is ambiguous: use a SPDX license expression')
917+
value = value[0]
918+
assert isinstance(value, str)
919+
if value == 'unknown':
920+
return None
921+
return str(canonicalize_license_expression(value)) # str() is to make mypy happy
922+
923+
@property
924+
def _meson_license_files(self) -> Optional[List[pathlib.Path]]:
925+
"""The license files specified with the ``license_files`` argument to ``project()`` in meson.build."""
926+
value = self._info('intro-projectinfo').get('license_files', None)
927+
if not value:
928+
return None
929+
assert isinstance(value, list)
930+
return [pathlib.Path(x) for x in value]
931+
888932
def sdist(self, directory: Path) -> pathlib.Path:
889933
"""Generates a sdist (source distribution) in the specified directory."""
890934
# Generate meson dist file.

tests/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,20 @@
1818

1919
import packaging.metadata
2020
import packaging.version
21+
import pyproject_metadata
2122
import pytest
2223

2324
import mesonpy
2425

2526
from mesonpy._util import chdir
2627

2728

29+
PYPROJECT_METADATA_VERSION = tuple(map(int, pyproject_metadata.__version__.split('.')[:2]))
30+
31+
_meson_ver_str = subprocess.run(['meson', '--version'], check=True, stdout=subprocess.PIPE, text=True).stdout
32+
MESON_VERSION = tuple(map(int, _meson_ver_str.split('.')[:3]))
33+
34+
2835
def metadata(data):
2936
meta, other = packaging.metadata.parse_email(data)
3037
# PEP-639 support requires packaging >= 24.1. Add minimal

tests/test_project.py

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import mesonpy
2121

22-
from .conftest import in_git_repo_context, package_dir
22+
from .conftest import MESON_VERSION, PYPROJECT_METADATA_VERSION, in_git_repo_context, metadata, package_dir
2323

2424

2525
def test_unsupported_python_version(package_unsupported_python_version):
@@ -40,6 +40,94 @@ def test_missing_dynamic_version(package_missing_dynamic_version):
4040
pass
4141

4242

43+
@pytest.mark.skipif(PYPROJECT_METADATA_VERSION < (0, 9), reason='pyproject-metadata too old')
44+
@pytest.mark.skipif(MESON_VERSION < (1, 6, 0), reason='meson too old')
45+
@pytest.mark.filterwarnings('ignore:canonicalization and validation of license expression')
46+
def test_meson_build_metadata(tmp_path):
47+
tmp_path.joinpath('pyproject.toml').write_text(textwrap.dedent('''
48+
[build-system]
49+
build-backend = 'mesonpy'
50+
requires = ['meson-python']
51+
'''), encoding='utf8')
52+
53+
tmp_path.joinpath('meson.build').write_text(textwrap.dedent('''
54+
project('test', version: '1.2.3', license: 'MIT', license_files: 'LICENSE')
55+
'''), encoding='utf8')
56+
57+
tmp_path.joinpath('LICENSE').write_text('')
58+
59+
p = mesonpy.Project(tmp_path, tmp_path / 'build')
60+
61+
assert metadata(bytes(p._metadata.as_rfc822())) == metadata(textwrap.dedent('''\
62+
Metadata-Version: 2.4
63+
Name: test
64+
Version: 1.2.3
65+
License-Expression: MIT
66+
License-File: LICENSE
67+
'''))
68+
69+
70+
@pytest.mark.skipif(PYPROJECT_METADATA_VERSION < (0, 9), reason='pyproject-metadata too old')
71+
@pytest.mark.skipif(MESON_VERSION < (1, 6, 0), reason='meson too old')
72+
@pytest.mark.filterwarnings('ignore:canonicalization and validation of license expression')
73+
def test_dynamic_license(tmp_path):
74+
tmp_path.joinpath('pyproject.toml').write_text(textwrap.dedent('''
75+
[build-system]
76+
build-backend = 'mesonpy'
77+
requires = ['meson-python']
78+
79+
[project]
80+
name = 'test'
81+
version = '1.0.0'
82+
dynamic = ['license']
83+
'''), encoding='utf8')
84+
85+
tmp_path.joinpath('meson.build').write_text(textwrap.dedent('''
86+
project('test', license: 'MIT')
87+
'''), encoding='utf8')
88+
89+
p = mesonpy.Project(tmp_path, tmp_path / 'build')
90+
91+
assert metadata(bytes(p._metadata.as_rfc822())) == metadata(textwrap.dedent('''\
92+
Metadata-Version: 2.4
93+
Name: test
94+
Version: 1.0.0
95+
License-Expression: MIT
96+
'''))
97+
98+
99+
@pytest.mark.skipif(PYPROJECT_METADATA_VERSION < (0, 9), reason='pyproject-metadata too old')
100+
@pytest.mark.skipif(MESON_VERSION < (1, 6, 0), reason='meson too old')
101+
@pytest.mark.filterwarnings('ignore:canonicalization and validation of license expression')
102+
def test_dynamic_license_files(tmp_path):
103+
tmp_path.joinpath('pyproject.toml').write_text(textwrap.dedent('''
104+
[build-system]
105+
build-backend = 'mesonpy'
106+
requires = ['meson-python']
107+
108+
[project]
109+
name = 'test'
110+
version = '1.0.0'
111+
dynamic = ['license', 'license-files']
112+
'''), encoding='utf8')
113+
114+
tmp_path.joinpath('meson.build').write_text(textwrap.dedent('''
115+
project('test', license: 'MIT', license_files: ['LICENSE'])
116+
'''), encoding='utf8')
117+
118+
tmp_path.joinpath('LICENSE').write_text('')
119+
120+
p = mesonpy.Project(tmp_path, tmp_path / 'build')
121+
122+
assert metadata(bytes(p._metadata.as_rfc822())) == metadata(textwrap.dedent('''\
123+
Metadata-Version: 2.4
124+
Name: test
125+
Version: 1.0.0
126+
License-Expression: MIT
127+
License-File: LICENSE
128+
'''))
129+
130+
43131
def test_user_args(package_user_args, tmp_path, monkeypatch):
44132
project_run = mesonpy.Project._run
45133
cmds = []

tests/test_sdist.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from .conftest import in_git_repo_context, metadata
2020

2121

22-
def test_no_pep621(sdist_library):
22+
def test_meson_build_metadata(sdist_library):
2323
with tarfile.open(sdist_library, 'r:gz') as sdist:
2424
sdist_pkg_info = sdist.extractfile('library-1.0.0/PKG-INFO').read()
2525

@@ -30,7 +30,7 @@ def test_no_pep621(sdist_library):
3030
'''))
3131

3232

33-
def test_pep621(sdist_full_metadata):
33+
def test_pep621_metadata(sdist_full_metadata):
3434
with tarfile.open(sdist_full_metadata, 'r:gz') as sdist:
3535
sdist_pkg_info = sdist.extractfile('full_metadata-1.2.3/PKG-INFO').read()
3636

tests/test_wheel.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,19 @@
66
import re
77
import shutil
88
import stat
9-
import subprocess
109
import sys
1110
import sysconfig
1211
import textwrap
1312

1413
import packaging.tags
15-
import pyproject_metadata
1614
import pytest
1715
import wheel.wheelfile
1816

1917
import mesonpy
2018

21-
from .conftest import adjust_packaging_platform_tag, metadata
19+
from .conftest import MESON_VERSION, PYPROJECT_METADATA_VERSION, adjust_packaging_platform_tag, metadata
2220

2321

24-
PYPROJECT_METADATA_VERSION = tuple(map(int, pyproject_metadata.__version__.split('.')[:2]))
25-
26-
_meson_ver_str = subprocess.run(['meson', '--version'], check=True, stdout=subprocess.PIPE, text=True).stdout
27-
MESON_VERSION = tuple(map(int, _meson_ver_str.split('.')[:3]))
28-
2922
EXT_SUFFIX = sysconfig.get_config_var('EXT_SUFFIX')
3023
if sys.version_info <= (3, 8, 7):
3124
if MESON_VERSION >= (0, 99):

0 commit comments

Comments
 (0)