diff options
Diffstat (limited to 'meta/lib/oe')
-rw-r--r-- | meta/lib/oe/license.py | 30 | ||||
-rw-r--r-- | meta/lib/oe/tests/test_license.py | 30 |
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 | |||
34 | class 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 | |||
58 | def 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 | |||
40 | class 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 | |||
61 | class 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"] | ||