(package_data, package_only=False)
| 87 | |
| 88 | |
| 89 | def build_package_data(package_data, package_only=False): |
| 90 | |
| 91 | # Note: A composer.json without name and description is not a usable PHP |
| 92 | # composer package. Name and description fields are required but only for |
| 93 | # published packages: https://getcomposer.org/doc/04-schema.md#name We want |
| 94 | # to catch both published and non-published packages here. Therefore, we use |
| 95 | # None as a package name if there is no name. |
| 96 | |
| 97 | ns_name = package_data.get('name') |
| 98 | is_private = False |
| 99 | if not ns_name: |
| 100 | ns = None |
| 101 | name = None |
| 102 | is_private = True |
| 103 | else: |
| 104 | ns, _, name = ns_name.rpartition('/') |
| 105 | |
| 106 | package_mapping = dict( |
| 107 | datasource_id=PhpComposerJsonHandler.datasource_id, |
| 108 | type=PhpComposerJsonHandler.default_package_type, |
| 109 | namespace=ns, |
| 110 | name=name, |
| 111 | repository_homepage_url=get_repository_homepage_url(ns, name), |
| 112 | api_data_url=get_api_data_url(ns, name), |
| 113 | primary_language=PhpComposerJsonHandler.default_primary_language, |
| 114 | is_private=is_private, |
| 115 | ) |
| 116 | package = models.PackageData.from_data(package_mapping, package_only) |
| 117 | |
| 118 | # mapping of top level composer.json items to the Package object field name |
| 119 | plain_fields = [ |
| 120 | ('version', 'version'), |
| 121 | ('description', 'summary'), |
| 122 | ('keywords', 'keywords'), |
| 123 | ('homepage', 'homepage_url'), |
| 124 | ] |
| 125 | |
| 126 | for source, target in plain_fields: |
| 127 | value = package_data.get(source) |
| 128 | if isinstance(value, str): |
| 129 | value = value.strip() |
| 130 | if value: |
| 131 | setattr(package, target, value) |
| 132 | |
| 133 | # mapping of top level composer.json items to a function accepting as |
| 134 | # arguments the composer.json element value and returning an iterable of |
| 135 | # key, values Package Object to update |
| 136 | field_mappers = [ |
| 137 | ('authors', author_mapper), |
| 138 | ('license', partial(licensing_mapper, is_private=is_private)), |
| 139 | ('support', support_mapper), |
| 140 | ('require', partial(_deps_mapper, scope='require', is_runtime=True)), |
| 141 | ('require-dev', partial(_deps_mapper, scope='require-dev', is_optional=True)), |
| 142 | ('provide', partial(_deps_mapper, scope='provide', is_runtime=True)), |
| 143 | ('conflict', partial(_deps_mapper, scope='conflict', is_runtime=True, is_optional=True)), |
| 144 | ('replace', partial(_deps_mapper, scope='replace', is_runtime=True, is_optional=True)), |
| 145 | ('suggest', partial(_deps_mapper, scope='suggest', is_runtime=True, is_optional=True)), |
| 146 | ('source', source_mapper), |
no test coverage detected