run_algorithm_gates.sh 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env bash
  2. # Algorithm facade + kit gates. Does not require RTL, soak, or hardware.
  3. set -uo pipefail
  4. export PATH="/opt/python-3.12/bin:/opt/python-3.11/bin:/opt/python-3.13/bin:/usr/bin:/bin:${PATH}"
  5. SRC="${HTH_SRC:-$(cd "$(dirname "$0")/.." && pwd)}"
  6. SDK="${SRC}/sdk/python"
  7. RUN="${HTH_RUN_DIR:-/tmp/hth-algo-run}"
  8. PKG="${HTH_PACKAGE_DIR:-/tmp}"
  9. mkdir -p "$RUN" "$PKG"
  10. SDK_BUILD="${RUN}/sdk-build-src"
  11. rm -rf "$SDK_BUILD"
  12. mkdir -p "$SDK_BUILD"
  13. cp -a "$SDK/." "$SDK_BUILD/"
  14. rm -rf "$SDK_BUILD/build" "$SDK_BUILD/hth_sdk.egg-info" "$SDK_BUILD/.pytest_cache"
  15. export HTH_RUN_DIR="$RUN"
  16. export HTH_PACKAGE_DIR="$PKG"
  17. export HTH_GIT_COMMIT="${HTH_GIT_COMMIT:-unknown}"
  18. if [[ -z "${HTH_TOOL_CACHE:-}" ]]; then
  19. if [[ -d /data/prod2u ]]; then
  20. export HTH_TOOL_CACHE=/data/prod2u/hth-tool-cache
  21. else
  22. export HTH_TOOL_CACHE="${RUN}/tool-cache"
  23. fi
  24. fi
  25. export SOURCE_DATE_EPOCH="${SOURCE_DATE_EPOCH:-$(date -u +%s)}"
  26. REC="${SRC}/tools/record_cmd.sh"
  27. fail=0
  28. rec() { local n="$1"; shift; if ! bash "${REC}" "${n}" "$@"; then fail=1; fi; }
  29. cd "$SDK"
  30. PY="${PYTHON:-python3}"
  31. rec algorithm-facade "$PY" -m pytest tests/test_facade.py tests/test_import.py tests/test_mock_client.py tests/test_doctor.py tests/test_zip_unix_modes.py -q
  32. rec algorithm-notebook python3 - <<'PY'
  33. import json
  34. from pathlib import Path
  35. nb = json.loads(Path("examples/algorithm_quickstart.ipynb").read_text(encoding="utf-8"))
  36. g = {}
  37. for cell in nb["cells"]:
  38. if cell.get("cell_type") != "code":
  39. continue
  40. exec("".join(cell.get("source") or []), g, g)
  41. print("NOTEBOOK_OK")
  42. PY
  43. BUILD_PY="${PYTHON:-python3}"
  44. if [[ -x /data/prod2u/venvs/hth-verify/bin/python ]]; then
  45. BUILD_PY=/data/prod2u/venvs/hth-verify/bin/python
  46. elif ! "$BUILD_PY" -c 'import build.__main__' >/dev/null 2>&1; then
  47. VENV="${RUN}/build-venv"
  48. "$BUILD_PY" -m venv "$VENV"
  49. "$VENV/bin/pip" install -q -i https://mirrors.aliyun.com/pypi/simple build
  50. BUILD_PY="$VENV/bin/python"
  51. fi
  52. rec algorithm-wheel "$BUILD_PY" -m build --wheel --outdir "${RUN}/dist" "$SDK_BUILD"
  53. WHEEL="$(ls -1t "${RUN}/dist"/hth_sdk-*.whl 2>/dev/null | head -n1 || true)"
  54. export HTH_SDK_WHEEL="$WHEEL"
  55. export HTH_USER_KIT="${HTH_USER_KIT:-linux}"
  56. rec algorithm-kit python3 "${SRC}/tools/package_hth_user_kits.py"
  57. rec algorithm-kit-zip python3 -m pytest tests/test_userkit.py -q
  58. ZIP="$(ls -1t "${PKG}"/hth-algorithm-python-kit-*.zip 2>/dev/null | head -n1 || true)"
  59. if [[ -n "$ZIP" ]]; then
  60. STAGE="${RUN}/kit-stage"
  61. rm -rf "$STAGE"
  62. mkdir -p "$STAGE"
  63. python3 - <<PY
  64. import sys
  65. from pathlib import Path
  66. sys.path.insert(0, r"${SRC}/tools")
  67. from zip_unix_modes import extract_zip_with_unix_modes
  68. extract_zip_with_unix_modes(Path(r"${ZIP}"), Path(r"${STAGE}"))
  69. PY
  70. rec python-kit-install bash -lc "cd ${STAGE}/hth-algorithm-python-kit-${HTH_SDK_VERSION:-0.3.0} && ./INSTALL.sh && ./.venv/bin/python app/quickstart.py && ./.venv/bin/python -m hth_sdk doctor"
  71. fi
  72. PORTABLE="$(ls -1t "${PKG}"/hth-algorithm-portable-linux-*.zip 2>/dev/null | head -n1 || true)"
  73. if [[ -n "$PORTABLE" ]]; then
  74. PSTAGE="${RUN}/portable-stage"
  75. rm -rf "$PSTAGE"
  76. mkdir -p "$PSTAGE"
  77. python3 - <<PY
  78. import sys
  79. from pathlib import Path
  80. sys.path.insert(0, r"${SRC}/tools")
  81. from zip_unix_modes import extract_zip_with_unix_modes
  82. extract_zip_with_unix_modes(Path(r"${PORTABLE}"), Path(r"${PSTAGE}"))
  83. PY
  84. rec algorithm-portable bash -lc "cd ${PSTAGE}/hth-algorithm-portable-linux-x86_64-${HTH_SDK_VERSION:-0.3.0} && ./START_HERE.sh"
  85. fi
  86. echo "ALGO_GATES_DONE fail=${fail}" | tee "${RUN}/GATES_DONE"
  87. exit "${fail}"