diff --git a/swh/indexer/metadata_dictionary.py b/swh/indexer/metadata_dictionary.py --- a/swh/indexer/metadata_dictionary.py +++ b/swh/indexer/metadata_dictionary.py @@ -246,6 +246,7 @@ d = xmltodict.parse(content)['project'] metadata = self.translate_dict(d, normalize=False) metadata[SCHEMA_URI+'codeRepository'] = self.parse_repositories(d) + metadata[SCHEMA_URI+'license'] = self.parse_licenses(d) return self.normalize_translation(metadata) _default_repository = {'url': 'https://repo.maven.apache.org/maven2/'} @@ -278,6 +279,62 @@ def normalize_groupId(self, id_): return {"@id": id_} + def parse_licenses(self, d): + """https://maven.apache.org/pom.html#Licenses + + The origin XML has the form: + + + + Apache License, Version 2.0 + https://www.apache.org/licenses/LICENSE-2.0.txt + + + + Which was translated to a dict by xmltodict and is given as `d`: + + >>> d = { + ... # ... + ... "licenses": { + ... "license": { + ... "name": "Apache License, Version 2.0", + ... "url": + ... "https://www.apache.org/licenses/LICENSE-2.0.txt" + ... } + ... } + ... } + >>> MavenMapping().parse_licenses(d) + [{'@id': 'https://www.apache.org/licenses/LICENSE-2.0.txt'}] + + or, if there are more than one license: + + >>> from pprint import pprint + >>> d = { + ... # ... + ... "licenses": { + ... "license": [ + ... { + ... "name": "Apache License, Version 2.0", + ... "url": + ... "https://www.apache.org/licenses/LICENSE-2.0.txt" + ... }, + ... { + ... "name": "MIT License, ", + ... "url": "https://opensource.org/licenses/MIT" + ... } + ... ] + ... } + ... } + >>> pprint(MavenMapping().parse_licenses(d)) + [{'@id': 'https://www.apache.org/licenses/LICENSE-2.0.txt'}, + {'@id': 'https://opensource.org/licenses/MIT'}] + """ + + licenses = d.get('licenses', {}).get('license', []) + if isinstance(licenses, dict): + licenses = [licenses] + return [{"@id": license['url']} for license in licenses] + def main(): raw_content = """{"name": "test_name", "unknown_term": "ut"}""" diff --git a/swh/indexer/tests/test_metadata.py b/swh/indexer/tests/test_metadata.py --- a/swh/indexer/tests/test_metadata.py +++ b/swh/indexer/tests/test_metadata.py @@ -426,6 +426,14 @@ + + + Apache License, Version 2.0 + https://www.apache.org/licenses/LICENSE-2.0.txt + repo + A business-friendly OSS license + + """ result = MAPPINGS["MavenMapping"].translate(raw_content) self.assertEqual(result, { @@ -434,9 +442,87 @@ 'name': 'Maven Default Project', 'identifier': 'com.mycompany.app', 'version': '1.2.3', + 'license': 'https://www.apache.org/licenses/LICENSE-2.0.txt', 'codeRepository': 'http://repo1.maven.org/maven2/com/mycompany/app/my-app', - }) + }) + + def test_compute_metadata_maven_minimal(self): + raw_content = b""" + + Maven Default Project + 4.0.0 + com.mycompany.app + my-app + 1.2.3 + """ + result = MAPPINGS["MavenMapping"].translate(raw_content) + self.assertEqual(result, { + '@context': 'https://doi.org/10.5063/schema/codemeta-2.0', + 'type': 'SoftwareSourceCode', + 'name': 'Maven Default Project', + 'identifier': 'com.mycompany.app', + 'version': '1.2.3', + 'codeRepository': + 'https://repo.maven.apache.org/maven2/com/mycompany/app/my-app', + 'license': [], + }) + + def test_compute_metadata_maven_multiple(self): + '''Tests when there are multiple code repos and licenses.''' + raw_content = b""" + + Maven Default Project + 4.0.0 + com.mycompany.app + my-app + 1.2.3 + + + central + Maven Repository Switchboard + default + http://repo1.maven.org/maven2 + + false + + + + example + Example Maven Repo + default + http://example.org/maven2 + + + + + Apache License, Version 2.0 + https://www.apache.org/licenses/LICENSE-2.0.txt + repo + A business-friendly OSS license + + + MIT license + https://opensource.org/licenses/MIT + + + """ + result = MAPPINGS["MavenMapping"].translate(raw_content) + self.assertEqual(result, { + '@context': 'https://doi.org/10.5063/schema/codemeta-2.0', + 'type': 'SoftwareSourceCode', + 'name': 'Maven Default Project', + 'identifier': 'com.mycompany.app', + 'version': '1.2.3', + 'license': [ + 'https://www.apache.org/licenses/LICENSE-2.0.txt', + 'https://opensource.org/licenses/MIT', + ], + 'codeRepository': [ + 'http://repo1.maven.org/maven2/com/mycompany/app/my-app', + 'http://example.org/maven2/com/mycompany/app/my-app', + ] + }) def test_revision_metadata_indexer(self): metadata_indexer = RevisionMetadataTestIndexer()