22
22
import sys
23
23
import typing
24
24
from argparse import ArgumentParser
25
+ from pathlib import Path
25
26
from tempfile import TemporaryDirectory
26
27
27
28
@@ -142,8 +143,7 @@ async def run_bridge(program, storage_dir=None, rpc_admin_port=None,
142
143
secured_device_port = None ):
143
144
args = []
144
145
if storage_dir is not None :
145
- args .extend (["--KVS" ,
146
- os .path .join (storage_dir , "chip_fabric_bridge_kvs" )])
146
+ args .extend (["--KVS" , storage_dir .joinpath ("chip_fabric_bridge_kvs" )])
147
147
if rpc_admin_port is not None :
148
148
args .extend (["--fabric-admin-server-port" , str (rpc_admin_port )])
149
149
if rpc_bridge_port is not None :
@@ -169,14 +169,13 @@ async def main(args):
169
169
170
170
storage_dir = args .storage_dir
171
171
if storage_dir is not None :
172
- os . makedirs ( storage_dir , exist_ok = True )
172
+ storage_dir . mkdir ( parents = True , exist_ok = True )
173
173
else :
174
174
storage = TemporaryDirectory (prefix = "fabric-sync-app" )
175
- storage_dir = storage .name
175
+ storage_dir = Path ( storage .name )
176
176
177
- pipe = args .stdin_pipe
178
- if pipe and not os .path .exists (pipe ):
179
- os .mkfifo (pipe )
177
+ if args .stdin_pipe and not args .stdin_pipe .exists ():
178
+ os .mkfifo (args .stdin_pipe )
180
179
181
180
admin , bridge = await asyncio .gather (
182
181
run_admin (
@@ -206,6 +205,8 @@ def terminate():
206
205
admin .terminate ()
207
206
with contextlib .suppress (ProcessLookupError ):
208
207
bridge .terminate ()
208
+ if args .stdin_pipe :
209
+ args .stdin_pipe .unlink (missing_ok = True )
209
210
loop .remove_signal_handler (signal .SIGINT )
210
211
loop .remove_signal_handler (signal .SIGTERM )
211
212
@@ -247,13 +248,17 @@ def terminate():
247
248
await admin .send (f"pairing open-commissioning-window { bridge_node_id } { cw_endpoint_id } "
248
249
f" { cw_option } { cw_timeout } { cw_iteration } { cw_discriminator } " )
249
250
251
+ def get_input_forwarder ():
252
+ if args .stdin_pipe :
253
+ return forward_pipe (args .stdin_pipe , admin .p .stdin )
254
+ return forward_stdin (admin .p .stdin )
255
+
250
256
try :
251
- forward = forward_pipe (pipe , admin .p .stdin ) if pipe else forward_stdin (admin .p .stdin )
252
257
# Wait for any of the tasks to complete.
253
258
_ , pending = await asyncio .wait ([
254
259
asyncio .create_task (admin .wait ()),
255
260
asyncio .create_task (bridge .wait ()),
256
- asyncio .create_task (forward ),
261
+ asyncio .create_task (get_input_forwarder () ),
257
262
], return_when = asyncio .FIRST_COMPLETED )
258
263
# Cancel the remaining tasks.
259
264
for task in pending :
@@ -269,22 +274,22 @@ def terminate():
269
274
270
275
if __name__ == "__main__" :
271
276
parser = ArgumentParser (description = "Fabric-Sync Example Application" )
272
- parser .add_argument ("--app-admin" , metavar = "PATH" ,
277
+ parser .add_argument ("--app-admin" , metavar = "PATH" , type = Path ,
273
278
default = shutil .which ("fabric-admin" ),
274
279
help = "path to the fabric-admin executable; default=%(default)s" )
275
- parser .add_argument ("--app-bridge" , metavar = "PATH" ,
280
+ parser .add_argument ("--app-bridge" , metavar = "PATH" , type = Path ,
276
281
default = shutil .which ("fabric-bridge-app" ),
277
282
help = "path to the fabric-bridge executable; default=%(default)s" )
278
283
parser .add_argument ("--app-admin-rpc-port" , metavar = "PORT" , type = int ,
279
284
help = "fabric-admin RPC server port" )
280
285
parser .add_argument ("--app-bridge-rpc-port" , metavar = "PORT" , type = int ,
281
286
help = "fabric-bridge RPC server port" )
282
- parser .add_argument ("--stdin-pipe" , metavar = "PATH" ,
287
+ parser .add_argument ("--stdin-pipe" , metavar = "PATH" , type = Path ,
283
288
help = "read input from a named pipe instead of stdin" )
284
- parser .add_argument ("--storage-dir" , metavar = "PATH" ,
289
+ parser .add_argument ("--storage-dir" , metavar = "PATH" , type = Path ,
285
290
help = ("directory to place storage files in; by default "
286
291
"volatile storage is used" ))
287
- parser .add_argument ("--paa-trust-store-path" , metavar = "PATH" ,
292
+ parser .add_argument ("--paa-trust-store-path" , metavar = "PATH" , type = Path ,
288
293
help = "path to directory holding PAA certificates" )
289
294
parser .add_argument ("--commissioner-name" , metavar = "NAME" ,
290
295
help = "commissioner name to use for the admin" )
@@ -299,9 +304,11 @@ def terminate():
299
304
parser .add_argument ("--passcode" , metavar = "NUM" , type = int ,
300
305
help = "passcode to use for the bridge" )
301
306
args = parser .parse_args ()
302
- if args .app_admin is None or not os . path .exists (args . app_admin ):
307
+ if args .app_admin is None or not args . app_admin .exists ():
303
308
parser .error ("fabric-admin executable not found in PATH. Use '--app-admin' argument to provide it." )
304
- if args .app_bridge is None or not os . path .exists (args . app_bridge ):
309
+ if args .app_bridge is None or not args . app_bridge .exists ():
305
310
parser .error ("fabric-bridge-app executable not found in PATH. Use '--app-bridge' argument to provide it." )
311
+ if args .stdin_pipe and args .stdin_pipe .exists () and not args .stdin_pipe .is_fifo ():
312
+ parser .error ("given stdin pipe exists and is not a named pipe" )
306
313
with contextlib .suppress (KeyboardInterrupt ):
307
314
asyncio .run (main (args ))
0 commit comments