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.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/meta/lib/oe/license.py b/meta/lib/oe/license.py
new file mode 100644
index 0000000000..b230d3ef45
--- /dev/null
+++ b/meta/lib/oe/license.py
@@ -0,0 +1,32 @@
1# vi:sts=4:sw=4:et
2"""Code for parsing OpenEmbedded license strings"""
3
4import ast
5import re
6
7class InvalidLicense(StandardError):
8 def __init__(self, license):
9 self.license = license
10 StandardError.__init__(self)
11
12 def __str__(self):
13 return "invalid license '%s'" % self.license
14
15license_operator = re.compile('([&|() ])')
16license_pattern = re.compile('[a-zA-Z0-9.+_\-]+$')
17
18class LicenseVisitor(ast.NodeVisitor):
19 """Syntax tree visitor which can accept OpenEmbedded license strings"""
20 def visit_string(self, licensestr):
21 new_elements = []
22 elements = filter(lambda x: x.strip(), license_operator.split(licensestr))
23 for pos, element in enumerate(elements):
24 if license_pattern.match(element):
25 if pos > 0 and license_pattern.match(elements[pos-1]):
26 new_elements.append('&')
27 element = '"' + element + '"'
28 elif not license_operator.match(element):
29 raise InvalidLicense(element)
30 new_elements.append(element)
31
32 self.visit(ast.parse(' '.join(new_elements)))