|
| 1 | +"""Sphinx extension for setting up the build settings.""" |
| 2 | + |
| 3 | +import subprocess |
| 4 | +from dataclasses import dataclass |
| 5 | +from functools import cache |
| 6 | +from pathlib import Path |
| 7 | +from tomllib import loads as parse_toml_string |
| 8 | +from typing import Literal |
| 9 | + |
| 10 | +from sphinx.application import Sphinx |
| 11 | +from sphinx.util import logging |
| 12 | + |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +DOCSITE_ROOT_DIR = Path(__file__).parents[1].resolve() |
| 18 | +DOCSITE_EOL_CONFIG_PATH = DOCSITE_ROOT_DIR / 'end_of_life.toml' |
| 19 | + |
| 20 | + |
| 21 | +@dataclass(frozen=True) |
| 22 | +class Distribution: |
| 23 | + end_of_life: list[str] |
| 24 | + supported: list[str] |
| 25 | + |
| 26 | + @classmethod |
| 27 | + def from_dict(cls, raw_dist: dict[str, list[str]]) -> 'Distribution': |
| 28 | + return cls( |
| 29 | + **{ |
| 30 | + kind.replace('-', '_'): versions |
| 31 | + for kind, versions in raw_dist.items() |
| 32 | + }, |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +EOLConfigType = dict[str, Distribution] |
| 37 | + |
| 38 | + |
| 39 | +@cache |
| 40 | +def _read_eol_data() -> EOLConfigType: |
| 41 | + raw_config_dict = parse_toml_string(DOCSITE_EOL_CONFIG_PATH.read_text()) |
| 42 | + |
| 43 | + return { |
| 44 | + dist_name: Distribution.from_dict(dist_data) |
| 45 | + for dist_name, dist_data in raw_config_dict['distribution'].items() |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | +@cache |
| 50 | +def _is_eol_build(git_branch: str, kind: str) -> bool: |
| 51 | + return git_branch in _read_eol_data()[kind].end_of_life |
| 52 | + |
| 53 | + |
| 54 | +@cache |
| 55 | +def _get_current_git_branch(): |
| 56 | + git_branch_cmd = 'git', 'rev-parse', '--abbrev-ref', 'HEAD' |
| 57 | + |
| 58 | + try: |
| 59 | + return subprocess.check_output(git_branch_cmd, text=True).strip() |
| 60 | + except subprocess.CalledProcessError as proc_err: |
| 61 | + raise LookupError( |
| 62 | + f'Failed to locate current Git branch: {proc_err !s}', |
| 63 | + ) from proc_err |
| 64 | + |
| 65 | + |
| 66 | +def _set_global_j2_context(app, config): |
| 67 | + if 'is_eol' in config.html_context: |
| 68 | + raise ValueError( |
| 69 | + '`is_eol` found in `html_context` unexpectedly. ' |
| 70 | + 'It should not be set in `conf.py`.', |
| 71 | + ) from None |
| 72 | + |
| 73 | + dist_name = ( |
| 74 | + 'ansible-core' if app.tags.has('core') |
| 75 | + else 'ansible' if app.tags.has('ansible') |
| 76 | + else None |
| 77 | + ) |
| 78 | + |
| 79 | + if dist_name is None: |
| 80 | + return |
| 81 | + |
| 82 | + try: |
| 83 | + git_branch = _get_current_git_branch() |
| 84 | + except LookupError as lookup_err: |
| 85 | + logger.info(str(lookup_err)) |
| 86 | + return |
| 87 | + |
| 88 | + config.html_context['is_eol'] = _is_eol_build( |
| 89 | + git_branch=git_branch, kind=dist_name, |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +def setup(app: Sphinx) -> dict[str, bool | str]: |
| 94 | + """Initialize the extension. |
| 95 | +
|
| 96 | + :param app: A Sphinx application object. |
| 97 | + :returns: Extension metadata as a dict. |
| 98 | + """ |
| 99 | + |
| 100 | + # NOTE: `config-inited` is used because it runs once as opposed to |
| 101 | + # NOTE: `html-page-context` that runs per each page. The data we |
| 102 | + # NOTE: compute is immutable throughout the build so there's no need |
| 103 | + # NOTE: to have a callback that would be executed hundreds of times. |
| 104 | + app.connect('config-inited', _set_global_j2_context) |
| 105 | + |
| 106 | + return { |
| 107 | + 'parallel_read_safe': True, |
| 108 | + 'parallel_write_safe': True, |
| 109 | + 'version': app.config.release, |
| 110 | + } |
0 commit comments