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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
import os
import re
import ipaddress
import sys
import ast
IPV4SEG = r'(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])'
IPV4ADDR = r'\b(?:(?:' + IPV4SEG + r'\.){3,3}' + IPV4SEG + r')\b'
IPV6SEG = r'(?:(?:[0-9a-fA-F]){1,4})'
IPV6GROUPS = (
r'(?:' + IPV6SEG + r':){7,7}' + IPV6SEG, # 1:2:3:4:5:6:7:8
r'(?:\s' + IPV6SEG + r':){1,7}:', # 1:: 1:2:3:4:5:6:7::
r'(?:' + IPV6SEG + r':){1,6}:' + IPV6SEG, # 1::8 1:2:3:4:5:6::8 1:2:3:4:5:6::8
r'(?:' + IPV6SEG + r':){1,5}(?::' + IPV6SEG + r'){1,2}', # 1::7:8 1:2:3:4:5::7:8 1:2:3:4:5::8
r'(?:' + IPV6SEG + r':){1,4}(?::' + IPV6SEG + r'){1,3}', # 1::6:7:8 1:2:3:4::6:7:8 1:2:3:4::8
r'(?:' + IPV6SEG + r':){1,3}(?::' + IPV6SEG + r'){1,4}', # 1::5:6:7:8 1:2:3::5:6:7:8 1:2:3::8
r'(?:' + IPV6SEG + r':){1,2}(?::' + IPV6SEG + r'){1,5}', # 1::4:5:6:7:8 1:2::4:5:6:7:8 1:2::8
IPV6SEG + r':(?:(?::' + IPV6SEG + r'){1,6})', # 1::3:4:5:6:7:8 1::3:4:5:6:7:8 1::8
r':(?:(?::' + IPV6SEG + r'){1,7}|:)', # ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 ::
r'fe80:(?::' + IPV6SEG + r'){0,4}%[0-9a-zA-Z]{1,}', # fe80::7:8%eth0 fe80::7:8%1 (link-local IPv6 addresses with zone index)
r'::(?:ffff(?::0{1,4}){0,1}:){0,1}[^\s:]' + IPV4ADDR, # ::255.255.255.255 ::ffff:255.255.255.255 ::ffff:0:255.255.255.255 (IPv4-mapped IPv6 addresses and IPv4-translated addresses)
r'(?:' + IPV6SEG + r':){1,4}:[^\s:]' + IPV4ADDR, # 2001:db8:3:4::192.0.2.33 64:ff9b::192.0.2.33 (IPv4-Embedded IPv6 Address)
)
IPV6ADDR = '|'.join(['(?:{})'.format(g) for g in IPV6GROUPS[::-1]]) # Reverse rows for greedy match
MAC = r'([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
NUMBER = r"([\s']\d+[\s'])"
SUPPORTED_EXTS = ('.md', '.rst', '.txt')
# MyST / Markdown fenced code block: leading whitespace + 3+ backticks or 3+ colons.
# Same character and length-or-greater closes.
MD_FENCE_RE = re.compile(r'^(\s*)(`{3,}|:{3,})(.*)$')
SUPPRESSION_MARKER_RE = {
'rst': {
'stop': re.compile(r'^\s*\.\.\s+stop_vyoslinter\s*$'),
'start': re.compile(r'^\s*\.\.\s+start_vyoslinter\s*$'),
},
'md': {
'stop': re.compile(r'^\s*%\s+stop_vyoslinter\s*$'),
'start': re.compile(r'^\s*%\s+start_vyoslinter\s*$'),
},
}
def is_suppression_marker(line, kind, in_md_fence, in_rst_codeblock,
md_fence_is_eval_rst, file_ext):
"""Detect valid stop/start markers in the parser context where they apply.
`% ...` is only valid in MyST Markdown (`.md`) at top level (outside any fence).
`.. ...` is valid in `.rst`/`.txt` at top level (outside RST code-block) and
in `.md` only when inside an `{eval-rst}` fence.
"""
if SUPPRESSION_MARKER_RE['md'][kind].match(line):
return file_ext == '.md' and not in_md_fence
if not SUPPRESSION_MARKER_RE['rst'][kind].match(line):
return False
if in_rst_codeblock:
return False
if file_ext in ('.rst', '.txt'):
return True
if in_md_fence:
return md_fence_is_eval_rst
return False
def lint_mac(cnt, line):
mac = re.search(MAC, line, re.I)
if mac is not None:
mac = mac.group()
u_mac = re.search(r'((00)[:-](53)([:-][0-9A-F]{2}){4})', mac, re.I)
m_mac = re.search(r'((90)[:-](10)([:-][0-9A-F]{2}){4})', mac, re.I)
if u_mac is None and m_mac is None:
return (f"Use MAC reserved for Documentation (RFC7042): {mac}", cnt, 'error')
def lint_ipv4(cnt, line):
ip = re.search(IPV4ADDR, line, re.I)
if ip is not None:
ip = ipaddress.ip_address(ip.group().strip(' '))
# https://docs.python.org/3/library/ipaddress.html#ipaddress.IPv4Address.is_private
if ip.is_private:
return None
if ip.is_multicast:
return None
if ip.is_global is False:
return None
return (f"Use IPv4 reserved for Documentation (RFC 5737) or private Space: {ip}", cnt, 'error')
def lint_ipv6(cnt, line):
ip = re.search(IPV6ADDR, line, re.I)
if ip is not None:
ip = ipaddress.ip_address(ip.group().strip(' '))
if ip.is_private:
return None
if ip.is_multicast:
return None
if ip.is_global is False:
return None
return (f"Use IPv6 reserved for Documentation (RFC 3849) or private Space: {ip}", cnt, 'error')
def lint_AS(cnt, line):
number = re.search(NUMBER, line, re.I)
if number:
pass
# find a way to detect AS numbers
def lint_linelen(cnt, line):
line = line.rstrip()
if len(line) > 80:
return (f"Line too long: len={len(line)}", cnt, 'warning')
def handle_file_action(filepath):
errors = []
file_ext = os.path.splitext(filepath)[1].lower()
# Stack of open MD/MyST fences: (char, min_len). Supports nesting like
# `:::{note}` containing `::::{code-block}`, where the inner opener does
# not close the outer note (CommonMark closing rule: matching closer must
# have no info string after the fence chars).
md_fence_stack = []
md_fence_is_eval_rst = False
in_rst_codeblock = False
rst_codeblock_indent = 0
start_vyoslinter = True
cnt = 0
with open(filepath) as fp:
for cnt, line in enumerate(fp, start=1):
# MD/MyST fenced code block tracking (``` or :::).
fence_match = MD_FENCE_RE.match(line)
if fence_match:
fence = fence_match.group(2)
fence_char = fence[0]
fence_len = len(fence)
info = fence_match.group(3).strip()
# A closer matches the top of the stack (same char, len >=
# opener) and has no info string. Anything else opens a new
# (possibly nested) fence.
is_closer = (
md_fence_stack
and not info
and fence_char == md_fence_stack[-1][0]
and fence_len >= md_fence_stack[-1][1]
)
if is_closer:
md_fence_stack.pop()
if not md_fence_stack:
md_fence_is_eval_rst = False
else:
if not md_fence_stack:
md_fence_is_eval_rst = info.startswith('{eval-rst}')
md_fence_stack.append((fence_char, fence_len))
in_md_fence = bool(md_fence_stack)
# RST `.. code-block::` tracking (existing semantics for .rst/.txt).
# Each `.. code-block::` directive resets the tracked indent so a
# later dedent past that column exits the block — even when the
# directive itself appears inside an already-open outer block.
if in_rst_codeblock:
if len(line) > rst_codeblock_indent and not line[rst_codeblock_indent].isspace():
in_rst_codeblock = False
if ".. code-block::" in line:
in_rst_codeblock = True
rst_codeblock_indent = 0
for ch in line:
if ch.isspace():
rst_codeblock_indent += 1
else:
break
if is_suppression_marker(
line, 'stop', in_md_fence, in_rst_codeblock,
md_fence_is_eval_rst, file_ext,
):
start_vyoslinter = False
if is_suppression_marker(
line, 'start', in_md_fence, in_rst_codeblock,
md_fence_is_eval_rst, file_ext,
):
start_vyoslinter = True
if not start_vyoslinter:
continue
test_line_length = not (in_md_fence or in_rst_codeblock)
err_mac = lint_mac(cnt, line.strip())
# disable mac detection for the moment, too many false positives
err_mac = None
err_ip4 = lint_ipv4(cnt, line.strip())
err_ip6 = lint_ipv6(cnt, line.strip())
err_len = lint_linelen(cnt, line) if test_line_length else None
for e in (err_mac, err_ip4, err_ip6, err_len):
if e:
errors.append(e)
if not start_vyoslinter:
errors.append(("Don't forget to turn linter back on", cnt, 'error'))
if len(errors) > 0:
'''
"::{$type} file={$filename},line={$line},col=$column::{$log}"
'''
print(f"File: {filepath}")
for error in errors:
print(f"::{error[2]} file={filepath},line={error[1]}::{error[0]}")
print('')
return False
def main():
bool_error = True
print('start')
try:
files = ast.literal_eval(sys.argv[1])
for file in files:
if file.endswith(SUPPORTED_EXTS) and "_build" not in file:
if handle_file_action(file) is False:
bool_error = False
except Exception as e:
for root, dirs, files in os.walk("docs"):
path = root.split(os.sep)
for file in files:
if file.endswith(SUPPORTED_EXTS) and "_build" not in path:
fpath = '/'.join(path)
filepath = f"{fpath}/{file}"
if handle_file_action(filepath) is False:
bool_error = False
return bool_error
if __name__ == "__main__":
if main() == False:
exit(1)
|