Yield one or more Package manifest objects given a file ``location`` pointing to a package archive, manifest or similar.
(cls, location, package_only=False)
| 48 | |
| 49 | @classmethod |
| 50 | def parse(cls, location, package_only=False): |
| 51 | """ |
| 52 | Yield one or more Package manifest objects given a file ``location`` pointing to a |
| 53 | package archive, manifest or similar. |
| 54 | """ |
| 55 | with io.open(location, encoding='utf-8') as loc: |
| 56 | package_data = saneyaml.load(loc.read()) |
| 57 | |
| 58 | # About files can contain any purl and also have a namespace |
| 59 | about_type = package_data.get('type') |
| 60 | about_ns = package_data.get('namespace') |
| 61 | purl_type = None |
| 62 | purl_ns = None |
| 63 | purl = package_data.get('purl') |
| 64 | if purl: |
| 65 | purl = PackageURL.from_string(purl) |
| 66 | if purl: |
| 67 | purl_type = purl.type |
| 68 | |
| 69 | package_type = about_type or purl_type or cls.default_package_type |
| 70 | package_ns = about_ns or purl_ns |
| 71 | |
| 72 | name = package_data.get('name') |
| 73 | version = package_data.get('version') |
| 74 | |
| 75 | homepage_url = package_data.get('home_url') or package_data.get('homepage_url') |
| 76 | download_url = package_data.get('download_url') |
| 77 | copyright_statement = package_data.get('copyright') |
| 78 | |
| 79 | declared_license_expression = package_data.get('license_expression') |
| 80 | |
| 81 | owner = package_data.get('owner') |
| 82 | if not isinstance(owner, str): |
| 83 | owner = repr(owner) |
| 84 | parties = [models.Party(type=models.party_person, name=owner, role='owner')] |
| 85 | |
| 86 | # FIXME: also include notice_file and license_file(s) as file_references |
| 87 | file_references = [] |
| 88 | about_resource = package_data.get('about_resource') |
| 89 | if about_resource: |
| 90 | file_references.append(models.FileReference(path=about_resource)) |
| 91 | |
| 92 | # FIXME: we should put the unprocessed attributes in extra data |
| 93 | package_data = dict( |
| 94 | datasource_id=cls.datasource_id, |
| 95 | type=package_type, |
| 96 | namespace=package_ns, |
| 97 | name=name, |
| 98 | version=version, |
| 99 | extracted_license_statement=declared_license_expression, |
| 100 | copyright=copyright_statement, |
| 101 | parties=parties, |
| 102 | homepage_url=homepage_url, |
| 103 | download_url=download_url, |
| 104 | file_references=file_references, |
| 105 | ) |
| 106 | yield models.PackageData.from_data(package_data, package_only) |
| 107 |