Skip to content

Commit 1593349

Browse files
committed
ENH: add support for licence and license-files dynamic fields
Fixes #270.
1 parent 16ec540 commit 1593349

File tree

6 files changed

+154
-13
lines changed

6 files changed

+154
-13
lines changed

docs/reference/meson-compatibility.rst

+8
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

+47-2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ def canonicalize_license_expression(s: str) -> str:
7676
MesonArgs = Mapping[MesonArgsKeys, List[str]]
7777

7878

79+
_PYPROJECT_METADATA_VERSION = tuple(map(int, pyproject_metadata.__version__.split('.')[:2]))
80+
_SUPPORTED_DYNAMIC_FIELDS = {'version', } if _PYPROJECT_METADATA_VERSION < (0, 9) else {'version', 'license', 'license-files'}
81+
7982
_NINJA_REQUIRED_VERSION = '1.8.2'
8083
_MESON_REQUIRED_VERSION = '0.63.3' # keep in sync with the version requirement in pyproject.toml
8184

@@ -257,7 +260,7 @@ def from_pyproject( # type: ignore[override]
257260
metadata = super().from_pyproject(data, project_dir, metadata_version)
258261

259262
# Check for unsupported dynamic fields.
260-
unsupported_dynamic = set(metadata.dynamic) - {'version', }
263+
unsupported_dynamic = set(metadata.dynamic) - _SUPPORTED_DYNAMIC_FIELDS # type: ignore[operator]
261264
if unsupported_dynamic:
262265
fields = ', '.join(f'"{x}"' for x in unsupported_dynamic)
263266
raise pyproject_metadata.ConfigurationError(f'Unsupported dynamic fields: {fields}')
@@ -728,13 +731,30 @@ def __init__(
728731
raise pyproject_metadata.ConfigurationError(
729732
'Field "version" declared as dynamic but version is not defined in meson.build')
730733
self._metadata.version = packaging.version.Version(version)
734+
if 'license' in self._metadata.dynamic:
735+
license = self._meson_license
736+
if license is None:
737+
raise pyproject_metadata.ConfigurationError(
738+
'Field "license" declared as dynamic but license is not specified in meson.build')
739+
# mypy is not happy when analyzing typing based on
740+
# pyproject-metadata < 0.9 where license needs to be of
741+
# License type. However, this code is not executed if
742+
# pyproject-metadata is older than 0.9 because then dynamic
743+
# license is not allowed.
744+
self._metadata.license = license # type: ignore[assignment, unused-ignore]
745+
if 'license-files' in self._metadata.dynamic:
746+
self._metadata.license_files = self._meson_license_files
731747
else:
732748
# if project section is missing, use minimal metdata from meson.build
733749
name, version = self._meson_name, self._meson_version
734750
if version is None:
735751
raise pyproject_metadata.ConfigurationError(
736752
'Section "project" missing in pyproject.toml and version is not defined in meson.build')
737-
self._metadata = Metadata(name=name, version=packaging.version.Version(version))
753+
kwargs = {
754+
'license': self._meson_license,
755+
'license_files': self._meson_license_files
756+
} if _PYPROJECT_METADATA_VERSION >= (0, 9) else {}
757+
self._metadata = Metadata(name=name, version=packaging.version.Version(version), **kwargs)
738758

739759
# verify that we are running on a supported interpreter
740760
if self._metadata.requires_python:
@@ -859,6 +879,31 @@ def _meson_version(self) -> Optional[str]:
859879
return None
860880
return value
861881

882+
@property
883+
def _meson_license(self) -> Optional[str]:
884+
"""The license specified with the ``license`` argument to ``project()`` in meson.build."""
885+
value = self._info('intro-projectinfo').get('license', None)
886+
if value is None:
887+
return None
888+
assert isinstance(value, list)
889+
if len(value) > 1:
890+
raise pyproject_metadata.ConfigurationError(
891+
'using a list of strings for the license declared in meson.build is ambiguous: use a SPDX license expression')
892+
value = value[0]
893+
assert isinstance(value, str)
894+
if value == 'unknown':
895+
return None
896+
return str(canonicalize_license_expression(value)) # str() is to make mypy happy
897+
898+
@property
899+
def _meson_license_files(self) -> Optional[List[pathlib.Path]]:
900+
"""The license files specified with the ``license_files`` argument to ``project()`` in meson.build."""
901+
value = self._info('intro-projectinfo').get('license_files', None)
902+
if not value:
903+
return None
904+
assert isinstance(value, list)
905+
return [pathlib.Path(x) for x in value]
906+
862907
def sdist(self, directory: Path) -> pathlib.Path:
863908
"""Generates a sdist (source distribution) in the specified directory."""
864909
# Generate meson dist file.

tests/conftest.py

+7
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

+89-1
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from .conftest import in_git_repo_context, metadata
1717

1818

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

@@ -27,7 +27,7 @@ def test_no_pep621(sdist_library):
2727
'''))
2828

2929

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

tests/test_wheel.py

+1-8
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)