2
2
import importlib
3
3
import os
4
4
import pathlib
5
+ import sys
5
6
from pathlib import Path
6
7
from types import ModuleType
7
- from typing import Callable , Generator , Optional
8
+ from typing import Callable , Generator , Optional , Union
8
9
9
10
os .environ ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION" ] = "python"
10
11
13
14
output_path_reference = root_path .joinpath ("output_reference" )
14
15
output_path_betterproto = root_path .joinpath ("output_betterproto" )
15
16
16
- if os .name == "nt" :
17
- plugin_path = root_path .joinpath (".." , "plugin.bat" ).resolve ()
18
- else :
19
- plugin_path = root_path .joinpath (".." , "plugin.py" ).resolve ()
20
-
21
17
22
18
def get_files (path , suffix : str ) -> Generator [str , None , None ]:
23
19
for r , dirs , files in os .walk (path ):
@@ -31,22 +27,25 @@ def get_directories(path):
31
27
yield directory
32
28
33
29
34
- async def protoc_plugin (path : str , output_dir : str ):
35
- proc = await asyncio .create_subprocess_shell (
36
- f"protoc --plugin=protoc-gen-custom={ plugin_path } --custom_out={ output_dir } --proto_path={ path } { path } /*.proto" ,
37
- stdout = asyncio .subprocess .PIPE ,
38
- stderr = asyncio .subprocess .PIPE ,
39
- )
40
- return (* (await proc .communicate ()), proc .returncode )
41
-
42
-
43
- async def protoc_reference (path : str , output_dir : str ):
44
- proc = await asyncio .create_subprocess_shell (
45
- f"protoc --python_out={ output_dir } --proto_path={ path } { path } /*.proto" ,
46
- stdout = asyncio .subprocess .PIPE ,
47
- stderr = asyncio .subprocess .PIPE ,
30
+ async def protoc (
31
+ path : Union [str , Path ], output_dir : Union [str , Path ], reference : bool = False
32
+ ):
33
+ path : Path = Path (path ).resolve ()
34
+ output_dir : Path = Path (output_dir ).resolve ()
35
+ python_out_option : str = "python_betterproto_out" if not reference else "python_out"
36
+ command = [
37
+ sys .executable ,
38
+ "-m" ,
39
+ "grpc.tools.protoc" ,
40
+ f"--proto_path={ path .as_posix ()} " ,
41
+ f"--{ python_out_option } ={ output_dir .as_posix ()} " ,
42
+ * [p .as_posix () for p in path .glob ("*.proto" )],
43
+ ]
44
+ proc = await asyncio .create_subprocess_exec (
45
+ * command , stdout = asyncio .subprocess .PIPE , stderr = asyncio .subprocess .PIPE ,
48
46
)
49
- return (* (await proc .communicate ()), proc .returncode )
47
+ stdout , stderr = await proc .communicate ()
48
+ return stdout , stderr , proc .returncode
50
49
51
50
52
51
def get_test_case_json_data (test_case_name : str , json_file_name : Optional [str ] = None ):
0 commit comments