7 from inspect import stack
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.'
32 help='Should we display (and log.debug) the global random seed?'
40 help='Override the global random seed with a particular number.'
46 help='Should we dump the Python import tree before main?'
49 '--audit_import_events',
52 help='Should we audit all import events?',
56 original_hook = sys.excepthook
58 def handle_uncaught_exception(exc_type, exc_value, exc_tb):
60 Top-level exception handler for exceptions that make it past any exception
61 handlers in the python code being run. Logs the error and stacktrace then
62 maybe attaches a debugger.
65 msg = f'Unhandled top level exception {exc_type}'
67 print(msg, file=sys.stderr)
68 if issubclass(exc_type, KeyboardInterrupt):
69 sys.__excepthook__(exc_type, exc_value, exc_tb)
73 not sys.stderr.isatty() or
74 not sys.stdin.isatty()
76 # stdin or stderr is redirected, just do the normal thing
77 original_hook(exc_type, exc_value, exc_tb)
79 # a terminal is attached and stderr is not redirected, maybe debug.
81 traceback.print_exception(exc_type, exc_value, exc_tb)
82 if config.config['debug_unhandled_exceptions']:
84 logger.info("Invoking the debugger...")
87 original_hook(exc_type, exc_value, exc_tb)
90 class ImportInterceptor(importlib.abc.MetaPathFinder):
93 self.module_by_filename_cache = {}
94 self.repopulate_modules_by_filename()
95 self.tree = collect.trie.Trie()
96 self.tree_node_by_module = {}
98 def repopulate_modules_by_filename(self):
99 self.module_by_filename_cache.clear()
100 for mod in sys.modules:
101 if hasattr(sys.modules[mod], '__file__'):
102 fname = getattr(sys.modules[mod], '__file__')
105 self.module_by_filename_cache[fname] = mod
107 def should_ignore_filename(self, filename: str) -> bool:
108 return 'importlib' in filename or 'six.py' in filename
110 def find_spec(self, loaded_module, path=None, target=None):
112 for x in range(3, len(s)):
113 filename = s[x].filename
114 if self.should_ignore_filename(filename):
117 loading_function = s[x].function
118 if filename in self.module_by_filename_cache:
119 loading_module = self.module_by_filename_cache[filename]
121 self.repopulate_modules_by_filename()
122 loading_module = self.module_by_filename_cache.get(filename, 'unknown')
124 path = self.tree_node_by_module.get(loading_module, [])
125 path.extend([loaded_module])
126 self.tree.insert(path)
127 self.tree_node_by_module[loading_module] = path
129 msg = f'*** Import {loaded_module} from {filename}:{s[x].lineno} in {loading_module}::{loading_function}'
133 msg = f'*** Import {loaded_module} from ?????'
137 def find_importer(self, module: str):
138 if module in self.tree_node_by_module:
139 node = self.tree_node_by_module[module]
144 # TODO: test this with python 3.8+
145 def audit_import_events(event, args):
150 # Audit import events? Note: this runs early in the lifetime of the
151 # process (assuming that import bootstrap happens early); config has
152 # (probably) not yet been loaded or parsed the commandline. Also,
153 # some things have probably already been imported while we weren't
154 # watching so this information may be incomplete.
156 # Also note: move bootstrap up in the global import list to catch
157 # more import events and have a more complete record.
158 import_interceptor = None
160 if arg == '--audit_import_events':
161 if not hasattr(sys, 'frozen'):
163 sys.version_info[0] == 3
164 and sys.version_info[1] < 8
166 import_interceptor = ImportInterceptor()
167 sys.meta_path = [import_interceptor] + sys.meta_path
169 sys.addaudithook(audit_import_events)
172 def dump_all_objects() -> None:
173 global import_interceptor
175 all_modules = sys.modules
176 for obj in object.__subclasses__():
177 if not hasattr(obj, '__name__'):
180 if not hasattr(obj, '__module__'):
182 class_mod_name = obj.__module__
183 if class_mod_name in all_modules:
184 mod = all_modules[class_mod_name]
185 if not hasattr(mod, '__name__'):
186 mod_name = class_mod_name
188 mod_name = mod.__name__
189 if hasattr(mod, '__file__'):
190 mod_file = mod.__file__
193 if import_interceptor is not None:
194 import_path = import_interceptor.find_importer(mod_name)
196 import_path = 'unknown'
197 msg = f'{class_mod_name}::{klass} ({mod_file})'
198 if import_path != 'unknown' and len(import_path) > 0:
199 msg += f' imported by {import_path}'
200 messages[f'{class_mod_name}::{klass}'] = msg
201 for x in sorted(messages.keys()):
202 logger.debug(messages[x])
206 def initialize(entry_point):
208 Remember to initialize config, initialize logging, set/log a random
209 seed, etc... before running main.
212 @functools.wraps(entry_point)
213 def initialize_wrapper(*args, **kwargs):
214 # Hook top level unhandled exceptions, maybe invoke debugger.
215 if sys.excepthook == sys.__excepthook__:
216 sys.excepthook = handle_uncaught_exception
218 # Try to figure out the name of the program entry point. Then
219 # parse configuration (based on cmdline flags, environment vars
222 '__globals__' in entry_point.__dict__ and
223 '__file__' in entry_point.__globals__
225 config.parse(entry_point.__globals__['__file__'])
229 # Initialize logging... and log some remembered messages from
231 logging_utils.initialize_logging(logging.getLogger())
232 config.late_logging()
234 # Allow programs that don't bother to override the random seed
235 # to be replayed via the commandline.
237 random_seed = config.config['set_random_seed']
238 if random_seed is not None:
239 random_seed = random_seed[0]
241 random_seed = int.from_bytes(os.urandom(4), 'little')
243 if config.config['show_random_seed']:
244 msg = f'Global random seed is: {random_seed}'
247 random.seed(random_seed)
249 # Do it, invoke the user's code. Pay attention to how long it takes.
250 logger.debug(f'Starting {entry_point.__name__} (program entry point)')
253 with stopwatch.Timer() as t:
254 ret = entry_point(*args, **kwargs)
256 f'{entry_point.__name__} (program entry point) returned {ret}.'
259 if config.config['dump_all_objects']:
262 if config.config['audit_import_events']:
263 global import_interceptor
264 if import_interceptor is not None:
265 print(import_interceptor.tree)
268 (utime, stime, cutime, cstime, elapsed_time) = os.times()
271 f'system: {stime}s\n'
272 f'child user: {cutime}s\n'
273 f'child system: {cstime}s\n'
274 f'machine uptime: {elapsed_time}s\n'
275 f'walltime: {walltime}s')
277 # If it doesn't return cleanly, call attention to the return value.
278 if ret is not None and ret != 0:
279 logger.error(f'Exit {ret}')
281 logger.debug(f'Exit {ret}')
283 return initialize_wrapper