fix(lib.sh): avoid cd side-effect in git_commit/git_dirty and safe JSON in write_metadata_json

This commit is contained in:
yy-fighting 2026-07-22 03:44:18 +00:00
parent b6dab8e139
commit 489c5a5e61

View File

@ -63,17 +63,18 @@ health_check() {
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
git_commit() { git_commit() {
cd "$ROOT_DIR" || return 1 ( cd "$ROOT_DIR" && git rev-parse --short HEAD 2>/dev/null || echo "unknown" )
git rev-parse --short HEAD 2>/dev/null || echo "unknown"
} }
git_dirty() { git_dirty() {
cd "$ROOT_DIR" || return 1 (
if [[ -n "$(git status --porcelain 2>/dev/null)" ]]; then cd "$ROOT_DIR" || exit 1
echo "dirty" if [[ -n "$(git status --porcelain 2>/dev/null)" ]]; then
else echo "dirty"
echo "clean" else
fi echo "clean"
fi
)
} }
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
@ -96,28 +97,34 @@ write_metadata_json() {
mkdir -p "$(dirname "$output_path")" mkdir -p "$(dirname "$output_path")"
cat > "$output_path" <<EOF "${PYTHON:-python3}" - "$output_path" "$experiment" "$run_id" "$model" "$backend" "$engine" "$hardware" "$accelerator" "$chip" "$script" "$env_path" "$description" "$(date --iso-8601=seconds)" "$(git_commit)" "$(git_dirty)" <<'PY'
{ import json, sys
"metadata": { (out, experiment, run_id, model, backend, engine, hardware, accelerator,
"experiment": "${experiment}", chip, script, env_path, description, timestamp, git_commit,
"run_id": "${run_id}", git_dirty) = sys.argv[1:16]
"timestamp": "$(date --iso-8601=seconds)", data = {
"model": "${model}", "metadata": {
"backend": "${backend}", "experiment": experiment,
"engine": "${engine}", "run_id": run_id,
"hardware": "${hardware}", "timestamp": timestamp,
"accelerator": "${accelerator}", "model": model,
"chip": "${chip}", "backend": backend,
"script": "${script}", "engine": engine,
"env": "${env_path}", "hardware": hardware,
"git_commit": "$(git_commit)", "accelerator": accelerator,
"git_dirty": "$(git_dirty)", "chip": chip,
"description": "${description}" "script": script,
}, "env": env_path,
"config": {}, "git_commit": git_commit,
"scenarios": [] "git_dirty": git_dirty,
"description": description,
},
"config": {},
"scenarios": [],
} }
EOF with open(out, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
PY
} }
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------