interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / tools / doi / doi.sh

116 lines · 4825 bytes Raw Blame History
1#!/bin/sh
2# Local-author IO adapter. JSON, metadata, links, DOI rewriting and tty consent are doi.om.
3# No Interplane request is made. Never enable shell xtrace in a shell holding a real token.
4set +x
5set -eu
6umask 077
7ulimit -c 0
8unset SSLKEYLOGFILE
9export LC_ALL=C
10D=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P)
11ENGINE=${DOI_ENGINE:-$D/../../.build/doi/doi}
12usage() {
13 echo 'usage: sh tools/doi/doi.sh PAPER_REPO [--production] [--source FILE] [--fixture DIR]' >&2
14 exit 2
15}
16[ "$#" -gt 0 ] || usage
17repo=$1
18shift
19mode=sandbox
20fixture=
21sourcefile=
22while [ "$#" -gt 0 ]; do
23 case "$1" in
24 --production) mode=production; shift;;
25 --source)
26 [ "$#" -ge 2 ] || usage
27 [ -z "$sourcefile" ] || usage
28 sourcefile=$2; shift 2;;
29 --fixture)
30 [ "$#" -ge 2 ] || usage
31 [ -z "$fixture" ] || usage
32 fixture=$(CDPATH= cd -- "$2" && pwd -P); shift 2;;
33 *) usage;;
34 esac
35done
36[ -x "$ENGINE" ] || { echo 'doi: build first: sh tools/doi/build.sh' >&2; exit 1; }
37"$ENGINE" token
38base=$("$ENGINE" base "$mode")
39work=$(mktemp -d)
40cleanup() {
41 rm -rf -- "$work"
42}
43trap cleanup EXIT
44trap 'exit 1' HUP INT TERM
45mkdir -- "$work/repo" "$work/files"
46mkfifo "$work/in"
47set --
48[ -z "$sourcefile" ] || set -- "$sourcefile"
49# The Omega keeper alone owns repository descriptors, snapshot enumeration and write-back.
50# The shell transports only READY/DOI/SAVED through pipes; no descriptor number is trusted input.
51# Both response pipes are established before keeper execution. No blocking FIFO open.
52"$ENGINE" serve "$mode" "$repo" "$work" "$@" | {
53IFS= read -r state && [ "$state" = READY ] || exit 1
54exec 3<>"$work/in"
55set -- "$work/files/"*
56printf '%s\n' 'Uploading ALL selected top-level PDFs (including untracked PDFs) and explicit source BEFORE publish consent:'
57for f do printf ' %s\n' "${f##*/}"; done
58# stdin is a pipe, not a config file. printf is a shell builtin; the bearer is not argv.
59# -q is first: ~/.curlrc cannot enable tracing, redirects, or another output.
60# No redirect, proxy, .netrc, user-supplied URL or curl option is admitted.
61http() {
62 method=$1; url=$2; response=$3; shift 3
63 if [ -n "$fixture" ]; then
64 code=$(printf 'header = "Authorization: Bearer %s"\n' "$ZENODO_TOKEN" |
65 sh "$D/fixture-curl.sh" "$fixture" -q --proto '=https' --max-redirs 0 \
66 --proxy '' --globoff --silent --show-error --config - --request "$method" \
67 --output "$response" --write-out '%{http_code}' "$@" --url "$url") || code=000
68 else
69 code=$(printf 'header = "Authorization: Bearer %s"\n' "$ZENODO_TOKEN" |
70 curl -q --proto '=https' --max-redirs 0 --proxy '' --globoff --silent --show-error \
71 --config - --request "$method" --output "$response" \
72 --write-out '%{http_code}' "$@" --url "$url") || code=000
73 fi
74}
75failed() {
76 echo "doi: $1 failed (HTTP $code). No automatic retry: inspect the draft on Zenodo before starting again." >&2
77 "$ENGINE" pick "$mode" "$2" message >&2 || :
78 exit 1
79}
80http POST "$base/deposit/depositions" "$work/create.json" \
81 --header 'Content-Type: application/json' --data-binary "@$work/body.json"
82[ "$code" = 201 ] || failed 'create draft' "$work/create.json"
83# Validate ALL values before the first upload, especially credential-bearing targets.
84bucket=$("$ENGINE" pick "$mode" "$work/create.json" bucket)
85publish=$("$ENGINE" pick "$mode" "$work/create.json" publish)
86html=$("$ENGINE" pick "$mode" "$work/create.json" html)
87doi=$("$ENGINE" pick "$mode" "$work/create.json" doi)
88printf 'Draft: %s\nReserved DOI: %s\n' "$html" "$doi"
89# Preserve the reservation locally even if a later upload or consent fails.
90printf '%s\n' "$doi" >&3
91IFS= read -r state && [ "$state" = SAVED ] || exit 1
92# DONE is emitted by Omega only after waitpid observes keeper success.
93IFS= read -r state && [ "$state" = DONE ] || exit 1
94exec 3>&-
95for f do
96 name=${f##*/}
97 http PUT "$bucket/$name" "$work/upload.json" --upload-file "$f"
98 case "$code" in 200|201) :;; *) failed "upload $name" "$work/upload.json";; esac
99 actual=$("$ENGINE" pick "$mode" "$work/upload.json" key)
100 bytes=$("$ENGINE" pick "$mode" "$work/upload.json" size)
101 expected=$(wc -c < "$f" | tr -d '[:space:]')
102 [ "$actual" = "$name" ] && [ "$bytes" = "$expected" ] || {
103 echo "doi: upload acknowledgement differs for $name; draft kept unpublished at $html" >&2
104 exit 1
105 }
106done
107if ! "$ENGINE" consent "$mode" "$doi"; then
108 echo "Draft kept, not published: $html"
109 exit 0
110fi
111http POST "$publish" "$work/publish.json" --header 'Content-Type: application/json' --data-binary '{}'
112[ "$code" = 202 ] || failed publish "$work/publish.json"
113finaldoi=$("$ENGINE" pick "$mode" "$work/publish.json" pubdoi)
114[ "$finaldoi" = "$doi" ] || { echo 'doi: published response DOI differs; inspect Zenodo' >&2; exit 1; }
115printf 'Published DOI: %s\nRecord: %s\n' "$doi" "$html"
116}