From e6be3683674e606c1626facb3d66a53804777437 Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Fri, 28 Jul 2023 16:32:38 +0200 Subject: [PATCH] Simplify whitespace handling --- modules/project/README.md | 6 ++---- tools/check_documentation.py | 15 ++++++--------- tools/tfdoc.py | 18 ++++++++---------- 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/modules/project/README.md b/modules/project/README.md index 1309c8199..b6f211938 100644 --- a/modules/project/README.md +++ b/modules/project/README.md @@ -4,7 +4,6 @@ This module implements the creation and management of one GCP project including ## TOC - - [TOC](#toc) - [Basic Project Creation](#basic-project-creation) @@ -28,7 +27,6 @@ This module implements the creation and management of one GCP project including - [Outputs](#outputs) - ## Basic Project Creation ```hcl @@ -579,8 +577,8 @@ output "compute_robot" { ``` - + ## Files | name | description | resources | @@ -648,5 +646,5 @@ output "compute_robot" { | [project_id](outputs.tf#L75) | Project id. | | | [service_accounts](outputs.tf#L94) | Product robot service accounts in project. | | | [sink_writer_identities](outputs.tf#L110) | Writer identities created for each sink. | | - + diff --git a/tools/check_documentation.py b/tools/check_documentation.py index f27c53b91..54d7b28bc 100755 --- a/tools/check_documentation.py +++ b/tools/check_documentation.py @@ -85,23 +85,20 @@ def _check_dir(dir_name, exclude_files=None, files=False, show_extra=False): outputs = [o.name for o in newouts if o.file.endswith('outputs.tf')] state = State.OK - d = difflib.Differ() if current_doc and new_doc.content != current_doc['doc']: state = State.FAIL_STALE_README header = f'----- {mod_name} diff -----\n' - ndiff = d.compare(current_doc['doc'].splitlines(keepends=True), - new_doc.content.splitlines(keepends=True)) - diff = ''.join([header] + list(ndiff)) - diff += f"\n\ncurrent\n''{current_doc['doc'].splitlines(keepends=True)}''" - diff += f"\n\nnew\n''{new_doc.content.splitlines(keepends=True)}''" + ndiff = difflib.ndiff(current_doc['doc'].splitlines(keepends=True), + new_doc.content.splitlines(keepends=True)) + diff = ''.join([header] + [x for x in ndiff if x[0] != ' ']) elif current_toc and new_toc != current_toc['toc']: state = State.FAIL_STALE_TOC header = f'----- {mod_name} diff -----\n' - ndiff = d.compare(current_toc['toc'].splitlines(keepends=True), - new_toc.splitlines(keepends=True)) - diff = ''.join([header] + list(ndiff)) + ndiff = difflib.ndiff(current_toc['toc'].splitlines(keepends=True), + new_toc.splitlines(keepends=True)) + diff = ''.join([header] + [x for x in ndiff if x[0] != ' ']) elif empty := [v.name for v in newvars if not v.description]: state = state.FAIL_VARIABLE_DESCRIPTION diff --git a/tools/tfdoc.py b/tools/tfdoc.py index e2c787ddf..2ff11df4a 100755 --- a/tools/tfdoc.py +++ b/tools/tfdoc.py @@ -400,17 +400,15 @@ def get_readme(readme_path): def render_doc(readme, doc): 'Replace document in module\'s README.md file.' result = get_doc(readme) - if not result: - raise SystemExit(f'Mark not found in README {readme}') - if doc == result['doc']: + if not result or doc == result['doc']: return readme try: return '\n'.join([ - readme[:result['start']], + readme[:result['start']].rstrip(), MARK_BEGIN, doc, MARK_END, - readme[result['end']:], + readme[result['end']:].lstrip(), ]) except (IOError, OSError) as e: raise SystemExit(f'Error replacing README {readme_path}: {e}') @@ -419,17 +417,17 @@ def render_doc(readme, doc): def render_toc(readme, toc): 'Replace document in module\'s README.md file.' result = get_toc(readme) - if not result: - raise SystemExit(f'TOC not found in README {readme}') - if toc == result['toc']: + if not result or toc == result['toc']: return readme try: return '\n'.join([ - readme[:result['start']], + readme[:result['start']].rstrip(), + '', TOC_BEGIN, toc, TOC_END, - readme[result['end']:], + '', + readme[result['end']:].lstrip(), ]) except (IOError, OSError) as e: raise SystemExit(f'Error replacing README {readme_path}: {e}')