[elbe-devel] [PATCH 6/8] elbepack: commands: parselicence: add licence extraction

Thomas Weißschuh thomas.weissschuh at linutronix.de
Fri Aug 30 09:10:13 CEST 2024


From: Eduard Krein <eduard.krein at linutronix.de>

Add a function which allows for the extraction of licenses from a licence file
for use outside of the parselicence command.

Signed-off-by: Eduard Krein <eduard.krein at linutronix.de>
---
 elbepack/commands/parselicence.py | 66 +++++++++++++++++++++++++++++++
 1 file changed, 66 insertions(+)

diff --git a/elbepack/commands/parselicence.py b/elbepack/commands/parselicence.py
index 4d4ed148b1f7..2e75ee6cc3ea 100644
--- a/elbepack/commands/parselicence.py
+++ b/elbepack/commands/parselicence.py
@@ -5,6 +5,7 @@
 import argparse
 import dataclasses
 import datetime
+import enum
 import io
 import os
 import subprocess
@@ -26,6 +27,19 @@ class Statistics:
         return ' '.join([f'{k}={v}' for k, v in dataclasses.asdict(self).items()])
 
 
+class LicenseType(enum.Enum):
+    SPDX = enum.auto()
+    SPDX_EXCEPTION = enum.auto()
+    UNKNOWN = enum.auto()
+
+
+ at dataclasses.dataclass
+class License:
+    type: LicenseType
+    name: str
+    text: str
+
+
 class license_dep5_to_spdx (dict):
     def __init__(self, xml_fname=None):
         super().__init__()
@@ -226,6 +240,58 @@ def _compute_statistics(licenses):
     return statistics
 
 
+def extract_licenses_from_report(licence_file, mapping_file):
+    extracted_licenses = {}
+    licenses = etree(licence_file)
+    mapping = license_dep5_to_spdx(mapping_file)
+    _apply_mapping(licenses, mapping)
+    for pkg in list(licenses.root):
+
+        pkg_name = pkg.et.attrib['name']
+
+        if pkg.has('spdx_licenses'):
+
+            licenses = []
+            for ll in pkg.node('spdx_licenses'):
+                if ll.et.text.find('UNKNOWN_MAPPING') != -1:
+                    license = License(name=ll.et.text.replace(
+                                      'UNKNOWN_MAPPING(', '').replace(')', ''),
+                                      type=LicenseType.UNKNOWN,
+                                      text=pkg.node('text').et.text)
+                elif ll.et.text.find(' AND ') != -1:
+                    temp = ll.et.text.split(' AND ')
+                    for i in temp:
+                        license = License(name=i,
+                                          type=LicenseType.SPDX,
+                                          text=None)
+                elif ll.et.text.find(' WITH ') != -1:
+                    license = License(name=ll.et.text,
+                                      type=LicenseType.SPDX_EXCEPTION,
+                                      text=None)
+                elif ll.et.text.find(' OR ') != -1:
+                    license = License(name=ll.et.text,
+                                      type=LicenseType.UNKNOWN,
+                                      text=pkg.node('text').et.text)
+                elif ll.et.text == 'Empty license':
+                    license = None
+                else:
+                    license = License(name=ll.et.text,
+                                      type=LicenseType.SPDX,
+                                      text=None)
+
+                licenses.append(license)
+
+            errors = []
+            if pkg.has('error'):
+                for error in pkg.all('error'):
+                    if error.et.text not in errors:
+                        errors.append(error.et.text)
+
+        extracted_licenses[pkg_name] = (licenses, errors)
+
+    return extracted_licenses
+
+
 def run_command(argv):
 
     aparser = argparse.ArgumentParser(prog='elbe parselicence')
-- 
2.46.0



More information about the elbe-devel mailing list