omega-principia
This project has not described itself yet.
Brought by drakestapleton
omega-principia / scripts / zenodo_metadata_cleanup.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | # scripts/zenodo_metadata_cleanup.sh -- Metadata-only Zenodo cleanup (ZERO new DOIs minted) |
| 4 | # Normalizes creator name ("Stapleton, M. Drake", ORCID 0009-0002-6890-7111), fills missing version ("1.0.0"), |
| 5 | # unifies publication_type ("preprint" when upload_type=="publication" and publication_type is "article" or empty), |
| 6 | # adds GitHub + Interplane isSupplementedBy links on OMSC01 records, and cross-links split Concept DOI pairs. |
| 7 | # Uses ONLY /actions/edit -> PUT -> /actions/publish (NEVER creates deposits or new versions; 0 new DOIs). |
| 8 | # |
| 9 | # Usage: |
| 10 | # sh scripts/zenodo_metadata_cleanup.sh --self-test |
| 11 | # sh scripts/zenodo_metadata_cleanup.sh --dry-run <before_audit_csv> [--only-non-omsc01] |
| 12 | # sh scripts/zenodo_metadata_cleanup.sh --canary <record_id> [add_repo:0|1] |
| 13 | # sh scripts/zenodo_metadata_cleanup.sh --all <before_audit_csv> [log_path] [--only-non-omsc01] |
| 14 | set -eu |
| 15 | |
| 16 | MODE="${1:-}" |
| 17 | ARG="${2:-}" |
| 18 | LOG_PATH="receipts/zenodo_cleanup_log.txt" |
| 19 | ADD_REPO_CANARY="auto" |
| 20 | ONLY_NON_OMSC01="0" |
| 21 | ORCID="0009-0002-6890-7111" |
| 22 | GITHUB_REPO_URL="https://github.com/drakestapleton/omega-principia" |
| 23 | INTERPLANE_REPO_URL="https://interplane.io/drakestapleton/omega-principia" |
| 24 | ZEN_EP="https://zenodo.org/api/deposit/depositions" |
| 25 | |
| 26 | if [ "$MODE" != "--self-test" ] && [ "$MODE" != "--dry-run" ] && [ "$MODE" != "--canary" ] && [ "$MODE" != "--all" ]; then |
| 27 | echo "Usage: $0 {--self-test | --dry-run <audit_csv> [--only-non-omsc01] | --canary <record_id> [--no-repo|--add-repo|0|1] | --all <audit_csv> [log_path] [--only-non-omsc01]}" >&2 |
| 28 | exit 2 |
| 29 | fi |
| 30 | if [ "$MODE" != "--self-test" ] && [ -z "$ARG" ]; then |
| 31 | echo "FAIL: missing argument for $MODE" >&2 |
| 32 | exit 2 |
| 33 | fi |
| 34 | shift 2 2>/dev/null || true |
| 35 | while [ $# -gt 0 ]; do |
| 36 | case "$1" in |
| 37 | --only-non-omsc01) ONLY_NON_OMSC01="1"; shift ;; |
| 38 | --no-repo) ADD_REPO_CANARY="0"; shift ;; |
| 39 | --add-repo) ADD_REPO_CANARY="1"; shift ;; |
| 40 | *) |
| 41 | if [ "$MODE" = "--canary" ]; then |
| 42 | ADD_REPO_CANARY="$1" |
| 43 | else |
| 44 | LOG_PATH="$1" |
| 45 | fi |
| 46 | shift |
| 47 | ;; |
| 48 | esac |
| 49 | done |
| 50 | |
| 51 | TMP="$(mktemp -d)" |
| 52 | trap 'rm -rf "$TMP"' EXIT |
| 53 | |
| 54 | # Exact 16 split Concept DOI pairs (linking each record to its partner's Concept DOI so links always resolve to latest version) |
| 55 | # All 32 Concept DOIs verified directly from receipts/zenodo_111_audit_before.csv |
| 56 | get_pair_link() { |
| 57 | case "$1" in |
| 58 | 24711245) printf 'isNewVersionOf 10.5281/zenodo.24710819' ;; |
| 59 | 24710820) printf 'isPreviousVersionOf 10.5281/zenodo.24711244' ;; |
| 60 | 24711155) printf 'isNewVersionOf 10.5281/zenodo.24710167' ;; |
| 61 | 24710168) printf 'isPreviousVersionOf 10.5281/zenodo.24711154' ;; |
| 62 | 24710621) printf 'isNewVersionOf 10.5281/zenodo.24710178' ;; |
| 63 | 24710179) printf 'isPreviousVersionOf 10.5281/zenodo.24710620' ;; |
| 64 | 24710214) printf 'isNewVersionOf 10.5281/zenodo.24709443' ;; |
| 65 | 24709444) printf 'isPreviousVersionOf 10.5281/zenodo.24710213' ;; |
| 66 | 24709937) printf 'isNewVersionOf 10.5281/zenodo.24709167' ;; |
| 67 | 24709168) printf 'isPreviousVersionOf 10.5281/zenodo.24709936' ;; |
| 68 | 24709453) printf 'isNewVersionOf 10.5281/zenodo.24708912' ;; |
| 69 | 24708913) printf 'isPreviousVersionOf 10.5281/zenodo.24709452' ;; |
| 70 | 24709115) printf 'isNewVersionOf 10.5281/zenodo.24708397' ;; |
| 71 | 24708398) printf 'isPreviousVersionOf 10.5281/zenodo.24709114' ;; |
| 72 | 24708653) printf 'isNewVersionOf 10.5281/zenodo.24708135' ;; |
| 73 | 24708136) printf 'isPreviousVersionOf 10.5281/zenodo.24708652' ;; |
| 74 | 24708371) printf 'isNewVersionOf 10.5281/zenodo.24707546' ;; |
| 75 | 24707547) printf 'isPreviousVersionOf 10.5281/zenodo.24708370' ;; |
| 76 | 24707892) printf 'isNewVersionOf 10.5281/zenodo.24707506' ;; |
| 77 | 24707507) printf 'isPreviousVersionOf 10.5281/zenodo.24707891' ;; |
| 78 | 24707346) printf 'isNewVersionOf 10.5281/zenodo.24706918' ;; |
| 79 | 24706919) printf 'isPreviousVersionOf 10.5281/zenodo.24707345' ;; |
| 80 | 24707215) printf 'isNewVersionOf 10.5281/zenodo.24706425' ;; |
| 81 | 24706426) printf 'isPreviousVersionOf 10.5281/zenodo.24707214' ;; |
| 82 | 24706866) printf 'isNewVersionOf 10.5281/zenodo.24706517' ;; |
| 83 | 24706518) printf 'isPreviousVersionOf 10.5281/zenodo.24706865' ;; |
| 84 | 24706230) printf 'isNewVersionOf 10.5281/zenodo.24705839' ;; |
| 85 | 24705840) printf 'isPreviousVersionOf 10.5281/zenodo.24706229' ;; |
| 86 | 24705871) printf 'isNewVersionOf 10.5281/zenodo.24704996' ;; |
| 87 | 24704997) printf 'isPreviousVersionOf 10.5281/zenodo.24705870' ;; |
| 88 | 24375848) printf 'isNewVersionOf 10.5281/zenodo.24236118' ;; |
| 89 | 24236254) printf 'isPreviousVersionOf 10.5281/zenodo.24236688' ;; |
| 90 | *) printf '' ;; |
| 91 | esac |
| 92 | } |
| 93 | |
| 94 | # Pure jq metadata transform (metadata-only; preserves all unrelated fields verbatim) |
| 95 | build_clean_metadata_payload() { |
| 96 | in_dep_json="$1" |
| 97 | out_put_json="$2" |
| 98 | rel_type="${3:-}" |
| 99 | rel_doi="${4:-}" |
| 100 | add_repo="${5:-0}" |
| 101 | |
| 102 | jq \ |
| 103 | --arg orcid "$ORCID" \ |
| 104 | --arg rtype "$rel_type" \ |
| 105 | --arg rdoi "$rel_doi" \ |
| 106 | --arg add_repo "$add_repo" \ |
| 107 | --arg gh_repo "$GITHUB_REPO_URL" \ |
| 108 | --arg ip_repo "$INTERPLANE_REPO_URL" ' |
| 109 | { |
| 110 | metadata: ( |
| 111 | .metadata |
| 112 | | .creators = ((.creators // []) | map( |
| 113 | if ((.orcid // "") == $orcid) or ((.name // "") | test("Stapleton"; "i")) then |
| 114 | .name = "Stapleton, M. Drake" |
| 115 | | .orcid = $orcid |
| 116 | | if ((.affiliation // "") == "") then .affiliation = "AIEN" else . end |
| 117 | else . end |
| 118 | )) |
| 119 | | if ((.version // "") == "") then .version = "1.0.0" else . end |
| 120 | | if ((.upload_type // "") == "publication" and ((.publication_type // "") == "article" or (.publication_type // "") == "")) then |
| 121 | .publication_type = "preprint" |
| 122 | else . end |
| 123 | | if ($rtype != "" and $rdoi != "") then |
| 124 | .related_identifiers = ( |
| 125 | (.related_identifiers // []) |
| 126 | | if any(.identifier == $rdoi and .relation == $rtype) then . |
| 127 | else . + [{"identifier": $rdoi, "relation": $rtype, "scheme": "doi"}] |
| 128 | end |
| 129 | ) |
| 130 | else . end |
| 131 | | if ($add_repo == "1") then |
| 132 | .related_identifiers = ( |
| 133 | (.related_identifiers // []) |
| 134 | | (if any(.identifier == $gh_repo and .relation == "isSupplementedBy") then . |
| 135 | else . + [{"identifier": $gh_repo, "relation": "isSupplementedBy", "scheme": "url"}] |
| 136 | end) |
| 137 | | (if any(.identifier == $ip_repo and .relation == "isSupplementedBy") then . |
| 138 | else . + [{"identifier": $ip_repo, "relation": "isSupplementedBy", "scheme": "url"}] |
| 139 | end) |
| 140 | ) |
| 141 | else . end |
| 142 | ) |
| 143 | } |
| 144 | ' "$in_dep_json" > "$out_put_json" |
| 145 | jq empty "$out_put_json" |
| 146 | } |
| 147 | |
| 148 | # Header-aware RFC4180 CSV parser emitting TSV: |
| 149 | # record_id \t creator_name \t resource_type \t version \t has_omp \t has_repo_link \t related_ver_links |
| 150 | parse_audit_csv_to_tsv() { |
| 151 | csv_in="$1" |
| 152 | tsv_out="$2" |
| 153 | awk ' |
| 154 | function parse_csv(line, arr, i, n, len, c, inq, field) { |
| 155 | n = 0; inq = 0; field = ""; len = length(line) |
| 156 | sub(/\r$/, "", line); len = length(line) |
| 157 | for (i = 1; i <= len; i++) { |
| 158 | c = substr(line, i, 1) |
| 159 | if (inq) { |
| 160 | if (c == "\"") { |
| 161 | if (i < len && substr(line, i + 1, 1) == "\"") { |
| 162 | field = field "\"" |
| 163 | i++ |
| 164 | } else { |
| 165 | inq = 0 |
| 166 | } |
| 167 | } else { |
| 168 | field = field c |
| 169 | } |
| 170 | } else { |
| 171 | if (c == "\"") { |
| 172 | inq = 1 |
| 173 | } else if (c == ",") { |
| 174 | arr[++n] = field |
| 175 | field = "" |
| 176 | } else { |
| 177 | field = field c |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | arr[++n] = field |
| 182 | return n |
| 183 | } |
| 184 | NR == 1 { |
| 185 | nf = parse_csv($0, hdr) |
| 186 | for (k = 1; k <= nf; k++) col[hdr[k]] = k |
| 187 | if (!col["record_id"] || !col["creator_name"] || !col["resource_type"] || !col["version"] || !col["has_omp"]) { |
| 188 | print "FAIL: audit CSV missing required header columns" > "/dev/stderr" |
| 189 | exit 2 |
| 190 | } |
| 191 | next |
| 192 | } |
| 193 | NF == 0 { next } |
| 194 | { |
| 195 | nf = parse_csv($0, f) |
| 196 | rid = f[col["record_id"]] |
| 197 | if (rid == "") next |
| 198 | cname = f[col["creator_name"]] |
| 199 | rtype = f[col["resource_type"]] |
| 200 | ver = f[col["version"]] |
| 201 | homp = f[col["has_omp"]] |
| 202 | hrepo = (col["has_repo_link"] ? f[col["has_repo_link"]] : "0") |
| 203 | rlink = (col["related_ver_links"] ? f[col["related_ver_links"]] : "0") |
| 204 | gsub(/\037/, " ", cname) |
| 205 | printf "%s\037%s\037%s\037%s\037%s\037%s\037%s\037_\n", rid, cname, rtype, ver, homp, hrepo, rlink |
| 206 | } |
| 207 | ' "$csv_in" > "$tsv_out" |
| 208 | } |
| 209 | |
| 210 | record_needs_cleanup() { |
| 211 | rid="$1" |
| 212 | cname="$2" |
| 213 | rtype="$3" |
| 214 | ver="$4" |
| 215 | has_omp="$5" |
| 216 | has_repo="$6" |
| 217 | rlinks="$7" |
| 218 | pair_info="$(get_pair_link "$rid")" |
| 219 | |
| 220 | [ "$cname" != "Stapleton, M. Drake" ] && return 0 |
| 221 | [ -z "$ver" ] && return 0 |
| 222 | case "$rtype" in |
| 223 | article|publication-article|"") return 0 ;; |
| 224 | esac |
| 225 | if [ -n "$pair_info" ] && [ "${rlinks:-0}" -eq 0 ] 2>/dev/null; then |
| 226 | return 0 |
| 227 | fi |
| 228 | if [ "$has_omp" = "1" ] && [ "$has_repo" != "1" ]; then |
| 229 | return 0 |
| 230 | fi |
| 231 | return 1 |
| 232 | } |
| 233 | |
| 234 | if [ "$MODE" = "--self-test" ]; then |
| 235 | # 1. Test jq metadata transform (author, ORCID, version, article->preprint, pair link, GitHub+Interplane links, idempotent) |
| 236 | cat > "$TMP/dep.json" <<'EOF' |
| 237 | { |
| 238 | "id": 24711245, |
| 239 | "conceptrecid": "24711244", |
| 240 | "doi": "10.5281/zenodo.24711245", |
| 241 | "metadata": { |
| 242 | "title": "Demo Paper", |
| 243 | "upload_type": "publication", |
| 244 | "publication_type": "article", |
| 245 | "creators": [{"name": "Stapleton, Drake", "orcid": "0009-0002-6890-7111"}], |
| 246 | "keywords": ["Omega"] |
| 247 | } |
| 248 | } |
| 249 | EOF |
| 250 | pi="$(get_pair_link 24711245)" |
| 251 | rt="$(printf '%s' "$pi" | awk '{print $1}')" |
| 252 | rd="$(printf '%s' "$pi" | awk '{print $2}')" |
| 253 | [ "$rt" = "isNewVersionOf" ] && [ "$rd" = "10.5281/zenodo.24710819" ] || { echo "FAIL: Concept DOI pair link lookup (got '$pi')" >&2; exit 1; } |
| 254 | [ "$(get_pair_link 24375848)" = "isNewVersionOf 10.5281/zenodo.24236118" ] || { echo "FAIL: 24375848 Concept DOI link" >&2; exit 1; } |
| 255 | [ "$(get_pair_link 24236254)" = "isPreviousVersionOf 10.5281/zenodo.24236688" ] || { echo "FAIL: 24236254 Concept DOI link" >&2; exit 1; } |
| 256 | build_clean_metadata_payload "$TMP/dep.json" "$TMP/put1.json" "$rt" "$rd" 1 |
| 257 | # Second pass idempotency check |
| 258 | build_clean_metadata_payload "$TMP/put1.json" "$TMP/put2.json" "$rt" "$rd" 1 |
| 259 | [ "$(jq -r '.metadata.creators[0].name' "$TMP/put2.json")" = "Stapleton, M. Drake" ] || { echo "FAIL: creator" >&2; exit 1; } |
| 260 | [ "$(jq -r '.metadata.version' "$TMP/put2.json")" = "1.0.0" ] || { echo "FAIL: version" >&2; exit 1; } |
| 261 | [ "$(jq -r '.metadata.publication_type' "$TMP/put2.json")" = "preprint" ] || { echo "FAIL: pubtype" >&2; exit 1; } |
| 262 | [ "$(jq -r '.metadata.related_identifiers | length' "$TMP/put2.json")" -eq 3 ] || { echo "FAIL: expected 3 related_identifiers (pair+GitHub+Interplane)" >&2; exit 1; } |
| 263 | |
| 264 | # 1b. Verify restricted record (e.g. 24236254) preserves access_right & access_conditions verbatim and technicalnote |
| 265 | cat > "$TMP/dep_restr.json" <<'EOF' |
| 266 | { |
| 267 | "id": 24236254, |
| 268 | "conceptrecid": "24236118", |
| 269 | "doi": "10.5281/zenodo.24236254", |
| 270 | "metadata": { |
| 271 | "title": "Restricted Early Note", |
| 272 | "upload_type": "publication", |
| 273 | "publication_type": "technicalnote", |
| 274 | "access_right": "restricted", |
| 275 | "access_conditions": "Restricted archive", |
| 276 | "creators": [{"name": "Stapleton, M. Drake", "orcid": "0009-0002-6890-7111"}] |
| 277 | } |
| 278 | } |
| 279 | EOF |
| 280 | build_clean_metadata_payload "$TMP/dep_restr.json" "$TMP/put_restr.json" "isPreviousVersionOf" "10.5281/zenodo.24236688" 0 |
| 281 | [ "$(jq -r '.metadata.access_right' "$TMP/put_restr.json")" = "restricted" ] || { echo "FAIL: access_right changed!" >&2; exit 1; } |
| 282 | [ "$(jq -r '.metadata.access_conditions' "$TMP/put_restr.json")" = "Restricted archive" ] || { echo "FAIL: access_conditions changed!" >&2; exit 1; } |
| 283 | [ "$(jq -r '.metadata.publication_type' "$TMP/put_restr.json")" = "technicalnote" ] || { echo "FAIL: technicalnote overwritten!" >&2; exit 1; } |
| 284 | [ "$(jq -r '.metadata.related_identifiers | length' "$TMP/put_restr.json")" -eq 1 ] || { echo "FAIL: non-OMSC01 should only have pair link" >&2; exit 1; } |
| 285 | |
| 286 | # 2. Test header-aware RFC4180 CSV parser on BOTH 21-col and 30-col schemas with quoted commas |
| 287 | cat > "$TMP/audit30.csv" <<'EOF' |
| 288 | record_id,conceptrecid,conceptdoi,doi,pub_date,creator_name,orcid_ok,license,resource_type,version,file_count,empty_files,has_pdf,has_tex,has_omp,has_lean,lean_count,has_om,om_count,has_sums,has_repo_link,sums_lines,non_manifest_files,sums_covered,pdf_in_sums,sums_status,related_ver_links,pair_role,file_list,title |
| 289 | 24711245,24711244,10.5281/zenodo.24711244,10.5281/zenodo.24711245,2026-12-01,"Stapleton, Drake",1,cc-by-4.0,article,,5,0,1,1,1,1,1,0,0,1,0,10,4,0,0,FAIL_STALE,0,newer,"a.pdf|b.tex","Title, with ""quotes"" and commas" |
| 290 | 24662414,24662413,10.5281/zenodo.24662413,10.5281/zenodo.24662414,2026-11-29,"Stapleton, M. Drake",1,cc-by-4.0,preprint,1.0.0,13,0,1,1,1,1,2,1,4,1,1,12,12,11,0,PARTIAL_NO_PDF,0,solo,"a.pdf|b.om","Clean Paper" |
| 291 | EOF |
| 292 | parse_audit_csv_to_tsv "$TMP/audit30.csv" "$TMP/parsed.tsv" |
| 293 | [ "$(wc -l < "$TMP/parsed.tsv" | tr -d ' ')" -eq 2 ] || { echo "FAIL: CSV parser row count" >&2; exit 1; } |
| 294 | r1_need=0; r2_need=0; r1_fields_ok=0 |
| 295 | US="$(printf '\037')" |
| 296 | while IFS="$US" read -r rid cname rtype ver homp hrepo rlink _end; do |
| 297 | if [ "$rid" = "24711245" ]; then |
| 298 | # Explicitly verify empty version does NOT collapse/shift trailing columns! |
| 299 | [ "$cname" = "Stapleton, Drake" ] && [ "$rtype" = "article" ] && [ -z "$ver" ] && [ "$homp" = "1" ] && [ "$hrepo" = "0" ] && [ "$rlink" = "0" ] && r1_fields_ok=1 |
| 300 | fi |
| 301 | if record_needs_cleanup "$rid" "$cname" "$rtype" "$ver" "$homp" "$hrepo" "$rlink"; then |
| 302 | [ "$rid" = "24711245" ] && r1_need=1 |
| 303 | [ "$rid" = "24662414" ] && r2_need=1 |
| 304 | fi |
| 305 | done < "$TMP/parsed.tsv" |
| 306 | [ "$r1_fields_ok" -eq 1 ] || { echo "FAIL: empty-field shift detected in CSV->US parser!" >&2; exit 1; } |
| 307 | [ "$r1_need" -eq 1 ] && [ "$r2_need" -eq 0 ] || { echo "FAIL: cleanup predicate r1=$r1_need r2=$r2_need" >&2; exit 1; } |
| 308 | echo "ZENODO_METADATA_CLEANUP_SELFTEST: PASS (idempotent jq M.Drake+ORCID+preprint+GitHub+Interplane+16-pairs, 30-col RFC4180 CSV+US parser zero-shift, 0 new DOI guarantee)" |
| 309 | exit 0 |
| 310 | fi |
| 311 | |
| 312 | if [ "$MODE" = "--dry-run" ]; then |
| 313 | [ -f "$ARG" ] || { echo "FAIL: audit CSV not found: $ARG" >&2; exit 1; } |
| 314 | parse_audit_csv_to_tsv "$ARG" "$TMP/parsed.tsv" |
| 315 | total=0; would_edit=0; would_skip=0; skipped_omsc01=0; pair_hits=0 |
| 316 | US="$(printf '\037')" |
| 317 | while IFS="$US" read -r rid cname rtype ver homp hrepo rlink _end; do |
| 318 | [ -n "$rid" ] || continue |
| 319 | total=$((total + 1)) |
| 320 | if [ "$ONLY_NON_OMSC01" = "1" ] && [ "$homp" = "1" ]; then |
| 321 | skipped_omsc01=$((skipped_omsc01 + 1)) |
| 322 | continue |
| 323 | fi |
| 324 | [ -n "$(get_pair_link "$rid")" ] && pair_hits=$((pair_hits + 1)) |
| 325 | if record_needs_cleanup "$rid" "$cname" "$rtype" "$ver" "$homp" "$hrepo" "$rlink"; then |
| 326 | would_edit=$((would_edit + 1)) |
| 327 | else |
| 328 | would_skip=$((would_skip + 1)) |
| 329 | fi |
| 330 | done < "$TMP/parsed.tsv" |
| 331 | echo "ZENODO_METADATA_CLEANUP_DRY_RUN: PASS csv=$ARG only_non_omsc01=$ONLY_NON_OMSC01 total=$total skipped_omsc01=$skipped_omsc01 would_edit=$would_edit would_skip=$would_skip pair_records_matched=$pair_hits (0 live API writes)" |
| 332 | exit 0 |
| 333 | fi |
| 334 | |
| 335 | resolve_spark_host() { |
| 336 | if ssh -o ConnectTimeout=8 -o ServerAliveInterval=15 -o ServerAliveCountMax=3 -o BatchMode=yes spark true >/dev/null 2>&1; then |
| 337 | printf 'spark' |
| 338 | elif ssh -o ConnectTimeout=8 -o ServerAliveInterval=15 -o ServerAliveCountMax=3 -o BatchMode=yes drakestapleton@192.168.1.108 true >/dev/null 2>&1; then |
| 339 | printf 'drakestapleton@192.168.1.108' |
| 340 | elif ssh -o ConnectTimeout=8 -o ServerAliveInterval=15 -o ServerAliveCountMax=3 -o BatchMode=yes drakestapleton@100.64.0.2 true >/dev/null 2>&1; then |
| 341 | printf 'drakestapleton@100.64.0.2' |
| 342 | else |
| 343 | printf '' |
| 344 | fi |
| 345 | } |
| 346 | |
| 347 | ZEN_TOKEN="${ZENODO_TOKEN:-}" |
| 348 | if [ -z "$ZEN_TOKEN" ] && [ -f "$HOME/.config/zenodo/token" ]; then |
| 349 | PERM="$(stat -f '%Lp' "$HOME/.config/zenodo/token" 2>/dev/null || stat -c '%a' "$HOME/.config/zenodo/token" 2>/dev/null || echo '999')" |
| 350 | if [ "$PERM" = "600" ]; then |
| 351 | ZEN_TOKEN="$(tr -d ' \t\r\n' < "$HOME/.config/zenodo/token")" |
| 352 | fi |
| 353 | fi |
| 354 | if [ -z "$ZEN_TOKEN" ] && command -v atlas-vault >/dev/null 2>&1; then |
| 355 | ZEN_TOKEN="$(atlas-vault get ZENODO_TOKEN 2>/dev/null | tr -d ' \t\r\n' || true)" |
| 356 | fi |
| 357 | if [ -z "$ZEN_TOKEN" ]; then |
| 358 | SPARK_HOST="$(resolve_spark_host)" |
| 359 | if [ -n "$SPARK_HOST" ]; then |
| 360 | ZEN_TOKEN="$(ssh -o ConnectTimeout=8 -o ServerAliveInterval=15 -o ServerAliveCountMax=3 -o BatchMode=yes "$SPARK_HOST" '~/.local/bin/atlas-vault get ZENODO_TOKEN 2>/dev/null || cat ~/.config/zenodo/token 2>/dev/null' 2>/dev/null | tr -d ' \t\r\n' || true)" |
| 361 | fi |
| 362 | fi |
| 363 | [ -n "$ZEN_TOKEN" ] || { echo "FAIL: missing ZENODO_TOKEN" >&2; exit 69; } |
| 364 | |
| 365 | ZEN_CFG="$TMP/zen.cfg" |
| 366 | (umask 077; printf 'header = "Authorization: Bearer %s"\n' "$ZEN_TOKEN" > "$ZEN_CFG") |
| 367 | |
| 368 | # Rate-limit & transient-5xx aware Zenodo request helper (respects Retry-After; never logs token) |
| 369 | zen_req() { |
| 370 | method="$1" |
| 371 | url="$2" |
| 372 | out_file="$3" |
| 373 | data_file="${4:-}" |
| 374 | hdr_file="$TMP/req_hdr.txt" |
| 375 | attempt=0 |
| 376 | code="000" |
| 377 | while [ "$attempt" -lt 5 ]; do |
| 378 | : > "$hdr_file" |
| 379 | if [ -n "$data_file" ]; then |
| 380 | code="$(curl -sS -K "$ZEN_CFG" --connect-timeout 15 --max-time 60 \ |
| 381 | -D "$hdr_file" -o "$out_file" -w '%{http_code}' \ |
| 382 | -X "$method" -H "Content-Type: application/json" \ |
| 383 | --data-binary @"$data_file" "$url" 2>/dev/null)" || true |
| 384 | else |
| 385 | code="$(curl -sS -K "$ZEN_CFG" --connect-timeout 15 --max-time 60 \ |
| 386 | -D "$hdr_file" -o "$out_file" -w '%{http_code}' \ |
| 387 | -X "$method" "$url" 2>/dev/null)" || true |
| 388 | fi |
| 389 | [ -n "$code" ] || code="000" |
| 390 | if [ "$code" = "429" ]; then |
| 391 | ra="$(awk 'tolower($1)=="retry-after:" { gsub(/[^0-9]/,"",$2); print $2; exit }' "$hdr_file" 2>/dev/null || true)" |
| 392 | [ -n "$ra" ] && [ "$ra" -gt 0 ] 2>/dev/null || ra=$((20 * (attempt + 1))) |
| 393 | [ "$ra" -gt 120 ] && ra=120 |
| 394 | echo "WARN: Zenodo HTTP 429 on $method $url; sleeping ${ra}s (attempt $((attempt + 1))/5)" >&2 |
| 395 | sleep "$ra" |
| 396 | attempt=$((attempt + 1)) |
| 397 | continue |
| 398 | fi |
| 399 | case "$code" in |
| 400 | 000*|500|502|503|504) |
| 401 | wait_s=$((5 * (attempt + 1))) |
| 402 | echo "WARN: Zenodo transient HTTP $code on $method $url; sleeping ${wait_s}s (attempt $((attempt + 1))/5)" >&2 |
| 403 | sleep "$wait_s" |
| 404 | attempt=$((attempt + 1)) |
| 405 | continue |
| 406 | ;; |
| 407 | esac |
| 408 | printf '%s' "$code" |
| 409 | return 0 |
| 410 | done |
| 411 | printf '%s' "$code" |
| 412 | return 0 |
| 413 | } |
| 414 | |
| 415 | edit_one_record() { |
| 416 | rid="$1" |
| 417 | add_repo="${2:-0}" |
| 418 | pair_info="$(get_pair_link "$rid")" |
| 419 | rel_type="" |
| 420 | rel_doi="" |
| 421 | if [ -n "$pair_info" ]; then |
| 422 | rel_type="$(printf '%s' "$pair_info" | awk '{print $1}')" |
| 423 | rel_doi="$(printf '%s' "$pair_info" | awk '{print $2}')" |
| 424 | fi |
| 425 | |
| 426 | # 1. Unlock published record for metadata-only edit (or fetch if already inprogress; NEVER creates new version) |
| 427 | code="$(zen_req POST "$ZEN_EP/$rid/actions/edit" "$TMP/dep.json")" |
| 428 | if [ "$code" = "400" ] || [ "$code" = "403" ]; then |
| 429 | code_get="$(zen_req GET "$ZEN_EP/$rid" "$TMP/dep.json")" |
| 430 | st="$(jq -r '.state // ""' "$TMP/dep.json" 2>/dev/null || true)" |
| 431 | if [ "$code_get" != "200" ] || [ "$st" != "inprogress" ]; then |
| 432 | printf 'FAIL_EDIT_%s' "$code" |
| 433 | return 1 |
| 434 | fi |
| 435 | elif [ "$code" != "200" ] && [ "$code" != "201" ] && [ "$code" != "202" ]; then |
| 436 | printf 'FAIL_EDIT_%s' "$code" |
| 437 | return 1 |
| 438 | fi |
| 439 | |
| 440 | orig_doi="$(jq -r '.doi // ""' "$TMP/dep.json")" |
| 441 | orig_cid="$(jq -r '(.conceptrecid // "") | tostring' "$TMP/dep.json")" |
| 442 | orig_access="$(jq -r '.metadata.access_right // ""' "$TMP/dep.json")" |
| 443 | |
| 444 | # 2. Build updated metadata payload (preserving all other fields including access_right verbatim) |
| 445 | build_clean_metadata_payload "$TMP/dep.json" "$TMP/put.json" "$rel_type" "$rel_doi" "$add_repo" |
| 446 | new_access="$(jq -r '.metadata.access_right // ""' "$TMP/put.json")" |
| 447 | if [ -n "$orig_access" ] && [ "$new_access" != "$orig_access" ]; then |
| 448 | zen_req POST "$ZEN_EP/$rid/actions/discard" "$TMP/disc.json" >/dev/null || true |
| 449 | printf 'FAIL_ACCESS_RIGHT_DRIFT_%s_to_%s' "$orig_access" "$new_access" |
| 450 | return 1 |
| 451 | fi |
| 452 | |
| 453 | # 3. PUT updated metadata (discard on error so record never stays locked) |
| 454 | pcode="$(zen_req PUT "$ZEN_EP/$rid" "$TMP/put_resp.json" "$TMP/put.json")" |
| 455 | if [ "$pcode" != "200" ] && [ "$pcode" != "201" ]; then |
| 456 | zen_req POST "$ZEN_EP/$rid/actions/discard" "$TMP/disc.json" >/dev/null || true |
| 457 | printf 'FAIL_PUT_%s' "$pcode" |
| 458 | return 1 |
| 459 | fi |
| 460 | |
| 461 | # 4. Republish metadata-only (preserves exact record ID, Concept ID, and DOI; ZERO new DOIs) |
| 462 | pubcode="$(zen_req POST "$ZEN_EP/$rid/actions/publish" "$TMP/pub_resp.json")" |
| 463 | if [ "$pubcode" != "200" ] && [ "$pubcode" != "201" ] && [ "$pubcode" != "202" ]; then |
| 464 | printf 'FAIL_PUB_%s' "$pubcode" |
| 465 | return 1 |
| 466 | fi |
| 467 | |
| 468 | out_id="$(jq -r '.id // ""' "$TMP/pub_resp.json")" |
| 469 | out_doi="$(jq -r '.doi // ""' "$TMP/pub_resp.json")" |
| 470 | out_cid="$(jq -r '.conceptrecid // ""' "$TMP/pub_resp.json")" |
| 471 | if [ "$out_id" != "$rid" ]; then |
| 472 | printf 'FAIL_ID_MISMATCH_%s' "$out_id" |
| 473 | return 1 |
| 474 | fi |
| 475 | if [ -n "$orig_doi" ] && [ "$out_doi" != "$orig_doi" ]; then |
| 476 | printf 'FAIL_DOI_CHANGED_%s_to_%s' "$orig_doi" "$out_doi" |
| 477 | return 1 |
| 478 | fi |
| 479 | if [ -n "$orig_cid" ] && [ "$out_cid" != "$orig_cid" ]; then |
| 480 | printf 'FAIL_CONCEPT_CHANGED_%s_to_%s' "$orig_cid" "$out_cid" |
| 481 | return 1 |
| 482 | fi |
| 483 | printf 'OK_%s' "$out_doi" |
| 484 | return 0 |
| 485 | } |
| 486 | |
| 487 | if [ "$MODE" = "--canary" ]; then |
| 488 | add_repo_flag="$ADD_REPO_CANARY" |
| 489 | if [ "$add_repo_flag" = "auto" ]; then |
| 490 | probe_code="$(zen_req GET "$ZEN_EP/$ARG" "$TMP/canary_probe.json")" |
| 491 | [ "$probe_code" = "200" ] || { echo "CANARY FAIL: GET $ARG returned HTTP $probe_code" >&2; exit 1; } |
| 492 | has_omp_live="$(jq -r '([.files[]?.filename // ""] | any(endswith(".omp"))) | if . then "1" else "0" end' "$TMP/canary_probe.json")" |
| 493 | add_repo_flag="$has_omp_live" |
| 494 | fi |
| 495 | echo "Running metadata-only canary on Zenodo record $ARG (add_repo=$add_repo_flag) ..." |
| 496 | res="$(edit_one_record "$ARG" "$add_repo_flag")" || { echo "CANARY FAIL: $res"; cat "$TMP/put_resp.json" "$TMP/pub_resp.json" 2>/dev/null | head -c 500; echo ""; exit 1; } |
| 497 | echo "Canary edit result: $res" |
| 498 | sleep 2 |
| 499 | curl -fsSL "https://zenodo.org/api/records/$ARG" -o "$TMP/ver.json" |
| 500 | v_doi="$(jq -r '.doi' "$TMP/ver.json")" |
| 501 | v_name="$(jq -r '.metadata.creators[0].name' "$TMP/ver.json")" |
| 502 | v_ver="$(jq -r '.metadata.version // ""' "$TMP/ver.json")" |
| 503 | v_type="$(jq -r '.metadata.resource_type.subtype // .metadata.resource_type.type // ""' "$TMP/ver.json")" |
| 504 | v_gh="$(jq -r --arg r "$GITHUB_REPO_URL" '[.metadata.related_identifiers[]? | select(.identifier == $r)] | length' "$TMP/ver.json")" |
| 505 | v_ip="$(jq -r --arg r "$INTERPLANE_REPO_URL" '[.metadata.related_identifiers[]? | select(.identifier == $r)] | length' "$TMP/ver.json")" |
| 506 | echo "CANARY_VERIFY: id=$ARG doi=$v_doi creator='$v_name' version='$v_ver' type='$v_type' gh_links=$v_gh ip_links=$v_ip" |
| 507 | [ "$v_doi" = "10.5281/zenodo.$ARG" ] || { echo "FAIL: DOI changed to $v_doi" >&2; exit 1; } |
| 508 | [ "$v_name" = "Stapleton, M. Drake" ] || { echo "FAIL: creator not updated ('$v_name')" >&2; exit 1; } |
| 509 | [ -n "$v_ver" ] || { echo "FAIL: version still empty" >&2; exit 1; } |
| 510 | if [ "$add_repo_flag" = "1" ]; then |
| 511 | [ "$v_gh" -ge 1 ] || { echo "FAIL: GitHub isSupplementedBy link missing" >&2; exit 1; } |
| 512 | [ "$v_ip" -ge 1 ] || { echo "FAIL: Interplane isSupplementedBy link missing" >&2; exit 1; } |
| 513 | fi |
| 514 | echo "ZENODO_CANARY_GATE: PASS (0 new DOIs minted)" |
| 515 | exit 0 |
| 516 | fi |
| 517 | |
| 518 | # --all mode: parse audit CSV by header, skip already-compliant records, update all others with polite pacing |
| 519 | [ -f "$ARG" ] || { echo "FAIL: audit CSV not found: $ARG" >&2; exit 1; } |
| 520 | parse_audit_csv_to_tsv "$ARG" "$TMP/parsed.tsv" |
| 521 | |
| 522 | mkdir -p "$(dirname "$LOG_PATH")" |
| 523 | printf 'record_id,action,result\n' > "$LOG_PATH" |
| 524 | |
| 525 | US="$(printf '\037')" |
| 526 | while IFS="$US" read -r rid cname rtype ver has_omp has_repo rlinks _end; do |
| 527 | [ -n "$rid" ] || continue |
| 528 | if [ "$ONLY_NON_OMSC01" = "1" ] && [ "$has_omp" = "1" ]; then |
| 529 | printf '%s,SKIP,OMSC01_HANDLED_BY_NEWVERSION\n' "$rid" >> "$LOG_PATH" |
| 530 | continue |
| 531 | fi |
| 532 | if ! record_needs_cleanup "$rid" "$cname" "$rtype" "$ver" "$has_omp" "$has_repo" "$rlinks"; then |
| 533 | printf '%s,SKIP,ALREADY_COMPLIANT\n' "$rid" >> "$LOG_PATH" |
| 534 | continue |
| 535 | fi |
| 536 | |
| 537 | if res="$(edit_one_record "$rid" "$has_omp")"; then |
| 538 | printf '%s,EDITED,%s\n' "$rid" "$res" >> "$LOG_PATH" |
| 539 | else |
| 540 | printf '%s,FAILED,%s\n' "$rid" "$res" >> "$LOG_PATH" |
| 541 | fi |
| 542 | sleep 2 |
| 543 | done < "$TMP/parsed.tsv" |
| 544 | |
| 545 | ed_cnt="$(grep -c ',EDITED,' "$LOG_PATH" || true)" |
| 546 | sk_cnt="$(grep -c ',SKIP,' "$LOG_PATH" || true)" |
| 547 | fl_cnt="$(grep -c ',FAILED,' "$LOG_PATH" || true)" |
| 548 | echo "ZENODO_CLEANUP_SUMMARY: edited=$ed_cnt skipped=$sk_cnt failed=$fl_cnt log=$LOG_PATH" |
| 549 | [ "$fl_cnt" -le 3 ] || { echo "FAIL: too many failed edits ($fl_cnt)" >&2; exit 1; } |
| 550 | echo "ZENODO_CLEANUP_GATE: PASS" |