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
52 '__globals__' in entry_point.__dict__ and
53 '__file__' in entry_point.__globals__
55 config.parse(entry_point.__globals__['__file__'])
59 logging_utils.initialize_logging(logging.getLogger())
63 logger.debug(f'Starting {entry_point.__name__} (program entry point)')
67 with stopwatch.Timer() as t:
68 ret = entry_point(*args, **kwargs)
70 f'{entry_point.__name__} (program entry point) returned {ret}.'
74 (utime, stime, cutime, cstime, elapsed_time) = os.times()
78 f'child user: {cutime}s\n'
79 f'child system: {cstime}s\n'
80 f'machine uptime: {elapsed_time}s\n'
81 f'walltime: {walltime}s')
83 logger.info(f'Exit {ret}')
85 logger.debug(f'Exit {ret}')
87 return initialize_wrapper