diff --git a/swh/deposit/cli/client.py b/swh/deposit/cli/client.py --- a/swh/deposit/cli/client.py +++ b/swh/deposit/cli/client.py @@ -60,7 +60,7 @@ def generate_metadata( - deposit_client: str, name: str, external_id: str, authors: List[str] + deposit_client: str, name: str, external_id: Optional[str], authors: List[str] ) -> str: """Generate sword compliant xml metadata with the minimum required metadata. @@ -94,11 +94,10 @@ import xmltodict # generate a metadata file with the minimum required metadata - codemetadata = { + document = { "atom:entry": { "@xmlns:atom": "http://www.w3.org/2005/Atom", "@xmlns:codemeta": "https://doi.org/10.5063/SCHEMA/CODEMETA-2.0", - "codemeta:identifier": external_id, "atom:updated": datetime.now(tz=timezone.utc), # mandatory, cf. docstring "atom:author": deposit_client, # mandatory, cf. docstring "atom:title": name, # mandatory, cf. docstring @@ -108,8 +107,11 @@ ], }, } - logging.debug("Metadata dict to generate as xml: %s", codemetadata) - return xmltodict.unparse(codemetadata, pretty=True) + if external_id: + document["atom:entry"]["codemeta:identifier"] = external_id + + logging.debug("Atom entry dict to generate as xml: %s", document) + return xmltodict.unparse(document, pretty=True) def _collection(client: PublicApiDepositClient) -> str: @@ -183,10 +185,6 @@ "swhid": optional deposit swhid "replace": whether the given deposit is to be replaced or not """ - - if not slug: # generate one as this is mandatory - slug = generate_slug() - if not metadata: if name and authors: metadata_path = os.path.join(temp_dir, "metadata.xml") diff --git a/swh/deposit/client.py b/swh/deposit/client.py --- a/swh/deposit/client.py +++ b/swh/deposit/client.py @@ -474,13 +474,15 @@ """Post an archive (binary) deposit client.""" def compute_headers(self, info): - return { - "SLUG": info["slug"], + headers = { "CONTENT_MD5": info["md5sum"], "IN-PROGRESS": str(info["in_progress"]), "CONTENT-TYPE": info["content-type"], "CONTENT-DISPOSITION": "attachment; filename=%s" % (info["filename"],), } + if "slug" in info: + headers["SLUG"] = info["slug"] + return headers def compute_information(self, *args, **kwargs) -> Dict[str, Any]: info = compute_unified_information( @@ -504,11 +506,13 @@ """Post a metadata deposit client.""" def compute_headers(self, info): - return { - "SLUG": info["slug"], + headers = { "IN-PROGRESS": str(info["in_progress"]), "CONTENT-TYPE": "application/atom+xml;type=entry", } + if "slug" in info: + headers["SLUG"] = info["slug"] + return headers def compute_information(self, *args, **kwargs) -> Dict[str, Any]: info = compute_unified_information( @@ -561,10 +565,11 @@ ] headers = { - "SLUG": info["slug"], "CONTENT_MD5": info["md5sum"], "IN-PROGRESS": str(info["in_progress"]), } + if "slug" in info: + headers["SLUG"] = info["slug"] return files, headers @@ -606,7 +611,7 @@ def deposit_create( self, collection: str, - slug: str, + slug: Optional[str], archive: Optional[str] = None, metadata: Optional[str] = None, in_progress: bool = False, @@ -635,7 +640,7 @@ self, collection: str, deposit_id: int, - slug: str, + slug: Optional[str], archive: Optional[str] = None, metadata: Optional[str] = None, in_progress: bool = False, diff --git a/swh/deposit/tests/cli/test_client.py b/swh/deposit/tests/cli/test_client.py --- a/swh/deposit/tests/cli/test_client.py +++ b/swh/deposit/tests/cli/test_client.py @@ -379,9 +379,9 @@ def test_cli_single_deposit_slug_generation( sample_archive, patched_tmp_path, requests_mock_datadir, cli_runner ): - """Single deposit scenario without providing the slug, the slug is generated nonetheless - https://docs.softwareheritage.org/devel/swh-deposit/getting-started.html#single-deposit - """ # noqa + """Single deposit scenario without providing the slug, it should + not be generated. + """ metadata_path = os.path.join(patched_tmp_path, "metadata.xml") # fmt: off result = cli_runner.invoke( @@ -409,7 +409,7 @@ with open(metadata_path) as fd: metadata_xml = fd.read() actual_metadata = dict(parse_xml(metadata_xml)) - assert actual_metadata["codemeta:identifier"] is not None + assert "codemeta:identifier" not in actual_metadata def test_cli_multisteps_deposit(