结果处理类
| 31 | |
| 32 | |
| 33 | class ResultMgr(object): |
| 34 | """ |
| 35 | 结果处理类 |
| 36 | """ |
| 37 | |
| 38 | def parse_result(self, regex_client, cmd_output): |
| 39 | """解析cpplint执行输出信息""" |
| 40 | errors = [] |
| 41 | for line in cmd_output: |
| 42 | reg_result = regex_client.findall(line) |
| 43 | if reg_result: |
| 44 | item = {} |
| 45 | file_path, line_num, item['error_msg'], item['error_type'], item['error_level'] = reg_result[0] |
| 46 | # 因为多线程执行的原因,读取到的stdout输出,路径前可能包含很多空字符,需要过滤掉 |
| 47 | if "\u0000" in file_path: |
| 48 | logger.warning("fix file_path(%s)." % file_path) |
| 49 | file_path = file_path.replace("\u0000", "") |
| 50 | item["file_path"] = file_path |
| 51 | line_num = int(line_num) |
| 52 | item["line_num"] = line_num if line_num > 0 else 1 |
| 53 | errors.append(item) |
| 54 | return errors |
| 55 | |
| 56 | def __format_message(self, source_dir, msg): |
| 57 | """ |
| 58 | 格式化issue的msg信息,如果msg中带有代码目录绝对路径,删除,只保留相对路径部分 |
| 59 | :param msg: |
| 60 | :return: |
| 61 | """ |
| 62 | if "/" in source_dir: |
| 63 | src_dir_prefix = source_dir + "/" |
| 64 | else: |
| 65 | src_dir_prefix = source_dir + "\\" |
| 66 | if src_dir_prefix in msg: |
| 67 | msg = msg.replace(src_dir_prefix, "") |
| 68 | return msg |
| 69 | |
| 70 | def format_result(self, source_dir, scan_result, rules): |
| 71 | """格式化工具执行结果""" |
| 72 | issues = [] |
| 73 | relpos = len(source_dir) + 1 |
| 74 | # scan_result 字典数组去重 |
| 75 | f = lambda x, y: x if y in x else x + [y] |
| 76 | scan_result = reduce( |
| 77 | f, |
| 78 | [ |
| 79 | [], |
| 80 | ] |
| 81 | + scan_result, |
| 82 | ) |
| 83 | # 格式化结果 |
| 84 | for error in scan_result: |
| 85 | rule_name = error["error_type"] |
| 86 | # 结果可能包含不需要扫描的规则,过滤掉 |
| 87 | if rules and rule_name not in rules: |
| 88 | logger.debug("ignore rule:%s" % rule_name) |
| 89 | continue |
| 90 | issue = {} |