diff --git a/pyproject.toml b/pyproject.toml index 92ab60e..412b125 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,6 +29,9 @@ Documentation = "https://github.com/unknown/ansible-module#readme" Issues = "https://github.com/unknown/ansible-module/issues" Source = "https://github.com/unknown/ansible-module" +[project.scripts] +update-doc = "ansible_module.update_doc:main" + [tool.hatch.version] path = "src/ansible_module/__init__.py" diff --git a/src/ansible_module/__main__.py b/src/ansible_module/__main__.py new file mode 100644 index 0000000..3da37ce --- /dev/null +++ b/src/ansible_module/__main__.py @@ -0,0 +1,6 @@ +from . import update_doc + + + +if __name__ == "__main__": + update_doc.main() diff --git a/src/ansible_module/update_doc.py b/src/ansible_module/update_doc.py new file mode 100755 index 0000000..e4cfad2 --- /dev/null +++ b/src/ansible_module/update_doc.py @@ -0,0 +1,46 @@ +#!/usr/bin/python +import importlib +import pathlib +import re +import sys + +sys.path.append(".") +mindocstring = "DOCUMENTATION = ''''''" +moduledir = pathlib.Path("plugins/modules") +regex = re.compile("DOCUMENTATION *= *r?(?P\"{3}|'{3})(---)?.*?(?P=quote)", re.MULTILINE | re.DOTALL) + + +def main(): + for modfile in moduledir.iterdir(): + if modfile.name in ("__init__.py", "__pycache__", "unit.py.example"): + continue + mod = importlib.import_module(".".join((modfile.parts[:-1]) + (modfile.stem,))) + if hasattr(mod, "Module"): + module = mod.Module + elif hasattr(mod, "__module_name__"): + module = getattr(mod, mod.__module_name__) + else: + print("Error loading Module of File: {}. No Module or __module_name__ defined".format(modfile)) + continue + try: + moddoc = module.doc() + except AttributeError: + print("Broken module. skipping {}".format(modfile)) + continue + except Exception as e: + print("Error in documentation of module {}: {}".format(modfile, e)) + continue + moddata = modfile.read_text() + match = regex.search(moddata) + if not match: + print( + "no Documentation set for module {}. Please add at least \"{}\" to the file".format( + modfile.stem, mindocstring + ) + ) + continue + newmod = "{pre}DOCUMENTATION = {quote}{doc}{quote}{post}".format( + pre=moddata[: match.start()], quote=match.group("quote"), doc=moddoc, post=moddata[match.end() :] + ) + modfile.write_text(newmod) + print("updated the documentation of module {}".format(module.name))