Skip to content

Commit ba8b549

Browse files
author
notactuallyfinn
committed
add codemeta_doi postprocess plugin
1 parent d514c9f commit ba8b549

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ rodare = "hermes.commands.deposit.rodare:RodareDepositPlugin"
7070
config_invenio_record_id = "hermes.commands.postprocess.invenio:config_record_id"
7171
config_invenio_rdm_record_id = "hermes.commands.postprocess.invenio_rdm:config_record_id"
7272
cff_doi = "hermes.commands.postprocess.invenio:cff_doi"
73+
codemeta_doi = "hermes.commands.postprocess.invenio:codemeta_doi"
7374

7475
[project.entry-points."hermes.process"]
7576
codemeta = "hermes.commands.process.standard_merge:CodemetaProcessPlugin"

src/hermes/commands/postprocess/invenio.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# SPDX-FileContributor: Michael Fritzsche
77
# SPDX-FileContributor: Stephan Druskat
88

9+
import json
910
import logging
1011

1112
from ruamel.yaml import YAML
@@ -73,3 +74,28 @@ def __call__(self, command: HermesCommand):
7374
yaml.dump(cff, open('CITATION.cff', 'w'))
7475
except Exception as e:
7576
raise RuntimeError("Update of CITATION.cff failed.") from e
77+
78+
79+
class codemeta_doi(HermesPostprocessPlugin):
80+
def __call__(self, command: HermesCommand):
81+
ctx = HermesContext()
82+
ctx.prepare_step("deposit")
83+
with ctx["invenio"] as manager:
84+
deposition = manager["result"]
85+
ctx.finalize_step("deposit")
86+
87+
try:
88+
with open("codemeta.json", "r") as file:
89+
codemeta = json.load(file)
90+
if "@id" not in codemeta:
91+
codemeta["@id"] = deposition['doi']
92+
if "referencePublication" not in codemeta:
93+
codemeta["referencePublication"] = deposition['doi']
94+
elif isinstance(codemeta["referencePublication"], list):
95+
codemeta["referencePublication"].append(deposition['doi'])
96+
else:
97+
codemeta["referencePublication"] = [codemeta["referencePublication"], deposition['doi']]
98+
with open("codemeta.json", "w") as file:
99+
json.dump(codemeta, file)
100+
except Exception as e:
101+
raise RuntimeError("Update of CITATION.cff failed.") from e

test/hermes_test/commands/postprocess/test_invenio_postprocess.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
# SPDX-FileContributor: Michael Fritzsche
66

7+
import json
78
import sys
89

910
from ruamel import yaml
@@ -23,10 +24,20 @@ def test_invenio_postprocess(tmp_path, monkeypatch):
2324
citation_file = tmp_path / "CITATION.cff"
2425
citation_file.write_text("cff-version: 1.2.0\ntitle: Test")
2526

27+
codemeta_file = tmp_path / "codemeta.json"
28+
codemeta_file.write_text(
29+
"""{
30+
"@context": "https://doi.org/10.5063/schema/codemeta-2.0",
31+
"type": "SoftwareSourceCode",
32+
"name": "Test"
33+
}
34+
"""
35+
)
36+
2637
config_file = tmp_path / "hermes.toml"
2738
config_file.write_text(
2839
"""[postprocess]
29-
run = ["config_invenio_record_id", "cff_doi"]
40+
run = ["config_invenio_record_id", "cff_doi", "codemeta_doi"]
3041
[deposit.invenio]
3142
site_url = "https://zenodo.org"
3243
"""
@@ -45,11 +56,12 @@ def test_invenio_postprocess(tmp_path, monkeypatch):
4556
finally:
4657
result_toml = toml.load(config_file)
4758
result_cff = yaml.YAML().load(citation_file)
59+
result_codemeta = json.loads(codemeta_file.read_text())
4860
sys.argv = orig_argv
4961

5062
assert result_toml == toml.loads(
5163
"""[postprocess]
52-
run = ["config_invenio_record_id", "cff_doi"]
64+
run = ["config_invenio_record_id", "cff_doi", "codemeta_doi"]
5365
[deposit.invenio]
5466
site_url = "https://zenodo.org"
5567
record_id = "foo"
@@ -64,3 +76,13 @@ def test_invenio_postprocess(tmp_path, monkeypatch):
6476
description: DOI for the published version 1.0.0 [generated by hermes]
6577
"""
6678
)
79+
assert result_codemeta == json.loads(
80+
"""{
81+
"@context": "https://doi.org/10.5063/schema/codemeta-2.0",
82+
"type": "SoftwareSourceCode",
83+
"@id": "my_doi",
84+
"name": "Test",
85+
"referencePublication": "my_doi"
86+
}
87+
"""
88+
)

0 commit comments

Comments
 (0)