Added support for multi-licensed packages in library.json using SPDX Expressions // Resolve #4037

This commit is contained in:
Ivan Kravets committed 2022-04-02 14:19:24 +03:00
1 parent d86f7fc25e
commit feda42f18f
4 files changed
+37 -7

No files matched your search

+13 -3
View File
@@ -15,6 +15,7 @@
# pylint: disable=too-many-ancestors
import json
import re
import marshmallow
import requests
@@ -254,9 +255,18 @@ class ManifestSchema(BaseSchema):
spdx = self.load_spdx_licenses()
except requests.exceptions.RequestException:
raise ValidationError("Could not load SPDX licenses for validation")
for item in spdx.get("licenses", []):
if item.get("licenseId") == value:
return True
known_ids = set(item.get("licenseId") for item in spdx.get("licenses", []))
if value in known_ids:
return True
# parse license expression
# https://spdx.github.io/spdx-spec/SPDX-license-expressions/
package_ids = [
item.strip()
for item in re.sub(r"(\s+(?:OR|AND|WITH)\s+|[\(\)])", " ", value).split(" ")
if item.strip()
]
if known_ids >= set(package_ids):
return True
raise ValidationError(
"Invalid SPDX license identifier. See valid identifiers at "
"https://spdx.org/licenses/"