@@ -118,12 +118,16 @@ def fix_generated_output(base_path: Path):
118
118
f .write ("except ImportError:\n pass" )
119
119
120
120
121
- if __name__ == "__main__" :
122
- # Due to issues with the Python protobuf 3.x vs protobuf 4.x libraries, we
123
- # must require that grpcio tools be on 1.48.x and protobuf be on 3.x for
124
- # generation of protos. We can't check __version__ on the module (not
125
- # present), and we can't use importlib.metadata due to its absence in 3.7,
126
- # so we just run pip and check there.
121
+ def check_proto_toolchain_versions ():
122
+ """
123
+ Check protobuf and grpcio versions.
124
+
125
+ Due to issues with the Python protobuf 3.x vs protobuf 4.x libraries, we
126
+ must require that grpcio tools be on 1.48.x and protobuf be on 3.x for
127
+ generation of protos. We can't check __version__ on the module (not
128
+ present), and we can't use importlib.metadata due to its absence in 3.7,
129
+ so we just run pip and check there.
130
+ """
127
131
proc = subprocess .run (
128
132
["pip" , "list" , "--format" , "freeze" ],
129
133
check = True ,
@@ -144,56 +148,61 @@ def fix_generated_output(base_path: Path):
144
148
"1.48."
145
149
), f"expected 1.48.x grpcio-tools, found { grpcio_tools_version } "
146
150
147
- print ("Generating protos..." , file = sys .stderr )
148
- with tempfile .TemporaryDirectory (dir = base_dir ) as temp_dir_raw :
149
- temp_dir = Path (temp_dir_raw )
150
- subprocess .check_call (
151
- [
152
- sys .executable ,
153
- "-mgrpc_tools.protoc" ,
154
- f"--proto_path={ api_proto_dir } " ,
155
- f"--proto_path={ core_proto_dir } " ,
156
- f"--proto_path={ testsrv_proto_dir } " ,
157
- f"--proto_path={ health_proto_dir } " ,
158
- f"--proto_path={ test_proto_dir } " ,
159
- f"--proto_path={ additional_proto_dir } " ,
160
- f"--python_out={ temp_dir } " ,
161
- f"--grpc_python_out={ temp_dir } " ,
162
- f"--mypy_out={ temp_dir } " ,
163
- f"--mypy_grpc_out={ temp_dir } " ,
164
- * map (str , proto_paths ),
165
- ]
151
+
152
+ def generate_protos (output_dir : Path ):
153
+ subprocess .check_call (
154
+ [
155
+ sys .executable ,
156
+ "-mgrpc_tools.protoc" ,
157
+ f"--proto_path={ api_proto_dir } " ,
158
+ f"--proto_path={ core_proto_dir } " ,
159
+ f"--proto_path={ testsrv_proto_dir } " ,
160
+ f"--proto_path={ health_proto_dir } " ,
161
+ f"--proto_path={ test_proto_dir } " ,
162
+ f"--proto_path={ additional_proto_dir } " ,
163
+ f"--python_out={ output_dir } " ,
164
+ f"--grpc_python_out={ output_dir } " ,
165
+ f"--mypy_out={ output_dir } " ,
166
+ f"--mypy_grpc_out={ output_dir } " ,
167
+ * map (str , proto_paths ),
168
+ ]
169
+ )
170
+ # Remove every _grpc.py file that isn't part of a Temporal "service"
171
+ for grpc_file in output_dir .glob ("**/*_grpc.py*" ):
172
+ if (
173
+ len (grpc_file .parents ) < 2
174
+ or grpc_file .parents [0 ].name != "v1"
175
+ or not grpc_file .parents [1 ].name .endswith ("service" )
176
+ ):
177
+ grpc_file .unlink ()
178
+ # Apply fixes before moving code
179
+ fix_generated_output (output_dir )
180
+ # Move protos
181
+ for p in (output_dir / "temporal" / "api" ).iterdir ():
182
+ shutil .rmtree (api_out_dir / p .name , ignore_errors = True )
183
+ p .replace (api_out_dir / p .name )
184
+ shutil .rmtree (api_out_dir / "dependencies" , ignore_errors = True )
185
+ for p in (output_dir / "temporal" / "sdk" / "core" ).iterdir ():
186
+ shutil .rmtree (sdk_out_dir / p .name , ignore_errors = True )
187
+ p .replace (sdk_out_dir / p .name )
188
+ shutil .rmtree (sdk_out_dir / "health" , ignore_errors = True )
189
+ (output_dir / "health" ).replace (sdk_out_dir / "health" )
190
+ # Move test protos
191
+ for v in ["__init__.py" , "proto_message_pb2.py" , "proto_message_pb2.pyi" ]:
192
+ shutil .copy2 (
193
+ output_dir / "worker" / "workflow_sandbox" / "testmodules" / "proto" / v ,
194
+ test_proto_dir
195
+ / "worker"
196
+ / "workflow_sandbox"
197
+ / "testmodules"
198
+ / "proto"
199
+ / v ,
166
200
)
167
- # Remove every _grpc.py file that isn't part of a Temporal "service"
168
- for grpc_file in temp_dir .glob ("**/*_grpc.py*" ):
169
- if (
170
- len (grpc_file .parents ) < 2
171
- or grpc_file .parents [0 ].name != "v1"
172
- or not grpc_file .parents [1 ].name .endswith ("service" )
173
- ):
174
- grpc_file .unlink ()
175
- # Apply fixes before moving code
176
- fix_generated_output (temp_dir )
177
- # Move protos
178
- for p in (temp_dir / "temporal" / "api" ).iterdir ():
179
- shutil .rmtree (api_out_dir / p .name , ignore_errors = True )
180
- p .replace (api_out_dir / p .name )
181
- shutil .rmtree (api_out_dir / "dependencies" , ignore_errors = True )
182
- for p in (temp_dir / "temporal" / "sdk" / "core" ).iterdir ():
183
- shutil .rmtree (sdk_out_dir / p .name , ignore_errors = True )
184
- p .replace (sdk_out_dir / p .name )
185
- shutil .rmtree (sdk_out_dir / "health" , ignore_errors = True )
186
- (temp_dir / "health" ).replace (sdk_out_dir / "health" )
187
- # Move test protos
188
- for v in ["__init__.py" , "proto_message_pb2.py" , "proto_message_pb2.pyi" ]:
189
- shutil .copy2 (
190
- temp_dir / "worker" / "workflow_sandbox" / "testmodules" / "proto" / v ,
191
- test_proto_dir
192
- / "worker"
193
- / "workflow_sandbox"
194
- / "testmodules"
195
- / "proto"
196
- / v ,
197
- )
198
201
202
+
203
+ if __name__ == "__main__" :
204
+ check_proto_toolchain_versions ()
205
+ print ("Generating protos..." , file = sys .stderr )
206
+ with tempfile .TemporaryDirectory (dir = base_dir ) as temp_dir :
207
+ generate_protos (Path (temp_dir ))
199
208
print ("Done" , file = sys .stderr )
0 commit comments