interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / tools / doi / doi.sh
| 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. |
| 4 | set +x |
| 5 | set -eu |
| 6 | umask 077 |
| 7 | ulimit -c 0 |
| 8 | unset SSLKEYLOGFILE |
| 9 | export LC_ALL=C |
| 10 | D=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P) |
| 11 | ENGINE=${DOI_ENGINE:-$D/../../.build/doi/doi} |
| 12 | usage() { |
| 13 | echo 'usage: sh tools/doi/doi.sh PAPER_REPO [--production] [--source FILE] [--fixture DIR]' >&2 |
| 14 | exit 2 |
| 15 | } |
| 16 | [ "$#" -gt 0 ] || usage |
| 17 | repo=$1 |
| 18 | shift |
| 19 | mode=sandbox |
| 20 | fixture= |
| 21 | sourcefile= |
| 22 | while [ "$#" -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 |
| 35 | done |
| 36 | [ -x "$ENGINE" ] || { echo 'doi: build first: sh tools/doi/build.sh' >&2; exit 1; } |
| 37 | "$ENGINE" token |
| 38 | base=$("$ENGINE" base "$mode") |
| 39 | work=$(mktemp -d) |
| 40 | cleanup() { |
| 41 | rm -rf -- "$work" |
| 42 | } |
| 43 | trap cleanup EXIT |
| 44 | trap 'exit 1' HUP INT TERM |
| 45 | mkdir -- "$work/repo" "$work/files" |
| 46 | mkfifo "$work/in" |
| 47 | set -- |
| 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" "$@" | { |
| 53 | IFS= read -r state && [ "$state" = READY ] || exit 1 |
| 54 | exec 3<>"$work/in" |
| 55 | set -- "$work/files/"* |
| 56 | printf '%s\n' 'Uploading ALL selected top-level PDFs (including untracked PDFs) and explicit source BEFORE publish consent:' |
| 57 | for 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. |
| 61 | http() { |
| 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 | } |
| 75 | failed() { |
| 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 | } |
| 80 | http 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. |
| 84 | bucket=$("$ENGINE" pick "$mode" "$work/create.json" bucket) |
| 85 | publish=$("$ENGINE" pick "$mode" "$work/create.json" publish) |
| 86 | html=$("$ENGINE" pick "$mode" "$work/create.json" html) |
| 87 | doi=$("$ENGINE" pick "$mode" "$work/create.json" doi) |
| 88 | printf 'Draft: %s\nReserved DOI: %s\n' "$html" "$doi" |
| 89 | # Preserve the reservation locally even if a later upload or consent fails. |
| 90 | printf '%s\n' "$doi" >&3 |
| 91 | IFS= read -r state && [ "$state" = SAVED ] || exit 1 |
| 92 | # DONE is emitted by Omega only after waitpid observes keeper success. |
| 93 | IFS= read -r state && [ "$state" = DONE ] || exit 1 |
| 94 | exec 3>&- |
| 95 | for 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 | } |
| 106 | done |
| 107 | if ! "$ENGINE" consent "$mode" "$doi"; then |
| 108 | echo "Draft kept, not published: $html" |
| 109 | exit 0 |
| 110 | fi |
| 111 | http POST "$publish" "$work/publish.json" --header 'Content-Type: application/json' --data-binary '{}' |
| 112 | [ "$code" = 202 ] || failed publish "$work/publish.json" |
| 113 | finaldoi=$("$ENGINE" pick "$mode" "$work/publish.json" pubdoi) |
| 114 | [ "$finaldoi" = "$doi" ] || { echo 'doi: published response DOI differs; inspect Zenodo' >&2; exit 1; } |
| 115 | printf 'Published DOI: %s\nRecord: %s\n' "$doi" "$html" |
| 116 | } |