def get_called_functions(self, driver_function): """return the called functions in their called sequence Returns: list(dict) -- called functions returned in a list(sorted) comprising of a dict keyed on filename, modulename and funcname. """ # spec = importlib.util.spec_from_file_location( # self.driver_module, self.driver_path) # foo = importlib.util.module_from_spec(spec) # spec.loader.exec_module(foo) # # main_2 = foo.main_2() # tracer = Trace(countfuncs=1) # function_to_be_called = foo.__getattribute__('main_2') # print(dir(function_to_be_called)) # func_name = function_to_be_called.__name__ # tracer.run('{}()'.format(func_name)) #resolve hardcoded driver function. # results = tracer.results() # print(results.calledfuncs) spec = importlib.util.spec_from_file_location( "driver", "/Users/aviralsrivastava/Desktop/source_code_to_study/driver.py") foo = importlib.util.module_from_spec(spec) spec.loader.exec_module(foo) # foo.main_2() tracer = Trace(countfuncs=1, ) tracer.run('foo.main_2()') results = tracer.results() return results.calledfuncs
def generate_sequential_function_calls(self): """generate sequential function calls for tracing source code and plotting sequence diagram. """ # generating sequence diagram for a use-case use_case, driver_path, driver_name, driver_function = self.get_driver_path_and_driver_name( ) generate_sequence_diagram = GenerateSequenceDiagram( driver_path, driver_name, self.source_code_path[0]) spec = importlib.util.spec_from_file_location(driver_name, driver_path) global foo foo = importlib.util.module_from_spec(spec) spec.loader.exec_module(foo) tracer = Trace(countfuncs=1, countcallers=1, timing=1) tracer.run('foo.{}()'.format(driver_function)) results = tracer.results() caller_functions = results.callers function_sequence = [] # consists of all functions called in sequence for caller, callee in caller_functions: _, caller_module, caller_function = caller _, _, callee_function = callee if caller_module not in self.source_code_modules: continue function_sequence.append([caller_function, callee_function]) for sequence in function_sequence: print(sequence) self.df = self.write_in_excel.integrate_sequence_diagram_in_df( self.df, function_sequence, use_case) self.write_in_excel.write_df_to_excel(self.df, 'sheet_one', self.skip_cols, self.classes_covered, use_case)
def generate_sequential_function_calls(self): """generate sequential function calls for tracing source code and plotting sequence diagram. """ # generating sequence diagram for a use-case # _ = GenerateSequenceDiagram( # self.driver_path, self.driver_name, self.source_code_path[0]) spec = importlib.util.spec_from_file_location(self.driver_name, self.driver_path) global foo foo = self.foo foo = importlib.util.module_from_spec(spec) spec.loader.exec_module(foo) tracer = Trace(countfuncs=1, countcallers=1, timing=1) tracer.run('foo.{}()'.format(self.driver_function)) results = tracer.results() caller_functions = results.callers function_sequence = [] # consists of all functions called in sequence for caller, callee in caller_functions: _, caller_module, caller_function = caller _, callee_module, callee_function = callee if caller_module not in self.source_code_modules or callee_module not in self.source_code_modules: logging.debug( "Following modules are not in source code and thus to be ignored:" ) logging.debug(caller_module) continue function_sequence.append([(caller_module, caller_function), (callee_module, callee_function)]) logging.debug("Function sequence is: ") for sequence in function_sequence: logging.debug(sequence) self.df = self.write_in_excel.integrate_sequence_diagram_in_df( self.df, function_sequence, self.use_case, self.driver_function, self.skip_cols) self.write_in_excel.write_df_to_excel(self.df, 'sheet_one', self.skip_cols, self.classes_covered, self.use_case)