OPEN SOURCE
software/scripts/compile_protos.py
このサイトで読み、必要なファイルを保存できます。
日常訳はAIを用いて作成された翻訳です。人間の聖書学・原語の専門家による全訳の検証は確認されていません。既存の翻訳や原語資料と照らし合わせてお読みください。
1#!/usr/bin/env python32"""Compile Protocol Buffer definitions to Python code.34This script compiles the .proto files in the protos/ directory to Python code5in the src/protos/ directory.67Usage:8uv run python scripts/compile_protos.py910Requirements:11Install the proto optional dependencies:12uv pip install -e ".[proto]"13"""1415from __future__ import annotations1617import subprocess18import sys19from pathlib import Path202122def main() -> int:23"""Compile proto files to Python."""24project_root = Path(__file__).parent.parent25protos_dir = project_root / "protos"26output_dir = project_root / "src" / "protos"2728# Ensure directories exist29protos_dir.mkdir(exist_ok=True)30output_dir.mkdir(exist_ok=True)3132# Find all .proto files33proto_files = list(protos_dir.glob("*.proto"))3435if not proto_files:36print("No .proto files found in protos/ directory")37return 13839print(f"Found {len(proto_files)} proto file(s):")40for pf in proto_files:41print(f" - {pf.name}")4243# Compile each proto file44for proto_file in proto_files:45print(f"\nCompiling {proto_file.name}...")4647cmd = [48sys.executable,49"-m",50"grpc_tools.protoc",51f"--proto_path={protos_dir}",52# Include Google protobuf well-known types53f"--proto_path={_get_protobuf_include_path()}",54f"--python_out={output_dir}",55f"--pyi_out={output_dir}", # Generate type stubs56str(proto_file),57]5859try:60result = subprocess.run(cmd, capture_output=True, text=True, check=False)6162if result.returncode != 0:63print(f"Error compiling {proto_file.name}:")64print(result.stderr)65return result.returncode6667print(f" Generated: {proto_file.stem}_pb2.py")68print(f" Generated: {proto_file.stem}_pb2.pyi")6970except FileNotFoundError:71print("Error: grpc_tools not found. Install with:")72print(' uv pip install -e ".[proto]"')73return 17475print("\nProto compilation complete!")76return 0777879def _get_protobuf_include_path() -> str:80"""Get the path to protobuf well-known types."""81try:82import grpc_tools.protoc8384grpc_tools_path = Path(grpc_tools.protoc.__file__).parent85# The _proto directory contains google/protobuf/*.proto86proto_path = grpc_tools_path / "_proto"87if proto_path.exists():88return str(proto_path)89except ImportError:90pass9192# Fallback: try to find in site-packages93try:94import google.protobuf9596protobuf_path = Path(google.protobuf.__file__).parent.parent97return str(protobuf_path)98except ImportError:99pass100101# Last resort: return empty string and let protoc find it102return ""103104105if __name__ == "__main__":106sys.exit(main())107