vendor_chattolib.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/bin/bash
  2. # Vendoring script for hermes-chatto-plugin
  3. #
  4. # Downloads chattolib and all its dependencies with uv and vendors them into
  5. # vendor/. Three of them (pyqwest, protobuf, protobuf-py-ext) ship compiled
  6. # extension modules, so the tree is split by platform:
  7. #
  8. # vendor/common/ pure-Python packages, shared
  9. # vendor/platform/<tag>/ compiled extensions, one dir per platform
  10. #
  11. # vendor_path.py picks the right directory at import time.
  12. #
  13. # Usage:
  14. # ./vendor_chattolib.sh # all default platforms
  15. # PLATFORMS="linux-x86_64" ./vendor_chattolib.sh
  16. # CHATTOLIB_SPEC="chattolib==0.4.20" ./vendor_chattolib.sh
  17. set -euo pipefail
  18. PLUGIN_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  19. VENDOR_DIR="$PLUGIN_DIR/vendor"
  20. BUILD_DIR="$(mktemp -d)"
  21. trap 'rm -rf "$BUILD_DIR"' EXIT
  22. CHATTOLIB_SPEC="${CHATTOLIB_SPEC:-chattolib>=0.4.20}"
  23. # Wheels are resolved for this Python version. 3.11 is the project's minimum
  24. # (pyproject: requires-python) AND the version for which the binary packages
  25. # still provide cp310-abi3 wheels — those are forward compatible, so one build
  26. # covers 3.11, 3.12, 3.13 and 3.14. Do not raise this without checking that
  27. # the resulting .so files are still abi3.
  28. PYTHON_VERSION="${PYTHON_VERSION:-3.11}"
  29. # Platform tag (as used in vendor/platform/ and by vendor_path.py) mapped to
  30. # the uv --python-platform target. Add entries here to support more platforms;
  31. # vendor_path.py derives the same tags from platform.system()/machine().
  32. platform_target() {
  33. case "$1" in
  34. linux-x86_64) echo "x86_64-unknown-linux-gnu" ;;
  35. linux-aarch64) echo "aarch64-unknown-linux-gnu" ;;
  36. linux-x86_64-musl) echo "x86_64-unknown-linux-musl" ;;
  37. linux-aarch64-musl) echo "aarch64-unknown-linux-musl" ;;
  38. macos-arm64) echo "aarch64-apple-darwin" ;;
  39. macos-x86_64) echo "x86_64-apple-darwin" ;;
  40. windows-amd64) echo "x86_64-pc-windows-msvc" ;;
  41. *) return 1 ;;
  42. esac
  43. }
  44. # Default set: Linux (deployment, both architectures) and Apple Silicon (dev).
  45. DEFAULT_PLATFORMS="linux-x86_64 linux-aarch64 macos-arm64"
  46. read -r -a PLATFORM_LIST <<< "${PLATFORMS:-$DEFAULT_PLATFORMS}"
  47. if ! command -v uv > /dev/null 2>&1; then
  48. echo "❌ uv nicht gefunden — siehe https://docs.astral.sh/uv/" >&2
  49. exit 1
  50. fi
  51. echo "📦 Vendoring '$CHATTOLIB_SPEC' für: ${PLATFORM_LIST[*]}"
  52. echo " (Python $PYTHON_VERSION, nur Binär-Wheels — kein Bauen aus Sourcen)"
  53. for tag in "${PLATFORM_LIST[@]}"; do
  54. if ! target="$(platform_target "$tag")"; then
  55. echo "❌ Unbekannte Plattform '$tag'. Bekannt: linux-x86_64, linux-aarch64," >&2
  56. echo " linux-x86_64-musl, linux-aarch64-musl, macos-arm64, macos-x86_64, windows-amd64" >&2
  57. exit 1
  58. fi
  59. echo " → $tag ($target)"
  60. # --only-binary :all: is required: a source distribution could not be built
  61. # for a foreign platform, and building it for the host would silently
  62. # produce artifacts for the wrong architecture.
  63. uv pip install \
  64. --target "$BUILD_DIR/$tag" \
  65. --python-platform "$target" \
  66. --python-version "$PYTHON_VERSION" \
  67. --only-binary :all: \
  68. --quiet \
  69. "$CHATTOLIB_SPEC"
  70. done
  71. echo "🧹 Bereinige alten vendor-Ordner..."
  72. rm -rf "$VENDOR_DIR"
  73. mkdir -p "$VENDOR_DIR"
  74. echo "✂️ Trenne plattformunabhängige von binären Paketen..."
  75. python3 "$PLUGIN_DIR/vendor_split.py" "$BUILD_DIR" "$VENDOR_DIR" "${PLATFORM_LIST[@]}"
  76. echo "📄 Erstelle __init__.py Marker..."
  77. touch "$VENDOR_DIR/__init__.py"
  78. find "$VENDOR_DIR" -type d -name "__pycache__" -exec rm -rf {} + 2> /dev/null || true
  79. echo "🔍 Verifiziere Import für die aktuelle Plattform..."
  80. if python3 -c "
  81. import sys
  82. sys.path.insert(0, '$PLUGIN_DIR')
  83. from vendor_path import setup_vendor_path, platform_tag
  84. try:
  85. setup_vendor_path()
  86. except ImportError as exc:
  87. print(f' ⓘ {exc}')
  88. sys.exit(0)
  89. import chattolib.client
  90. print(f' ✓ chattolib importiert ({platform_tag()})')
  91. "; then
  92. :
  93. else
  94. echo "❌ Import-Verifikation fehlgeschlagen" >&2
  95. exit 1
  96. fi
  97. echo "✓ Fertig: $(du -sh "$VENDOR_DIR" | cut -f1) in $VENDOR_DIR"
  98. echo " 'git add vendor/' nicht vergessen."