对单个代码文件进行正则匹配 :param source_dir: 项目代码根目录 :param file_path: 单个代码文件路径 :param rules: { rule_name: { 'reg_pattern': reg_pattern, 'exclude': exclude_paths, 'include': include_paths, 'msg': msg
(source_dir, file_path, rules)
| 65 | |
| 66 | @staticmethod |
| 67 | def scan_file(source_dir, file_path, rules): |
| 68 | """对单个代码文件进行正则匹配 |
| 69 | :param source_dir: 项目代码根目录 |
| 70 | :param file_path: 单个代码文件路径 |
| 71 | :param rules: { |
| 72 | rule_name: { |
| 73 | 'reg_pattern': reg_pattern, |
| 74 | 'exclude': exclude_paths, |
| 75 | 'include': include_paths, |
| 76 | 'msg': msg |
| 77 | } |
| 78 | } |
| 79 | :return: [{ |
| 80 | "path":文件相对路径, |
| 81 | "line":行号, |
| 82 | "column":列号, |
| 83 | "msg":提示信息, |
| 84 | "rule":规则名 |
| 85 | }, |
| 86 | ... |
| 87 | ] |
| 88 | """ |
| 89 | source_dir = source_dir.replace(os.sep, '/').rstrip('/') |
| 90 | file_path = file_path.replace(os.sep, '/') |
| 91 | relpos = len(source_dir) + 1 |
| 92 | relative_path = file_path[relpos:] |
| 93 | rules_check_path = {} |
| 94 | |
| 95 | for rule_name, params in rules.items(): |
| 96 | # 根据include和exclude判断path是否需要过滤 |
| 97 | filter_util = WildcardPathFilter(path_include=params['include'], path_exclude=params['exclude']) |
| 98 | if filter_util.should_filter_path(relative_path): |
| 99 | continue |
| 100 | rules_check_path[rule_name] = params |
| 101 | |
| 102 | if not rules_check_path: |
| 103 | return [] |
| 104 | |
| 105 | results = [] |
| 106 | # 对文件路径进行正则匹配 |
| 107 | if rules_check_path: |
| 108 | issues = RegexScanner.scan_file_path(relative_path, rules_check_path) |
| 109 | results.extend(issues) |
| 110 | |
| 111 | return results |
| 112 | |
| 113 | |
| 114 | class RegexFileScan(CodeLintModel): |