forked from cfengine/cfengine-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocs.py
More file actions
394 lines (322 loc) · 11.9 KB
/
docs.py
File metadata and controls
394 lines (322 loc) · 11.9 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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
"""
Tooling to extract code snippets from docs and then run
commands on them (syntax checking, formatting, etc.)
This was moved from cfengine/documentation repo.
TODO: This code needs several adjustments to better fit into
the CFEngine CLI.
"""
import os
import json
import subprocess
import markdown_it
from cfbs.pretty import pretty_file
from cfengine_cli.lint import lint_policy_file
from cfengine_cli.utils import UserError
IGNORED_DIRS = [".git"]
def count_indent(string: str) -> int:
stripped = string.lstrip(" ")
return len(string) - len(stripped)
def extract_inline_code(path, languages):
"""extract inline code, language and filters from markdown"""
with open(path, "r") as f:
content = f.read()
lines = content.split("\n")
md = markdown_it.MarkdownIt("commonmark")
ast = md.parse(content)
for child in ast:
if child.type != "fence":
continue
if not child.info:
continue
info_string = child.info.split()
language = info_string[0]
flags = info_string[1:]
if flags and flags[0][0] == "{" and flags[-1][-1] == "}":
flags[0] = flags[0][1:]
flags[-1] = flags[-1][0:-1]
if language in languages:
assert child.map is not None
# Index of first line to include, the triple backtick fence:
first_line = child.map[0]
# The first line is triple backticks preceded by some spaces, count those:
indent = count_indent(lines[first_line])
# Index of first line to NOT include, the line after closing triple backtick:
last_line = child.map[1]
yield {
"language": language,
"flags": flags,
"first_line": child.map[0],
"last_line": child.map[1],
"indent": indent,
"lines": lines[
first_line:last_line
], # Includes backtick fences on both sides
}
def get_markdown_files(start, languages):
"""locate all markdown files and call extract_inline_code on them"""
if os.path.isfile(start):
return {
"files": {
start: {"code-blocks": list(extract_inline_code(start, languages))}
}
}
return_dict = {"files": {}}
for root, dirs, files in os.walk(start):
dirs[:] = [d for d in dirs if d not in IGNORED_DIRS]
for f in files:
if f.endswith(".markdown") or f.endswith(".md"):
path = os.path.join(root, f)
return_dict["files"][path] = {
"code-blocks": list(extract_inline_code(path, languages))
}
return return_dict
def fn_extract(origin_path, snippet_path, _language, first_line, last_line):
try:
with open(origin_path, "r") as f:
content = f.read()
code_snippet = "\n".join(content.split("\n")[first_line + 1 : last_line - 1])
with open(snippet_path, "w") as f:
f.write(code_snippet)
except IOError:
raise UserError(f"Couldn't open '{origin_path}' or '{snippet_path}'")
def lint_json_file(
snippet_abs_path, origin_path, snippet_number, first_line, prefix=None
):
with open(snippet_abs_path, "r") as f:
json.loads(f.read())
if prefix:
print(prefix, end="")
print(f"PASS: Snippet {snippet_number} at '{origin_path}:{first_line}' (json)")
return 0
def fn_check_syntax(
origin_path,
snippet_path,
language,
first_line,
_last_line,
snippet_number,
prefix=None,
):
snippet_abs_path = os.path.abspath(snippet_path)
if not os.path.exists(snippet_path):
raise UserError(
f"Couldn't find the file '{snippet_path}'. Run --extract to extract the inline code."
)
match language:
case "cf":
r = lint_policy_file(
snippet_abs_path, origin_path, first_line + 1, snippet_number, prefix
)
if r != 0:
raise UserError(f"Error when checking '{origin_path}'")
case "json":
try:
lint_json_file(
snippet_abs_path, origin_path, snippet_number, first_line, prefix
)
except json.decoder.JSONDecodeError as e:
raise UserError(f"Error when checking '{snippet_abs_path}': {str(e)}")
except Exception as e:
print(str(e))
raise UserError(f"Unknown error when checking '{snippet_abs_path}'")
def fn_check_output():
pass
def fn_replace(origin_path, snippet_path, _language, first_line, last_line, indent):
try:
with open(snippet_path, "r") as f:
pretty_content = f.read().strip()
with open(origin_path, "r") as f:
origin_lines = f.read().split("\n")
pretty_lines = pretty_content.split("\n")
pretty_lines = [" " * indent + x for x in pretty_lines]
offset = len(pretty_lines) - len(
origin_lines[first_line + 1 : last_line - 1]
)
origin_lines[first_line + 1 : last_line - 1] = pretty_lines
with open(origin_path, "w") as f:
f.write("\n".join(origin_lines))
except FileNotFoundError:
raise UserError(
f"Couldn't find the file '{snippet_path}'. Run --extract to extract the inline code."
)
except IOError:
raise UserError(f"Couldn't open '{origin_path}' or '{snippet_path}'")
return offset # TODO: offset can be undefined here
def fn_autoformat(_origin_path, snippet_path, language, _first_line, _last_line):
match language:
case "json":
try:
pretty_file(snippet_path)
except FileNotFoundError:
raise UserError(
f"Couldn't find the file '{snippet_path}'. Run --extract to extract the inline code."
)
except PermissionError:
raise UserError(f"Not enough permissions to open '{snippet_path}'")
except IOError:
raise UserError(f"Couldn't open '{snippet_path}'")
except json.decoder.JSONDecodeError:
raise UserError(f"Invalid json in '{snippet_path}'")
def _translate_language(x):
if x == "cf3" or x == "cfengine3":
return "cf"
if x == "yaml":
return "yml"
return x
SUPPORTED_LANGUAGES = ["cf", "cfengine3", "cf3", "json", "yml", "yaml"]
def _process_markdown_code_blocks(
path, languages, extract, syntax_check, output_check, autoformat, replace, cleanup
):
if not os.path.exists(path):
raise UserError("This path doesn't exist")
languages = set(languages)
if "cf3" in languages or "cf" in languages or "cfengine3" in languages:
languages.add("cf3")
languages.add("cfengine3")
languages.add("cf")
if "yaml" in languages or "yml" in languages:
languages.add("yml")
languages.add("yaml")
for language in languages:
if language not in SUPPORTED_LANGUAGES:
raise UserError(
f"Unsupported language '{language}'. The supported languages are: {', '.join(SUPPORTED_LANGUAGES)}"
)
parsed_markdowns = get_markdown_files(path, languages)
origin_paths = sorted(parsed_markdowns["files"].keys())
origin_paths_len = len(origin_paths)
for origin_paths_i, origin_path in enumerate(origin_paths):
percentage = int(100 * (origin_paths_i + 1) / origin_paths_len)
prefix = f"[{origin_paths_i + 1}/{origin_paths_len} ({percentage}%)] "
offset = 0
for i, code_block in enumerate(
parsed_markdowns["files"][origin_path]["code-blocks"]
):
# adjust line numbers after replace
for cb in parsed_markdowns["files"][origin_path]["code-blocks"][i:]:
cb["first_line"] += offset
cb["last_line"] += offset
language = _translate_language(code_block["language"])
snippet_number = i + 1
snippet_path = f"{origin_path}.snippet-{snippet_number}.{language}"
flags = code_block["flags"]
if "noextract" in flags or "skip" in flags:
# code block was marked to be skipped
continue
if extract:
fn_extract(
origin_path,
snippet_path,
language,
code_block["first_line"],
code_block["last_line"],
)
if syntax_check and "novalidate" not in code_block["flags"]:
try:
fn_check_syntax(
origin_path,
snippet_path,
language,
code_block["first_line"],
code_block["last_line"],
snippet_number,
prefix,
)
except Exception as e:
if cleanup:
os.remove(snippet_path)
raise e
if output_check and "noexecute" not in code_block["flags"]:
fn_check_output()
if autoformat and "noautoformat" not in code_block["flags"]:
fn_autoformat(
origin_path,
snippet_path,
language,
code_block["first_line"],
code_block["last_line"],
)
if replace and "noreplace" not in code_block["flags"]:
offset = fn_replace(
origin_path,
snippet_path,
language,
code_block["first_line"],
code_block["last_line"],
code_block["indent"],
)
if cleanup:
os.remove(snippet_path)
def _run_black():
path = "."
assert os.path.isdir(path)
try:
subprocess.run(
["black", path],
capture_output=True,
text=True,
check=True,
cwd=path,
)
except:
raise UserError(
"Encountered an error running black\nInstall: pipx install black"
)
def _run_prettier():
path = "."
assert os.path.isdir(path)
try:
subprocess.run(
["prettier", "--write", "**.markdown", "**.md"],
capture_output=True,
text=True,
check=True,
cwd=path,
)
except:
raise UserError(
"Encountered an error running prettier\nInstall: npm install --global prettier"
)
def update_docs() -> int:
"""
Iterate through entire docs repo (.), autoformatting as much as possible:
- python code with black
- markdown files with prettier
- code blocks inside markdown files are formatted for the formats supported by prettier
- JSON code blocks are re-formatted by cfbs pretty (we plan to expand this to CFEngine code blocks)
Run by the command:
cfengine dev format-docs
"""
print("Formatting python files with black...")
_run_black()
print("Formatting markdown files with prettier...")
_run_prettier()
print("Formatting markdown code blocks according to our rules...")
_process_markdown_code_blocks(
path=".",
languages=["json"], # TODO: Add cfengine3 here
extract=True,
syntax_check=False,
output_check=False,
autoformat=True,
replace=True,
cleanup=False,
)
return 0
def check_docs() -> int:
"""
Run checks / tests on docs.
Currently only JSON syntax checking.
Run by the command:
cfengine dev lint-docs"""
_process_markdown_code_blocks(
path=".",
languages=["json", "cf3"],
extract=True,
syntax_check=True,
output_check=False,
autoformat=False,
replace=False,
cleanup=True,
)
return 0