Skip to content

Commit

Permalink
Re-map SPDX-URLs to alternatives where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
led02 committed Oct 30, 2023
1 parent 098fd05 commit 37eccd8
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/hermes/commands/deposit/invenio.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,14 +557,31 @@ def _get_license_identifier(ctx: CodeMetaContext, license_api_url: str):
)
response.raise_for_status()

for license_info in response.json()['hits']['hits']:
try:
if license_info['props']['url'] == license_url:
valid_licenses = response.json()
def _search_license_info(_url):
for license_info in valid_licenses['hits']['hits']:
try:
if license_info['props']['url'] == _url:
return license_info
except KeyError:
continue
else:
return None

license_info = _search_license_info(license_url)
if license_info is None and license_url.startswith('https://spdx.org/licenses/'):
response = requests.get(f"{license_url}.json", headers={"User-Agent": hermes_user_agent})
response.raise_for_status()

for license_cross_ref in response.json()['crossRef']:
if not license_cross_ref['isValid']:
continue

license_info = _search_license_info(license_cross_ref["url"])
if license_info is not None:
break
except KeyError:
continue
else:
raise RuntimeError(f"Not a valid license identifier: {license_url}")
else:
raise RuntimeError(f"Could not resolve license URL {license_url} to a valid identifier.")

return license_info["id"]

Expand Down

0 comments on commit 37eccd8

Please sign in to comment.