|
23 | 23 | """ |
24 | 24 |
|
25 | 25 | from ...scanossbase import ScanossBase |
26 | | - |
27 | | -DEFAULT_COPYLEFT_LICENSES = { |
28 | | - 'agpl-3.0-only', |
29 | | - 'artistic-1.0', |
30 | | - 'artistic-2.0', |
31 | | - 'cc-by-sa-4.0', |
32 | | - 'cddl-1.0', |
33 | | - 'cddl-1.1', |
34 | | - 'cecill-2.1', |
35 | | - 'epl-1.0', |
36 | | - 'epl-2.0', |
37 | | - 'gfdl-1.1-only', |
38 | | - 'gfdl-1.2-only', |
39 | | - 'gfdl-1.3-only', |
40 | | - 'gpl-1.0-only', |
41 | | - 'gpl-2.0-only', |
42 | | - 'gpl-3.0-only', |
43 | | - 'lgpl-2.1-only', |
44 | | - 'lgpl-3.0-only', |
45 | | - 'mpl-1.1', |
46 | | - 'mpl-2.0', |
47 | | - 'sleepycat', |
48 | | - 'watcom-1.0', |
49 | | -} |
| 26 | +from scanoss.osadl_copyleft import OsadlCopyleft |
50 | 27 |
|
51 | 28 |
|
52 | 29 | class LicenseUtil(ScanossBase): |
53 | 30 | """ |
54 | 31 | A utility class for handling software licenses, particularly copyleft licenses. |
55 | 32 |
|
56 | | - This class provides functionality to initialize, manage, and query a set of |
57 | | - copyleft licenses. It also offers a method to generate URLs for license information. |
| 33 | + Uses OSADL (Open Source Automation Development Lab) authoritative copyleft data |
| 34 | + with optional include/exclude/explicit filters. |
58 | 35 | """ |
59 | 36 |
|
60 | 37 | BASE_SPDX_ORG_URL = 'https://spdx.org/licenses' |
61 | 38 |
|
62 | 39 | def __init__(self, debug: bool = False, trace: bool = True, quiet: bool = False): |
63 | 40 | super().__init__(debug, trace, quiet) |
64 | | - self.default_copyleft_licenses = set(DEFAULT_COPYLEFT_LICENSES) |
65 | | - self.copyleft_licenses = set() |
| 41 | + self.osadl = OsadlCopyleft(debug=debug) |
| 42 | + self.include_licenses = set() |
| 43 | + self.exclude_licenses = set() |
| 44 | + self.explicit_licenses = set() |
66 | 45 |
|
67 | 46 | def init(self, include: str = None, exclude: str = None, explicit: str = None): |
68 | 47 | """ |
69 | | - Initialize the set of copyleft licenses based on user input. |
70 | | -
|
71 | | - This method allows for customization of the copyleft license set by: |
72 | | - - Setting an explicit list of licenses |
73 | | - - Including additional licenses to the default set |
74 | | - - Excluding specific licenses from the default set |
| 48 | + Initialize copyleft license filters. |
75 | 49 |
|
76 | | - :param include: Comma-separated string of licenses to include |
77 | | - :param exclude: Comma-separated string of licenses to exclude |
78 | | - :param explicit: Comma-separated string of licenses to use exclusively |
| 50 | + :param include: Comma-separated licenses to mark as copyleft (in addition to OSADL) |
| 51 | + :param exclude: Comma-separated licenses to mark as NOT copyleft (override OSADL) |
| 52 | + :param explicit: Comma-separated licenses to use exclusively (ignore OSADL) |
79 | 53 | """ |
80 | | - if self.debug: |
81 | | - self.print_stderr(f'Include Copyleft licenses: ${include}') |
82 | | - self.print_stderr(f'Exclude Copyleft licenses: ${exclude}') |
83 | | - self.print_stderr(f'Explicit Copyleft licenses: ${explicit}') |
| 54 | + # Parse explicit list (if provided, ignore OSADL completely) |
84 | 55 | if explicit: |
85 | | - explicit = explicit.strip() |
86 | | - if explicit: |
87 | | - exp = [item.strip().lower() for item in explicit.split(',')] |
88 | | - self.copyleft_licenses = set(exp) |
89 | | - self.print_debug(f'Copyleft licenses: ${self.copyleft_licenses}') |
| 56 | + self.explicit_licenses = {lic.strip().lower() for lic in explicit.split(',') if lic.strip()} |
| 57 | + self.print_debug(f'Explicit copyleft licenses: {self.explicit_licenses}') |
90 | 58 | return |
91 | | - # If no explicit licenses were set, set default ones |
92 | | - self.copyleft_licenses = self.default_copyleft_licenses.copy() |
93 | | - if include: |
94 | | - include = include.strip() |
| 59 | + |
| 60 | + # Parse include list (mark these as copyleft in addition to OSADL) |
95 | 61 | if include: |
96 | | - inc = [item.strip().lower() for item in include.split(',')] |
97 | | - self.copyleft_licenses.update(inc) |
98 | | - if exclude: |
99 | | - exclude = exclude.strip() |
| 62 | + self.include_licenses = {lic.strip().lower() for lic in include.split(',') if lic.strip()} |
| 63 | + self.print_debug(f'Include licenses: {self.include_licenses}') |
| 64 | + |
| 65 | + # Parse exclude list (mark these as NOT copyleft, overriding OSADL) |
100 | 66 | if exclude: |
101 | | - inc = [item.strip().lower() for item in exclude.split(',')] |
102 | | - for lic in inc: |
103 | | - self.copyleft_licenses.discard(lic) |
104 | | - self.print_debug(f'Copyleft licenses: ${self.copyleft_licenses}') |
| 67 | + self.exclude_licenses = {lic.strip().lower() for lic in exclude.split(',') if lic.strip()} |
| 68 | + self.print_debug(f'Exclude licenses: {self.exclude_licenses}') |
105 | 69 |
|
106 | 70 | def is_copyleft(self, spdxid: str) -> bool: |
107 | 71 | """ |
108 | | - Check if a given license is considered copyleft. |
| 72 | + Check if a license is copyleft. |
109 | 73 |
|
110 | | - :param spdxid: The SPDX identifier of the license to check |
111 | | - :return: True if the license is copyleft, False otherwise |
| 74 | + Logic: |
| 75 | + 1. If explicit list provided → check if license in explicit list |
| 76 | + 2. If license in include list → return True |
| 77 | + 3. If license in exclude list → return False |
| 78 | + 4. Otherwise → use OSADL authoritative data |
| 79 | +
|
| 80 | + :param spdxid: SPDX license identifier |
| 81 | + :return: True if copyleft, False otherwise |
112 | 82 | """ |
113 | | - return spdxid.lower() in self.copyleft_licenses |
| 83 | + if not spdxid: |
| 84 | + return False |
| 85 | + |
| 86 | + spdxid_lc = spdxid.lower() |
| 87 | + |
| 88 | + # Explicit mode: use only the explicit list |
| 89 | + if self.explicit_licenses: |
| 90 | + return spdxid_lc in self.explicit_licenses |
| 91 | + |
| 92 | + # Include filter: if license in include list, force copyleft=True |
| 93 | + if spdxid_lc in self.include_licenses: |
| 94 | + return True |
| 95 | + |
| 96 | + # Exclude filter: if license in exclude list, force copyleft=False |
| 97 | + if spdxid_lc in self.exclude_licenses: |
| 98 | + return False |
| 99 | + |
| 100 | + # No filters matched, use OSADL authoritative data |
| 101 | + return self.osadl.is_copyleft(spdxid) |
114 | 102 |
|
115 | 103 | def get_spdx_url(self, spdxid: str) -> str: |
116 | 104 | """ |
|
0 commit comments