77 lines
2.3 KiB
Python
77 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Reproducibly generate the Python Shard-protocol stubs from the schema.
|
|
|
|
This is the documented, no-manual-copy generation entry point referenced by
|
|
``evidence/DGR-002/README.md``. It runs the pinned ``grpc_tools.protoc`` with the
|
|
same flags ``meshnet_node.native_protocol.generate()`` uses on demand, but is
|
|
kept self-contained (it does not import ``meshnet_node``) so it works regardless
|
|
of which checkout the editable install points at.
|
|
|
|
Usage (from the project .venv):
|
|
|
|
python packages/node/native/scripts/generate_python.py
|
|
|
|
Output: ``packages/node/native/build/python/shard_runtime_pb2{,_grpc}.py``
|
|
(``build/`` is gitignored).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pathlib
|
|
import sys
|
|
|
|
_NATIVE_ROOT = pathlib.Path(__file__).resolve().parents[1]
|
|
PROTO_DIR = _NATIVE_ROOT / "proto"
|
|
PROTO_FILE = PROTO_DIR / "shard_runtime.proto"
|
|
GEN_DIR = _NATIVE_ROOT / "build" / "python"
|
|
|
|
|
|
def _well_known_include() -> str | None:
|
|
try:
|
|
import grpc_tools
|
|
|
|
candidate = pathlib.Path(grpc_tools.__file__).parent / "_proto"
|
|
return str(candidate) if candidate.is_dir() else None
|
|
except Exception:
|
|
return None
|
|
|
|
|
|
def main() -> int:
|
|
if not PROTO_FILE.exists():
|
|
print(f"schema not found: {PROTO_FILE}", file=sys.stderr)
|
|
return 2
|
|
try:
|
|
from grpc_tools import protoc
|
|
except ImportError:
|
|
print(
|
|
"grpc_tools is required (pip install grpcio-tools); it is present in "
|
|
"the project .venv.",
|
|
file=sys.stderr,
|
|
)
|
|
return 3
|
|
|
|
GEN_DIR.mkdir(parents=True, exist_ok=True)
|
|
well_known = _well_known_include()
|
|
args = [
|
|
"grpc_tools.protoc",
|
|
f"-I{PROTO_DIR}",
|
|
*([f"-I{well_known}"] if well_known else []),
|
|
f"--python_out={GEN_DIR}",
|
|
f"--grpc_python_out={GEN_DIR}",
|
|
PROTO_FILE.name,
|
|
]
|
|
rc = protoc.main(args)
|
|
if rc != 0:
|
|
print(f"grpc_tools.protoc exited with status {rc}", file=sys.stderr)
|
|
return rc
|
|
|
|
print(f"generated Python stubs into: {GEN_DIR}")
|
|
for name in ("shard_runtime_pb2.py", "shard_runtime_pb2_grpc.py"):
|
|
target = GEN_DIR / name
|
|
print(f" {name}: {'ok' if target.exists() else 'MISSING'}")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|