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