# vi:sts=4:sw=4:et """Code for parsing OpenEmbedded license strings""" import ast import re from fnmatch import fnmatchcase as fnmatch class LicenseError(StandardError): pass class LicenseSyntaxError(LicenseError): def __init__(self, licensestr, exc): self.licensestr = licensestr self.exc = exc LicenseError.__init__(self) def __str__(self): return "error in '%s': %s" % (self.licensestr, self.exc) class InvalidLicense(LicenseError): def __init__(self, license): self.license = license LicenseError.__init__(self) def __str__(self): return "invalid characters in license '%s'" % self.license license_operator = re.compile('([&|() ])') license_pattern = re.compile('[a-zA-Z0-9.+_\-]+$') class LicenseVisitor(ast.NodeVisitor): """Syntax tree visitor which can accept OpenEmbedded license strings""" def visit_string(self, licensestr): new_elements = [] elements = filter(lambda x: x.strip(), license_operator.split(licensestr)) for pos, element in enumerate(elements): if license_pattern.match(element): if pos > 0 and license_pattern.match(elements[pos-1]): new_elements.append('&') element = '"' + element + '"' elif not license_operator.match(element): raise InvalidLicense(element) new_elements.append(element) self.visit(ast.parse(' '.join(new_elements))) class FlattenVisitor(LicenseVisitor): """Flatten a license tree (parsed from a string) by selecting one of each set of OR options, in the way the user specifies""" def __init__(self, choose_licenses): self.choose_licenses = choose_licenses self.licenses = [] LicenseVisitor.__init__(self) def visit_Str(self, node): self.licenses.append(node.s) def visit_BinOp(self, node): if isinstance(node.op, ast.BitOr): left = FlattenVisitor(self.choose_licenses) left.visit(node.left) right = FlattenVisitor(self.choose_licenses) right.visit(node.right) selected = self.choose_licenses(left.licenses, right.licenses) self.licenses.extend(selected) else: self.generic_visit(node) def flattened_licenses(licensestr, choose_licenses): """Given a license string and choose_licenses function, return a flat list of licenses""" flatten = FlattenVisitor(choose_licenses) try: flatten.visit_string(licensestr) except SyntaxError as exc: raise LicenseSyntaxError(licensestr, exc) return flatten.licenses def is_included(licensestr, whitelist=None, blacklist=None): """Given a license string and whitelist and blacklist, determine if the license string matches the whitelist and does not match the blacklist. Returns a tuple holding the boolean state and a list of the applicable licenses which were excluded (or None, if the state is True) """ def include_license(license): return (any(fnmatch(license, pattern) for pattern in whitelist) and not any(fnmatch(license, pattern) for pattern in blacklist)) def choose_licenses(alpha, beta): """Select the option in an OR which is the 'best' (has the most included licenses).""" alpha_weight = len(filter(include_license, alpha)) beta_weight = len(filter(include_license, beta)) if alpha_weight > beta_weight: return alpha else: return beta if not whitelist: whitelist = ['*'] if not blacklist: blacklist = [] licenses = flattened_licenses(licensestr, choose_licenses) excluded = filter(lambda lic: not include_license(lic), licenses) if excluded: return False, excluded else: return True, None id='n11' href='#n11'>11</a> <a id='n12' href='#n12'>12</a> <a id='n13' href='#n13'>13</a> <a id='n14' href='#n14'>14</a> <a id='n15' href='#n15'>15</a> <a id='n16' href='#n16'>16</a> <a id='n17' href='#n17'>17</a> <a id='n18' href='#n18'>18</a> <a id='n19' href='#n19'>19</a> <a id='n20' href='#n20'>20</a> <a id='n21' href='#n21'>21</a> <a id='n22' href='#n22'>22</a> <a id='n23' href='#n23'>23</a> <a id='n24' href='#n24'>24</a> <a id='n25' href='#n25'>25</a> <a id='n26' href='#n26'>26</a> <a id='n27' href='#n27'>27</a> <a id='n28' href='#n28'>28</a> <a id='n29' href='#n29'>29</a> <a id='n30' href='#n30'>30</a> <a id='n31' href='#n31'>31</a> <a id='n32' href='#n32'>32</a> <a id='n33' href='#n33'>33</a> <a id='n34' href='#n34'>34</a> <a id='n35' href='#n35'>35</a> </pre></td> <td class='lines'><pre><code>