| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import contextlib as _contextlib |
| 4 |
import dataclasses as _dataclasses |
| 5 |
from importlib import resources as _importlib_resources, util as _importlib_util |
| 6 |
import inspect as _inspect |
| 7 |
import pathlib as _pathlib |
| 8 |
import typing as _t |
| 9 |
|
| 10 |
if _t.TYPE_CHECKING: |
| 11 |
from ansible.module_utils.compat.typing import LiteralString |
| 12 |
|
| 13 |
|
| 14 |
class EmbedManager: |
| 15 |
"""Utility class for embedding arbitrary content in an AnsiballZ payload.""" |
| 16 |
@classmethod |
| 17 |
def embed(cls, package: LiteralString, resource: LiteralString, /) -> EmbeddedResource: |
| 18 |
""" |
| 19 |
Request embedding of the specified `resource` from `package` in the generated AnsiballZ payload. |
| 20 |
The `package` argument must resolve to a Python package beneath `ansible` or `ansible_collections`. |
| 21 |
Relative import-style strings are supported (e.g. `..module_utils.something`). |
| 22 |
The `resource` argument must match the target filename. |
| 23 |
The method returns an `EmbeddedResource` object that must be stored and used to access the embedded content at runtime. |
| 24 |
|
| 25 |
To enable embedding analysis during payload build, the `embed` module must be imported in one of the following ways: |
| 26 |
`from ansible.module_utils.embed import EmbedManager` (optional alias supported) |
| 27 |
`from ansible.module_utils import embed` (optional alias supported) |
| 28 |
|
| 29 |
To be properly detected during payload build analysis, the `embed` call must: |
| 30 |
* Occur at the topmost level of a module or module_util. |
| 31 |
* Assign its result to a valid variable. |
| 32 |
* Use only inline literal string values as positional arguments. |
| 33 |
* Use only the as-imported name of the `embed` module or `EmbedManager` type (including import aliases). |
| 34 |
""" |
| 35 |
if package.startswith('.'): |
| 36 |
st = _inspect.stack() |
| 37 |
sp = _importlib_util.find_spec(st[1].frame.f_globals['__name__']) |
| 38 |
package = _importlib_util.resolve_name(package, sp.parent) |
| 39 |
# FUTURE: register this value for runtime discovery of "what's embedded here" |
| 40 |
return EmbeddedResource(package, resource) |
| 41 |
|
| 42 |
|
| 43 |
@_dataclasses.dataclass(frozen=True) |
| 44 |
class EmbeddedResource: |
| 45 |
"""Wrapper object returned by `EmbedManager.embed()` for runtime access to content embedded in an AnsiballZ payload.""" |
| 46 |
package: str |
| 47 |
resource: str |
| 48 |
|
| 49 |
@property |
| 50 |
def path_context_manager(self) -> _contextlib.AbstractContextManager[_pathlib.Path]: |
| 51 |
""" |
| 52 |
Returns a context manager that, once entered, provides a `pathlib.Path` to the embedded content. |
| 53 |
|
| 54 |
Path validity is only guaranteed until the context manager exits, as the temporarily extracted content is deleted when running from a zip archive. |
| 55 |
""" |
| 56 |
return _importlib_resources.path(self.package, self.resource) |
| 57 |
|
| 58 |
@property |
| 59 |
def python_module_ref(self) -> str: |
| 60 |
"""Returns a fully-qualified Python module reference to the embedded content (with the `.py` extension suppressed).""" |
| 61 |
return self.package + "." + str(_pathlib.Path(self.resource).with_suffix('')) |