Skip to content

Commit d3fb723

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 ec8e3bf commit d3fb723

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
@@ -690,7 +691,7 @@ class Project():
690691
_ALLOWED_DYNAMIC_FIELDS: ClassVar[List[str]] = [
691692
'version',
692693
]
693-
_metadata: Optional[pyproject_metadata.StandardMetadata]
694+
_metadata: pyproject_metadata.StandardMetadata
694695

695696
def __init__(
696697
self,
@@ -745,20 +746,6 @@ def __init__(
745746
# load pyproject.toml
746747
pyproject = tomllib.loads(self._source_dir.joinpath('pyproject.toml').read_text())
747748

748-
# package metadata
749-
self._pep621 = 'project' in pyproject:
750-
if self.pep621:
751-
self._metadata = pyproject_metadata.StandardMetadata.from_pyproject(pyproject, self._source_dir)
752-
else:
753-
print(
754-
'{yellow}{bold}! Using Meson to generate the project metadata '
755-
'(no `project` section in pyproject.toml){reset}'.format(**_STYLES)
756-
)
757-
self._metadata = None
758-
759-
if self._metadata:
760-
self._validate_metadata()
761-
762749
# load meson args from pyproject.toml
763750
pyproject_config = _validate_pyproject_config(pyproject)
764751
for key, value in pyproject_config.get('args', {}).items():
@@ -792,9 +779,21 @@ def __init__(
792779
# run meson setup
793780
self._configure(reconfigure=reconfigure)
794781

795-
# set version if dynamic (this fetches it from Meson)
796-
if self._metadata and 'version' in self._metadata.dynamic:
797-
self._metadata.version = self.version
782+
# package metadata
783+
if 'project' in pyproject:
784+
self._metadata = pyproject_metadata.StandardMetadata.from_pyproject(pyproject, self._source_dir)
785+
else:
786+
self._metadata = pyproject_metadata.StandardMetadata(
787+
name=self._meson_name, version=packaging.version.Version(self._meson_version))
788+
print(
789+
'{yellow}{bold}! Using Meson to generate the project metadata '
790+
'(no `project` section in pyproject.toml){reset}'.format(**_STYLES)
791+
)
792+
self._validate_metadata()
793+
794+
# set version from meson.build if dynamic
795+
if 'version' in self._metadata.dynamic:
796+
self._metadata.version = packaging.version.Version(self._meson_version)
798797

799798
def _run(self, cmd: Sequence[str]) -> None:
800799
"""Invoke a subprocess."""
@@ -834,8 +833,6 @@ def _configure(self, reconfigure: bool = False) -> None:
834833
def _validate_metadata(self) -> None:
835834
"""Check the pyproject.toml metadata and see if there are any issues."""
836835

837-
assert self._metadata
838-
839836
# check for unsupported dynamic fields
840837
unsupported_dynamic = {
841838
key for key in self._metadata.dynamic
@@ -958,45 +955,18 @@ def _meson_version(self) -> str:
958955

959956
@property
960957
def name(self) -> str:
961-
"""Project name. Specified in pyproject.toml."""
962-
name = self._metadata.name if self._metadata else self._meson_name
963-
assert isinstance(name, str)
964-
return name.replace('-', '_')
958+
"""Project name."""
959+
return self._metadata.name.replace('-', '_')
965960

966961
@property
967962
def version(self) -> str:
968-
"""Project version. Either specified in pyproject.toml or meson.build."""
969-
if self._metadata and 'version' not in self._metadata.dynamic:
970-
version = str(self._metadata.version)
971-
else:
972-
version = self._meson_version
973-
assert isinstance(version, str)
974-
return version
963+
"""Project version."""
964+
return str(self._metadata.version)
975965

976966
@cached_property
977967
def metadata(self) -> bytes:
978-
"""Project metadata."""
979-
# the rest of the keys are only available when using PEP 621 metadata
980-
if not self.pep621:
981-
data = textwrap.dedent(f'''
982-
Metadata-Version: 2.1
983-
Name: {self.name}
984-
Version: {self.version}
985-
''').strip()
986-
return data.encode()
987-
988-
# re-import pyproject_metadata to raise ModuleNotFoundError if it is really missing
989-
import pyproject_metadata # noqa: F401
990-
assert self._metadata
991-
992-
core_metadata = self._metadata.as_rfc822()
993-
# use self.version as the version may be dynamic -- fetched from Meson
994-
#
995-
# we need to overwrite this field in the RFC822 field as
996-
# pyproject_metadata removes 'version' from the dynamic fields when
997-
# giving it a value via the dataclass
998-
core_metadata.headers['Version'] = [self.version]
999-
return bytes(core_metadata)
968+
"""Project metadata as an RFC822 message."""
969+
return bytes(self._metadata.as_rfc822())
1000970

1001971
@property
1002972
def license_file(self) -> Optional[pathlib.Path]:
@@ -1011,11 +981,6 @@ def is_pure(self) -> bool:
1011981
"""Is the wheel "pure" (architecture independent)?"""
1012982
return bool(self._wheel_builder.is_pure)
1013983

1014-
@property
1015-
def pep621(self) -> bool:
1016-
"""Does the project use PEP 621 metadata?"""
1017-
return self._pep621
1018-
1019984
def sdist(self, directory: Path) -> pathlib.Path:
1020985
"""Generates a sdist (source distribution) in the specified directory."""
1021986
# 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)