Various
[python_utils.git] / bootstrap.py
1 #!/usr/bin/env python3
2
3 import functools
4 import logging
5 import os
6 import pdb
7 import sys
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
13 from argparse_utils import ActionNoYes
14 import config
15 import logging_utils
16
17 logger = logging.getLogger(__name__)
18
19 args = config.add_commandline_args(
20     f'Bootstrap ({__file__})',
21     'Args related to python program bootstrapper and Swiss army knife')
22 args.add_argument(
23     '--debug_unhandled_exceptions',
24     action=ActionNoYes,
25     default=False,
26     help='Break into pdb on top level unhandled exceptions.'
27 )
28
29
30 def handle_uncaught_exception(
31         exc_type,
32         exc_value,
33         exc_traceback):
34     if issubclass(exc_type, KeyboardInterrupt):
35         sys.__excepthook__(exc_type, exc_value, exc_traceback)
36         return
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...")
42         pdb.pm()
43
44
45 def initialize(entry_point):
46
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
51         if (
52                 '__globals__' in entry_point.__dict__ and
53                 '__file__' in entry_point.__globals__
54         ):
55             config.parse(entry_point.__globals__['__file__'])
56         else:
57             config.parse(None)
58
59         logging_utils.initialize_logging(logging.getLogger())
60
61         config.late_logging()
62
63         logger.debug(f'Starting {entry_point.__name__} (program entry point)')
64
65         ret = None
66         import stopwatch
67         with stopwatch.Timer() as t:
68             ret = entry_point(*args, **kwargs)
69         logger.debug(
70             f'{entry_point.__name__} (program entry point) returned {ret}.'
71         )
72
73         walltime = t()
74         (utime, stime, cutime, cstime, elapsed_time) = os.times()
75         logger.debug('\n'
76                      f'user: {utime}s\n'
77                      f'system: {stime}s\n'
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')
82         if ret != 0:
83             logger.info(f'Exit {ret}')
84         else:
85             logger.debug(f'Exit {ret}')
86         sys.exit(ret)
87     return initialize_wrapper