|
| 1 | +import glob |
| 2 | +import os.path |
| 3 | +import re |
| 4 | + |
| 5 | + |
| 6 | +def main(): |
| 7 | + cpp_files = glob.glob("*.cpp") |
| 8 | + check_presence_in_cmakelists(cpp_files) |
| 9 | + check_contents_in_readme(cpp_files) |
| 10 | + |
| 11 | + |
| 12 | +def check_presence_in_cmakelists(cpp_files): |
| 13 | + with open("CMakeLists.txt", "r") as f: |
| 14 | + cmake_content = f.read() |
| 15 | + for cpp_file in cpp_files: |
| 16 | + cpp_target = os.path.splitext(cpp_file)[0] |
| 17 | + if cpp_target not in cmake_content: |
| 18 | + print(f"Warning: {cpp_target} is not listed in CMakeLists.txt") |
| 19 | + |
| 20 | + |
| 21 | +def check_contents_in_readme(cpp_files): |
| 22 | + with open("readme.md", "r") as f: |
| 23 | + readme_content = f.read() |
| 24 | + sources = extract_code_blocks(readme_content) |
| 25 | + sources_names = sorted([fname for fname in sources.keys()]) |
| 26 | + |
| 27 | + check_file_lists(cpp_files, sources_names) |
| 28 | + |
| 29 | + check_file_contents(cpp_files, sources) |
| 30 | + |
| 31 | + |
| 32 | +def extract_code_blocks(readme_content): |
| 33 | + # Match: "(`filename.cpp``):\n```c++(...)```" |
| 34 | + pattern = r"\(`([a-zA-Z0-9_]+\.cpp)`\):\s*```c\+\+\s*([\s\S]*?)```" |
| 35 | + return dict(re.findall(pattern, readme_content)) |
| 36 | + |
| 37 | + |
| 38 | +def check_file_lists(cpp_files, readme_files): |
| 39 | + sym_diff = list(set(cpp_files) ^ set(readme_files)) |
| 40 | + if sym_diff: |
| 41 | + print("The following files are mismatched between readme.md and the directory:") |
| 42 | + for fname in sym_diff: |
| 43 | + print(f" {fname}") |
| 44 | + |
| 45 | + |
| 46 | +def check_file_contents(cpp_files, sources): |
| 47 | + for cpp_file in cpp_files: |
| 48 | + with open(cpp_file, "r") as f: |
| 49 | + code_in_file = f.read() |
| 50 | + code_in_readme = sources.get(cpp_file, "") |
| 51 | + if code_in_file != code_in_readme: |
| 52 | + print(f"Warning: Content mismatch for {cpp_file} between file and readme.md") |
| 53 | + |
| 54 | + |
| 55 | +if __name__ == '__main__': |
| 56 | + main() |
0 commit comments