| 25 | |
| 26 | # 删除readme文件中不存在的相关脚本分组 |
| 27 | def delete_related_readme(directory_path, not_in_map): |
| 28 | for file in os.listdir(directory_path): |
| 29 | if file.endswith('.md') and os.path.isfile(os.path.join(directory_path, file)): |
| 30 | file_path = os.path.join(directory_path, file) |
| 31 | with open(file_path, 'r', encoding='utf-8') as f: |
| 32 | content = f.read() |
| 33 | is_modified = False |
| 34 | |
| 35 | # 删除对应标签和内容 |
| 36 | for key in not_in_map: |
| 37 | start_tag = f"<!--RELATED-{key}-->" |
| 38 | end_tag = f"<!--RELATED-{key}-END-->" |
| 39 | pattern = re.compile(re.escape(start_tag) + r'.*?' + re.escape(end_tag), re.DOTALL) |
| 40 | if re.search(pattern, content): |
| 41 | content = re.sub(pattern, '', content) |
| 42 | print(f" {file_path} 中的失效分组[\033[31m{key}\033[0m]已被删除。") |
| 43 | is_modified = True |
| 44 | if is_modified: |
| 45 | with open(file_path, 'w', encoding='utf-8', newline='\n') as f: |
| 46 | f.write(content) |
| 47 | |
| 48 | |
| 49 | def main(): |