ACL uses enums, some more tests, other stuff.
[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 # This module is commonly used by others in here and should avoid
11 # taking any unnecessary dependencies back on them.
12 import argparse_utils
13 import config
14
15
16 logger = logging.getLogger(__name__)
17
18 args = config.add_commandline_args(
19     f'Bootstrap ({__file__})',
20     'Args related to python program bootstrapper and Swiss army knife')
21 args.add_argument(
22     '--debug_unhandled_exceptions',
23     action=argparse_utils.ActionNoYes,
24     default=False,
25     help='Break into debugger on top level unhandled exceptions for interactive debugging'
26 )
27
28
29 def handle_uncaught_exception(
30         exc_type,
31         exc_value,
32         exc_traceback):
33     if issubclass(exc_type, KeyboardInterrupt):
34         sys.__excepthook__(exc_type, exc_value, exc_traceback)
35         return
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...")
41         breakpoint()
42
43
44 def initialize(funct):
45     import logging_utils
46
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
51         config.parse()
52         logging_utils.initialize_logging(logging.getLogger())
53         config.late_logging()
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'
61                      f'system: {stime}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')
66         if ret != 0:
67             logger.info(f'Exit {ret}')
68         else:
69             logger.debug(f'Exit {ret}')
70         sys.exit(ret)
71     return initialize_wrapper