interplane.Get Omega
Read in your language

omtex

aien-dev/omtex

Brought by drakestapleton

branch main Branches Tags History

omtex / tools / zenodo-draft.sh

97 lines · 5390 bytes Raw Blame History
1#!/bin/sh
2# zenodo-draft.sh -- put a built paper PDF into an UNPUBLISHED Zenodo draft. It never publishes:
3# every request goes through api(), which refuses any path containing "publish", and the script
4# finishes by re-reading the deposition and failing unless it is still an unsubmitted draft.
5# Publishing stays a human action in the Zenodo web UI.
6#
7# zenodo-draft.sh [--sandbox] new PDF METADATA.json
8# new standalone draft; METADATA.json is the Zenodo deposit "metadata" object
9# zenodo-draft.sh [--sandbox] newversion RECORD_ID PDF [METADATA.json]
10# new-version draft of a published record: inherited files are removed, PDF uploaded,
11# publication_date set to today (UTC), optional METADATA.json fields merged over the old ones
12# zenodo-draft.sh [--sandbox] show DEPOSITION_ID
13# print state, URL and files; exits 6 unless it is an unpublished draft
14# zenodo-draft.sh [--sandbox] discard DEPOSITION_ID
15# delete an unpublished draft (Zenodo refuses this for published records)
16#
17# Token: $ZENODO_TOKEN (sandbox: $ZENODO_SANDBOX_TOKEN), e.g.
18# atlas-vault run ZENODO_TOKEN -- sh tools/zenodo-draft.sh ...
19# The token is passed to curl in a mode-600 header file, never in argv, and never printed.
20set -eu
21host=https://zenodo.org tokvar=ZENODO_TOKEN
22if [ "${1:-}" = --sandbox ]; then host=https://sandbox.zenodo.org tokvar=ZENODO_SANDBOX_TOKEN; shift; fi
23eval "tok=\${$tokvar:-}"
24# Zenodo tokens are alphanumeric; a stored value may carry quotes or a newline from how it was saved.
25tok=$(printf %s "$tok" | tr -d "'\"[:space:]")
26[ -n "$tok" ] || { echo "zenodo-draft: \$$tokvar is not set" >&2; exit 2; }
27W=$(mktemp -d "${TMPDIR:-/tmp}/zenodo-draft.XXXXXX")
28trap 'rm -rf "$W"' EXIT
29umask 077
30printf 'Authorization: Bearer %s\n' "$tok" > "$W/h"
31unset tok
32
33# api METHOD URL-or-PATH [curl args...] -> body on stdout; fails on HTTP >= 400.
34api() {
35 m=$1 u=$2; shift 2
36 case $u in http*) ;; *) u=$host/api$u ;; esac
37 case $u in *publish*) echo "zenodo-draft: refusing $u (this tool never publishes)" >&2; exit 3 ;; esac
38 code=$(curl -sS -o "$W/body" -w '%{http_code}' -X "$m" -H @"$W/h" "$@" "$u")
39 if [ "$code" -ge 400 ]; then echo "zenodo-draft: $m $u -> HTTP $code: $(cut -c1-400 "$W/body")" >&2; exit 4; fi
40 cat "$W/body"
41}
42upload() { # upload BUCKET-URL PDF
43 name=$(basename "$2")
44 api PUT "$1/$name" -H 'Content-Type: application/octet-stream' --data-binary @"$2" > "$W/up"
45 want=$(md5sum < "$2" | cut -d' ' -f1)
46 got=$(jq -r '.checksum' < "$W/up")
47 [ "$got" = "md5:$want" ] || { echo "zenodo-draft: upload checksum $got, local md5:$want" >&2; exit 5; }
48 echo "file $name md5:$want size $(wc -c < "$2")"
49}
50verify() { # verify ID: must still be an unsubmitted draft
51 api GET "/deposit/depositions/$1" > "$W/d"
52 st=$(jq -r '.state' < "$W/d"); sub=$(jq -r '.submitted' < "$W/d")
53 echo "draft $1 state=$st submitted=$sub"
54 echo "title $(jq -r '.metadata.title' < "$W/d") | publication_date $(jq -r '.metadata.publication_date // "-"' < "$W/d")"
55 echo "url $(jq -r '.links.html' < "$W/d")"
56 jq -r '.files[] | "zenodo-file \(.filename) md5:\(.checksum)"' < "$W/d"
57 [ "$sub" = false ] && [ "$st" != done ] || { echo "zenodo-draft: deposition $1 is not an unpublished draft" >&2; exit 6; }
58}
59need_pdf() { [ -f "$1" ] && [ "$(head -c 5 "$1")" = %PDF- ] || { echo "zenodo-draft: $1 is not a PDF" >&2; exit 2; }; }
60
61cmd=${1:-}; [ $# -gt 0 ] && shift
62case $cmd in
63new)
64 [ $# = 2 ] || { echo "usage: zenodo-draft.sh [--sandbox] new PDF METADATA.json" >&2; exit 2; }
65 need_pdf "$1"; jq -e 'type == "object" and has("title")' < "$2" > /dev/null
66 api POST /deposit/depositions -H 'Content-Type: application/json' -d '{}' > "$W/n"
67 id=$(jq -r '.id' < "$W/n"); bucket=$(jq -r '.links.bucket' < "$W/n")
68 upload "$bucket" "$1"
69 jq '{metadata: .}' < "$2" > "$W/m"
70 api PUT "/deposit/depositions/$id" -H 'Content-Type: application/json' --data-binary @"$W/m" > /dev/null
71 verify "$id" ;;
72newversion)
73 [ $# -ge 2 ] || { echo "usage: zenodo-draft.sh [--sandbox] newversion RECORD_ID PDF [METADATA.json]" >&2; exit 2; }
74 need_pdf "$2"
75 api POST "/deposit/depositions/$1/actions/newversion" > "$W/v"
76 draft=$(jq -r '.links.latest_draft' < "$W/v")
77 api GET "$draft" > "$W/n"
78 id=$(jq -r '.id' < "$W/n"); bucket=$(jq -r '.links.bucket' < "$W/n")
79 jq -r '.files[].id' < "$W/n" | while read -r f; do api DELETE "/deposit/depositions/$id/files/$f" > /dev/null; done
80 upload "$bucket" "$2"
81 today=$(date -u +%Y-%m-%d)
82 if [ $# -ge 3 ]; then jq --arg d "$today" --slurpfile x "$3" '{metadata: (.metadata + $x[0] + {publication_date: $d})}' < "$W/n" > "$W/m"
83 else jq --arg d "$today" '{metadata: (.metadata + {publication_date: $d})}' < "$W/n" > "$W/m"; fi
84 jq 'del(.metadata.doi)' < "$W/m" > "$W/m2"
85 api PUT "/deposit/depositions/$id" -H 'Content-Type: application/json' --data-binary @"$W/m2" > /dev/null
86 verify "$id" ;;
87discard)
88 [ $# = 1 ] || { echo "usage: zenodo-draft.sh [--sandbox] discard DEPOSITION_ID" >&2; exit 2; }
89 api GET "/deposit/depositions/$1" > "$W/d"
90 [ "$(jq -r '.submitted' < "$W/d")" = false ] || { echo "zenodo-draft: $1 is published; not touching it" >&2; exit 6; }
91 api DELETE "/deposit/depositions/$1" > /dev/null
92 echo "discarded draft $1" ;;
93show)
94 [ $# = 1 ] || { echo "usage: zenodo-draft.sh [--sandbox] show DEPOSITION_ID" >&2; exit 2; }
95 verify "$1" ;;
96*) sed -n '2,20p' "$0" >&2; exit 2 ;;
97esac