MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / parse

Method parse

src/packagedcode/build.py:141–214  ·  view source on GitHub ↗
(cls, location, package_only=False)

Source from the content-addressed store, hash-verified

139
140 @classmethod
141 def parse(cls, location, package_only=False):
142 # Thanks to Starlark being a Python dialect, we can use `ast` to parse it
143 with open(location, 'rb') as f:
144 tree = ast.parse(f.read())
145
146 build_rules = defaultdict(list)
147 for statement in tree.body:
148 # We only care about function calls or assignments to functions whose
149 # names ends with one of the strings in `rule_types`
150 if (
151 isinstance(statement, ast.Expr)
152 or isinstance(statement, ast.Call)
153 or isinstance(statement, ast.Assign)
154 and isinstance(statement.value, ast.Call)
155 and isinstance(statement.value.func, ast.Name)
156 ):
157 rule_name = statement.value.func.id
158 # Ensure that we are only creating packages from the proper
159 # build rules
160 if not check_rule_name_ending(rule_name):
161 continue
162 # Process the rule arguments
163 args = {}
164 for kw in statement.value.keywords:
165 arg_name = kw.arg
166 if isinstance(kw.value, ast.Constant) and isinstance(kw.value.value, str):
167 args[arg_name] = kw.value.value
168
169 if isinstance(kw.value, ast.List):
170 # We collect the elements of a list if the element is
171 # not a function call
172 args[arg_name] = [
173 elt.value for elt in kw.value.elts
174 if not isinstance(elt, ast.Call)
175 ]
176 if args:
177 build_rules[rule_name].append(args)
178
179 if build_rules:
180 for rule_name, rule_instances_args in build_rules.items():
181 for args in rule_instances_args:
182 name = args.get('name')
183
184 # FIXME: we could still return partial package data
185 if not name:
186 continue
187
188 license_files = args.get('licenses')
189
190 if TRACE:
191 logger_debug(f"build: parse: license_files: {license_files}")
192
193 package_data = dict(
194 datasource_id=cls.datasource_id,
195 type=cls.default_package_type,
196 name=name,
197 extracted_license_statement=license_files,
198 )

Callers

nothing calls this directly

Calls 7

check_rule_name_endingFunction · 0.85
from_dataMethod · 0.80
logger_debugFunction · 0.70
getMethod · 0.65
parseMethod · 0.45
readMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected