| 1 |
""" |
| 2 |
Update CITATION.cff and README.md with the latest Zenodo and Software Heritage records |
| 3 |
""" |
| 4 |
import os |
| 5 |
import re |
| 6 |
import subprocess |
| 7 |
|
| 8 |
import pyaml |
| 9 |
import requests |
| 10 |
import yaml |
| 11 |
|
| 12 |
basedir = os.path.abspath( |
| 13 |
os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..") |
| 14 |
) |
| 15 |
|
| 16 |
# Get the latest tag from git |
| 17 |
tag = ( |
| 18 |
subprocess.check_output(["git", "describe", "--tags", "--abbrev=0"]) |
| 19 |
.strip() |
| 20 |
.decode("utf-8") |
| 21 |
) |
| 22 |
|
| 23 |
print("Latest tag/release:", tag) |
| 24 |
ZENODO_API = "https://zenodo.org/api" |
| 25 |
# DOI always linking to the latest version (escaped slash for elasticsearch syntax) |
| 26 |
CONCEPT_DOI = r"10.5281\/zenodo.654861" |
| 27 |
|
| 28 |
print("Searching for Zenodo record...") |
| 29 |
# Get the latest version via concept doi |
| 30 |
# The Zenodo search guide says that one can directly search by "version:...", but it does not work (BAD REQUEST) |
| 31 |
r = requests.get(ZENODO_API + "/records", params={"q": f"conceptdoi:{CONCEPT_DOI}"}) |
| 32 |
if not r.ok: |
| 33 |
raise RuntimeError("Request failed: ", r.reason) |
| 34 |
data = r.json() |
| 35 |
assert data["hits"]["total"] == 1 |
| 36 |
latest_zenodo_id = data["hits"]["hits"][0]["id"] |
| 37 |
|
| 38 |
# Get all versions |
| 39 |
r = requests.get(ZENODO_API + f"/records/{latest_zenodo_id}/versions") |
| 40 |
if not r.ok: |
| 41 |
raise RuntimeError("Request failed: ", r.reason) |
| 42 |
data = r.json() |
| 43 |
versions = data["hits"]["hits"] |
| 44 |
latest_version = [v for v in versions if v["metadata"]["version"] == tag] |
| 45 |
if latest_version: |
| 46 |
zenodo_record = latest_version[0] |
| 47 |
print("Found Zenodo record for version", tag) |
| 48 |
else: |
| 49 |
raise RuntimeError("No Zenodo record found for version " + tag) |
| 50 |
|
| 51 |
print("Searching for SWH record...") |
| 52 |
|
| 53 |
# Find Software Heritage |
| 54 |
resp = requests.get( |
| 55 |
"https://archive.softwareheritage.org/api/1/origin/https://github.com/brian-team/brian2/get/" |
| 56 |
) |
| 57 |
if not resp.ok: |
| 58 |
raise RuntimeError("Request failed: ", resp.reason) |
| 59 |
data = resp.json() |
| 60 |
visits_url = data["origin_visits_url"] |
| 61 |
resp = requests.get(visits_url) |
| 62 |
if not resp.ok: |
| 63 |
raise RuntimeError("Request failed: ", resp.reason) |
| 64 |
data = resp.json() |
| 65 |
latest_visit = sorted(data, key=lambda x: x["date"], reverse=True)[0] |
| 66 |
snapshot_url = latest_visit["snapshot_url"] |
| 67 |
resp = requests.get(snapshot_url) |
| 68 |
if not resp.ok: |
| 69 |
raise RuntimeError("Request failed: ", resp.reason) |
| 70 |
data = resp.json() |
| 71 |
swh_record = data["branches"].get(f"refs/tags/{tag}") |
| 72 |
|
| 73 |
if swh_record: |
| 74 |
print("Found SWH record for version", tag) |
| 75 |
else: |
| 76 |
swh_record = None |
| 77 |
print("No SWH record found for version", tag) |
| 78 |
|
| 79 |
print("Updating CITATION.cff and README.md...") |
| 80 |
with open(os.path.join(basedir, "CITATION.cff"), "r") as f: |
| 81 |
citation_cff = yaml.load(f, Loader=yaml.SafeLoader) |
| 82 |
for identifier in citation_cff["identifiers"]: |
| 83 |
if zenodo_record and identifier["description"].startswith( |
| 84 |
"This is the archived snapshot of version" |
| 85 |
): |
| 86 |
identifier["value"] = zenodo_record["metadata"]["doi"] |
| 87 |
identifier["description"] = ( |
| 88 |
f"This is the archived snapshot of version {tag} of Brian 2" |
| 89 |
) |
| 90 |
if swh_record and identifier["type"] == "swh": |
| 91 |
identifier["value"] = f"swh:1:rel:{swh_record['target']}" |
| 92 |
identifier["description"] = ( |
| 93 |
f"Software Heritage identifier for version {tag} of Brian 2" |
| 94 |
) |
| 95 |
|
| 96 |
with open(os.path.join(basedir, "CITATION.cff"), "w") as f: |
| 97 |
pyaml.dump(citation_cff, f, sort_keys=False) |
| 98 |
|
| 99 |
with open(os.path.join(basedir, "README.md"), "r") as f: |
| 100 |
readme = f.read() |
| 101 |
|
| 102 |
if zenodo_record: |
| 103 |
# Replace the old DOI with the new one |
| 104 |
readme = re.sub( |
| 105 |
r"\[!\[DOI\]\(https://zenodo.org/badge/DOI/.*\.svg\)\]\(https://zenodo.org/doi/.*\)", |
| 106 |
f"[](https://zenodo.org/doi/{zenodo_record['metadata']['doi']})", |
| 107 |
readme, |
| 108 |
) |
| 109 |
|
| 110 |
if swh_record: |
| 111 |
# Replace the old SWH badge with the new one |
| 112 |
# [](https://archive.softwareheritage.org/swh:1:rel:2d4c5c8c8a6d2318332889df93ab74aef53e2c61;origin=https://github.com/brian-team/brian2;visit=swh:1:snp:a90ab7416901a9c5cf6f56d68b3455c65d322afc) |
| 113 |
readme = re.sub( |
| 114 |
r"\[!\[Software Heritage \(release\)\]\(https://archive.softwareheritage.org/badge/swh:1:rel:.*\)\]\(https://archive.softwareheritage.org/swh:1:rel:.*;origin=.*\)", |
| 115 |
f"[](https://archive.softwareheritage.org/swh:1:rel:{swh_record['target']};origin=https://github.com/brian-team/brian2;visit=swh:1:snp:{latest_visit['snapshot']})", |
| 116 |
readme, |
| 117 |
) |
| 118 |
|
| 119 |
with open(os.path.join(basedir, "README.md"), "w") as f: |
| 120 |
f.write(readme) |