Skip to content
Snippets Groups Projects
Verified Commit 163df3ee authored by Antoine R. Dumont's avatar Antoine R. Dumont
Browse files

cli: Warn when metadata-only deposit without metadata provenance

Related to T3677
parent cf92b5f2
No related branches found
No related tags found
No related merge requests found
...@@ -604,16 +604,24 @@ def metadata_only(ctx, url, username, password, metadata_path, output_format): ...@@ -604,16 +604,24 @@ def metadata_only(ctx, url, username, password, metadata_path, output_format):
from xml.etree import ElementTree from xml.etree import ElementTree
from swh.deposit.client import PublicApiDepositClient from swh.deposit.client import PublicApiDepositClient
from swh.deposit.utils import parse_swh_reference from swh.deposit.utils import parse_swh_metadata_provenance, parse_swh_reference
# Parse to check for a swhid presence within the metadata file # Parse to check for a swhid presence within the metadata file
with open(metadata_path, "r") as f: with open(metadata_path, "r") as f:
metadata_raw = f.read() metadata_raw = f.read()
actual_swhid = parse_swh_reference(ElementTree.fromstring(metadata_raw)) metadata_tree = ElementTree.fromstring(metadata_raw)
actual_swhid = parse_swh_reference(metadata_tree)
if not actual_swhid: if not actual_swhid:
raise InputError("A SWHID must be provided for a metadata-only deposit") raise InputError("A SWHID must be provided for a metadata-only deposit")
meta_prov_url = parse_swh_metadata_provenance(metadata_tree)
if not meta_prov_url:
logger.warning(
"A '<swh:metadata-provenance>' should be provided for a metadata-only "
"deposit"
)
with trap_and_report_exceptions(): with trap_and_report_exceptions():
client = PublicApiDepositClient(url=_url(url), auth=(username, password)) client = PublicApiDepositClient(url=_url(url), auth=(username, password))
collection = _collection(client) collection = _collection(client)
......
...@@ -8,6 +8,7 @@ import contextlib ...@@ -8,6 +8,7 @@ import contextlib
import json import json
import logging import logging
import os import os
from typing import Optional
from unittest.mock import MagicMock from unittest.mock import MagicMock
from xml.etree import ElementTree from xml.etree import ElementTree
...@@ -860,8 +861,17 @@ def test_cli_update_metadata_with_swhid_on_other_status_deposit( ...@@ -860,8 +861,17 @@ def test_cli_update_metadata_with_swhid_on_other_status_deposit(
} }
@pytest.mark.parametrize(
"metadata_entry_key", ["entry-data-with-swhid", "entry-data-with-swhid-no-prov"]
)
def test_cli_metadata_only_deposit_full_metadata_file( def test_cli_metadata_only_deposit_full_metadata_file(
datadir, requests_mock_datadir, cli_runner, atom_dataset, tmp_path, datadir,
requests_mock_datadir,
cli_runner,
atom_dataset,
tmp_path,
metadata_entry_key,
caplog,
): ):
"""Post metadata-only deposit through cli """Post metadata-only deposit through cli
...@@ -870,7 +880,7 @@ def test_cli_metadata_only_deposit_full_metadata_file( ...@@ -870,7 +880,7 @@ def test_cli_metadata_only_deposit_full_metadata_file(
""" """
api_url_basename = "deposit.test.metadataonly" api_url_basename = "deposit.test.metadataonly"
swhid = "swh:1:dir:ef04a768181417fbc5eef4243e2507915f24deea" swhid = "swh:1:dir:ef04a768181417fbc5eef4243e2507915f24deea"
metadata = atom_dataset["entry-data-with-swhid"].format(swhid=swhid) metadata = atom_dataset[metadata_entry_key].format(swhid=swhid)
metadata_path = os.path.join(tmp_path, "entry-data-with-swhid.xml") metadata_path = os.path.join(tmp_path, "entry-data-with-swhid.xml")
with open(metadata_path, "w") as m: with open(metadata_path, "w") as m:
m.write(metadata) m.write(metadata)
...@@ -901,6 +911,19 @@ def test_cli_metadata_only_deposit_full_metadata_file( ...@@ -901,6 +911,19 @@ def test_cli_metadata_only_deposit_full_metadata_file(
assert "error" not in actual_deposit_status assert "error" not in actual_deposit_status
assert actual_deposit_status == expected_deposit_status assert actual_deposit_status == expected_deposit_status
count_warnings = 0
warning_record: Optional[str] = None
for (_, log_level, msg) in caplog.record_tuples:
if log_level == logging.WARNING:
count_warnings += 1
warning_record = msg
if "no-prov" in metadata_entry_key:
assert count_warnings == 1
assert "metadata-provenance>' should be provided" in warning_record
else:
assert count_warnings == 0
def test_cli_metadata_only_deposit_invalid_swhid( def test_cli_metadata_only_deposit_invalid_swhid(
datadir, requests_mock_datadir, cli_runner, atom_dataset, tmp_path, datadir, requests_mock_datadir, cli_runner, atom_dataset, tmp_path,
......
<?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:codemeta="https://doi.org/10.5063/SCHEMA/CODEMETA-2.0"
xmlns:swh="https://www.softwareheritage.org/schema/2018/deposit">
<title>Awesome Compiler</title>
<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
<author>dudess</author>
<swh:deposit>
<swh:reference>
<swh:object swhid="{swhid}" />
</swh:reference>
</swh:deposit>
</entry>
<?xml version="1.0"?> <?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom" <entry xmlns="http://www.w3.org/2005/Atom"
xmlns:codemeta="https://doi.org/10.5063/SCHEMA/CODEMETA-2.0" xmlns:codemeta="https://doi.org/10.5063/SCHEMA/CODEMETA-2.0"
xmlns:schema="http://schema.org/"
xmlns:swh="https://www.softwareheritage.org/schema/2018/deposit"> xmlns:swh="https://www.softwareheritage.org/schema/2018/deposit">
<title>Awesome Compiler</title> <title>Awesome Compiler</title>
<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id> <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
...@@ -9,5 +10,8 @@ ...@@ -9,5 +10,8 @@
<swh:reference> <swh:reference>
<swh:object swhid="{swhid}" /> <swh:object swhid="{swhid}" />
</swh:reference> </swh:reference>
<swh:metadata-provenance>
<schema:url>https://inria.halpreprod.archives-ouvertes.fr/hal-abcdefgh</schema:url>
</swh:metadata-provenance>
</swh:deposit> </swh:deposit>
</entry> </entry>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment