10 # This module is commonly used by others in here and should avoid
11 # taking any unnecessary dependencies back on them.
16 logger = logging.getLogger(__name__)
18 args = config.add_commandline_args(
19 f'Bootstrap ({__file__})',
20 'Args related to python program bootstrapper and Swiss army knife')
22 '--debug_unhandled_exceptions',
23 action=argparse_utils.ActionNoYes,
25 help='Break into debugger on top level unhandled exceptions for interactive debugging'
29 def handle_uncaught_exception(
33 if issubclass(exc_type, KeyboardInterrupt):
34 sys.__excepthook__(exc_type, exc_value, exc_traceback)
36 logger.exception(f'Unhandled top level {exc_type}',
37 exc_info=(exc_type, exc_value, exc_traceback))
38 traceback.print_exception(exc_type, exc_value, exc_traceback)
39 if config.config['debug_unhandled_exceptions']:
40 logger.info("Invoking the debugger...")
44 def initialize(funct):
47 """Remember to initialize config and logging before running main."""
48 @functools.wraps(funct)
49 def initialize_wrapper(*args, **kwargs):
50 sys.excepthook = handle_uncaught_exception
52 logging_utils.initialize_logging(logging.getLogger())
54 logger.debug(f'Starting {funct.__name__}')
55 start = time.perf_counter()
56 ret = funct(*args, **kwargs)
57 end = time.perf_counter()
58 logger.debug(f'{funct} returned {ret}.')
59 (utime, stime, cutime, cstime, elapsed_time) = os.times()
60 logger.debug(f'\nuser: {utime}s\n'
62 f'child user: {cutime}s\n'
63 f'child system: {cstime}s\n'
64 f'elapsed: {elapsed_time}s\n'
65 f'walltime: {end - start}s\n')
67 logger.info(f'Exit {ret}')
69 logger.debug(f'Exit {ret}')
71 return initialize_wrapper