| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import dataclasses |
| 4 |
import json |
| 5 |
|
| 6 |
import typing as t |
| 7 |
|
| 8 |
from ansible.module_utils._internal._ansiballz import _extensions |
| 9 |
from ansible.module_utils._internal._ansiballz._extensions import _debugpy, _pydevd, _coverage |
| 10 |
from ansible.constants import config |
| 11 |
|
| 12 |
|
| 13 |
class ExtensionManager: |
| 14 |
"""AnsiballZ extension manager.""" |
| 15 |
|
| 16 |
def __init__( |
| 17 |
self, |
| 18 |
pydevd: _pydevd.Options | None = None, |
| 19 |
debugpy: _debugpy.Options | None = None, |
| 20 |
coverage: _coverage.Options | None = None, |
| 21 |
) -> None: |
| 22 |
options = dict( |
| 23 |
_pydevd=pydevd, |
| 24 |
_debugpy=debugpy, |
| 25 |
_coverage=coverage, |
| 26 |
) |
| 27 |
|
| 28 |
self._pydevd = pydevd |
| 29 |
self._debugpy = debugpy |
| 30 |
self._coverage = coverage |
| 31 |
self._extension_names = tuple(name for name, option in options.items() if option) |
| 32 |
self._module_names = tuple(f'{_extensions.__name__}.{name}' for name in self._extension_names) |
| 33 |
|
| 34 |
self.source_mapping: dict[str, str] = {} |
| 35 |
|
| 36 |
@property |
| 37 |
def debugger_enabled(self) -> bool: |
| 38 |
"""Returns True if the debugger extension is enabled, otherwise False.""" |
| 39 |
return bool(self._pydevd or self._debugpy) |
| 40 |
|
| 41 |
@property |
| 42 |
def extension_names(self) -> tuple[str, ...]: |
| 43 |
"""Names of extensions to include in the AnsiballZ payload.""" |
| 44 |
return self._extension_names |
| 45 |
|
| 46 |
@property |
| 47 |
def module_names(self) -> tuple[str, ...]: |
| 48 |
"""Python module names of extensions to include in the AnsiballZ payload.""" |
| 49 |
return self._module_names |
| 50 |
|
| 51 |
def get_extensions(self) -> dict[str, dict[str, object]]: |
| 52 |
"""Return the configured extensions and their options.""" |
| 53 |
extension_options: dict[str, t.Any] = {} |
| 54 |
|
| 55 |
if self._debugpy: |
| 56 |
extension_options['_debugpy'] = dataclasses.replace( |
| 57 |
self._debugpy, |
| 58 |
source_mapping=self._get_source_mapping(self._debugpy.source_mapping), |
| 59 |
) |
| 60 |
|
| 61 |
if self._pydevd: |
| 62 |
extension_options['_pydevd'] = dataclasses.replace( |
| 63 |
self._pydevd, |
| 64 |
source_mapping=self._get_source_mapping(self._pydevd.source_mapping), |
| 65 |
) |
| 66 |
|
| 67 |
if self._coverage: |
| 68 |
extension_options['_coverage'] = self._coverage |
| 69 |
|
| 70 |
extensions = {extension: dataclasses.asdict(options) for extension, options in extension_options.items()} |
| 71 |
|
| 72 |
return extensions |
| 73 |
|
| 74 |
def _get_source_mapping(self, debugger_mapping: dict[str, str]) -> dict[str, str]: |
| 75 |
"""Get the source mapping, adjusting the source root as needed.""" |
| 76 |
if debugger_mapping: |
| 77 |
source_mapping = {self._translate_path(key, debugger_mapping): value for key, value in self.source_mapping.items()} |
| 78 |
else: |
| 79 |
source_mapping = self.source_mapping |
| 80 |
|
| 81 |
return source_mapping |
| 82 |
|
| 83 |
@staticmethod |
| 84 |
def _translate_path(path: str, debugger_mapping: dict[str, str]) -> str: |
| 85 |
"""Translate a local path to a foreign path.""" |
| 86 |
for replace, match in debugger_mapping.items(): |
| 87 |
if path.startswith(match): |
| 88 |
return replace + path[len(match) :] |
| 89 |
|
| 90 |
return path |
| 91 |
|
| 92 |
@classmethod |
| 93 |
def create(cls, task_vars: dict[str, object]) -> t.Self: |
| 94 |
"""Create an instance using the provided task vars.""" |
| 95 |
return cls( |
| 96 |
pydevd=cls._get_options('_ANSIBALLZ_PYDEVD_CONFIG', _pydevd.Options, task_vars), |
| 97 |
debugpy=cls._get_options('_ANSIBALLZ_DEBUGPY_CONFIG', _debugpy.Options, task_vars), |
| 98 |
coverage=cls._get_options('_ANSIBALLZ_COVERAGE_CONFIG', _coverage.Options, task_vars), |
| 99 |
) |
| 100 |
|
| 101 |
@classmethod |
| 102 |
def _get_options[T](cls, name: str, config_type: type[T], task_vars: dict[str, object]) -> T | None: |
| 103 |
"""Parse configuration from the named environment variable as the specified type, or None if not configured.""" |
| 104 |
if (value := config.get_config_value(name, variables=task_vars)) is None: |
| 105 |
return None |
| 106 |
|
| 107 |
data = json.loads(value) if isinstance(value, str) else value |
| 108 |
options = config_type(**data) |
| 109 |
|
| 110 |
return options |