Skip to content

Commit 615f768

Browse files
committed
MAINT: simplify package metadata handling
Code can be simplified using pyproject_metadata.StandardMetadata also when we infer the package metadata from meson.build.
1 parent a100fca commit 615f768

File tree

3 files changed

+33
-63
lines changed

3 files changed

+33
-63
lines changed

mesonpy/__init__.py

Lines changed: 23 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
else:
4242
import tomllib
4343

44+
import packaging.version
4445
import pyproject_metadata
4546

4647
import mesonpy._compat
@@ -694,7 +695,7 @@ class Project():
694695
_ALLOWED_DYNAMIC_FIELDS: ClassVar[List[str]] = [
695696
'version',
696697
]
697-
_metadata: Optional[pyproject_metadata.StandardMetadata]
698+
_metadata: pyproject_metadata.StandardMetadata
698699

699700
def __init__( # noqa: C901
700701
self,
@@ -748,20 +749,6 @@ def __init__( # noqa: C901
748749
# load pyproject.toml
749750
pyproject = tomllib.loads(self._source_dir.joinpath('pyproject.toml').read_text())
750751

751-
# package metadata
752-
self._pep621 = 'project' in pyproject:
753-
if self.pep621:
754-
self._metadata = pyproject_metadata.StandardMetadata.from_pyproject(pyproject, self._source_dir)
755-
else:
756-
print(
757-
'{yellow}{bold}! Using Meson to generate the project metadata '
758-
'(no `project` section in pyproject.toml){reset}'.format(**_STYLES)
759-
)
760-
self._metadata = None
761-
762-
if self._metadata:
763-
self._validate_metadata()
764-
765752
# load meson args from pyproject.toml
766753
pyproject_config = _validate_pyproject_config(pyproject)
767754
for key, value in pyproject_config.get('args', {}).items():
@@ -802,9 +789,21 @@ def __init__( # noqa: C901
802789
has_valid_build_dir = self._build_dir.joinpath('meson-private', 'coredata.dat').is_file()
803790
self._configure(reconfigure=has_valid_build_dir and not native_file_mismatch)
804791

805-
# set version if dynamic (this fetches it from Meson)
806-
if self._metadata and 'version' in self._metadata.dynamic:
807-
self._metadata.version = self.version
792+
# package metadata
793+
if 'project' in pyproject:
794+
self._metadata = pyproject_metadata.StandardMetadata.from_pyproject(pyproject, self._source_dir)
795+
else:
796+
self._metadata = pyproject_metadata.StandardMetadata(
797+
name=self._meson_name, version=packaging.version.Version(self._meson_version))
798+
print(
799+
'{yellow}{bold}! Using Meson to generate the project metadata '
800+
'(no `project` section in pyproject.toml){reset}'.format(**_STYLES)
801+
)
802+
self._validate_metadata()
803+
804+
# set version from meson.build if dynamic
805+
if 'version' in self._metadata.dynamic:
806+
self._metadata.version = packaging.version.Version(self._meson_version)
808807

809808
def _proc(self, *args: str) -> None:
810809
"""Invoke a subprocess."""
@@ -858,8 +857,6 @@ def _configure(self, reconfigure: bool = False) -> None:
858857
def _validate_metadata(self) -> None:
859858
"""Check the pyproject.toml metadata and see if there are any issues."""
860859

861-
assert self._metadata
862-
863860
# check for unsupported dynamic fields
864861
unsupported_dynamic = {
865862
key for key in self._metadata.dynamic
@@ -991,45 +988,18 @@ def _meson_version(self) -> str:
991988

992989
@property
993990
def name(self) -> str:
994-
"""Project name. Specified in pyproject.toml."""
995-
name = self._metadata.name if self._metadata else self._meson_name
996-
assert isinstance(name, str)
997-
return name.replace('-', '_')
991+
"""Project name."""
992+
return str(self._metadata.name).replace('-', '_')
998993

999994
@property
1000995
def version(self) -> str:
1001-
"""Project version. Either specified in pyproject.toml or meson.build."""
1002-
if self._metadata and 'version' not in self._metadata.dynamic:
1003-
version = str(self._metadata.version)
1004-
else:
1005-
version = self._meson_version
1006-
assert isinstance(version, str)
1007-
return version
996+
"""Project version."""
997+
return str(self._metadata.version)
1008998

1009999
@cached_property
10101000
def metadata(self) -> bytes:
1011-
"""Project metadata."""
1012-
# the rest of the keys are only available when using PEP 621 metadata
1013-
if not self.pep621:
1014-
data = textwrap.dedent(f'''
1015-
Metadata-Version: 2.1
1016-
Name: {self.name}
1017-
Version: {self.version}
1018-
''').strip()
1019-
return data.encode()
1020-
1021-
# re-import pyproject_metadata to raise ModuleNotFoundError if it is really missing
1022-
import pyproject_metadata # noqa: F401
1023-
assert self._metadata
1024-
1025-
core_metadata = self._metadata.as_rfc822()
1026-
# use self.version as the version may be dynamic -- fetched from Meson
1027-
#
1028-
# we need to overwrite this field in the RFC822 field as
1029-
# pyproject_metadata removes 'version' from the dynamic fields when
1030-
# giving it a value via the dataclass
1031-
core_metadata.headers['Version'] = [self.version]
1032-
return bytes(core_metadata)
1001+
"""Project metadata as an RFC822 message."""
1002+
return bytes(self._metadata.as_rfc822())
10331003

10341004
@property
10351005
def license_file(self) -> Optional[pathlib.Path]:
@@ -1044,11 +1014,6 @@ def is_pure(self) -> bool:
10441014
"""Is the wheel "pure" (architecture independent)?"""
10451015
return bool(self._wheel_builder.is_pure)
10461016

1047-
@property
1048-
def pep621(self) -> bool:
1049-
"""Does the project use PEP 621 metadata?"""
1050-
return self._pep621
1051-
10521017
def sdist(self, directory: Path) -> pathlib.Path:
10531018
"""Generates a sdist (source distribution) in the specified directory."""
10541019
# generate meson dist file
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-FileCopyrightText: 2021 The meson-python developers
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
project('unsupported-dynamic', version: '1.0.0')

tests/test_metadata.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ def test_no_pep621(sdist_library):
1010
with tarfile.open(sdist_library, 'r:gz') as sdist:
1111
sdist_pkg_info = sdist.extractfile('library-1.0.0/PKG-INFO').read().decode()
1212

13-
assert sdist_pkg_info == textwrap.dedent('''
13+
assert sdist_pkg_info == textwrap.dedent('''\
1414
Metadata-Version: 2.1
1515
Name: library
1616
Version: 1.0.0
17-
''').strip()
17+
''')
1818

1919

2020
def test_pep621(sdist_full_metadata):
@@ -61,10 +61,10 @@ def test_pep621(sdist_full_metadata):
6161

6262
def test_dynamic_version(sdist_dynamic_version):
6363
with tarfile.open(sdist_dynamic_version, 'r:gz') as sdist:
64-
sdist_pkg_info = sdist.extractfile('dynamic_version-1.0.0/PKG-INFO').read().decode().strip()
64+
sdist_pkg_info = sdist.extractfile('dynamic_version-1.0.0/PKG-INFO').read().decode()
6565

66-
assert sdist_pkg_info == textwrap.dedent('''
66+
assert sdist_pkg_info == textwrap.dedent('''\
6767
Metadata-Version: 2.1
6868
Name: dynamic-version
6969
Version: 1.0.0
70-
''').strip()
70+
''')

0 commit comments

Comments
 (0)