summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristopher Larson <kergoth@gmail.com>2011-12-04 20:03:51 -0500
committerRichard Purdie <richard.purdie@linuxfoundation.org>2011-12-08 15:24:29 +0000
commit9fb67f895c2616c4dfb8521d3dd5c36758570d83 (patch)
treed89b3e073b0eb281a31ebc9e08c12293413f52dc
parenta57de1ac9de96eef13f1fd6d37f3f73808d075ac (diff)
downloadpoky-9fb67f895c2616c4dfb8521d3dd5c36758570d83.tar.gz
oe.license: add license flattening code
This flattens a license tree by selecting one side of each OR operation (chosen via the user supplied function). (From OE-Core rev: 6984961314c8ba2aceab9acabb658f96ed249fef) Signed-off-by: Christopher Larson <kergoth@gmail.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
-rw-r--r--meta/lib/oe/license.py30
-rw-r--r--meta/lib/oe/tests/test_license.py30
2 files changed, 60 insertions, 0 deletions
diff --git a/meta/lib/oe/license.py b/meta/lib/oe/license.py
index b230d3ef45..7ab66e762f 100644
--- a/meta/lib/oe/license.py
+++ b/meta/lib/oe/license.py
@@ -30,3 +30,33 @@ class LicenseVisitor(ast.NodeVisitor):
30 new_elements.append(element) 30 new_elements.append(element)
31 31
32 self.visit(ast.parse(' '.join(new_elements))) 32 self.visit(ast.parse(' '.join(new_elements)))
33
34class FlattenVisitor(LicenseVisitor):
35 """Flatten a license tree (parsed from a string) by selecting one of each
36 set of OR options, in the way the user specifies"""
37 def __init__(self, choose_licenses):
38 self.choose_licenses = choose_licenses
39 self.licenses = []
40 LicenseVisitor.__init__(self)
41
42 def visit_Str(self, node):
43 self.licenses.append(node.s)
44
45 def visit_BinOp(self, node):
46 if isinstance(node.op, ast.BitOr):
47 left = FlattenVisitor(self.choose_licenses)
48 left.visit(node.left)
49
50 right = FlattenVisitor(self.choose_licenses)
51 right.visit(node.right)
52
53 selected = self.choose_licenses(left.licenses, right.licenses)
54 self.licenses.extend(selected)
55 else:
56 self.generic_visit(node)
57
58def flattened_licenses(licensestr, choose_licenses):
59 """Given a license string and choose_licenses function, return a flat list of licenses"""
60 flatten = FlattenVisitor(choose_licenses)
61 flatten.visit_string(licensestr)
62 return flatten.licenses
diff --git a/meta/lib/oe/tests/test_license.py b/meta/lib/oe/tests/test_license.py
index cb949fc76f..c388886184 100644
--- a/meta/lib/oe/tests/test_license.py
+++ b/meta/lib/oe/tests/test_license.py
@@ -36,3 +36,33 @@ class TestSingleLicense(unittest.TestCase):
36 with self.assertRaises(oe.license.InvalidLicense) as cm: 36 with self.assertRaises(oe.license.InvalidLicense) as cm:
37 self.parse(license) 37 self.parse(license)
38 self.assertEqual(cm.exception.license, license) 38 self.assertEqual(cm.exception.license, license)
39
40class TestSimpleCombinations(unittest.TestCase):
41 tests = {
42 "FOO&BAR": ["FOO", "BAR"],
43 "BAZ & MOO": ["BAZ", "MOO"],
44 "ALPHA|BETA": ["ALPHA"],
45 "BAZ&MOO|FOO": ["FOO"],
46 "FOO&BAR|BAZ": ["FOO", "BAR"],
47 }
48 preferred = ["ALPHA", "FOO", "BAR"]
49
50 def test_tests(self):
51 def choose(a, b):
52 if all(lic in self.preferred for lic in b):
53 return b
54 else:
55 return a
56
57 for license, expected in self.tests.items():
58 licenses = oe.license.flattened_licenses(license, choose)
59 self.assertListEqual(licenses, expected)
60
61class TestComplexCombinations(TestSimpleCombinations):
62 tests = {
63 "FOO & (BAR | BAZ)&MOO": ["FOO", "BAR", "MOO"],
64 "(ALPHA|(BETA&THETA)|OMEGA)&DELTA": ["OMEGA", "DELTA"],
65 "((ALPHA|BETA)&FOO)|BAZ": ["BETA", "FOO"],
66 "(GPL-2.0|Proprietary)&BSD-4-clause&MIT": ["GPL-2.0", "BSD-4-clause", "MIT"],
67 }
68 preferred = ["BAR", "OMEGA", "BETA", "GPL-2.0"]