Verified Commit 650766bb authored by Raphaël Bleuse's avatar Raphaël Bleuse
Browse files

Unify semantic of return codes

parent 800c1361
...@@ -10,6 +10,7 @@ import io ...@@ -10,6 +10,7 @@ import io
import json import json
import logging import logging
import sys import sys
import textwrap
import time import time
from pybatsim import __version__ from pybatsim import __version__
...@@ -88,6 +89,15 @@ class _JsonStoreAction(argparse.Action): ...@@ -88,6 +89,15 @@ class _JsonStoreAction(argparse.Action):
def _build_parser(): def _build_parser():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description='Run a PyBatsim scheduler.', description='Run a PyBatsim scheduler.',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=textwrap.dedent('''\
exit status:
%(prog)s can exit with the following return codes:
0 success
1 simulation failure
2 argument parsing error
'''
)
) )
parser.add_argument( parser.add_argument(
'--version', '--version',
...@@ -134,17 +144,16 @@ def _build_parser(): ...@@ -134,17 +144,16 @@ def _build_parser():
return parser return parser
def _abort_on_ambiguous_scheduler_name(name): def _abort_on_ambiguous_scheduler_name(name, *, parser):
ambiguous_names = find_ambiguous_scheduler_names() ambiguous_names = find_ambiguous_scheduler_names()
if name in ambiguous_names: if name in ambiguous_names:
print( errmsg = (
f'Error in definition of \'{SCHEDULER_ENTRY_POINT}\' entry point,', f'overlapping bindings in \'{SCHEDULER_ENTRY_POINT}\' entry point, '
'check your packaging!', 'check your packaging! '
f'\'{name}\' is defined more than once, and binds to:', f'\'{name}\' is defined more than once, and binds to: '
', '.join(ambiguous_names[name]),
file=sys.stderr,
) )
sys.exit(1) errmsg += ', '.join(ambiguous_names[name])
parser.error(errmsg)
def run_simulation(scheduler, *, socket_endpoint, event_socket_endpoint, timeout): def run_simulation(scheduler, *, socket_endpoint, event_socket_endpoint, timeout):
...@@ -183,12 +192,19 @@ def run_simulation(scheduler, *, socket_endpoint, event_socket_endpoint, timeout ...@@ -183,12 +192,19 @@ def run_simulation(scheduler, *, socket_endpoint, event_socket_endpoint, timeout
def main(args=None): def main(args=None):
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
# retrieve arguments
parser = _build_parser() parser = _build_parser()
arguments = parser.parse_args(args) arguments = parser.parse_args(args)
logging.debug(f'parsed arguments: {vars(arguments)}') logging.debug(f'parsed arguments: {vars(arguments)}')
# instantiate scheduler # instantiate scheduler
_abort_on_ambiguous_scheduler_name(arguments.scheduler) _abort_on_ambiguous_scheduler_name(arguments.scheduler, parser=parser)
scheduler = get_scheduler_by_name(arguments.scheduler, options=arguments.scheduler_options) try:
scheduler = get_scheduler_by_name(arguments.scheduler, options=arguments.scheduler_options)
except ValueError:
parser.error(f'unknown scheduler \'{arguments.scheduler}\'')
# launch simulation # launch simulation
run_simulation( run_simulation(
scheduler=scheduler, scheduler=scheduler,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment