generate_pb.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env bash
  2. # Regenerate the vendored Python protobuf + ConnectRPC bindings under
  3. # src/chattolib/_pb.
  4. #
  5. # Fetches the .proto sources for a pinned Chatto ref (default: the version
  6. # recorded in pyproject.toml, as a `v<version>` tag) and runs protoc with
  7. # the built-in Python generator and the connect-python plugin.
  8. #
  9. # ALWAYS pin to a released tag. Fetching from `main` risks shipping
  10. # unreleased wire changes that the deployed Chatto server does not speak
  11. # — chattolib 0.4.19 shipped that way and broke realtime for every
  12. # downstream client. Override the pin explicitly if you know what you are
  13. # doing:
  14. #
  15. # CHATTO_REF=v0.4.19 ./scripts/generate_pb.sh
  16. # CHATTO_REF=main ./scripts/generate_pb.sh # unreleased
  17. #
  18. # Requirements:
  19. # - protoc on PATH
  20. # - protoc-gen-chattolib on PATH (bundled with chattolib itself — run
  21. # `pip install -e .` and it is on PATH; no separate install needed)
  22. # - curl on PATH
  23. #
  24. # Run from the repo root: ./scripts/generate_pb.sh
  25. set -euo pipefail
  26. if ! command -v protoc >/dev/null; then
  27. echo "protoc not found on PATH" >&2
  28. exit 1
  29. fi
  30. repo_root=$(cd "$(dirname "$0")/.." && pwd)
  31. cd "$repo_root"
  32. # Default the Chatto ref to `v<base-version-in-pyproject.toml>`. Strip any
  33. # post/pre-release suffix — those are chattolib-only fixes against the same
  34. # server release. Callers can override with CHATTO_REF.
  35. default_version=$(sed -nE 's/^version *= *"([^"]+)".*/\1/p' pyproject.toml | head -1)
  36. default_base=${default_version%%.post*}
  37. default_base=${default_base%%.dev*}
  38. default_base=${default_base%%a*}
  39. default_base=${default_base%%b*}
  40. default_base=${default_base%%rc*}
  41. default_ref="v${default_base}"
  42. chatto_ref=${CHATTO_REF:-$default_ref}
  43. echo "Fetching proto sources from chattocorp/chatto@${chatto_ref}"
  44. mkdir -p proto/chatto/{api,admin,auth,discovery,realtime}/v1 proto/buf/validate
  45. chatto_base="https://raw.githubusercontent.com/chattocorp/chatto/${chatto_ref}/proto/chatto"
  46. fetch() {
  47. local rel=$1
  48. curl -sfL "$chatto_base/${rel#chatto/}" -o "proto/$rel"
  49. echo " $rel"
  50. }
  51. echo "Fetching proto sources ..."
  52. # chatto.api.v1
  53. for f in \
  54. account.proto attachments.proto asset_uploads.proto common.proto \
  55. external_identities.proto link_previews.proto member_directory.proto \
  56. messages.proto message_types.proto \
  57. notifications.proto pagination.proto permissions.proto presence.proto \
  58. push_notifications.proto reactions.proto read_state.proto \
  59. room_directory.proto rooms.proto room_timeline.proto roles.proto \
  60. server.proto server_state.proto threads.proto users.proto \
  61. user_status.proto viewer.proto voice_calls.proto
  62. do
  63. fetch "chatto/api/v1/$f"
  64. done
  65. # chatto.admin.v1
  66. for f in \
  67. diagnostics.proto event_log.proto members.proto permissions.proto \
  68. roles.proto room_layout.proto server.proto
  69. do
  70. fetch "chatto/admin/v1/$f"
  71. done
  72. # chatto.auth.v1
  73. fetch chatto/auth/v1/external_identity_auth.proto
  74. # chatto.discovery.v1
  75. fetch chatto/discovery/v1/server.proto
  76. # chatto.realtime.v1
  77. fetch chatto/realtime/v1/realtime.proto
  78. # buf.validate — sourced from bufbuild/protovalidate
  79. curl -sfL \
  80. "https://raw.githubusercontent.com/bufbuild/protovalidate/main/proto/protovalidate/buf/validate/validate.proto" \
  81. -o proto/buf/validate/validate.proto
  82. echo " buf/validate/validate.proto"
  83. echo "Regenerating Python bindings ..."
  84. rm -rf src/chattolib/_pb/chatto src/chattolib/_pb/buf
  85. mkdir -p src/chattolib/_pb
  86. # Collect every input .proto so protoc's plugins visit each service.
  87. mapfile -t proto_files < <(find proto -type f -name '*.proto' | sort)
  88. protoc --proto_path=proto \
  89. --python_out=src/chattolib/_pb \
  90. "${proto_files[@]}"
  91. # Generate the Connect client stubs with chattolib's own plugin. The
  92. # realtime proto has no services, so it yields only *_pb2.py (above).
  93. # The plugin is a standalone script (scripts/protoc-gen-chattolib) loaded by
  94. # file path, so it runs even before the stubs it generates exist. We expose it
  95. # on PATH via a temp bin dir for the duration of this protoc invocation.
  96. if [ -f "$repo_root/scripts/protoc-gen-chattolib" ]; then
  97. _plugin_bin=$(mktemp -d)
  98. ln -s "$repo_root/scripts/protoc-gen-chattolib" "$_plugin_bin/protoc-gen-chattolib"
  99. PATH="$_plugin_bin:$PATH" protoc --proto_path=proto \
  100. --chattolib_out=src/chattolib/_pb \
  101. "${proto_files[@]}"
  102. rm -rf "$_plugin_bin"
  103. else
  104. echo "protoc-gen-chattolib not found; skipped connect stubs" >&2
  105. fi
  106. # Add __init__.py at every level so the generated modules form a package.
  107. find src/chattolib/_pb \( -path '*/chatto*' -o -path '*/buf*' \) -type d \
  108. | while read -r dir; do touch "$dir/__init__.py"; done
  109. echo "Done."