def extract_rules(self, file_path): fd = open(file_path, 'r') std_description = False rl_description = False rule = Rule(None, None, None, None, None, None) splittedPath = file_path.split(os.sep) rule.key = splittedPath[-1].split('.')[0] rule.configKey = rule.key rule.category = "Reliability" rule.name ='' for line in fd.readlines(): if std_description: if not len(line)==0 and not line.startswith('---') and not line.startswith('Definition:'): rule.name += line elif rl_description: if line.startswith('Example:') or line.startswith('.'): rl_description=False else : rule.name = rule.name.strip() + " " rule.name += line if line.startswith('.SEVERITY '): severity = line[len('.SEVERITY ')-1:].strip() if severity in self.user_severities.keys(): rule.priority = self.user_severities[severity] elif severity in self.severities.keys(): rule.priority = self.severities[severity] elif re.match("\d+", severity): rule.priority = self.calculate_sonar_severity(severity) else: rule.priority = 'INFO' elif line.startswith('.DESCRIPTION'): std_description = True elif line.startswith('.TITLE Description'): rl_description = True if std_description: #Trying to get characters until "." is met if rule.name.find('.') != -1: rule.name = rule.name[:rule.name.find('.')] #Last problem : sometimes, the above verification is not enough strengthful. So we have to check that there isn't more than the description in the string if rule.name.find(".SEVERITY") != -1: rule.name = rule.name[:rule.name.find(".SEVERITY")] rule.name = rule.name.strip() rule.description = rule.name if (len(rule.name)>= name_length): rule.name = rule.name[:name_length-1] fd.close() return rule
def handle_klocwork_config_file(errors_node, rules): enabled_rules = [] for error in errors_node.getElementsByTagName("error"): for rule in rules: if error.getAttribute("enabled") == "true" and error.getAttribute("id") == rule.key: new_rule = Rule(rule.key, rule.name, rule.configKey, rule.description, rule.priority) if error.getAttribute("severity") != None and len(error.getAttribute("severity").strip()) != 0: print error.getAttribute("id") new_rule.priority = KlocworkExtractor.severities[error.getAttribute("severity")] enabled_rules.append(new_rule) return enabled_rules
def add_rule(self, rules, line): pattern = re.compile('^\\s*(?:reportErr|reportError)\\(\\s*[\\w-]+?\\s*,\\s*(.+?)\\s*,\\s*\\\"(.+?)\\\"\\s*,\\s*.+?\\s*\\).*$') search = pattern.search(line) if search != None: values = search.groups() rule = Rule() rule.key = values[1] if rule.key != 'uninitVar': #rules present twice rule.configKey = values[1] rule.category = 'Reliability' rule.description = values[1] rule.name = values[1] splittedSeverity = values[0].split('::') rule.priority = 'MAJOR' if len(splittedSeverity) >= 2: severity = splittedSeverity[1] if ' ' in severity: cleaned_severity = severity.split() severity = cleaned_severity[0] rule.priority = self.severities[severity.strip()] rules.append(rule)