Implemented more auto docs

This commit is contained in:
2025-03-20 14:13:03 +01:00
parent d5f10276ee
commit 4c29fc9f02
14 changed files with 135 additions and 21 deletions

View File

@@ -0,0 +1,42 @@
import os
import yaml
import argparse
def generate_ansible_roles_doc(roles_dir, output_dir):
"""Generates reStructuredText documentation for Ansible roles."""
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for role in os.listdir(roles_dir):
role_path = os.path.join(roles_dir, role)
meta_file = os.path.join(role_path, "meta/main.yml")
readme_file = os.path.join(role_path, "README.md")
if os.path.exists(meta_file):
with open(meta_file, "r", encoding="utf-8") as f:
meta_data = yaml.safe_load(f)
role_doc = os.path.join(output_dir, f"{role}.rst")
with open(role_doc, "w", encoding="utf-8") as f:
f.write(f"{role.capitalize()} Role\n")
f.write("=" * (len(role) + 7) + "\n\n")
f.write(f"**Description:** {meta_data.get('description', 'No description available')}\n\n")
f.write("### Variables\n")
for key, value in meta_data.get('galaxy_info', {}).items():
f.write(f"- **{key}**: {value}\n")
if os.path.exists(readme_file):
f.write("\n### README\n")
with open(readme_file, "r", encoding="utf-8") as readme:
f.write("\n" + readme.read())
print(f"Ansible roles documentation has been generated in {output_dir}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generate documentation for Ansible roles.")
parser.add_argument("--roles-dir", required=True, help="Directory containing Ansible roles.")
parser.add_argument("--output-dir", required=True, help="Directory where documentation will be saved.")
args = parser.parse_args()
generate_ansible_roles_doc(args.roles_dir, args.output_dir)

View File

@@ -0,0 +1,49 @@
import os
import argparse
import pathspec
def load_gitignore_patterns(source_dir):
"""Loads .gitignore patterns from the given source directory and returns a PathSpec object."""
gitignore_path = os.path.join(source_dir, ".gitignore")
if not os.path.exists(gitignore_path):
return pathspec.PathSpec.from_lines("gitwildmatch", [])
with open(gitignore_path, "r", encoding="utf-8") as f:
patterns = f.readlines()
return pathspec.PathSpec.from_lines("gitwildmatch", patterns)
def generate_yaml_index(source_dir, output_file):
"""Generates an index file listing all YAML files in the specified directory while respecting .gitignore rules."""
yaml_files = []
spec = load_gitignore_patterns(source_dir) # Load .gitignore rules
# Walk through the source directory and collect YAML files
for root, _, files in os.walk(source_dir):
for file in files:
file_path = os.path.relpath(os.path.join(root, file), start=source_dir)
if file.endswith(('.yml', '.yaml')) and not spec.match_file(file_path):
yaml_files.append(os.path.join(root, file))
# Create the output directory if it doesn't exist
os.makedirs(os.path.dirname(output_file), exist_ok=True)
# Write the YAML index to the output file
with open(output_file, "w", encoding="utf-8") as f:
f.write("YAML Files\n===========\n\n")
f.write("This document lists all `.yaml` and `.yml` files found in the specified directory, excluding ignored files.\n\n")
for file in sorted(yaml_files):
f.write(f".. literalinclude:: {file}\n :language: yaml\n :linenos:\n\n")
print(f"YAML index has been generated at {output_file}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generate an index for YAML files while respecting .gitignore.")
parser.add_argument("--source-dir", required=True, help="Directory containing YAML files.")
parser.add_argument("--output-file", required=True, help="Path to the output .rst file.")
args = parser.parse_args()
generate_yaml_index(args.source_dir, args.output_file)