Skip to content

Commit 86f7252

Browse files
committed
feat: protobuf gen dynamically instead of static inclusion
- Update CI workflow to use simplified protobuf generation approach - Modify .gitignore to exclude generated protobuf code from version control - Update compile_proto.sh to be more flexible and handle multiple proto files - Fix all import paths for new directory structure (resolver_athena_client) - Update AGENTS.md documentation to reflect new workflow - Change package name in pyproject.toml to use underscores This brings the protobuf compilation process in line with modern practices where generated code is built dynamically rather than checked into the repo.
1 parent ea63e98 commit 86f7252

6 files changed

Lines changed: 52 additions & 41 deletions

File tree

.github/workflows/ci.yml

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,31 +52,11 @@ jobs:
5252
- name: Install the project
5353
run: uv sync --locked --all-extras --dev
5454

55-
- name: Ensure no differences in generated code
56-
if: runner.os != 'Windows'
55+
- name: Generate protobuf code
5756
shell: bash
5857
run: |
5958
source .venv/bin/activate
60-
GENERATED_DIR="src/resolver_athena_client/generated"
61-
BACKUP_DIR="src/resolver_athena_client/generated_backup"
62-
63-
cp -r $GENERATED_DIR $BACKUP_DIR
64-
65-
./scripts/compile_proto.sh || (echo "Protobuf compilation failed. Ensure submodules are initialized and the proto file exists." && exit 1)
66-
67-
# Fix imports in generated files
68-
if [[ -f "$GENERATED_DIR/athena/athena_pb2_grpc.py" && -f "$GENERATED_DIR/athena/athena_pb2.py" ]]; then
69-
sed -i.bak 's/^from athena /from resolver_athena_client.generated.athena /' "$GENERATED_DIR/athena/athena_pb2_grpc.py"
70-
sed -i.bak 's/^from athena /from resolver_athena_client.generated.athena /' "$GENERATED_DIR/athena/athena_pb2.py"
71-
rm -f "$GENERATED_DIR/athena/athena_pb2_grpc.py.bak" "$GENERATED_DIR/athena/athena_pb2.py.bak"
72-
else
73-
echo "Error: Expected files not found in $GENERATED_DIR/athena"
74-
exit 1
75-
fi
76-
77-
diff -r $GENERATED_DIR $BACKUP_DIR || (echo "Generated code differs. Please commit the changes after running compile_proto.sh." && exit 1)
78-
79-
rm -rf $BACKUP_DIR
59+
./scripts/compile_proto.sh || (echo "Protobuf compilation failed. Ensure submodules are initialized and proto files exist." && exit 1)
8060
8161
- name: Run linter
8262
run: |
@@ -123,6 +103,17 @@ jobs:
123103
with:
124104
enable-cache: true
125105

106+
- name: Set up Python
107+
run: uv python install
108+
109+
- name: Install the project
110+
run: uv sync --locked --all-extras --dev
111+
112+
- name: Generate protobuf code
113+
run: |
114+
source .venv/bin/activate
115+
./scripts/compile_proto.sh || (echo "Protobuf compilation failed. Ensure submodules are initialized and proto files exist." && exit 1)
116+
126117
- name: Set version from tag
127118
run: |
128119
if [[ "$GITHUB_REF" == refs/tags/* ]]; then

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ wheels/
1111
.env
1212

1313
docs/_build/
14+
15+
# Generated protobuf code
16+
src/resolver_athena_client/generated/

AGENTS.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
- Format code: `ruff format`
99
- Lint code: `ruff check`
1010
- Install git hooks: `pre-commit install`
11-
- Compile protobufs: `bash scripts/compile_proto.sh` (run from root)
11+
- Compile protobufs: `bash scripts/compile_proto.sh` (run from root, required for local development)
1212

1313
## Code style
1414
- Use Python type hints throughout
@@ -30,7 +30,7 @@
3030

3131
## PR instructions
3232
- Title format: [component] Description
33-
- Run `ruff check`, `pyright`, and `pytest` before committing
33+
- Run `bash scripts/compile_proto.sh`, `ruff check`, `pyright`, and `pytest` before committing
3434
- Keep PRs focused on a single change
3535
- Add tests for new functionality
3636
- Update documentation for API changes
@@ -40,7 +40,8 @@
4040
- Use `uv` package manager instead of pip
4141
- Don't use `uv pip` commands, just the base `uv` commands
4242
- Run formatters before committing
43-
- Check generated code in `src/athena_client/generated/`
43+
- Generated code is built automatically in CI, but run `bash scripts/compile_proto.sh` locally for development
44+
- Generated code in `src/resolver_athena_client/generated/` is not committed to the repo
4445
- Add error handling at each pipeline stage
4546
- Use correlation IDs for request tracing
4647

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[project]
2-
name = "resolver-athena-client"
2+
name = "resolver_athena_client"
33
version = "0.0.0"
44
description = "Python client library for Athena API - CSAM detection and content classification"
55
readme = "README.md"

scripts/compile_proto.sh

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,50 @@ OUT_DIR="src/resolver_athena_client/generated"
77
# Ensure output directory exists
88
mkdir -p "$OUT_DIR"
99

10+
# Create __init__.py files for Python packages
11+
touch "$OUT_DIR/__init__.py"
12+
mkdir -p "$OUT_DIR/athena"
13+
touch "$OUT_DIR/athena/__init__.py"
14+
1015
# Compile .proto files
11-
if [ ! -f "$PROTO_DIR/athena/athena.proto" ]; then
12-
echo "Error: Protobuf file not found at $PROTO_DIR/athena/athena.proto. Ensure the submodule is initialized and updated."
16+
if [ ! -d "$PROTO_DIR/athena" ]; then
17+
echo "Error: Athena protobuf directory not found at $PROTO_DIR/athena. Ensure the submodule is initialized and updated."
1318
exit 1
1419
fi
1520

16-
python -m grpc_tools.protoc \
21+
# Find all .proto files in the athena directory
22+
PROTO_FILES=$(find "$PROTO_DIR/athena" -name "*.proto")
23+
if [ -z "$PROTO_FILES" ]; then
24+
echo "Error: No .proto files found in $PROTO_DIR/athena."
25+
exit 1
26+
fi
27+
28+
echo "Found proto files: $PROTO_FILES"
29+
30+
uv run python -m grpc_tools.protoc \
1731
--proto_path="$PROTO_DIR" \
18-
--proto_path="$(python -c "import grpc_tools; print(grpc_tools.__path__[0])")" \
1932
--python_out="$OUT_DIR" \
2033
--grpc_python_out="$OUT_DIR" \
2134
--mypy_out="$OUT_DIR" \
22-
"$PROTO_DIR/athena/models.proto" \
23-
"$PROTO_DIR/athena/athena.proto"
35+
$PROTO_FILES
2436

2537
# Fix imports in generated files
26-
if [[ "$OSTYPE" == "darwin"* ]]; then
27-
sed -i '' -e 's/^from athena /from resolver_athena_client.generated.athena /' "$OUT_DIR/athena/athena_pb2_grpc.py"
28-
sed -i '' -e 's/^from athena /from resolver_athena_client.generated.athena /' "$OUT_DIR/athena/athena_pb2.py"
29-
sed -i '' -e 's/^from athena /from resolver_athena_client.generated.athena /' "$OUT_DIR/athena/models_pb2.py"
38+
GENERATED_FILES=$(find "$OUT_DIR/athena" -name "*_pb2*.py" 2>/dev/null)
39+
if [ -n "$GENERATED_FILES" ]; then
40+
for file in $GENERATED_FILES; do
41+
echo "Fixing imports in $file"
42+
if [[ "$OSTYPE" == "darwin"* ]]; then
43+
sed -i '' -e 's/^from athena /from resolver_athena_client.generated.athena /' "$file"
44+
else
45+
sed -i -e 's/^from athena /from resolver_athena_client.generated.athena /' "$file"
46+
fi
47+
done
3048
else
31-
sed -i -e 's/^from athena /from resolver_athena_client.generated.athena /' "$OUT_DIR/athena/athena_pb2_grpc.py"
32-
sed -i -e 's/^from athena /from resolver_athena_client.generated.athena /' "$OUT_DIR/athena/athena_pb2.py"
33-
sed -i -e 's/^from athena /from resolver_athena_client.generated.athena /' "$OUT_DIR/athena/models_pb2.py"
49+
echo "Warning: No generated Python files found to fix imports."
3450
fi
3551

36-
if [ ! -f "$OUT_DIR/athena/athena_pb2.py" ] || [ ! -f "$OUT_DIR/athena/athena_pb2_grpc.py" ] || [ ! -f "$OUT_DIR/athena/models_pb2.py" ]; then
52+
# Verify at least some files were generated
53+
if [ ! -d "$OUT_DIR/athena" ] || [ -z "$(find "$OUT_DIR/athena" -name "*_pb2*.py" 2>/dev/null)" ]; then
3754
echo "Error: Protobuf files were not generated successfully in $OUT_DIR."
3855
exit 1
3956
fi

src/resolver_athena_client/generated/__init__.py

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)