10 # This module is commonly used by others in here and should avoid
11 # taking any unnecessary dependencies back on them.
13 from argparse_utils import ActionNoYes
17 logger = logging.getLogger(__name__)
19 args = config.add_commandline_args(
20 f'Bootstrap ({__file__})',
21 'Args related to python program bootstrapper and Swiss army knife')
23 '--debug_unhandled_exceptions',
26 help='Break into pdb on top level unhandled exceptions.'
30 def handle_uncaught_exception(
34 if issubclass(exc_type, KeyboardInterrupt):
35 sys.__excepthook__(exc_type, exc_value, exc_traceback)
37 logger.exception(f'Unhandled top level {exc_type}',
38 exc_info=(exc_type, exc_value, exc_traceback))
39 traceback.print_exception(exc_type, exc_value, exc_traceback)
40 if config.config['debug_unhandled_exceptions']:
41 logger.info("Invoking the debugger...")
45 def initialize(entry_point):
47 """Remember to initialize config and logging before running main."""
48 @functools.wraps(entry_point)
49 def initialize_wrapper(*args, **kwargs):
50 sys.excepthook = handle_uncaught_exception
51 config.parse(entry_point.__globals__['__file__'])
54 logging_utils.initialize_logging(logging.getLogger())
58 logger.debug(f'Starting {entry_point.__name__} (program entry point)')
62 with timer.Timer() as t:
63 ret = entry_point(*args, **kwargs)
65 f'{entry_point.__name__} (program entry point) returned {ret}.'
69 (utime, stime, cutime, cstime, elapsed_time) = os.times()
73 f'child user: {cutime}s\n'
74 f'child system: {cstime}s\n'
75 f'elapsed: {elapsed_time}s\n'
76 f'walltime: {walltime}s\n')
78 logger.info(f'Exit {ret}')
80 logger.debug(f'Exit {ret}')
82 return initialize_wrapper