15 logger = logging.getLogger(__name__)
17 args = config.add_commandline_args(
18 f'Bootstrap ({__file__})',
19 'Args related to python program bootstrapper and Swiss army knife')
21 '--debug_unhandled_exceptions',
22 action=argparse_utils.ActionNoYes,
24 help='Break into debugger on top level unhandled exceptions for interactive debugging'
28 def handle_uncaught_exception(
32 if issubclass(exc_type, KeyboardInterrupt):
33 sys.__excepthook__(exc_type, exc_value, exc_traceback)
35 logger.exception(f'Unhandled top level {exc_type}',
36 exc_info=(exc_type, exc_value, exc_traceback))
37 traceback.print_exception(exc_type, exc_value, exc_traceback)
38 if config.config['debug_unhandled_exceptions']:
39 logger.info("Invoking the debugger...")
43 def initialize(funct):
44 """Remember to initialize config and logging before running main."""
45 @functools.wraps(funct)
46 def initialize_wrapper(*args, **kwargs):
47 sys.excepthook = handle_uncaught_exception
49 logging_utils.initialize_logging(logging.getLogger())
51 logger.debug(f'Starting {funct.__name__}')
52 start = time.perf_counter()
53 ret = funct(*args, **kwargs)
54 end = time.perf_counter()
55 logger.debug(f'{funct} returned {ret}.')
56 (utime, stime, cutime, cstime, elapsed_time) = os.times()
57 logger.debug(f'\nuser: {utime}s\n'
59 f'child user: {cutime}s\n'
60 f'child system: {cstime}s\n'
61 f'elapsed: {elapsed_time}s\n'
62 f'walltime: {end - start}s\n')
64 logger.info(f'Exit {ret}')
66 logger.debug(f'Exit {ret}')
68 return initialize_wrapper