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