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 (
cd "$ROOT_DIR" || exit 1
if [[ -n "$(git status --porcelain 2>/dev/null)" ]]; then if [[ -n "$(git status --porcelain 2>/dev/null)" ]]; then
echo "dirty" echo "dirty"
else else
echo "clean" echo "clean"
fi 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
(out, experiment, run_id, model, backend, engine, hardware, accelerator,
chip, script, env_path, description, timestamp, git_commit,
git_dirty) = sys.argv[1:16]
data = {
"metadata": { "metadata": {
"experiment": "${experiment}", "experiment": experiment,
"run_id": "${run_id}", "run_id": run_id,
"timestamp": "$(date --iso-8601=seconds)", "timestamp": timestamp,
"model": "${model}", "model": model,
"backend": "${backend}", "backend": backend,
"engine": "${engine}", "engine": engine,
"hardware": "${hardware}", "hardware": hardware,
"accelerator": "${accelerator}", "accelerator": accelerator,
"chip": "${chip}", "chip": chip,
"script": "${script}", "script": script,
"env": "${env_path}", "env": env_path,
"git_commit": "$(git_commit)", "git_commit": git_commit,
"git_dirty": "$(git_dirty)", "git_dirty": git_dirty,
"description": "${description}" "description": description,
}, },
"config": {}, "config": {},
"scenarios": [] "scenarios": [],
} }
EOF with open(out, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
PY
} }
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------