6 from inspect import stack
9 # This module is commonly used by others in here and should avoid
10 # taking any unnecessary dependencies back on them.
12 from argparse_utils import ActionNoYes
16 logger = logging.getLogger(__name__)
18 args = config.add_commandline_args(
19 f'Bootstrap ({__file__})',
20 '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?',
58 help='Should we run cProfile on this code?',
61 original_hook = sys.excepthook
64 def handle_uncaught_exception(exc_type, exc_value, exc_tb):
66 Top-level exception handler for exceptions that make it past any exception
67 handlers in the python code being run. Logs the error and stacktrace then
68 maybe attaches a debugger.
72 msg = f'Unhandled top level exception {exc_type}'
74 print(msg, file=sys.stderr)
75 if issubclass(exc_type, KeyboardInterrupt):
76 sys.__excepthook__(exc_type, exc_value, exc_tb)
79 if not sys.stderr.isatty() or not sys.stdin.isatty():
80 # stdin or stderr is redirected, just do the normal thing
81 original_hook(exc_type, exc_value, exc_tb)
83 # a terminal is attached and stderr is not redirected, maybe debug.
86 traceback.print_exception(exc_type, exc_value, exc_tb)
87 if config.config['debug_unhandled_exceptions']:
90 logger.info("Invoking the debugger...")
93 original_hook(exc_type, exc_value, exc_tb)
96 class ImportInterceptor(object):
100 self.module_by_filename_cache = {}
101 self.repopulate_modules_by_filename()
102 self.tree = collect.trie.Trie()
103 self.tree_node_by_module = {}
105 def repopulate_modules_by_filename(self):
106 self.module_by_filename_cache.clear()
107 for mod in sys.modules:
108 if hasattr(sys.modules[mod], '__file__'):
109 fname = getattr(sys.modules[mod], '__file__')
112 self.module_by_filename_cache[fname] = mod
114 def should_ignore_filename(self, filename: str) -> bool:
115 return 'importlib' in filename or 'six.py' in filename
117 def find_spec(self, loaded_module, path=None, target=None):
119 for x in range(3, len(s)):
120 filename = s[x].filename
121 if self.should_ignore_filename(filename):
124 loading_function = s[x].function
125 if filename in self.module_by_filename_cache:
126 loading_module = self.module_by_filename_cache[filename]
128 self.repopulate_modules_by_filename()
129 loading_module = self.module_by_filename_cache.get(filename, 'unknown')
131 path = self.tree_node_by_module.get(loading_module, [])
132 path.extend([loaded_module])
133 self.tree.insert(path)
134 self.tree_node_by_module[loading_module] = path
136 msg = f'*** Import {loaded_module} from {filename}:{s[x].lineno} in {loading_module}::{loading_function}'
140 msg = f'*** Import {loaded_module} from ?????'
144 def find_importer(self, module: str):
145 if module in self.tree_node_by_module:
146 node = self.tree_node_by_module[module]
151 # Audit import events? Note: this runs early in the lifetime of the
152 # process (assuming that import bootstrap happens early); config has
153 # (probably) not yet been loaded or parsed the commandline. Also,
154 # some things have probably already been imported while we weren't
155 # watching so this information may be incomplete.
157 # Also note: move bootstrap up in the global import list to catch
158 # more import events and have a more complete record.
159 import_interceptor = None
161 if arg == '--audit_import_events':
162 import_interceptor = ImportInterceptor()
163 sys.meta_path = [import_interceptor] + sys.meta_path
166 def dump_all_objects() -> None:
167 global import_interceptor
169 all_modules = sys.modules
170 for obj in object.__subclasses__():
171 if not hasattr(obj, '__name__'):
174 if not hasattr(obj, '__module__'):
176 class_mod_name = obj.__module__
177 if class_mod_name in all_modules:
178 mod = all_modules[class_mod_name]
179 if not hasattr(mod, '__name__'):
180 mod_name = class_mod_name
182 mod_name = mod.__name__
183 if hasattr(mod, '__file__'):
184 mod_file = mod.__file__
187 if import_interceptor is not None:
188 import_path = import_interceptor.find_importer(mod_name)
190 import_path = 'unknown'
191 msg = f'{class_mod_name}::{klass} ({mod_file})'
192 if import_path != 'unknown' and len(import_path) > 0:
193 msg += f' imported by {import_path}'
194 messages[f'{class_mod_name}::{klass}'] = msg
195 for x in sorted(messages.keys()):
196 logger.debug(messages[x])
200 def initialize(entry_point):
202 Remember to initialize config, initialize logging, set/log a random
203 seed, etc... before running main.
207 @functools.wraps(entry_point)
208 def initialize_wrapper(*args, **kwargs):
209 # Hook top level unhandled exceptions, maybe invoke debugger.
210 if sys.excepthook == sys.__excepthook__:
211 sys.excepthook = handle_uncaught_exception
213 # Try to figure out the name of the program entry point. Then
214 # parse configuration (based on cmdline flags, environment vars
217 '__globals__' in entry_point.__dict__
218 and '__file__' in entry_point.__globals__
220 config.parse(entry_point.__globals__['__file__'])
224 # Initialize logging... and log some remembered messages from
226 logging_utils.initialize_logging(logging.getLogger())
227 config.late_logging()
229 # Allow programs that don't bother to override the random seed
230 # to be replayed via the commandline.
233 random_seed = config.config['set_random_seed']
234 if random_seed is not None:
235 random_seed = random_seed[0]
237 random_seed = int.from_bytes(os.urandom(4), 'little')
239 if config.config['show_random_seed']:
240 msg = f'Global random seed is: {random_seed}'
243 random.seed(random_seed)
245 # Do it, invoke the user's code. Pay attention to how long it takes.
246 logger.debug(f'Starting {entry_point.__name__} (program entry point)')
250 if config.config['run_profiler']:
252 from pstats import SortKey
254 with stopwatch.Timer() as t:
256 "ret = entry_point(*args, **kwargs)",
263 with stopwatch.Timer() as t:
264 ret = entry_point(*args, **kwargs)
266 logger.debug(f'{entry_point.__name__} (program entry point) returned {ret}.')
268 if config.config['dump_all_objects']:
271 if config.config['audit_import_events']:
272 global import_interceptor
273 if import_interceptor is not None:
274 print(import_interceptor.tree)
277 (utime, stime, cutime, cstime, elapsed_time) = os.times()
281 f'system: {stime}s\n'
282 f'child user: {cutime}s\n'
283 f'child system: {cstime}s\n'
284 f'machine uptime: {elapsed_time}s\n'
285 f'walltime: {walltime}s'
288 # If it doesn't return cleanly, call attention to the return value.
289 if ret is not None and ret != 0:
290 logger.error(f'Exit {ret}')
292 logger.debug(f'Exit {ret}')
295 return initialize_wrapper