添加yaml规则文件
(self, work_dir, rules)
| 37 | self.tool_name = self.__class__.__name__ |
| 38 | |
| 39 | def __add_rules(self, work_dir, rules): |
| 40 | """添加yaml规则文件 |
| 41 | """ |
| 42 | rules_path = os.path.join(self.tool_home, "rules") |
| 43 | relpos = len(rules_path) + 1 |
| 44 | endsuff = [".yaml", ".yml"] |
| 45 | filelist = [] |
| 46 | for dirpath, _, files in os.walk(rules_path): |
| 47 | for filename in files: |
| 48 | if filename.lower().endswith(tuple(endsuff)): |
| 49 | filelist.append(os.path.join(dirpath, filename)) |
| 50 | config_rules_path = os.path.join(work_dir, "config_rules") |
| 51 | if os.path.exists(config_rules_path): |
| 52 | shutil.rmtree(config_rules_path) |
| 53 | os.mkdir(config_rules_path) |
| 54 | for single_file in filelist: |
| 55 | rel_path = single_file[relpos:] |
| 56 | file_path = os.path.join(config_rules_path, rel_path) |
| 57 | with open(single_file,'r') as fp: |
| 58 | data = yaml.safe_load(fp) |
| 59 | if data: |
| 60 | if data.__contains__('rules'): |
| 61 | for rule_data in data['rules']: |
| 62 | if rule_data["name"] in rules: |
| 63 | if not os.path.exists(os.path.dirname(file_path)): |
| 64 | os.makedirs(os.path.dirname(file_path)) |
| 65 | shutil.copy(single_file, file_path) |
| 66 | break |
| 67 | return config_rules_path |
| 68 | |
| 69 | def __get_regexes_exp(self, regex_type, rule_params_dict): |
| 70 | """获取正则列表 |