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

Method parse

src/packagedcode/build.py:336–421  ·  view source on GitHub ↗
(cls, location, package_only=True)

Source from the content-addressed store, hash-verified

334
335 @classmethod
336 def parse(cls, location, package_only=True):
337
338 with open(location, 'rb') as f:
339 tree = ast.parse(f.read())
340
341 metadata_fields = {}
342 for statement in tree.body:
343 if not (hasattr(statement, 'targets') and isinstance(statement, ast.Assign)):
344 continue
345
346 # We are looking for a dictionary assigned to the variable `METADATA`
347 for target in statement.targets:
348 if not (target.id == 'METADATA' and isinstance(statement.value, ast.Dict)):
349 continue
350 # Once we find the dictionary assignment, get and store its contents
351 statement_keys = statement.value.keys
352 statement_values = statement.value.values
353 for statement_k, statement_v in zip(statement_keys, statement_values):
354 if isinstance(statement_k, ast.Constant) and isinstance(statement_k.value, str):
355 key_name = statement_k.value
356 # The list values in a `METADATA.bzl` file seem to only contain strings
357 if isinstance(statement_v, ast.List):
358 value = []
359 for e in statement_v.elts:
360 if not (isinstance(e, ast.Constant) and isinstance(e.value, str)):
361 continue
362 value.append(e.value)
363 if isinstance(statement_v, ast.Constant):
364 value = statement_v.value
365 metadata_fields[key_name] = value
366
367 parties = []
368 maintainers = metadata_fields.get('maintainers', []) or []
369 for maintainer in maintainers:
370 parties.append(
371 models.Party(
372 type=models.party_org,
373 name=maintainer,
374 role='maintainer',
375 )
376 )
377
378 # TODO: Create function that determines package type from download URL,
379 # then create a package of that package type from the metadata info
380
381 if 'upstream_type' in metadata_fields:
382 package_type = metadata_fields['upstream_type']
383 elif 'package_type' in metadata_fields:
384 package_type = metadata_fields['package_type']
385 else:
386 package_type = cls.default_package_type
387
388 if 'licenses' in metadata_fields:
389 extracted_license_statement = metadata_fields['licenses']
390 else:
391 extracted_license_statement = metadata_fields.get('license_expression')
392
393 if 'upstream_address' in metadata_fields:

Callers 1

parseMethod · 0.45

Calls 6

from_dataMethod · 0.80
getMethod · 0.65
readMethod · 0.45
appendMethod · 0.45
updateMethod · 0.45
to_dictMethod · 0.45

Tested by

no test coverage detected