@@ -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.
0 commit comments