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-19 05:17:20 -08:00
|
|
|
'''Recursively check freshness of tfdoc's generated tables in README files.
|
|
|
|
|
|
|
|
This tool recursively checks that the embedded variables and outputs tables in
|
|
|
|
README files, match what is generated at runtime by tfdoc based on current
|
|
|
|
sources. As such, it accepts pretty much the same options as tfdoc does. Its
|
|
|
|
main use is in CI pipelines triggered by pull requests.
|
|
|
|
'''
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2022-01-27 08:31:08 -08:00
|
|
|
def _check_dir(dir_name, exclude_files=None, files=False, show_extra=False):
|
2022-01-19 05:17:20 -08:00
|
|
|
'Invoke tfdoc on folder, using the relevant options.'
|
2021-12-20 23:51:51 -08:00
|
|
|
dir_path = BASEDIR / dir_name
|
2022-01-22 02:34:18 -08:00
|
|
|
for readme_path in sorted(dir_path.glob('**/README.md')):
|
2021-12-22 06:35:12 -08:00
|
|
|
if '.terraform' in str(readme_path):
|
|
|
|
continue
|
2022-01-19 05:17:20 -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:
|
2022-01-27 08:31:08 -08:00
|
|
|
new_doc = tfdoc.create_doc(readme_path.parent, files, show_extra,
|
|
|
|
exclude_files, readme)
|
2021-12-20 23:51:51 -08:00
|
|
|
except SystemExit:
|
|
|
|
state = state.SKIP
|
|
|
|
else:
|
2022-01-19 05:17:20 -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)
|
2022-01-27 08:31:08 -08:00
|
|
|
@click.option('--exclude-file', '-x', multiple=True)
|
|
|
|
@click.option('--files/--no-files', default=False)
|
|
|
|
@click.option('--show-diffs/--no-show-diffs', default=False)
|
|
|
|
@click.option('--show-extra/--no-show-extra', default=False)
|
|
|
|
def main(dirs, exclude_file=None, 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-22 02:34:18 -08:00
|
|
|
print(f'files: {files}, extra: {show_extra}, diffs: {show_diffs}\n')
|
2022-01-19 05:17:20 -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-27 08:31:08 -08:00
|
|
|
for mod_name, state, diff in _check_dir(dir_name, exclude_file, files,
|
|
|
|
show_extra):
|
2022-01-19 05:17:20 -08:00
|
|
|
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-19 05:17:20 -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()
|