Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
| 2 | # SPDX-License-Identifier: GPL-2.0-only |
| 3 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 4 | """Code for parsing OpenEmbedded license strings""" |
| 5 | |
| 6 | import ast |
| 7 | import re |
| 8 | from fnmatch import fnmatchcase as fnmatch |
| 9 | |
| 10 | def license_ok(license, dont_want_licenses): |
| 11 | """ Return False if License exist in dont_want_licenses else True """ |
| 12 | for dwl in dont_want_licenses: |
| 13 | # If you want to exclude license named generically 'X', we |
| 14 | # surely want to exclude 'X+' as well. In consequence, we |
| 15 | # will exclude a trailing '+' character from LICENSE in |
| 16 | # case INCOMPATIBLE_LICENSE is not a 'X+' license. |
| 17 | lic = license |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 18 | if not re.search(r'\+$', dwl): |
| 19 | lic = re.sub(r'\+', '', license) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 20 | if fnmatch(lic, dwl): |
| 21 | return False |
| 22 | return True |
| 23 | |
| 24 | class LicenseError(Exception): |
| 25 | pass |
| 26 | |
| 27 | class LicenseSyntaxError(LicenseError): |
| 28 | def __init__(self, licensestr, exc): |
| 29 | self.licensestr = licensestr |
| 30 | self.exc = exc |
| 31 | LicenseError.__init__(self) |
| 32 | |
| 33 | def __str__(self): |
| 34 | return "error in '%s': %s" % (self.licensestr, self.exc) |
| 35 | |
| 36 | class InvalidLicense(LicenseError): |
| 37 | def __init__(self, license): |
| 38 | self.license = license |
| 39 | LicenseError.__init__(self) |
| 40 | |
| 41 | def __str__(self): |
| 42 | return "invalid characters in license '%s'" % self.license |
| 43 | |
| 44 | license_operator_chars = '&|() ' |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 45 | license_operator = re.compile(r'([' + license_operator_chars + '])') |
| 46 | license_pattern = re.compile(r'[a-zA-Z0-9.+_\-]+$') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 47 | |
| 48 | class LicenseVisitor(ast.NodeVisitor): |
| 49 | """Get elements based on OpenEmbedded license strings""" |
| 50 | def get_elements(self, licensestr): |
| 51 | new_elements = [] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 52 | elements = list([x for x in license_operator.split(licensestr) if x.strip()]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 53 | for pos, element in enumerate(elements): |
| 54 | if license_pattern.match(element): |
| 55 | if pos > 0 and license_pattern.match(elements[pos-1]): |
| 56 | new_elements.append('&') |
| 57 | element = '"' + element + '"' |
| 58 | elif not license_operator.match(element): |
| 59 | raise InvalidLicense(element) |
| 60 | new_elements.append(element) |
| 61 | |
| 62 | return new_elements |
| 63 | |
| 64 | """Syntax tree visitor which can accept elements previously generated with |
| 65 | OpenEmbedded license string""" |
| 66 | def visit_elements(self, elements): |
| 67 | self.visit(ast.parse(' '.join(elements))) |
| 68 | |
| 69 | """Syntax tree visitor which can accept OpenEmbedded license strings""" |
| 70 | def visit_string(self, licensestr): |
| 71 | self.visit_elements(self.get_elements(licensestr)) |
| 72 | |
| 73 | class FlattenVisitor(LicenseVisitor): |
| 74 | """Flatten a license tree (parsed from a string) by selecting one of each |
| 75 | set of OR options, in the way the user specifies""" |
| 76 | def __init__(self, choose_licenses): |
| 77 | self.choose_licenses = choose_licenses |
| 78 | self.licenses = [] |
| 79 | LicenseVisitor.__init__(self) |
| 80 | |
| 81 | def visit_Str(self, node): |
| 82 | self.licenses.append(node.s) |
| 83 | |
| 84 | def visit_BinOp(self, node): |
| 85 | if isinstance(node.op, ast.BitOr): |
| 86 | left = FlattenVisitor(self.choose_licenses) |
| 87 | left.visit(node.left) |
| 88 | |
| 89 | right = FlattenVisitor(self.choose_licenses) |
| 90 | right.visit(node.right) |
| 91 | |
| 92 | selected = self.choose_licenses(left.licenses, right.licenses) |
| 93 | self.licenses.extend(selected) |
| 94 | else: |
| 95 | self.generic_visit(node) |
| 96 | |
| 97 | def flattened_licenses(licensestr, choose_licenses): |
| 98 | """Given a license string and choose_licenses function, return a flat list of licenses""" |
| 99 | flatten = FlattenVisitor(choose_licenses) |
| 100 | try: |
| 101 | flatten.visit_string(licensestr) |
| 102 | except SyntaxError as exc: |
| 103 | raise LicenseSyntaxError(licensestr, exc) |
| 104 | return flatten.licenses |
| 105 | |
| 106 | def is_included(licensestr, whitelist=None, blacklist=None): |
| 107 | """Given a license string and whitelist and blacklist, determine if the |
| 108 | license string matches the whitelist and does not match the blacklist. |
| 109 | |
| 110 | Returns a tuple holding the boolean state and a list of the applicable |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 111 | licenses that were excluded if state is False, or the licenses that were |
| 112 | included if the state is True. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 113 | """ |
| 114 | |
| 115 | def include_license(license): |
| 116 | return any(fnmatch(license, pattern) for pattern in whitelist) |
| 117 | |
| 118 | def exclude_license(license): |
| 119 | return any(fnmatch(license, pattern) for pattern in blacklist) |
| 120 | |
| 121 | def choose_licenses(alpha, beta): |
| 122 | """Select the option in an OR which is the 'best' (has the most |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 123 | included licenses and no excluded licenses).""" |
| 124 | # The factor 1000 below is arbitrary, just expected to be much larger |
| 125 | # that the number of licenses actually specified. That way the weight |
| 126 | # will be negative if the list of licenses contains an excluded license, |
| 127 | # but still gives a higher weight to the list with the most included |
| 128 | # licenses. |
| 129 | alpha_weight = (len(list(filter(include_license, alpha))) - |
| 130 | 1000 * (len(list(filter(exclude_license, alpha))) > 0)) |
| 131 | beta_weight = (len(list(filter(include_license, beta))) - |
| 132 | 1000 * (len(list(filter(exclude_license, beta))) > 0)) |
| 133 | if alpha_weight >= beta_weight: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 134 | return alpha |
| 135 | else: |
| 136 | return beta |
| 137 | |
| 138 | if not whitelist: |
| 139 | whitelist = ['*'] |
| 140 | |
| 141 | if not blacklist: |
| 142 | blacklist = [] |
| 143 | |
| 144 | licenses = flattened_licenses(licensestr, choose_licenses) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 145 | excluded = [lic for lic in licenses if exclude_license(lic)] |
| 146 | included = [lic for lic in licenses if include_license(lic)] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 147 | if excluded: |
| 148 | return False, excluded |
| 149 | else: |
| 150 | return True, included |
| 151 | |
| 152 | class ManifestVisitor(LicenseVisitor): |
| 153 | """Walk license tree (parsed from a string) removing the incompatible |
| 154 | licenses specified""" |
| 155 | def __init__(self, dont_want_licenses, canonical_license, d): |
| 156 | self._dont_want_licenses = dont_want_licenses |
| 157 | self._canonical_license = canonical_license |
| 158 | self._d = d |
| 159 | self._operators = [] |
| 160 | |
| 161 | self.licenses = [] |
| 162 | self.licensestr = '' |
| 163 | |
| 164 | LicenseVisitor.__init__(self) |
| 165 | |
| 166 | def visit(self, node): |
| 167 | if isinstance(node, ast.Str): |
| 168 | lic = node.s |
| 169 | |
| 170 | if license_ok(self._canonical_license(self._d, lic), |
| 171 | self._dont_want_licenses) == True: |
| 172 | if self._operators: |
| 173 | ops = [] |
| 174 | for op in self._operators: |
| 175 | if op == '[': |
| 176 | ops.append(op) |
| 177 | elif op == ']': |
| 178 | ops.append(op) |
| 179 | else: |
| 180 | if not ops: |
| 181 | ops.append(op) |
| 182 | elif ops[-1] in ['[', ']']: |
| 183 | ops.append(op) |
| 184 | else: |
| 185 | ops[-1] = op |
| 186 | |
| 187 | for op in ops: |
| 188 | if op == '[' or op == ']': |
| 189 | self.licensestr += op |
| 190 | elif self.licenses: |
| 191 | self.licensestr += ' ' + op + ' ' |
| 192 | |
| 193 | self._operators = [] |
| 194 | |
| 195 | self.licensestr += lic |
| 196 | self.licenses.append(lic) |
| 197 | elif isinstance(node, ast.BitAnd): |
| 198 | self._operators.append("&") |
| 199 | elif isinstance(node, ast.BitOr): |
| 200 | self._operators.append("|") |
| 201 | elif isinstance(node, ast.List): |
| 202 | self._operators.append("[") |
| 203 | elif isinstance(node, ast.Load): |
| 204 | self.licensestr += "]" |
| 205 | |
| 206 | self.generic_visit(node) |
| 207 | |
| 208 | def manifest_licenses(licensestr, dont_want_licenses, canonical_license, d): |
| 209 | """Given a license string and dont_want_licenses list, |
| 210 | return license string filtered and a list of licenses""" |
| 211 | manifest = ManifestVisitor(dont_want_licenses, canonical_license, d) |
| 212 | |
| 213 | try: |
| 214 | elements = manifest.get_elements(licensestr) |
| 215 | |
| 216 | # Replace '()' to '[]' for handle in ast as List and Load types. |
| 217 | elements = ['[' if e == '(' else e for e in elements] |
| 218 | elements = [']' if e == ')' else e for e in elements] |
| 219 | |
| 220 | manifest.visit_elements(elements) |
| 221 | except SyntaxError as exc: |
| 222 | raise LicenseSyntaxError(licensestr, exc) |
| 223 | |
| 224 | # Replace '[]' to '()' for output correct license. |
| 225 | manifest.licensestr = manifest.licensestr.replace('[', '(').replace(']', ')') |
| 226 | |
| 227 | return (manifest.licensestr, manifest.licenses) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 228 | |
| 229 | class ListVisitor(LicenseVisitor): |
| 230 | """Record all different licenses found in the license string""" |
| 231 | def __init__(self): |
| 232 | self.licenses = set() |
| 233 | |
| 234 | def visit_Str(self, node): |
| 235 | self.licenses.add(node.s) |
| 236 | |
| 237 | def list_licenses(licensestr): |
| 238 | """Simply get a list of all licenses mentioned in a license string. |
| 239 | Binary operators are not applied or taken into account in any way""" |
| 240 | visitor = ListVisitor() |
| 241 | try: |
| 242 | visitor.visit_string(licensestr) |
| 243 | except SyntaxError as exc: |
| 244 | raise LicenseSyntaxError(licensestr, exc) |
| 245 | return visitor.licenses |