Skip to content

Commit ffd1a82

Browse files
committed
scripts: ignore github directory
This commit adds .github directory to exclusion to not check it in CI job. Signed-off-by: Alexander Kuleshov <kuleshovmail@gmail.com>
1 parent aae7e56 commit ffd1a82

1 file changed

Lines changed: 51 additions & 40 deletions

File tree

scripts/check-code-snippets.py

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,69 @@
1+
"""
2+
A script that takes the lines of the Linux kernel source code from the comments
3+
in the markdown files that are attached to the code and checks their validity.
4+
"""
15
import os
26
import re
37
import sys
48
from typing import Optional, Tuple
59

610
import requests
711

8-
def split_url_and_range(url: str) -> Tuple[str, Optional[int], Optional[int]]:
12+
exclude_dirs = ["./.github"]
13+
14+
def __split_url_and_range__(url: str) -> Tuple[str, Optional[int], Optional[int]]:
915
base, frag = url.split("#", 1)
1016
m = re.match(r'L(\d+)(?:-L?(\d+))?$', frag)
1117
start = int(m.group(1))
1218
end = int(m.group(2)) if m.group(2) else None
1319
return base, start, end
1420

15-
def fetch_raw(source: str) -> str:
16-
r = requests.get(source)
21+
def __fetch_raw__(source: str) -> str:
22+
r = requests.get(source, timeout=5.0)
1723
return r.text
1824

19-
def main():
25+
def __handle_md__(md: str):
2026
in_code = False
2127
code = ''
2228
content = ''
23-
start_line = 0
24-
end_line = 0
29+
30+
md_lines = md.splitlines()
31+
32+
for line in md_lines:
33+
if in_code:
34+
if re.search("^```[a-zA-Z].*", line):
35+
continue
36+
37+
if re.search("^```$", line):
38+
in_code = False
39+
continue
40+
41+
code += line + '\n'
42+
continue
43+
44+
if line.startswith("<!--"):
45+
in_code = True
46+
(uri, start, end) = __split_url_and_range__(line.split(' ')[1])
47+
content = "\n".join(__fetch_raw__(uri).splitlines()[start-1:end]).rstrip()
48+
continue
49+
50+
if code != '':
51+
if code.rstrip() != content:
52+
print("Error in", sys.argv[1])
53+
print("Code in book:")
54+
print(code)
55+
print("Code from github:")
56+
print(content)
57+
sys.exit(1)
58+
59+
code = ''
60+
content = ''
61+
continue
62+
63+
def __main__():
2564
md_files = []
26-
27-
for root, dirs, files in os.walk(sys.argv[1]):
65+
66+
for root, _dirs, files in os.walk(sys.argv[1]):
2867
for name in files:
2968
if name.endswith('.md'):
3069
md_files.append(os.path.join(root, name))
@@ -33,41 +72,13 @@ def main():
3372

3473
for md in md_files:
3574
print("Checking code in the", md)
75+
if os.path.dirname(md) in exclude_dirs:
76+
continue
3677

3778
with open(md, "r", encoding="utf-8") as f:
3879
md = f.read()
3980

40-
md_lines = md.splitlines()
41-
for line in md_lines:
42-
if in_code:
43-
if re.search("^```[a-zA-Z].*", line):
44-
continue
45-
46-
if re.search("^```$", line):
47-
in_code = False
48-
continue
49-
50-
code += line + '\n'
51-
continue
52-
53-
if line.startswith("<!--"):
54-
in_code = True
55-
(uri, start, end) = split_url_and_range(line.split(' ')[1])
56-
content = "\n".join(fetch_raw(uri).splitlines()[start-1:end]).rstrip()
57-
continue
58-
59-
if code != '':
60-
if code.rstrip() != content:
61-
print("Error in", sys.argv[1])
62-
print("Code in book:")
63-
print(code)
64-
print("Code from github:")
65-
print(content)
66-
exit(1)
67-
68-
code = ''
69-
content = ''
70-
continue
81+
__handle_md__(md)
7182

7283
if __name__ == "__main__":
73-
main()
84+
__main__()

0 commit comments

Comments
 (0)