Skip to content

Commit ed32850

Browse files
authored
Merge pull request #869 from 0xAX/fix-ci
scripts: ignore github directory
2 parents aae7e56 + 8b874e2 commit ed32850

3 files changed

Lines changed: 85 additions & 74 deletions

File tree

.github/workflows/check-code-snippets.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ jobs:
2929
pip install requests
3030
- name: Validate code snippets
3131
run: |
32-
python ./scripts/check-code-snippets.py .
32+
python ./scripts/check_code_snippets.py .

scripts/check-code-snippets.py

Lines changed: 0 additions & 73 deletions
This file was deleted.

scripts/check_code_snippets.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
"""
5+
import os
6+
import re
7+
import sys
8+
from typing import Optional, Tuple
9+
10+
import requests
11+
12+
exclude_dirs = ["./.github"]
13+
14+
def __split_url_and_range__(url: str) -> Tuple[str, Optional[int], Optional[int]]:
15+
base, frag = url.split("#", 1)
16+
m = re.match(r'L(\d+)(?:-L?(\d+))?$', frag)
17+
start = int(m.group(1))
18+
end = int(m.group(2)) if m.group(2) else None
19+
return base, start, end
20+
21+
def __fetch_raw__(source: str) -> str:
22+
r = requests.get(source, timeout=5.0)
23+
return r.text
24+
25+
def __handle_md__(md: str):
26+
in_code = False
27+
code = ''
28+
content = ''
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__():
64+
md_files = []
65+
66+
for root, _dirs, files in os.walk(sys.argv[1]):
67+
for name in files:
68+
if name.endswith('.md'):
69+
md_files.append(os.path.join(root, name))
70+
else:
71+
continue
72+
73+
for md in md_files:
74+
print("Checking code in the", md)
75+
if os.path.dirname(md) in exclude_dirs:
76+
continue
77+
78+
with open(md, "r", encoding="utf-8") as f:
79+
md = f.read()
80+
81+
__handle_md__(md)
82+
83+
if __name__ == "__main__":
84+
__main__()

0 commit comments

Comments
 (0)