1
1
import asyncio
2
- import atexit
3
2
import os
4
- import platform
5
3
import sys
6
- import tempfile
7
4
from collections .abc import Generator
8
5
from pathlib import Path
9
6
@@ -30,53 +27,23 @@ def get_directories(path):
30
27
async def protoc (path : str | Path , output_dir : str | Path , reference : bool = False , pydantic_dataclasses : bool = False ):
31
28
path : Path = Path (path ).resolve ()
32
29
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" )
30
+ python_out_option : str = "python_out" if reference else "python_betterproto2_out"
31
+
32
+ command = [
33
+ sys .executable ,
34
+ "-m" ,
35
+ "grpc.tools.protoc" ,
36
+ f"--proto_path={ path .as_posix ()} " ,
37
+ f"--{ python_out_option } ={ output_dir .as_posix ()} " ,
38
+ * [p .as_posix () for p in path .glob ("*.proto" )],
39
+ ]
40
+
41
+ if not reference :
42
+ command .insert (3 , "--python_betterproto2_opt=server_generation=async" )
43
+ command .insert (3 , "--python_betterproto2_opt=client_generation=async_sync" )
44
+
45
+ if pydantic_dataclasses :
46
+ command .insert (3 , "--python_betterproto2_opt=pydantic_dataclasses" )
80
47
81
48
proc = await asyncio .create_subprocess_exec (
82
49
* command ,
0 commit comments