扫描单个文件,根据注释忽略issue :param source_dir: :param fileissue: :return:
(self, source_dir, fileissue)
| 69 | self._rule_pattern = re.compile(each_rule_pattern) |
| 70 | |
| 71 | def scan_file(self, source_dir, fileissue): |
| 72 | """ |
| 73 | 扫描单个文件,根据注释忽略issue |
| 74 | :param source_dir: |
| 75 | :param fileissue: |
| 76 | :return: |
| 77 | """ |
| 78 | issues = fileissue.get("issues") |
| 79 | if not issues: |
| 80 | return |
| 81 | |
| 82 | path = fileissue.get("path") |
| 83 | full_path = os.path.join(source_dir, path) |
| 84 | with open(full_path, "rb") as rf: |
| 85 | # 读取文件内容并解码为字符串 |
| 86 | file_text = CodecClient().decode(rf.read()) |
| 87 | file_lines = file_text.splitlines() |
| 88 | file_len = len(file_lines) |
| 89 | |
| 90 | if not file_len: # 空文件,跳过 |
| 91 | return |
| 92 | |
| 93 | # 收集需要解析的代码行 |
| 94 | check_lines = set() |
| 95 | # 收集文件类型的规则名 |
| 96 | file_type_rules = set() |
| 97 | for issue in issues: |
| 98 | line_no = int(issue["line"]) |
| 99 | if line_no == 0: |
| 100 | file_type_rules.add(issue["rule"]) |
| 101 | else: |
| 102 | # 将当前行加入到检查范围 |
| 103 | check_lines.add(line_no) |
| 104 | # 如果不是首行,将上一行也加入到检查范围 |
| 105 | if line_no > 1: |
| 106 | check_lines.add(line_no - 1) |
| 107 | # 只要存在一个文件类型的规则issue,所有行都需要解析注释 |
| 108 | if file_type_rules: |
| 109 | check_lines = set(range(1, file_len + 1)) |
| 110 | |
| 111 | # LogPrinter.info(f">> {path}, check_lines: {check_lines}") |
| 112 | |
| 113 | ignore_rules_dict = {} |
| 114 | for line_no in check_lines: |
| 115 | line_ignore_rules = self.__get_line_ignore_rules( |
| 116 | full_path, file_lines[line_no - 1], line_no, file_type_rules |
| 117 | ) |
| 118 | ignore_rules_dict.update(line_ignore_rules) |
| 119 | |
| 120 | for issue in issues: |
| 121 | key = self.__get_key_name(issue["rule"], issue["line"], file_type_rules) |
| 122 | ignore_reason = ignore_rules_dict.get(key) |
| 123 | if ignore_reason: |
| 124 | issue["resolution"] = COMMENTIGNORE |
| 125 | issue["ignore_reason"] = ignore_reason |
| 126 | |
| 127 | def __get_line_ignore_rules(self, file_path, line_text, line_no, file_type_rules): |
| 128 | """ |
no test coverage detected