forked from proppy/rad-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_documentation.py
103 lines (85 loc) · 2.73 KB
/
check_documentation.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
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
#!/usr/bin/env python3
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import enum
import pathlib
import sys
import click
import tfdoc
BASEDIR = pathlib.Path(__file__).resolve().parents[1]
class DocState(enum.Enum):
OK = 1
FAIL = 2
UNKNOWN = 3
def __str__(self):
return {
self.FAIL.value: '✗',
self.OK.value: '✓',
self.UNKNOWN.value: '?'
}[self.value]
def check_path(pathname):
path = BASEDIR / pathname
subpaths = sorted(list(path.iterdir()))
for subpath in subpaths:
errors = []
if not subpath.is_dir():
continue
if subpath.stem.startswith('_'):
continue
doc = subpath / 'README.md'
if not doc.exists():
errors.append(f'{doc} does not exist')
variables = tfdoc.get_variables(subpath)
variable_names = [v.name for v in variables]
for variable in variables:
if not variable.description:
errors.append(f'variable {variable.name} has no description')
if sorted(variable_names) != variable_names:
message = f'variable order should be: {sorted(variable_names)}'
errors.append(message)
outputs = tfdoc.get_outputs(subpath)
output_names = [v.name for v in outputs]
for output in outputs:
if not output.description:
errors.append(f'output {output.name} has no description')
if sorted(output_names) != output_names:
message = f'output order should be: {sorted(output_names)}'
errors.append(message)
state = tfdoc.check_state(subpath)
if state is False:
errors.append("documentation is out of date")
elif state:
pass
else:
yield DocState.UNKNOWN, subpath.stem, errors
continue
yield DocState.FAIL if errors else DocState.OK, subpath.stem, errors
@click.command()
@click.argument('paths', type=str, nargs=-1)
def main(paths):
"Cycle through modules and ensure READMEs are up-to-date."
error = False
for path in paths:
print(f'checking {path}')
for state, name, errors in check_path(path):
if state == DocState.FAIL:
error = True
print(f' [{state}] {name}')
for error in errors:
print(f' {error}')
if error:
print('errors were present')
sys.exit(1)
if __name__ == '__main__':
main()