|
| 1 | +# coding: utf-8 |
| 2 | +# Author: Felix Fontein <[email protected]> |
| 3 | +# License: GPLv3+ |
| 4 | +# Copyright: Ansible Project, 2022 |
| 5 | +"""Lint plugin docs.""" |
| 6 | + |
| 7 | +import os |
| 8 | +import shutil |
| 9 | +import tempfile |
| 10 | +import typing as t |
| 11 | + |
| 12 | +import sh |
| 13 | + |
| 14 | +from antsibull_core.compat import asyncio_run |
| 15 | +from antsibull_core.venv import FakeVenvRunner |
| 16 | + |
| 17 | +from .lint_helpers import ( |
| 18 | + load_collection_info, |
| 19 | +) |
| 20 | + |
| 21 | +from .docs_parsing.ansible_doc import ( |
| 22 | + parse_ansible_galaxy_collection_list, |
| 23 | +) |
| 24 | + |
| 25 | +from .augment_docs import augment_docs |
| 26 | +from .cli.doc_commands.stable import ( |
| 27 | + normalize_all_plugin_info, |
| 28 | + get_plugin_contents, |
| 29 | + get_collection_contents, |
| 30 | +) |
| 31 | +from .collection_links import load_collections_links |
| 32 | +from .docs_parsing.parsing import get_ansible_plugin_info |
| 33 | +from .docs_parsing.routing import ( |
| 34 | + load_all_collection_routing, |
| 35 | + remove_redirect_duplicates, |
| 36 | +) |
| 37 | +from .jinja2.environment import doc_environment |
| 38 | +from .write_docs import create_plugin_rst |
| 39 | +from .rstcheck import check_rst_content |
| 40 | + |
| 41 | + |
| 42 | +class CollectionCopier: |
| 43 | + dir: t.Optional[str] |
| 44 | + |
| 45 | + def __init__(self): |
| 46 | + self.dir = None |
| 47 | + |
| 48 | + def __enter__(self): |
| 49 | + if self.dir is None: |
| 50 | + raise AssertionError('Collection copier already initialized') |
| 51 | + self.dir = os.path.realpath(tempfile.mkdtemp(prefix='antsibull-docs-')) |
| 52 | + return self |
| 53 | + |
| 54 | + def add_collection(self, collecion_source_path: str, namespace: str, name: str) -> None: |
| 55 | + self_dir = self.dir |
| 56 | + if self_dir is None: |
| 57 | + raise AssertionError('Collection copier not initialized') |
| 58 | + collection_container_dir = os.path.join( |
| 59 | + self_dir, 'ansible_collections', namespace) |
| 60 | + os.makedirs(collection_container_dir, exist_ok=True) |
| 61 | + |
| 62 | + collection_dir = os.path.join(collection_container_dir, name) |
| 63 | + shutil.copytree(collecion_source_path, collection_dir, symlinks=True) |
| 64 | + |
| 65 | + def __exit__(self, type_, value, traceback_): |
| 66 | + self_dir = self.dir |
| 67 | + if self_dir is None: |
| 68 | + raise AssertionError('Collection copier not initialized') |
| 69 | + shutil.rmtree(self_dir, ignore_errors=True) |
| 70 | + self.dir = None |
| 71 | + |
| 72 | + |
| 73 | +class CollectionFinder: |
| 74 | + def __init__(self): |
| 75 | + self.collections = {} |
| 76 | + stdout = sh.Command('ansible-galaxy')('collection', 'list').stdout |
| 77 | + raw_output = stdout.decode('utf-8', errors='surrogateescape') |
| 78 | + for namespace, name, path, _ in reversed(parse_ansible_galaxy_collection_list(raw_output)): |
| 79 | + self.collections[f'{namespace}.{name}'] = path |
| 80 | + |
| 81 | + def find(self, namespace, name): |
| 82 | + return self.collections.get(f'{namespace}.{name}') |
| 83 | + |
| 84 | + |
| 85 | +def _lint_collection_plugin_docs(collections_dir: str, collection_name: str, |
| 86 | + original_path_to_collection: str, |
| 87 | + ) -> t.List[t.Tuple[str, int, int, str]]: |
| 88 | + # Load collection docs |
| 89 | + venv = FakeVenvRunner() |
| 90 | + plugin_info, collection_metadata = asyncio_run(get_ansible_plugin_info( |
| 91 | + venv, collections_dir, collection_names=[collection_name])) |
| 92 | + # Load routing information |
| 93 | + collection_routing = asyncio_run(load_all_collection_routing(collection_metadata)) |
| 94 | + # Process data |
| 95 | + remove_redirect_duplicates(plugin_info, collection_routing) |
| 96 | + plugin_info, nonfatal_errors = asyncio_run(normalize_all_plugin_info(plugin_info)) |
| 97 | + augment_docs(plugin_info) |
| 98 | + # Load link data |
| 99 | + link_data = asyncio_run(load_collections_links( |
| 100 | + {name: data.path for name, data in collection_metadata.items()})) |
| 101 | + # More processing |
| 102 | + plugin_contents = get_plugin_contents(plugin_info, nonfatal_errors) |
| 103 | + collection_to_plugin_info = get_collection_contents(plugin_contents) |
| 104 | + for collection in collection_metadata: |
| 105 | + collection_to_plugin_info[collection] # pylint:disable=pointless-statement |
| 106 | + # Collect non-fatal errors |
| 107 | + result = [] |
| 108 | + for plugin_type, plugins in sorted(nonfatal_errors.items()): |
| 109 | + for plugin_name, errors in sorted(plugins.items()): |
| 110 | + for error in errors: |
| 111 | + result.append(( |
| 112 | + os.path.join(original_path_to_collection, 'plugins', plugin_type, plugin_name), |
| 113 | + 0, |
| 114 | + 0, |
| 115 | + error, |
| 116 | + )) |
| 117 | + # Compose RST files and check for errors |
| 118 | + # Setup the jinja environment |
| 119 | + env = doc_environment(('antsibull_docs.data', 'docsite')) |
| 120 | + # Get the templates |
| 121 | + plugin_tmpl = env.get_template('plugin.rst.j2') |
| 122 | + role_tmpl = env.get_template('role.rst.j2') |
| 123 | + error_tmpl = env.get_template('plugin-error.rst.j2') |
| 124 | + |
| 125 | + for collection_name_, plugins_by_type in collection_to_plugin_info.items(): |
| 126 | + for plugin_type, plugins in plugins_by_type.items(): |
| 127 | + plugin_type_tmpl = plugin_tmpl |
| 128 | + if plugin_type == 'role': |
| 129 | + plugin_type_tmpl = role_tmpl |
| 130 | + for plugin_short_name, dummy_ in plugins.items(): |
| 131 | + plugin_name = '.'.join((collection_name_, plugin_short_name)) |
| 132 | + rst_content = create_plugin_rst( |
| 133 | + collection_name_, collection_metadata[collection_name_], |
| 134 | + link_data[collection_name_], |
| 135 | + plugin_short_name, plugin_type, |
| 136 | + plugin_info[plugin_type].get(plugin_name), |
| 137 | + nonfatal_errors[plugin_type][plugin_name], |
| 138 | + plugin_type_tmpl, error_tmpl, |
| 139 | + use_html_blobs=False, |
| 140 | + ) |
| 141 | + path = os.path.join( |
| 142 | + original_path_to_collection, 'plugins', plugin_type, |
| 143 | + f'{plugin_short_name}.rst') |
| 144 | + rst_results = check_rst_content( |
| 145 | + rst_content, filename=path, |
| 146 | + ignore_directives=['rst-class'], |
| 147 | + ) |
| 148 | + result.extend([(path, result[0], result[1], result[2]) for result in rst_results]) |
| 149 | + return result |
| 150 | + |
| 151 | + |
| 152 | +def lint_collection_plugin_docs(path_to_collection: str) -> t.List[t.Tuple[str, int, int, str]]: |
| 153 | + try: |
| 154 | + info = load_collection_info(path_to_collection) |
| 155 | + namespace = info['namespace'] |
| 156 | + name = info['name'] |
| 157 | + dependencies = info.get('dependencies') or {} |
| 158 | + except Exception: # pylint:disable=broad-except |
| 159 | + return [( |
| 160 | + path_to_collection, 0, 0, |
| 161 | + 'Cannot identify collection with galaxy.yml or MANIFEST.json at this path')] |
| 162 | + result = [] |
| 163 | + collection_name = f'{namespace}.{name}' |
| 164 | + done_dependencies = {collection_name} |
| 165 | + dependencies = sorted(dependencies) |
| 166 | + with CollectionCopier() as copier: |
| 167 | + # Copy collection |
| 168 | + copier.add_collection(path_to_collection, namespace, name) |
| 169 | + # Copy all dependencies |
| 170 | + if dependencies: |
| 171 | + collection_finder = CollectionFinder() |
| 172 | + while dependencies: |
| 173 | + dependency = dependencies.pop(0) |
| 174 | + if dependency in done_dependencies: |
| 175 | + continue |
| 176 | + dep_namespace, dep_name = dependency.split('.', 2) |
| 177 | + dep_collection_path = collection_finder.find(dep_namespace, dep_name) |
| 178 | + if dep_collection_path: |
| 179 | + copier.add_collection(dep_collection_path, dep_namespace, dep_name) |
| 180 | + try: |
| 181 | + info = load_collection_info(dep_collection_path) |
| 182 | + dependencies.extend(sorted(info.get('dependencies') or {})) |
| 183 | + except Exception: # pylint:disable=broad-except |
| 184 | + result.append(( |
| 185 | + dep_collection_path, 0, 0, |
| 186 | + 'Cannot identify collection with galaxy.yml or MANIFEST.json' |
| 187 | + ' at this path')) |
| 188 | + # Load docs |
| 189 | + result.extend(_lint_collection_plugin_docs( |
| 190 | + copier.dir, collection_name, path_to_collection)) |
| 191 | + return result |
0 commit comments