remotivelabs.topology.cli
CLI argument parsers for RemotiveTopology.
This package provides argument parsers for the two built-in command-line entry points:
remotivelabs.topology.cli.behavioral_model.BehavioralModelArgs- parses arguments for behavioral model scripts (broker URL, API key, delay multiplier, log level).remotivelabs.topology.cli.mock.ECUMockArgs- parses arguments for the ECU mock runner (namespace/ECU mappings, broker URL, delay multiplier, log level).
The mock subpackage also ships a ready-to-run __main__ that starts an ECUMock directly:
python -m remotivelabs.topology.cli.mock -n <namespace>=<ecu name> -u http://127.0.0.1:50051
For behavioral model scripts, use BehavioralModelArgs.parse() at the top of your own __main__ block:
from remotivelabs.topology.cli.behavioral_model import BehavioralModelArgs
args = BehavioralModelArgs.parse()
Start the script with remotivelabs.topology.cli.run instead of asyncio.run. It works the same, but also
cancels main on SIGTERM, so docker stop tears down the behavioral model cleanly:
from remotivelabs.topology.cli import run
run(main(args))
1""" 2# CLI argument parsers for RemotiveTopology. 3 4<!-- start-include --> 5This package provides argument parsers for the two built-in command-line entry points: 6 7- `remotivelabs.topology.cli.behavioral_model.BehavioralModelArgs` - parses arguments for 8 behavioral model scripts (broker URL, API key, delay multiplier, log level). 9- `remotivelabs.topology.cli.mock.ECUMockArgs` - parses arguments for the ECU mock runner 10 (namespace/ECU mappings, broker URL, delay multiplier, log level). 11 12The `mock` subpackage also ships a ready-to-run `__main__` that starts an `ECUMock` directly: 13 14```bash 15python -m remotivelabs.topology.cli.mock -n <namespace>=<ecu name> -u http://127.0.0.1:50051 16``` 17 18For behavioral model scripts, use `BehavioralModelArgs.parse()` at the top of your own `__main__` block: 19 20```python 21from remotivelabs.topology.cli.behavioral_model import BehavioralModelArgs 22 23args = BehavioralModelArgs.parse() 24``` 25 26Start the script with `remotivelabs.topology.cli.run` instead of `asyncio.run`. It works the same, but also 27cancels `main` on SIGTERM, so `docker stop` tears down the behavioral model cleanly: 28 29```python 30from remotivelabs.topology.cli import run 31 32run(main(args)) 33``` 34<!-- end-include --> 35""" 36 37from remotivelabs.topology.cli.runner import run 38 39__all__ = ["run"]
17def run(main: Coroutine[Any, Any, _T]) -> _T: 18 """ 19 Utility wrapper to run `main` like `asyncio.run`, but also stop it cleanly on SIGTERM 20 (e.g. when receiving a `docker stop` command). 21 22 SIGTERM cancels `main`, the same way `asyncio.run` already treats Ctrl-C. Once `main` has unwound, 23 the process exits with status 143 (128 + SIGTERM). A second SIGTERM is ignored so it cannot interrupt the 24 teardown the first one started. 25 26 ```python 27 from remotivelabs.topology.cli import run 28 29 if __name__ == "__main__": 30 run(main()) 31 ``` 32 33 Signal handlers can only be installed on the main thread of a Unix event loop; elsewhere this behaves 34 exactly like `asyncio.run`. 35 """ 36 terminated = False 37 38 async def _cancel_on_sigterm() -> _T: 39 loop = asyncio.get_running_loop() 40 task = asyncio.current_task() 41 assert task is not None 42 43 def _on_sigterm() -> None: 44 nonlocal terminated 45 if terminated: 46 return 47 terminated = True 48 _logger.info("Received SIGTERM, shutting down") 49 task.cancel() 50 51 try: 52 loop.add_signal_handler(signal.SIGTERM, _on_sigterm) 53 except (NotImplementedError, RuntimeError): 54 return await main 55 try: 56 return await main 57 finally: 58 loop.remove_signal_handler(signal.SIGTERM) 59 60 try: 61 return asyncio.run(_cancel_on_sigterm()) 62 except asyncio.CancelledError: 63 if not terminated: 64 raise 65 raise SystemExit(128 + signal.SIGTERM)
Utility wrapper to run main like asyncio.run, but also stop it cleanly on SIGTERM
(e.g. when receiving a docker stop command).
SIGTERM cancels main, the same way asyncio.run already treats Ctrl-C. Once main has unwound,
the process exits with status 143 (128 + SIGTERM). A second SIGTERM is ignored so it cannot interrupt the
teardown the first one started.
from remotivelabs.topology.cli import run
if __name__ == "__main__":
run(main())
Signal handlers can only be installed on the main thread of a Unix event loop; elsewhere this behaves
exactly like asyncio.run.