把[{'name':str, 'params':str}...]格式的rule_list转换为 rule_name-value的格式,并把reg_exp编译好 :param rule_list: [{'name':str, 'params':str}...] :return: { rule_name: { 'reg_pattern': reg_pattern, 'exclude': exclude_paths, 'include': inclu
(self, rule_list)
| 113 | |
| 114 | class RegexFileScan(CodeLintModel): |
| 115 | def __format_rules(self, rule_list): |
| 116 | """把[{'name':str, 'params':str}...]格式的rule_list转换为 rule_name-value的格式,并把reg_exp编译好 |
| 117 | :param rule_list: [{'name':str, 'params':str}...] |
| 118 | :return: { |
| 119 | rule_name: { |
| 120 | 'reg_pattern': reg_pattern, |
| 121 | 'exclude': exclude_paths, |
| 122 | 'include': include_paths, |
| 123 | 'msg': msg, |
| 124 | 'ignore_comment': True|False |
| 125 | } |
| 126 | } |
| 127 | """ |
| 128 | rules = {} |
| 129 | for rule in rule_list: |
| 130 | rule_name = rule['name'] |
| 131 | if not rule.get('params'): |
| 132 | logger.error(f"{rule_name}规则参数为空,跳过该规则.") |
| 133 | continue |
| 134 | if "[regexcheck]" in rule['params']: |
| 135 | rule_params = rule['params'] |
| 136 | else: |
| 137 | rule_params = "[regexcheck]\r\n" + rule['params'] |
| 138 | rule_params_dict = ConfigReader(cfg_string=rule_params).read('regexcheck') |
| 139 | |
| 140 | reg_exp = rule_params_dict.get('regex', '') |
| 141 | if not reg_exp: |
| 142 | logger.error(f"{rule_name}规则参数有误,未填写正则表达式,跳过该规则.") |
| 143 | continue |
| 144 | reg_pattern = re.compile(reg_exp) |
| 145 | exclude_paths = rule_params_dict.get('exclude', '') |
| 146 | exclude_paths = [p.strip() for p in exclude_paths.split(';') if p.strip()] if exclude_paths else [] |
| 147 | include_paths = rule_params_dict.get('include', '') |
| 148 | include_paths = [p.strip() for p in include_paths.split(';') if p.strip()] if include_paths else [] |
| 149 | # 大小写不敏感,可以支持True|true|False|false等 |
| 150 | msg = rule_params_dict.get('msg', "发现不规范文件: %s") |
| 151 | rules[rule_name] = { |
| 152 | 'reg_pattern': reg_pattern, |
| 153 | 'exclude': exclude_paths, |
| 154 | 'include': include_paths, |
| 155 | 'msg': msg |
| 156 | } |
| 157 | return rules |
| 158 | |
| 159 | def analyze(self, params): |
| 160 | """执行分析任务 |