def chase_database(h5file): """ Chase an HDF5 database to track information about stack and source code. The chase consist of a group in the root of the `h5file` having nodes that each profide information about the program execution that led to this chase of the database. Parameters ---------- h5file : tables.file.File The handle to the HDF5 database that should be chased. See Also -------- magni.reproducibility._chase.get_main_file_name : Name of main file magni.reproducibility._chase.get_main_file_source : Main file source code magni.reproducibility._chase.get_main_source : Source code around main magni.reproducibility._chase.get_stack_trace : Complete stack trace Notes ----- The chase include the following information: * main_file_name - Name of the main file/script that called this function * main_file_source - Full source code of the main file/script * main_source - Extract of main file source code that called this function * stack_trace - Complete stack trace up until the call to this function Examples -------- Chase the database named 'db.hdf5': >>> import magni >>> from magni.reproducibility.io import chase_database >>> with magni.utils.multiprocessing.File('db.hdf5', mode='a') as h5file: ... chase_database(h5file) """ @_decorate_validation def validate_input(): _generic('h5file', tables.file.File) validate_input() chases = { 'main_file_name': json.dumps(_chase.get_main_file_name()), 'main_file_source': json.dumps(_chase.get_main_file_source()), 'main_source': json.dumps(_chase.get_main_source()), 'stack_trace': json.dumps(_chase.get_stack_trace()) } try: chase_group = h5file.create_group('/', 'chases') for chase in chases: h5file.create_array(chase_group, chase, obj=chases[chase].encode()) h5file.flush() except tables.NodeError: raise tables.NodeError('The database has already been chased. ' + 'Remove the existing chase prior to ' + '(re)chasing the database.')
def test_get_main_source(self): main_source = _chase.get_main_source() self.assertIsInstance(main_source, str) self.assertNotEqual(main_source, '') self.assertNotIn('Failed', main_source)