From 37ba6e4745898d663fb520e4cdc21214f13802a8 Mon Sep 17 00:00:00 2001 From: routel-nolan Date: Fri, 6 Dec 2024 17:17:39 +0100 Subject: [PATCH] Refactor gen-wiki.py to fix module processing and update documentation generation --- gen-wiki.py | 80 ++--------------------------------------------------- 1 file changed, 3 insertions(+), 77 deletions(-) diff --git a/gen-wiki.py b/gen-wiki.py index 2be7e02..e33ef5e 100644 --- a/gen-wiki.py +++ b/gen-wiki.py @@ -5,71 +5,7 @@ import argparse import subprocess -MODULES_NAME = [] - -def check_next_module_name(curent_name, next_name): - curent = curent_name.split('-') - second = next_name.split('-') - if curent[0] == second[0]: - return True - return False - -def compare_directory_names(directory_names): - matching_directories = [] - for i in range(len(directory_names)): - for j in range(i+1, len(directory_names)): - if check_next_module_name(directory_names[i], directory_names[j]): - matching_directories.append((directory_names[i], directory_names[j])) - return matching_directories - -def process_files(module_dir, directory): - files = os.listdir(f"{module_dir}/{directory}") - for file in files: - if file.endswith('.md'): - return f"{module_dir}/{directory}/{file}" - -def remove_module_from_list(module_list, module_to_remove): - clean_least = [i for i in module_list if i not in module_to_remove] - return clean_least - -def create_str_for_multiple_modules(paths, module_name, module_full_name): - content = "" - content += f"## {module_name.capitalize()}\n\n" - for path in paths: - with open(path, 'r') as f: - tmp = f.read() - if tmp.strip(): - content += f"### {module_full_name.capitalize()}\n\n" - content += tmp - MODULES_NAME.append(module_name) - return content - -def create_modules_documentation(output_dir): - modules_dir = 'terraform/modules' - markdown_content = "" - paths = [] - directory_names = [d for d in os.listdir(modules_dir) if os.path.isdir(os.path.join(modules_dir, d))] - multiple_directory = compare_directory_names(directory_names) - if multiple_directory is not None: - for module in multiple_directory: - paths.append(process_files(modules_dir, module[0])) - paths.append(process_files(modules_dir, module[1])) - paths_filtered = [path for path in paths if path is not None] - markdown_content += create_str_for_multiple_modules(paths_filtered, multiple_directory[0][0].split('-')[0], multiple_directory[0][0]) - directory_names = remove_module_from_list(directory_names, multiple_directory[0]) - for directory in directory_names: - tmp = process_files(modules_dir, directory) - if tmp != None: - markdown_content += f"## {directory.capitalize()}\n\n" - markdown_content += tmp - MODULES_NAME.append(directory) - - if markdown_content.strip() != f"#TMP\n\n": - output_file = os.path.join(output_dir, f"modules-documentation.md") - os.makedirs(output_dir, exist_ok=True) - with open(output_file, 'w') as f: - f.write(markdown_content) - print(f"Markdown file generated for modules: {output_file}") +TEMP_DIR = 'temp_wiki' def load_config(config_file): with open(config_file, 'r') as f: @@ -132,10 +68,6 @@ def download_bucket(directory): except Exception as e: print(f"Error: {e}") return None - -def create_link_to_module_doc(module_name): - content = f"Module documentation [here](https://github.com/skyscrapers/{os.getenv('REPO_NAME')}/wiki/modules-documentation#{module_name})\n" - return content def process_directory(directory, config): json_content = download_bucket(directory) @@ -155,10 +87,7 @@ def process_directory(directory, config): if markdown_content: directory = directory.split('/', 1)[1] - markdown_content = f"## {directory.capitalize()}\n\n" - if directory in MODULES_NAME: - markdown_content += create_link_to_module_doc(directory) - markdown_content += markdown_content + markdown_content = f"## {directory.capitalize()}\n\n" + markdown_content return markdown_content @@ -209,15 +138,12 @@ def copy_wiki(md_files): args = parser.parse_args() - create_modules_documentation(f"terraform/live/{args.output_dir}") config = load_config(args.config_file) os.chdir("terraform/live") directories = [d for d in os.listdir(os.getcwd()) if os.path.isdir(os.path.join(os.getcwd(), d))] for directory in directories: process_environment(directory, config, args.output_dir) - + md_files = list_md_files(args.output_dir) os.chdir("../..") - copy_wiki(md_files) - \ No newline at end of file