-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathmeta_header_check.py
More file actions
63 lines (53 loc) · 2.15 KB
/
Copy pathmeta_header_check.py
File metadata and controls
63 lines (53 loc) · 2.15 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
57
58
59
60
61
62
63
import glob
import os
import sys
import re
def find_header_or_module(line: str) -> tuple[re.Match[str] | None, str]:
m = re.fullmatch(r'[\*-] (.*?)\[meta header\]', line, re.MULTILINE)
if m:
return m, "header"
m = re.fullmatch(r'[\*-] (.*?)\[meta category\]', line, re.MULTILINE)
if m:
return m, "category"
m = re.fullmatch(r'[\*-] (.*?)\[meta module\]', line, re.MULTILINE)
if m:
return m, "module"
return None, ""
def check_header(text: str, filename: str) -> bool:
found_error: bool = False
dirs = filename.split("/")
for line in text.split("\n"):
m, header = find_header_or_module(line)
if m:
meta_header = m.group(1)
if len(dirs) < 2:
found_error = True
print("[{}] invalid directory: {}".format(filename, meta_header))
elif len(dirs) == 2 and dirs[0] in ("reference", "module"):
own_filename = os.path.splitext(os.path.basename(filename))[0]
if meta_header != own_filename:
found_error = True
print("[{0}] invalid meta {1} \"{2}\". You should specify \"{3}\" as meta {1}".format(
filename, header, meta_header, own_filename))
elif len(dirs) > 2 and dirs[0] in ("reference", "module"):
own_header = dirs[1]
if meta_header != own_header:
found_error = True
print("[{0}] invalid meta {1} \"{2}\". You should specify \"{3}\" as meta {1}".format(
filename, header, meta_header, own_header))
return not found_error
if len(dirs) >= 2 and dirs[0] in ("reference", "module"):
found_error = True
print("[{0}] meta header is empty.".format(filename))
return not found_error
def check() -> bool:
found_error = False
for p in sorted(list(glob.glob("**/*.md", recursive=True))):
with open(p) as f:
text = f.read()
if not check_header(text, p):
found_error = True
return not found_error
if __name__ == '__main__':
if not check():
sys.exit(1)