summaryrefslogtreecommitdiffstats
path: root/meta/lib/oe/license.py
diff options
context:
space:
mode:
Diffstat (limited to 'meta/lib/oe/license.py')
-rw-r--r--meta/lib/oe/license.py77
1 files changed, 77 insertions, 0 deletions
diff --git a/meta/lib/oe/license.py b/meta/lib/oe/license.py
index 254279db53..f0f661c3ba 100644
--- a/meta/lib/oe/license.py
+++ b/meta/lib/oe/license.py
@@ -138,3 +138,80 @@ def is_included(licensestr, whitelist=None, blacklist=None):
138 return False, excluded 138 return False, excluded
139 else: 139 else:
140 return True, included 140 return True, included
141
142class ManifestVisitor(LicenseVisitor):
143 """Walk license tree (parsed from a string) removing the incompatible
144 licenses specified"""
145 def __init__(self, dont_want_licenses, canonical_license, d):
146 self._dont_want_licenses = dont_want_licenses
147 self._canonical_license = canonical_license
148 self._d = d
149 self._operators = []
150
151 self.licenses = []
152 self.licensestr = ''
153
154 LicenseVisitor.__init__(self)
155
156 def visit(self, node):
157 if isinstance(node, ast.Str):
158 lic = node.s
159
160 if license_ok(self._canonical_license(self._d, lic),
161 self._dont_want_licenses) == True:
162 if self._operators:
163 ops = []
164 for op in self._operators:
165 if op == '[':
166 ops.append(op)
167 elif op == ']':
168 ops.append(op)
169 else:
170 if not ops:
171 ops.append(op)
172 elif ops[-1] in ['[', ']']:
173 ops.append(op)
174 else:
175 ops[-1] = op
176
177 for op in ops:
178 if op == '[' or op == ']':
179 self.licensestr += op
180 elif self.licenses:
181 self.licensestr += ' ' + op + ' '
182
183 self._operators = []
184
185 self.licensestr += lic
186 self.licenses.append(lic)
187 elif isinstance(node, ast.BitAnd):
188 self._operators.append("&")
189 elif isinstance(node, ast.BitOr):
190 self._operators.append("|")
191 elif isinstance(node, ast.List):
192 self._operators.append("[")
193 elif isinstance(node, ast.Load):
194 self.licensestr += "]"
195
196 self.generic_visit(node)
197
198def manifest_licenses(licensestr, dont_want_licenses, canonical_license, d):
199 """Given a license string and dont_want_licenses list,
200 return license string filtered and a list of licenses"""
201 manifest = ManifestVisitor(dont_want_licenses, canonical_license, d)
202
203 try:
204 elements = manifest.get_elements(licensestr)
205
206 # Replace '()' to '[]' for handle in ast as List and Load types.
207 elements = ['[' if e == '(' else e for e in elements]
208 elements = [']' if e == ')' else e for e in elements]
209
210 manifest.visit_elements(elements)
211 except SyntaxError as exc:
212 raise LicenseSyntaxError(licensestr, exc)
213
214 # Replace '[]' to '()' for output correct license.
215 manifest.licensestr = manifest.licensestr.replace('[', '(').replace(']', ')')
216
217 return (manifest.licensestr, manifest.licenses)