|
| 1 | +# Created with python 3.13.2 |
| 2 | + |
| 3 | +# This script removes duplicate include directives from the codebase of Generals and GeneralsMD. |
| 4 | + |
| 5 | +import os |
| 6 | +import re |
| 7 | + |
| 8 | +current_dir = os.path.dirname(os.path.abspath(__file__)) |
| 9 | +root_dir = os.path.join(current_dir, "..", "..") |
| 10 | +root_dir = os.path.normpath(root_dir) |
| 11 | +core_dir = os.path.join(root_dir, "Core") |
| 12 | +generals_dir = os.path.join(root_dir, "Generals", "Code") |
| 13 | +generalsmd_dir = os.path.join(root_dir, "GeneralsMD", "Code") |
| 14 | + |
| 15 | +def remove_duplicate_includes_from_file(filepath): |
| 16 | + include_pattern = re.compile(r'^\s*#\s*include\s+[<"].+[>"].*$', re.MULTILINE) |
| 17 | + seen = set() |
| 18 | + output_lines = [] |
| 19 | + |
| 20 | + with open(filepath, 'r', encoding='utf-8') as f: |
| 21 | + for line in f: |
| 22 | + match = include_pattern.match(line) |
| 23 | + if match: |
| 24 | + normalized = line.strip() |
| 25 | + if normalized in seen: |
| 26 | + continue # Skip duplicate |
| 27 | + seen.add(normalized) |
| 28 | + output_lines.append(line) |
| 29 | + |
| 30 | + with open(filepath, 'w', encoding='utf-8') as f: |
| 31 | + f.writelines(output_lines) |
| 32 | + |
| 33 | +def process_directory(root_dir): |
| 34 | + for subdir, _, files in os.walk(root_dir): |
| 35 | + for file in files: |
| 36 | + if file.endswith(('.cpp', '.h', '.hpp', '.c')): |
| 37 | + filepath = os.path.join(subdir, file) |
| 38 | + remove_duplicate_includes_from_file(filepath) |
| 39 | + |
| 40 | +process_directory(generals_dir) |
| 41 | +process_directory(generalsmd_dir) |
0 commit comments