|
26 | 26 | from __future__ import print_function |
27 | 27 | from __future__ import unicode_literals |
28 | 28 |
|
| 29 | +from collections import defaultdict |
| 30 | +from collections import OrderedDict |
| 31 | +import ast |
29 | 32 | import logging |
| 33 | +import os |
30 | 34 |
|
31 | 35 | import attr |
32 | 36 |
|
33 | 37 | from commoncode import filetype |
34 | 38 | from commoncode import fileutils |
35 | 39 | from packagedcode import models |
| 40 | +from packagedcode.utils import combine_expressions |
| 41 | +from scancode.api import get_licenses |
36 | 42 |
|
37 | 43 |
|
38 | 44 | TRACE = False |
@@ -91,7 +97,94 @@ class AutotoolsPackage(BaseBuildManifestPackage): |
91 | 97 | default_type = 'autotools' |
92 | 98 |
|
93 | 99 |
|
| 100 | +starlark_rule_types = [ |
| 101 | + 'binary', |
| 102 | + 'library' |
| 103 | +] |
| 104 | + |
| 105 | + |
| 106 | +@attr.s() |
| 107 | +class StarlarkManifestPackage(BaseBuildManifestPackage): |
| 108 | + @classmethod |
| 109 | + def recognize(cls, location): |
| 110 | + if not cls._is_build_manifest(location): |
| 111 | + return |
| 112 | + |
| 113 | + # Thanks to Starlark being a Python dialect, we can use the `ast` |
| 114 | + # library to parse it |
| 115 | + with open(location, 'rb') as f: |
| 116 | + tree = ast.parse(f.read()) |
| 117 | + |
| 118 | + build_rules = defaultdict(list) |
| 119 | + for statement in tree.body: |
| 120 | + # We only care about function calls or assignments to functions whose |
| 121 | + # names ends with one of the strings in `rule_types` |
| 122 | + if (isinstance(statement, ast.Expr) |
| 123 | + or isinstance(statement, ast.Call) |
| 124 | + or isinstance(statement, ast.Assign) |
| 125 | + and isinstance(statement.value, ast.Call) |
| 126 | + and isinstance(statement.value.func, ast.Name) |
| 127 | + and statement.value.func.id.endswith(starlark_rule_types)): |
| 128 | + rule_name = statement.value.func.id |
| 129 | + # Process the rule arguments |
| 130 | + args = OrderedDict() |
| 131 | + for kw in statement.value.keywords: |
| 132 | + arg_name = kw.arg |
| 133 | + if isinstance(kw.value, ast.Str): |
| 134 | + args[arg_name] = kw.value.s |
| 135 | + if isinstance(kw.value, ast.List): |
| 136 | + # We collect the elements of a list if the element is not a function call |
| 137 | + args[arg_name] = [elt.s for elt in kw.value.elts if not isinstance(elt, ast.Call)] |
| 138 | + if args: |
| 139 | + build_rules[rule_name].append(args) |
| 140 | + |
| 141 | + if build_rules: |
| 142 | + for rule_name, rule_instances_args in build_rules.items(): |
| 143 | + for args in rule_instances_args: |
| 144 | + name = args.get('name') |
| 145 | + if not name: |
| 146 | + continue |
| 147 | + license_files = args.get('licenses') |
| 148 | + yield cls( |
| 149 | + name=name, |
| 150 | + declared_license=license_files, |
| 151 | + root_path=fileutils.parent_directory(location) |
| 152 | + ) |
| 153 | + else: |
| 154 | + # If we don't find anything in the manifest file, we yield a Package with |
| 155 | + # the parent directory as the name |
| 156 | + yield cls( |
| 157 | + name=fileutils.file_name(fileutils.parent_directory(location)) |
| 158 | + ) |
| 159 | + |
| 160 | + def compute_normalized_license(self): |
| 161 | + """ |
| 162 | + Return a normalized license expression string detected from a list of |
| 163 | + declared license items. |
| 164 | + """ |
| 165 | + declared_license = self.declared_license |
| 166 | + manifest_parent_path = self.root_path |
| 167 | + |
| 168 | + if not declared_license or not manifest_parent_path: |
| 169 | + return |
| 170 | + |
| 171 | + license_expressions = [] |
| 172 | + for license_file in declared_license: |
| 173 | + license_file_path = os.path.join(manifest_parent_path, license_file) |
| 174 | + if os.path.exists(license_file_path) and os.path.isfile(license_file_path): |
| 175 | + licenses = get_licenses(license_file_path) |
| 176 | + license_expressions.extend(licenses.get('license_expressions', [])) |
| 177 | + |
| 178 | + return combine_expressions(license_expressions) |
| 179 | + |
| 180 | + |
94 | 181 | @attr.s() |
95 | | -class BazelPackage(BaseBuildManifestPackage): |
| 182 | +class BazelPackage(StarlarkManifestPackage): |
96 | 183 | metafiles = ('BUILD',) |
97 | 184 | default_type = 'bazel' |
| 185 | + |
| 186 | + |
| 187 | +@attr.s() |
| 188 | +class BuckPackage(StarlarkManifestPackage): |
| 189 | + metafiles = ('BUCK',) |
| 190 | + default_type = 'buck' |
0 commit comments