Initial revision
[python_utils.git] / bootstrap.py
1 #!/usr/bin/env python3
2
3 import functools
4 import logging
5 import os
6 import sys
7 import time
8 import traceback
9
10 import argparse_utils
11 import config
12 import logging_utils
13
14
15 logger = logging.getLogger(__name__)
16
17 args = config.add_commandline_args(
18     f'Bootstrap ({__file__})',
19     'Args related to python program bootstrapper and Swiss army knife')
20 args.add_argument(
21     '--debug_unhandled_exceptions',
22     action=argparse_utils.ActionNoYes,
23     default=False,
24     help='Break into debugger on top level unhandled exceptions for interactive debugging'
25 )
26
27
28 def handle_uncaught_exception(
29         exc_type,
30         exc_value,
31         exc_traceback):
32     if issubclass(exc_type, KeyboardInterrupt):
33         sys.__excepthook__(exc_type, exc_value, exc_traceback)
34         return
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...")
40         breakpoint()
41
42
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
48         config.parse()
49         logging_utils.initialize_logging(logging.getLogger())
50         logger.debug(f"About to invoke {funct}...")
51         start = time.perf_counter()
52         ret = funct(*args, **kwargs)
53         end = time.perf_counter()
54         logger.debug(f'{funct} returned {ret}.')
55         (utime, stime, cutime, cstime, elapsed_time) = os.times()
56         logger.debug(f'\nuser: {utime}s\n'
57                      f'system: {stime}s\n'
58                      f'child user: {cutime}s\n'
59                      f'child system: {cstime}s\n'
60                      f'elapsed: {elapsed_time}s\n'
61                      f'walltime: {end - start}s\n')
62         logger.info(f'Exit {ret}')
63         sys.exit(ret)
64     return initialize_wrapper