con-site-specific-annex/tools/linkml_check.sh
Claude c8195b1073
ci: run both record gates on every pull request, and as pre-commit hooks
.pre-commit-config.yaml runs:
- check-yaml and check-merge-conflict;
- tools/validate_records.py (the curation rules) on any commit touching
  metadata/ or sources/curation-evidence/;
- tools/linkml_check.sh (the schema) as a manual-stage hook, since it
  takes about 40 seconds and needs a things-schemas checkout.

.github/workflows/validate.yml runs the commit hooks over all files in
one job and the schema hook in another.  The schema job checks out
things-schemas at cb6c791, the commit orinoco-lite-dev 94c3178 pins,
which is the package revision the downstream sites build with.  Actions
are pinned by commit, as in the downstream.

Fault-injected before relying on it:
- a malformed record fails check-yaml and the curation gate;
- a changed date fails the curation gate on its evidence value;
- an unknown slot fails the schema gate.

The last one exposed a silent failure in linkml_check.sh, fixed here.
Under `set -e`, a non-zero exit from linkml-validate ended the script at
the assignment, before it printed which class failed or why.  CI would
have reported a bare exit 1.  Now it prints the FAIL line and the error,
carries on with the other classes, and exits 1 at the end.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01R7n7cc51H6cRd4v7oQypjt
2026-10-14 16:48:03 +00:00

62 lines
2.7 KiB
Shell
Executable file

#!/bin/bash
# Validate a records tree against demo-research-information, one class per dir.
#
# Usage: tools/linkml_check.sh [records-dir]
# THINGS_SCHEMAS=/path/to/things-schemas tools/linkml_check.sh
#
# The schema imports siblings by URL prefix (dlschemas:things-prov/v1), which
# linkml cannot resolve offline, so a copy is made with the imports rewritten
# to relative paths -- the same rewrite the upstream Makefile's `imports-local`
# target performs. The copy is cached and rebuilt when the source is newer.
#
# NOTE on reading the result: linkml-validate exits 0 even when it prints
# [ERROR], and its --config form silently validated nothing at all here (four
# deliberately broken records all came back "No issues found"). So: direct
# -s/-C invocation, and a non-zero exit or a traceback is a failure.
set -e
ROOT=$(cd "$(dirname "$0")/.." && pwd)
RECORDS="${1:-$ROOT/metadata/records}"
# Locate things-schemas: an explicit override, a sibling checkout, or the
# ORINOCO-Lite submodule tree.
if [ -z "$THINGS_SCHEMAS" ]; then
for c in "$ROOT/../orinoco-lite-dev/submodules/things-schemas" \
"$ROOT/../things-schemas" \
"$HOME/proj/orinoco-lite/orinoco-lite-dev/submodules/things-schemas"; do
[ -d "$c/src/demo-research-information" ] && THINGS_SCHEMAS=$(cd "$c" && pwd) && break
done
fi
if [ ! -d "$THINGS_SCHEMAS/src/demo-research-information" ]; then
echo "things-schemas not found; set THINGS_SCHEMAS=/path/to/things-schemas" >&2
exit 3
fi
CACHE="${TMPDIR:-/tmp}/con-linkml-schemas-$(id -u)"
SCHEMA="$CACHE/demo-research-information/unreleased.yaml"
if [ ! -f "$SCHEMA" ] || [ "$THINGS_SCHEMAS/src/demo-research-information/unreleased.yaml" -nt "$SCHEMA" ]; then
rm -rf "$CACHE"
cp -r "$THINGS_SCHEMAS/src" "$CACHE"
# dlschemas:things-prov/v1 -> ../things-prov/v1
sed -i -e 's,- dlschemas:\(.*/.*\)$,- ../\1,' "$CACHE"/*/*.yaml
fi
rc=0
for d in "$RECORDS"/XYZ*/; do
[ -d "$d" ] || continue
cls=$(basename "$d")
mapfile -t files < <(find "$d" -name '*.yaml' -not -name '.*' | sort)
[ ${#files[@]} -eq 0 ] && continue
# `|| vrc=$?`, not a bare assignment: under `set -e` a failing
# linkml-validate ended the script right here, before the FAIL line and the
# errors below were printed, so a broken record failed with no diagnostics.
vrc=0
out=$(uv run --quiet --with linkml linkml-validate -s "$SCHEMA" -C "$cls" "${files[@]}" 2>&1) || vrc=$?
if [ $vrc -ne 0 ] || echo "$out" | grep -qE '\[ERROR\]|Traceback'; then
printf '%-20s %3d file(s) FAIL\n' "$cls" "${#files[@]}"
echo "$out" | grep -E '\[ERROR\]|Error' | head -10 | sed 's/^/ /'
rc=1
else
printf '%-20s %3d file(s) OK\n' "$cls" "${#files[@]}"
fi
done
exit $rc