Skip to content

Commit 3c92604

Browse files
authored
Merge pull request #809 from yangfan-yf-yf/fix/spdx-with-exceptions
Support SPDX WITH expressions
2 parents 1da96f9 + 33a7a39 commit 3c92604

5 files changed

Lines changed: 123 additions & 13 deletions

File tree

‎doc/pyproject_toml.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Version constraints:
3030
- For now, all packages should specify ``<5``, so they won't be impacted by
3131
changes in the next major version.
3232
- ``import-names`` and ``import-namespaces`` require ``flit_core >=4``.
33+
- License expressions using ``WITH`` require ``flit_core >=4.1``.
3334
- ``license-files`` and license expressions in the ``license`` field require
3435
``flit_core >=3.11``.
3536
- :ref:`pyproject_toml_project` requires ``flit_core >=3.2``.

‎flit_core/flit_core/_spdx_data.py‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,3 +649,80 @@
649649
'zpl-1.1': {'id': 'ZPL-1.1'},
650650
'zpl-2.0': {'id': 'ZPL-2.0'},
651651
'zpl-2.1': {'id': 'ZPL-2.1'}}
652+
exceptions = \
653+
{'389-exception': {'id': '389-exception'},
654+
'asterisk-exception': {'id': 'Asterisk-exception'},
655+
'asterisk-linking-protocols-exception': {'id': 'Asterisk-linking-protocols-exception'},
656+
'autoconf-exception-2.0': {'id': 'Autoconf-exception-2.0'},
657+
'autoconf-exception-3.0': {'id': 'Autoconf-exception-3.0'},
658+
'autoconf-exception-generic': {'id': 'Autoconf-exception-generic'},
659+
'autoconf-exception-generic-3.0': {'id': 'Autoconf-exception-generic-3.0'},
660+
'autoconf-exception-macro': {'id': 'Autoconf-exception-macro'},
661+
'bison-exception-1.24': {'id': 'Bison-exception-1.24'},
662+
'bison-exception-2.2': {'id': 'Bison-exception-2.2'},
663+
'bootloader-exception': {'id': 'Bootloader-exception'},
664+
'cgal-linking-exception': {'id': 'CGAL-linking-exception'},
665+
'classpath-exception-2.0': {'id': 'Classpath-exception-2.0'},
666+
'clisp-exception-2.0': {'id': 'CLISP-exception-2.0'},
667+
'cryptsetup-openssl-exception': {'id': 'cryptsetup-OpenSSL-exception'},
668+
'digirule-foss-exception': {'id': 'DigiRule-FOSS-exception'},
669+
'ecos-exception-2.0': {'id': 'eCos-exception-2.0'},
670+
'erlang-otp-linking-exception': {'id': 'erlang-otp-linking-exception'},
671+
'fawkes-runtime-exception': {'id': 'Fawkes-Runtime-exception'},
672+
'fltk-exception': {'id': 'FLTK-exception'},
673+
'fmt-exception': {'id': 'fmt-exception'},
674+
'font-exception-2.0': {'id': 'Font-exception-2.0'},
675+
'freertos-exception-2.0': {'id': 'freertos-exception-2.0'},
676+
'gcc-exception-2.0': {'id': 'GCC-exception-2.0'},
677+
'gcc-exception-2.0-note': {'id': 'GCC-exception-2.0-note'},
678+
'gcc-exception-3.1': {'id': 'GCC-exception-3.1'},
679+
'gmsh-exception': {'id': 'Gmsh-exception'},
680+
'gnat-exception': {'id': 'GNAT-exception'},
681+
'gnome-examples-exception': {'id': 'GNOME-examples-exception'},
682+
'gnu-compiler-exception': {'id': 'GNU-compiler-exception'},
683+
'gnu-javamail-exception': {'id': 'gnu-javamail-exception'},
684+
'gpl-3.0-389-ds-base-exception': {'id': 'GPL-3.0-389-ds-base-exception'},
685+
'gpl-3.0-interface-exception': {'id': 'GPL-3.0-interface-exception'},
686+
'gpl-3.0-linking-exception': {'id': 'GPL-3.0-linking-exception'},
687+
'gpl-3.0-linking-source-exception': {'id': 'GPL-3.0-linking-source-exception'},
688+
'gpl-cc-1.0': {'id': 'GPL-CC-1.0'},
689+
'gstreamer-exception-2005': {'id': 'GStreamer-exception-2005'},
690+
'gstreamer-exception-2008': {'id': 'GStreamer-exception-2008'},
691+
'harbour-exception': {'id': 'harbour-exception'},
692+
'i2p-gpl-java-exception': {'id': 'i2p-gpl-java-exception'},
693+
'independent-modules-exception': {'id': 'Independent-modules-exception'},
694+
'kicad-libraries-exception': {'id': 'KiCad-libraries-exception'},
695+
'lgpl-3.0-linking-exception': {'id': 'LGPL-3.0-linking-exception'},
696+
'libpri-openh323-exception': {'id': 'libpri-OpenH323-exception'},
697+
'libtool-exception': {'id': 'Libtool-exception'},
698+
'linux-syscall-note': {'id': 'Linux-syscall-note'},
699+
'llgpl': {'id': 'LLGPL'},
700+
'llvm-exception': {'id': 'LLVM-exception'},
701+
'lzma-exception': {'id': 'LZMA-exception'},
702+
'mif-exception': {'id': 'mif-exception'},
703+
'mxml-exception': {'id': 'mxml-exception'},
704+
'ocaml-lgpl-linking-exception': {'id': 'OCaml-LGPL-linking-exception'},
705+
'occt-exception-1.0': {'id': 'OCCT-exception-1.0'},
706+
'openjdk-assembly-exception-1.0': {'id': 'OpenJDK-assembly-exception-1.0'},
707+
'openvpn-openssl-exception': {'id': 'openvpn-openssl-exception'},
708+
'pcre2-exception': {'id': 'PCRE2-exception'},
709+
'ps-or-pdf-font-exception-20170817': {'id': 'PS-or-PDF-font-exception-20170817'},
710+
'qpl-1.0-inria-2004-exception': {'id': 'QPL-1.0-INRIA-2004-exception'},
711+
'qt-gpl-exception-1.0': {'id': 'Qt-GPL-exception-1.0'},
712+
'qt-lgpl-exception-1.1': {'id': 'Qt-LGPL-exception-1.1'},
713+
'qwt-exception-1.0': {'id': 'Qwt-exception-1.0'},
714+
'romic-exception': {'id': 'romic-exception'},
715+
'rrdtool-floss-exception-2.0': {'id': 'RRDtool-FLOSS-exception-2.0'},
716+
'sane-exception': {'id': 'SANE-exception'},
717+
'shl-2.0': {'id': 'SHL-2.0'},
718+
'shl-2.1': {'id': 'SHL-2.1'},
719+
'stunnel-exception': {'id': 'stunnel-exception'},
720+
'swi-exception': {'id': 'SWI-exception'},
721+
'swift-exception': {'id': 'Swift-exception'},
722+
'texinfo-exception': {'id': 'Texinfo-exception'},
723+
'u-boot-exception-2.0': {'id': 'u-boot-exception-2.0'},
724+
'ubdl-exception': {'id': 'UBDL-exception'},
725+
'universal-foss-exception-1.0': {'id': 'Universal-FOSS-exception-1.0'},
726+
'vsftpd-openssl-exception': {'id': 'vsftpd-openssl-exception'},
727+
'wxwindows-exception-3.1': {'id': 'WxWindows-exception-3.1'},
728+
'x11vnc-openssl-exception': {'id': 'x11vnc-openssl-exception'}}

‎flit_core/flit_core/config.py‎

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
except ImportError:
1818
import tomli as tomllib
1919

20-
from ._spdx_data import licenses
20+
from ._spdx_data import exceptions, licenses
2121
from .common import normalise_core_metadata_name
2222
from .versionno import normalise_version
2323

@@ -700,7 +700,7 @@ def normalise_compound_license_expr(s: str) -> str:
700700
"""Validate and normalise a compound SPDX license expression.
701701
702702
Per the specification, licence expression operators (AND, OR and WITH)
703-
are matched case-sensitively. The WITH operator is not currently supported.
703+
are matched case-sensitively.
704704
705705
Spec: https://spdx.github.io/spdx-spec/v2.2.2/SPDX-license-expressions/
706706
"""
@@ -712,9 +712,15 @@ def normalise_compound_license_expr(s: str) -> str:
712712
parts = []
713713
try:
714714
for part in filter(None, re.split(r' +|([()])', s)):
715-
if part.upper() == 'WITH':
716-
# provide a sensible error message for the WITH operator
717-
raise ConfigError("The SPDX 'WITH' operator is not yet supported!")
715+
if part == 'WITH':
716+
if (
717+
not parts
718+
or parts[-1] in {' AND ', ' OR ', ' WITH ', '('}
719+
or parts[-1].lower() in exceptions
720+
):
721+
reason = "a license ID is missing before 'WITH'"
722+
raise ConfigError(invalid_msg.format(s=s, reason=reason))
723+
parts.append(' WITH ')
718724
elif part in {'AND', 'OR'}:
719725
if not parts or parts[-1] in {' AND ', ' OR ', ' WITH ', '('}:
720726
reason = f"a license ID is missing before '{part}'"
@@ -740,10 +746,13 @@ def normalise_compound_license_expr(s: str) -> str:
740746
raise ConfigError(invalid_msg.format(s=s, reason=reason))
741747
parts.append(part)
742748
else:
743-
if parts and parts[-1] not in {' AND ', ' OR ', '('}:
744-
reason = "a license ID must follow either AND, OR, or '('"
749+
if parts and parts[-1] not in {' AND ', ' OR ', ' WITH ', '('}:
750+
reason = "a license or exception ID must follow AND, OR, WITH, or '('"
745751
raise ConfigError(invalid_msg.format(s=s, reason=reason))
746-
simple_expr = normalise_simple_license_expr(part)
752+
if parts and parts[-1] == ' WITH ':
753+
simple_expr = normalise_license_exception(part)
754+
else:
755+
simple_expr = normalise_simple_license_expr(part)
747756
parts.append(simple_expr)
748757

749758
if stack != 0:
@@ -790,3 +799,11 @@ def normalise_simple_license_expr(s: str) -> str:
790799
if or_later:
791800
return f'{normalised_id}+'
792801
return normalised_id
802+
803+
804+
def normalise_license_exception(s: str) -> str:
805+
"""Normalise an SPDX license exception ID."""
806+
try:
807+
return exceptions[s.lower()]['id']
808+
except KeyError:
809+
raise ConfigError(f"{s!r} is not a recognised SPDX license exception ID")

‎flit_core/tests_core/test_config.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ def test_bad_pep621_readme(readme, err_match):
218218
("MIT AND Apache-2.0", "MIT AND Apache-2.0"),
219219
("MIT AND Apache-2.0+ OR 0BSD", "MIT AND Apache-2.0+ OR 0BSD"),
220220
("MIT AND (Apache-2.0+ OR (0BSD))", "MIT AND (Apache-2.0+ OR (0BSD))"),
221+
("mit WITH classpath-exception-2.0", "MIT WITH Classpath-exception-2.0"),
222+
("MIT OR MIT WITH Classpath-exception-2.0", "MIT OR MIT WITH Classpath-exception-2.0"),
221223
("MIT OR(mit)", "MIT OR (MIT)"),
222224
("(mit)AND mit", "(MIT) AND MIT"),
223225
("MIT OR (MIT OR ( MIT )) AND ((MIT) AND MIT) OR MIT", "MIT OR (MIT OR (MIT)) AND ((MIT) AND MIT) OR MIT"),
@@ -342,6 +344,7 @@ def test_license_expr_error_empty(invalid_expr: str):
342344
"MIT and MIT",
343345
"MIT AND MIT or MIT",
344346
"MIT AND (MIT or MIT)",
347+
"MIT with Classpath-exception-2.0",
345348
])
346349
def test_license_expr_error_lowercase(invalid_expr: str):
347350
proj = {
@@ -360,14 +363,18 @@ def test_license_expr_error_lowercase(invalid_expr: str):
360363
"MIT WITH MIT-Exception",
361364
"(MIT WITH MIT-Exception)",
362365
"MIT OR MIT WITH MIT-Exception",
366+
"MIT WITH Classpath-exception-2.0 WITH Autoconf-exception-2.0",
363367
"MIT WITH MIT-Exception OR (MIT AND MIT)",
364368
])
365-
def test_license_expr_error_unsupported_with(invalid_expr: str):
369+
def test_license_expr_error_with(invalid_expr: str):
366370
proj = {
367371
'name': 'module1', 'version': '1.0', 'description': 'x',
368372
'license': invalid_expr,
369373
}
370-
with pytest.raises(config.ConfigError, match="not yet supported"):
374+
with pytest.raises(
375+
config.ConfigError,
376+
match="(license ID is missing before|must be uppercase|not a recognised SPDX license exception ID)",
377+
):
371378
config.read_pep621_metadata(proj, samples_dir / 'pep621' / 'pyproject.toml')
372379

373380

‎prepare_license_list.py‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,23 @@
99
list_data_repo = Path(sys.argv[1])
1010
with (list_data_repo / 'json' / 'licenses.json').open('rb') as f:
1111
licenses_json = json.load(f)
12+
with (list_data_repo / 'json' / 'exceptions.json').open('rb') as f:
13+
exceptions_json = json.load(f)
1214

13-
condensed = {
15+
licenses = {
1416
l['licenseId'].lower(): {'id': l['licenseId']}
1517
for l in licenses_json['licenses']
1618
if not l['isDeprecatedLicenseId']
1719
}
20+
exceptions = {
21+
e['licenseExceptionId'].lower(): {'id': e['licenseExceptionId']}
22+
for e in exceptions_json['exceptions']
23+
if not e['isDeprecatedLicenseId']
24+
}
1825

1926
with Path('flit_core', 'flit_core', '_spdx_data.py').open('w') as f:
2027
f.write("# This file is generated from SPDX license data; don't edit it manually.\n\n")
2128

22-
f.write("licenses = \\\n")
23-
pprint.pprint(condensed, f)
29+
for name, data in (("licenses", licenses), ("exceptions", exceptions)):
30+
f.write(f"{name} = \\\n")
31+
pprint.pprint(data, f)

0 commit comments

Comments
 (0)