11import asyncio
2- import atexit
32import os
4- import platform
53import sys
6- import tempfile
7- from collections .abc import Generator
84from pathlib import Path
95
106os .environ ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION" ] = "python"
1612output_path_betterproto_pydantic = root_path .joinpath ("output_betterproto_pydantic" )
1713
1814
19- def get_files (path , suffix : str ) -> Generator [str , None , None ]:
20- for r , dirs , files in os .walk (path ):
21- for filename in [f for f in files if f .endswith (suffix )]:
22- yield os .path .join (r , filename )
23-
24-
2515def get_directories (path ):
2616 for root , directories , files in os .walk (path ):
2717 yield from directories
@@ -30,53 +20,23 @@ def get_directories(path):
3020async def protoc (path : str | Path , output_dir : str | Path , reference : bool = False , pydantic_dataclasses : bool = False ):
3121 path : Path = Path (path ).resolve ()
3222 output_dir : Path = Path (output_dir ).resolve ()
33- python_out_option : str = "python_betterproto2_out" if not reference else "python_out"
34-
35- if pydantic_dataclasses :
36- plugin_path = Path ("src/betterproto2_compiler/plugin/main.py" )
37-
38- if "Win" in platform .system ():
39- with tempfile .NamedTemporaryFile ("w" , encoding = "UTF-8" , suffix = ".bat" , delete = False ) as tf :
40- # See https://stackoverflow.com/a/42622705
41- tf .writelines (
42- [
43- "@echo off" ,
44- f"\n chdir { os .getcwd ()} " ,
45- f"\n { sys .executable } -u { plugin_path .as_posix ()} " ,
46- ],
47- )
48-
49- tf .flush ()
50-
51- plugin_path = Path (tf .name )
52- atexit .register (os .remove , plugin_path )
53-
54- command = [
55- sys .executable ,
56- "-m" ,
57- "grpc.tools.protoc" ,
58- f"--plugin=protoc-gen-custom={ plugin_path .as_posix ()} " ,
59- "--experimental_allow_proto3_optional" ,
60- "--custom_opt=pydantic_dataclasses" ,
61- "--custom_opt=client_generation=async_sync" ,
62- "--custom_opt=server_generation=async" ,
63- f"--proto_path={ path .as_posix ()} " ,
64- f"--custom_out={ output_dir .as_posix ()} " ,
65- * [p .as_posix () for p in path .glob ("*.proto" )],
66- ]
67- else :
68- command = [
69- sys .executable ,
70- "-m" ,
71- "grpc.tools.protoc" ,
72- f"--proto_path={ path .as_posix ()} " ,
73- f"--{ python_out_option } ={ output_dir .as_posix ()} " ,
74- * [p .as_posix () for p in path .glob ("*.proto" )],
75- ]
76-
77- if not reference :
78- command .insert (3 , "--python_betterproto2_opt=server_generation=async" )
79- command .insert (3 , "--python_betterproto2_opt=client_generation=async_sync" )
23+ python_out_option : str = "python_out" if reference else "python_betterproto2_out"
24+
25+ command = [
26+ sys .executable ,
27+ "-m" ,
28+ "grpc.tools.protoc" ,
29+ f"--proto_path={ path .as_posix ()} " ,
30+ f"--{ python_out_option } ={ output_dir .as_posix ()} " ,
31+ * [p .as_posix () for p in path .glob ("*.proto" )],
32+ ]
33+
34+ if not reference :
35+ command .insert (3 , "--python_betterproto2_opt=server_generation=async" )
36+ command .insert (3 , "--python_betterproto2_opt=client_generation=async_sync" )
37+
38+ if pydantic_dataclasses :
39+ command .insert (3 , "--python_betterproto2_opt=pydantic_dataclasses" )
8040
8141 proc = await asyncio .create_subprocess_exec (
8242 * command ,
0 commit comments