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