2020-11-07 00:12:41 -08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2022-01-01 06:52:31 -08:00
|
|
|
# Copyright 2022 Google LLC
|
2020-11-07 00:12:41 -08:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2022-01-17 01:23:01 -08:00
|
|
|
import difflib
|
2020-11-07 00:12:41 -08:00
|
|
|
import enum
|
|
|
|
import pathlib
|
|
|
|
|
|
|
|
import click
|
|
|
|
import tfdoc
|
|
|
|
|
|
|
|
|
2021-12-20 23:51:51 -08:00
|
|
|
BASEDIR = pathlib.Path(__file__).resolve().parents[1]
|
2020-11-07 00:12:41 -08:00
|
|
|
|
2021-10-08 09:26:04 -07:00
|
|
|
|
2021-12-20 23:51:51 -08:00
|
|
|
State = enum.Enum('State', 'OK FAIL SKIP')
|
2021-10-08 09:26:04 -07:00
|
|
|
|
|
|
|
|
2021-12-22 06:35:12 -08:00
|
|
|
def _check_dir(dir_name, files=False, show_extra=False):
|
2021-12-20 23:51:51 -08:00
|
|
|
dir_path = BASEDIR / dir_name
|
|
|
|
for readme_path in dir_path.glob('**/README.md'):
|
2021-12-22 06:35:12 -08:00
|
|
|
if '.terraform' in str(readme_path):
|
|
|
|
continue
|
2022-01-17 01:23:01 -08:00
|
|
|
diff = None
|
2021-12-20 23:51:51 -08:00
|
|
|
readme = readme_path.read_text()
|
|
|
|
mod_name = str(readme_path.relative_to(dir_path).parent)
|
|
|
|
result = tfdoc.get_doc(readme)
|
|
|
|
if not result:
|
|
|
|
state = State.SKIP
|
2020-11-07 00:12:41 -08:00
|
|
|
else:
|
2021-12-20 23:51:51 -08:00
|
|
|
try:
|
2021-12-22 06:35:12 -08:00
|
|
|
new_doc = tfdoc.create_doc(
|
|
|
|
readme_path.parent, files=files, show_extra=show_extra)
|
2021-12-20 23:51:51 -08:00
|
|
|
except SystemExit:
|
|
|
|
state = state.SKIP
|
|
|
|
else:
|
2022-01-17 01:23:01 -08:00
|
|
|
if new_doc == result['doc']:
|
|
|
|
state = State.OK
|
|
|
|
else:
|
|
|
|
state = State.FAIL
|
|
|
|
diff = '\n'.join(
|
|
|
|
[f'----- {mod_name} diff -----\n'] +
|
|
|
|
list(difflib.ndiff(
|
|
|
|
result['doc'].split('\n'), new_doc.split('\n')
|
|
|
|
)))
|
|
|
|
yield mod_name, state, diff
|
2020-11-07 00:12:41 -08:00
|
|
|
|
|
|
|
|
|
|
|
@click.command()
|
2021-12-20 23:51:51 -08:00
|
|
|
@click.argument('dirs', type=str, nargs=-1)
|
2021-12-22 06:35:12 -08:00
|
|
|
@ click.option('--files/--no-files', default=False)
|
2022-01-17 01:23:01 -08:00
|
|
|
@ click.option('--show-diffs/--no-show-diffs', default=False)
|
|
|
|
@ click.option('--show-extra/--no-show-extra', default=False)
|
|
|
|
def main(dirs, files=False, show_diffs=False, show_extra=False):
|
2021-12-20 23:51:51 -08:00
|
|
|
'Cycle through modules and ensure READMEs are up-to-date.'
|
2022-01-17 01:23:01 -08:00
|
|
|
errors = []
|
2021-12-20 23:51:51 -08:00
|
|
|
state_labels = {State.FAIL: '✗', State.OK: '✓', State.SKIP: '?'}
|
|
|
|
for dir_name in dirs:
|
|
|
|
print(f'----- {dir_name} -----')
|
2022-01-17 01:23:01 -08:00
|
|
|
for mod_name, state, diff in _check_dir(dir_name, files, show_extra):
|
|
|
|
if state == State.FAIL:
|
|
|
|
errors.append(diff)
|
2021-12-20 23:51:51 -08:00
|
|
|
print(f'[{state_labels[state]}] {mod_name}')
|
2021-12-22 06:35:12 -08:00
|
|
|
if errors:
|
2022-01-17 01:23:01 -08:00
|
|
|
if show_diffs:
|
|
|
|
print('\n'.join(errors))
|
2021-12-22 06:35:12 -08:00
|
|
|
raise SystemExit('Errors found.')
|
2020-11-07 00:12:41 -08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|