Skip to content

Commit 24674f7

Browse files
committed
ENH: add support for licence and license-files dynamic fields
Fixes mesonbuild#270.
1 parent ff6c4ab commit 24674f7

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.18.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
@@ -260,7 +262,7 @@ def from_pyproject(
260262
metadata = super().from_pyproject(data, project_dir, metadata_version)
261263

262264
# Check for unsupported dynamic fields.
263-
unsupported_dynamic = set(metadata.dynamic) - {'version', }
265+
unsupported_dynamic = set(metadata.dynamic) - _SUPPORTED_DYNAMIC_FIELDS
264266
if unsupported_dynamic:
265267
fields = ', '.join(f'"{x}"' for x in unsupported_dynamic)
266268
raise pyproject_metadata.ConfigurationError(f'Unsupported dynamic fields: {fields}')
@@ -731,13 +733,30 @@ def __init__(
731733
raise pyproject_metadata.ConfigurationError(
732734
'Field "version" declared as dynamic but version is not defined in meson.build')
733735
self._metadata.version = packaging.version.Version(version)
736+
if 'license' in self._metadata.dynamic:
737+
license = self._meson_license
738+
if license is None:
739+
raise pyproject_metadata.ConfigurationError(
740+
'Field "license" declared as dynamic but license is not specified in meson.build')
741+
# mypy is not happy when analyzing typing based on
742+
# pyproject-metadata < 0.9 where license needs to be of
743+
# License type. However, this code is not executed if
744+
# pyproject-metadata is older than 0.9 because then dynamic
745+
# license is not allowed.
746+
self._metadata.license = license # type: ignore[assignment]
747+
if 'license-files' in self._metadata.dynamic:
748+
self._metadata.license_files = self._meson_license_files
734749
else:
735750
# if project section is missing, use minimal metdata from meson.build
736751
name, version = self._meson_name, self._meson_version
737752
if version is None:
738753
raise pyproject_metadata.ConfigurationError(
739754
'Section "project" missing in pyproject.toml and version is not defined in meson.build')
740-
self._metadata = Metadata(name=name, version=packaging.version.Version(version))
755+
kwargs = {
756+
'license': self._meson_license,
757+
'license_files': self._meson_license_files
758+
} if _PYPROJECT_METADATA_VERSION >= (0, 9) else {}
759+
self._metadata = Metadata(name=name, version=packaging.version.Version(version), **kwargs)
741760

742761
# verify that we are running on a supported interpreter
743762
if self._metadata.requires_python:
@@ -862,6 +881,31 @@ def _meson_version(self) -> Optional[str]:
862881
return None
863882
return value
864883

884+
@property
885+
def _meson_license(self) -> Optional[str]:
886+
"""The license specified with the ``license`` argument to ``project()`` in meson.build."""
887+
value = self._info('intro-projectinfo').get('license', None)
888+
if value is None:
889+
return None
890+
assert isinstance(value, list)
891+
if len(value) > 1:
892+
raise pyproject_metadata.ConfigurationError(
893+
'using a list of strings for the license declared in meson.build is ambiguous: use a SPDX license expression')
894+
value = value[0]
895+
assert isinstance(value, str)
896+
if value == 'unknown':
897+
return None
898+
return str(canonicalize_license_expression(value)) # str() is to make mypy happy
899+
900+
@property
901+
def _meson_license_files(self) -> Optional[List[pathlib.Path]]:
902+
"""The license files specified with the ``license_files`` argument to ``project()`` in meson.build."""
903+
value = self._info('intro-projectinfo').get('license_files', None)
904+
if not value:
905+
return None
906+
assert isinstance(value, list)
907+
return [pathlib.Path(x) for x in value]
908+
865909
def sdist(self, directory: Path) -> pathlib.Path:
866910
"""Generates a sdist (source distribution) in the specified directory."""
867911
# 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
@@ -17,7 +17,7 @@
1717
from .conftest import in_git_repo_context, metadata
1818

1919

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

@@ -28,7 +28,7 @@ def test_no_pep621(sdist_library):
2828
'''))
2929

3030

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

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)