def require_tifa(self): """ Confirms that TIFA was run successfully, otherwise raises a CaitException. TODO: Is this deprecated? """ if not self.report[TIFA_TOOL_NAME]['success']: system_error(TOOL_NAME, "TIFA was not successfully run prior to CAIT.") raise CaitException("TIFA was not run prior to CAIT.")
def _parse_source(code, report=MAIN_REPORT): """ Parses the given code and returns its Cait representation. If the parse was unsuccessful, it attaches the error to the report. Args: code (str): A string of Python code. report (Report): A Report to store information in. Returns: AstNode: The parsed AST reprensetation, or None """ try: parsed = ast.parse(code) except SyntaxError as e: system_error(TOOL_NAME, "Could not parse code:" + str(e), report=report) report[TOOL_NAME]['success'] = False report[TOOL_NAME]['error'] = e return ast.parse("") return parsed
def process_code(self, code, filename=None, reset=True): """ Processes the AST of the given source code to generate a report. Args: code (str): The Python source code filename (str): The filename of the source code (defaults to the submissions' main file). reset (bool): Whether or not to reset the results from the previous analysis before running this one. Returns: Report: The successful or successful report object """ if reset or self.analysis is None: self.analysis = TifaAnalysis() filename = filename or self.report.submission.main_file self.line_offset = self.report.submission.line_offsets.get(filename, 0) # Attempt parsing - might fail! try: ast_tree = ast.parse(code, filename) except Exception as error: self.analysis.fail(error) system_error(TOOL_NAME, "Could not parse code: " + str(error), report=self.report) return self.analysis # Attempt processing code - might fail! try: self.process_ast(ast_tree) except Exception as error: self.analysis.fail(error) system_error(TOOL_NAME, "Successfully parsed but could not " "process AST: " + str(error), report=self.report) # Return whatever we got return self.analysis