-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcheck-cli-examples.py
64 lines (53 loc) · 2.04 KB
/
check-cli-examples.py
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
import ast
import pathlib
import re
import sys
CODE_ROOT = pathlib.Path(__file__).resolve().parent.parent
EXECUTION_MODULES_PATH = CODE_ROOT / "src" / "saltext" / "kubernetes" / "modules"
def check_cli_examples(files):
"""
Check that every function on every execution module provides a CLI example
"""
errors = 0
for file in files:
path = pathlib.Path(file).resolve()
try:
relpath = path.relative_to(EXECUTION_MODULES_PATH)
if str(relpath.parent) != ".":
# We don't want to check nested packages
continue
except ValueError:
# We're only interested in execution modules
continue
module = ast.parse(path.read_text(), filename=str(path))
for funcdef in [node for node in module.body if isinstance(node, ast.FunctionDef)]:
if funcdef.name.startswith("_"):
# We're not interested in internal functions
continue
docstring = ast.get_docstring(funcdef, clean=False)
if not docstring:
errors += 1
print(
"The function {!r} on '{}' does not have a docstring".format(
funcdef.name,
path.relative_to(CODE_ROOT),
),
file=sys.stderr,
)
continue
if _check_cli_example_present(docstring) is False:
errors += 1
print(
"The function {!r} on '{}' does not have a 'CLI Example:' in it's docstring".format(
funcdef.name,
path.relative_to(CODE_ROOT),
),
file=sys.stderr,
)
continue
sys.exit(errors)
CLI_EXAMPLE_PRESENT_RE = re.compile(r"CLI Example(?:s)?:")
def _check_cli_example_present(docstring):
return CLI_EXAMPLE_PRESENT_RE.search(docstring) is not None
if __name__ == "__main__":
check_cli_examples(sys.argv[1:])