| 1 |
#!/usr/bin/env python |
| 2 |
# Copyright (c) Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
# Run this script with `nox -Re update-docs-fragments`! |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
import importlib |
| 11 |
import importlib.machinery |
| 12 |
import importlib.util |
| 13 |
import os |
| 14 |
import re |
| 15 |
import sys |
| 16 |
import textwrap |
| 17 |
|
| 18 |
import yaml |
| 19 |
|
| 20 |
PROVIDERS = ["hetzner", "hosttech"] |
| 21 |
|
| 22 |
DEPENDENT_FRAGMENTS = [ |
| 23 |
( |
| 24 |
"RECORD_TYPE_CHOICES", |
| 25 |
[ |
| 26 |
( |
| 27 |
"record_type", |
| 28 |
"options.type", |
| 29 |
[ |
| 30 |
"module_record", |
| 31 |
"module_record_info", |
| 32 |
"module_record_set", |
| 33 |
"module_record_set_info", |
| 34 |
], |
| 35 |
), |
| 36 |
], |
| 37 |
), |
| 38 |
( |
| 39 |
"RECORD_DEFAULT_TTL", |
| 40 |
[ |
| 41 |
( |
| 42 |
"record_default_ttl", |
| 43 |
"options.ttl", |
| 44 |
[ |
| 45 |
"module_record", |
| 46 |
"module_record_set", |
| 47 |
], |
| 48 |
), |
| 49 |
], |
| 50 |
), |
| 51 |
( |
| 52 |
"RECORD_TYPE_CHOICES_RECORD_SETS_MODULE", |
| 53 |
[ |
| 54 |
( |
| 55 |
"record_type", |
| 56 |
"options.record_sets.suboptions.type", |
| 57 |
[ |
| 58 |
"module_record_sets", |
| 59 |
], |
| 60 |
), |
| 61 |
( |
| 62 |
"record_default_ttl", |
| 63 |
"options.record_sets.suboptions.ttl", |
| 64 |
[ |
| 65 |
"module_record_sets", |
| 66 |
], |
| 67 |
), |
| 68 |
], |
| 69 |
), |
| 70 |
( |
| 71 |
"RECORD_TYPE_CHOICES_RECORDS_INVENTORY", |
| 72 |
[ |
| 73 |
( |
| 74 |
"record_type", |
| 75 |
"options.simple_filters.suboptions.type", |
| 76 |
[ |
| 77 |
"inventory_records", |
| 78 |
], |
| 79 |
), |
| 80 |
], |
| 81 |
), |
| 82 |
( |
| 83 |
"ZONE_ID_TYPE", |
| 84 |
[ |
| 85 |
( |
| 86 |
"zone_id_type", |
| 87 |
"options.zone_id", |
| 88 |
[ |
| 89 |
"module_record", |
| 90 |
"module_record_info", |
| 91 |
"module_record_set", |
| 92 |
"module_record_set_info", |
| 93 |
"module_record_sets", |
| 94 |
"module_zone_info", |
| 95 |
"inventory_records", |
| 96 |
], |
| 97 |
), |
| 98 |
], |
| 99 |
), |
| 100 |
] |
| 101 |
|
| 102 |
|
| 103 |
def get_provider_informations(providers): |
| 104 |
provider_infos = {} |
| 105 |
errors = [] |
| 106 |
|
| 107 |
for provider in providers: |
| 108 |
full_py_path = ( |
| 109 |
f"ansible_collections.community.dns.plugins.module_utils._{provider}.api" |
| 110 |
) |
| 111 |
try: |
| 112 |
the_module = importlib.import_module(full_py_path) |
| 113 |
except Exception as e: |
| 114 |
errors.append(f"Error while importing module {full_py_path}: {e}") |
| 115 |
continue |
| 116 |
|
| 117 |
create_provider_info_fn_name = f"create_{provider}_provider_information" |
| 118 |
try: |
| 119 |
create_provider_info_fn = the_module.__dict__[create_provider_info_fn_name] |
| 120 |
provider_infos[provider] = create_provider_info_fn() |
| 121 |
except KeyError: |
| 122 |
errors.append( |
| 123 |
f"{full_py_path}: Cannot find function {create_provider_info_fn}" |
| 124 |
) |
| 125 |
except Exception as e: |
| 126 |
errors.append( |
| 127 |
f"{full_py_path}: Error while invoking function {create_provider_info_fn}: {e}" |
| 128 |
) |
| 129 |
|
| 130 |
return provider_infos, errors |
| 131 |
|
| 132 |
|
| 133 |
class DocFragmentParseError(Exception): |
| 134 |
def __init__(self, path, error_message): |
| 135 |
self.path = path |
| 136 |
self.error_message = error_message |
| 137 |
super().__init__(f"Error while parsing {path}: {error_message}") |
| 138 |
|
| 139 |
|
| 140 |
DOC_FRAGMENT_START_MATCHER = re.compile(r'^ ([A-Z_]+) = r"""$') |
| 141 |
|
| 142 |
|
| 143 |
class Dumper(yaml.SafeDumper): |
| 144 |
def ignore_aliases(self, *args): |
| 145 |
return True |
| 146 |
|
| 147 |
# pylint: disable-next=keyword-arg-before-vararg |
| 148 |
def increase_indent(self, flow=False, *args, **kwargs): |
| 149 |
self.best_indent = kwargs.pop("ident_override", 2) |
| 150 |
return super().increase_indent(*args, **kwargs) |
| 151 |
|
| 152 |
def expect_block_sequence(self): |
| 153 |
self.increase_indent(flow=False, indentless=False, ident_override=2) |
| 154 |
self.state = self.expect_first_block_sequence_item |
| 155 |
|
| 156 |
|
| 157 |
class DocFragment: |
| 158 |
def __init__(self, path, prefix_lines, name, lines): |
| 159 |
self.prefix_lines = prefix_lines |
| 160 |
self.name = name |
| 161 |
self.lines = lines |
| 162 |
|
| 163 |
try: |
| 164 |
self.data = yaml.safe_load("\n".join(self.lines)) |
| 165 |
except Exception as e: |
| 166 |
raise DocFragmentParseError( |
| 167 |
path, f"Error while parsing part {name}: {e}" |
| 168 |
) from e |
| 169 |
|
| 170 |
def recreate_lines(self): |
| 171 |
data = yaml.dump( |
| 172 |
self.data, |
| 173 |
default_flow_style=False, |
| 174 |
indent=2, |
| 175 |
Dumper=Dumper, |
| 176 |
sort_keys=False, |
| 177 |
) |
| 178 |
self.lines = data.splitlines() |
| 179 |
|
| 180 |
def serialize_lines(self): |
| 181 |
return self.prefix_lines + [f' {self.name} = r"""'] + self.lines + ['"""'] |
| 182 |
|
| 183 |
|
| 184 |
class DocFragmentFile: |
| 185 |
def __init__(self, path): |
| 186 |
with open(path, "r", encoding="utf-8") as f: |
| 187 |
lines = f.read().splitlines() |
| 188 |
|
| 189 |
self.prefix = [] |
| 190 |
self.fragments = [] |
| 191 |
self.fragments_by_name = {} |
| 192 |
|
| 193 |
where = "prefix" |
| 194 |
body_prefix = [] |
| 195 |
body_name = "" |
| 196 |
body_lines = [] |
| 197 |
for line in lines: |
| 198 |
if where == "prefix": |
| 199 |
self.prefix.append(line) |
| 200 |
if line == "class ModuleDocFragment:": |
| 201 |
where = "body" |
| 202 |
body_prefix = [] |
| 203 |
body_name = None |
| 204 |
body_lines = [] |
| 205 |
elif where == "body": |
| 206 |
if body_name is None: |
| 207 |
m = DOC_FRAGMENT_START_MATCHER.match(line) |
| 208 |
if m: |
| 209 |
body_name = m.group(1) |
| 210 |
else: |
| 211 |
body_prefix.append(line) |
| 212 |
elif line == '"""': |
| 213 |
fragment = DocFragment(path, body_prefix, body_name, body_lines) |
| 214 |
self.fragments.append(fragment) |
| 215 |
self.fragments_by_name[body_name] = fragment |
| 216 |
body_prefix = [] |
| 217 |
body_name = None |
| 218 |
body_lines = [] |
| 219 |
else: |
| 220 |
body_lines.append(line) |
| 221 |
|
| 222 |
if where == "prefix": |
| 223 |
raise DocFragmentParseError(path, "Cannot find body") |
| 224 |
|
| 225 |
def serialize_to_string(self): |
| 226 |
lines = [] |
| 227 |
lines.extend(self.prefix) |
| 228 |
for fragment in self.fragments: |
| 229 |
lines.extend(fragment.serialize_lines()) |
| 230 |
lines.append("") |
| 231 |
return "\n".join(lines) |
| 232 |
|
| 233 |
|
| 234 |
def doc_fragment_fn(name): |
| 235 |
return os.path.join("plugins", "doc_fragments", f"_{name}.py") |
| 236 |
|
| 237 |
|
| 238 |
def load_doc_fragment(name): |
| 239 |
fn = doc_fragment_fn(name) |
| 240 |
return DocFragmentFile(fn) |
| 241 |
|
| 242 |
|
| 243 |
def load_single_doc_fragment(name): |
| 244 |
fragment = "DOCUMENTATION" |
| 245 |
if "." in name: |
| 246 |
name, fragment = name.split(".", 1) |
| 247 |
fragment = fragment.upper() |
| 248 |
doc_fragment = load_doc_fragment(name) |
| 249 |
return doc_fragment.fragments_by_name[fragment] |
| 250 |
|
| 251 |
|
| 252 |
def write_doc_fragment(name, doc_fragment): |
| 253 |
fn = doc_fragment_fn(name) |
| 254 |
data = doc_fragment.serialize_to_string() |
| 255 |
with open(fn, "w", encoding="utf-8") as f: |
| 256 |
f.write(data) |
| 257 |
|
| 258 |
|
| 259 |
def compare_doc_fragment(name, doc_fragment): |
| 260 |
fn = doc_fragment_fn(name) |
| 261 |
data = doc_fragment.serialize_to_string() |
| 262 |
with open(fn, "r", encoding="utf-8") as f: |
| 263 |
compare_data = f.read() |
| 264 |
return data == compare_data |
| 265 |
|
| 266 |
|
| 267 |
def augment_fragment(provider_fragment, provider_info): |
| 268 |
# pylint: disable=too-many-nested-blocks |
| 269 |
|
| 270 |
data = { |
| 271 |
"record_type": {"choices": sorted(provider_info.get_supported_record_types())}, |
| 272 |
"zone_id_type": {"type": provider_info.get_zone_id_type()}, |
| 273 |
"record_id_type": {"type": provider_info.get_record_id_type()}, |
| 274 |
"record_default_ttl": {"default": provider_info.get_record_default_ttl()}, |
| 275 |
} |
| 276 |
|
| 277 |
for fragment_name, fragment_insertion_data in DEPENDENT_FRAGMENTS: |
| 278 |
insertion_fragment = provider_fragment.fragments_by_name.get(fragment_name) |
| 279 |
if insertion_fragment is None: |
| 280 |
insertion_fragment = DocFragment("", [], fragment_name, []) |
| 281 |
provider_fragment.fragments.append(insertion_fragment) |
| 282 |
provider_fragment.fragments_by_name[fragment_name] = insertion_fragment |
| 283 |
|
| 284 |
insertion_fragment.data = {} |
| 285 |
all_doc_fragment_names = set() |
| 286 |
for what, insertion_point, doc_fragment_names in fragment_insertion_data: |
| 287 |
all_doc_fragment_names.update(doc_fragment_names) |
| 288 |
doc_fragments = [ |
| 289 |
load_single_doc_fragment(doc_fragment) |
| 290 |
for doc_fragment in doc_fragment_names |
| 291 |
] |
| 292 |
insertion_point = insertion_point.split(".") |
| 293 |
|
| 294 |
insertion_pos = insertion_fragment.data |
| 295 |
original_pos = doc_fragments[0].data # FIXME |
| 296 |
for depth, part in enumerate(insertion_point): |
| 297 |
if part not in insertion_pos: |
| 298 |
insertion_pos[part] = {} |
| 299 |
insertion_pos = insertion_pos[part] |
| 300 |
original_pos = original_pos[part] |
| 301 |
if depth >= 2: |
| 302 |
for x in original_pos: |
| 303 |
if x not in insertion_pos: |
| 304 |
insertion_pos[x] = original_pos[x] |
| 305 |
insertion_pos.update(data[what]) |
| 306 |
|
| 307 |
insertion_fragment.prefix_lines = [ |
| 308 |
"", |
| 309 |
" # WARNING: This section is automatically generated by update-docs-fragments.py.", |
| 310 |
] |
| 311 |
plural_s = "s" if len(all_doc_fragment_names) != 1 else "" |
| 312 |
fragment_names = ", ".join(sorted(all_doc_fragment_names)) |
| 313 |
insertion_fragment.prefix_lines.extend( |
| 314 |
[ |
| 315 |
f" # {line}" |
| 316 |
for line in textwrap.wrap( |
| 317 |
f"It is used to augment the docs fragment{plural_s} {fragment_names}.", |
| 318 |
width=80, |
| 319 |
) |
| 320 |
] |
| 321 |
) |
| 322 |
insertion_fragment.prefix_lines.append(" # DO NOT EDIT MANUALLY!") |
| 323 |
insertion_fragment.recreate_lines() |
| 324 |
|
| 325 |
|
| 326 |
def main(program, arguments): |
| 327 |
lint = "--lint" in arguments |
| 328 |
provider_infos, errors = get_provider_informations(PROVIDERS) |
| 329 |
try: |
| 330 |
for provider, provider_info in sorted(provider_infos.items()): |
| 331 |
try: |
| 332 |
doc_fragment = load_doc_fragment(provider) |
| 333 |
|
| 334 |
augment_fragment(doc_fragment, provider_info) |
| 335 |
|
| 336 |
if not compare_doc_fragment(provider, doc_fragment): |
| 337 |
path = doc_fragment_fn(provider) |
| 338 |
if lint: |
| 339 |
errors.append( |
| 340 |
f"{path}: Needs to be updated by running 'nox -Re update-docs-fragments'" |
| 341 |
) |
| 342 |
else: |
| 343 |
print(f"Writing {path}...") |
| 344 |
write_doc_fragment(provider, doc_fragment) |
| 345 |
|
| 346 |
except DocFragmentParseError as e: |
| 347 |
errors.append( |
| 348 |
f"{e.path}: Error while parsing docs fragment: {e.error_message}" |
| 349 |
) |
| 350 |
|
| 351 |
except Exception as e: |
| 352 |
errors.append(f"{program}: Unexpected error: {e}") |
| 353 |
|
| 354 |
for error in errors: |
| 355 |
print(error) |
| 356 |
return 5 if errors else 0 |
| 357 |
|
| 358 |
|
| 359 |
if __name__ == "__main__": |
| 360 |
sys.exit(main(sys.argv[0], sys.argv[1:])) |