| 1 |
"""Content configuration.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import os |
| 6 |
import pickle |
| 7 |
import typing as t |
| 8 |
|
| 9 |
from .constants import ( |
| 10 |
CONTROLLER_PYTHON_VERSIONS, |
| 11 |
SUPPORTED_PYTHON_VERSIONS, |
| 12 |
) |
| 13 |
|
| 14 |
from .compat.packaging import ( |
| 15 |
PACKAGING_IMPORT_ERROR, |
| 16 |
SpecifierSet, |
| 17 |
Version, |
| 18 |
) |
| 19 |
|
| 20 |
from .compat.yaml import ( |
| 21 |
YAML_IMPORT_ERROR, |
| 22 |
yaml_load, |
| 23 |
) |
| 24 |
|
| 25 |
from .io import ( |
| 26 |
open_binary_file, |
| 27 |
read_text_file, |
| 28 |
) |
| 29 |
|
| 30 |
from .util import ( |
| 31 |
ApplicationError, |
| 32 |
display, |
| 33 |
) |
| 34 |
|
| 35 |
from .data import ( |
| 36 |
data_context, |
| 37 |
) |
| 38 |
|
| 39 |
from .config import ( |
| 40 |
EnvironmentConfig, |
| 41 |
ContentConfig, |
| 42 |
ModulesConfig, |
| 43 |
) |
| 44 |
|
| 45 |
MISSING = object() |
| 46 |
|
| 47 |
|
| 48 |
def parse_modules_config(data: t.Any) -> ModulesConfig: |
| 49 |
"""Parse the given dictionary as module config and return it.""" |
| 50 |
if not isinstance(data, dict): |
| 51 |
raise Exception('config must be type `dict` not `%s`' % type(data)) |
| 52 |
|
| 53 |
python_requires = data.get('python_requires', MISSING) |
| 54 |
|
| 55 |
if python_requires == MISSING: |
| 56 |
raise KeyError('python_requires is required') |
| 57 |
|
| 58 |
return ModulesConfig( |
| 59 |
python_requires=python_requires, |
| 60 |
python_versions=parse_python_requires(python_requires), |
| 61 |
controller_only=python_requires == 'controller', |
| 62 |
) |
| 63 |
|
| 64 |
|
| 65 |
def parse_content_config(data: t.Any) -> ContentConfig: |
| 66 |
"""Parse the given dictionary as content config and return it.""" |
| 67 |
if not isinstance(data, dict): |
| 68 |
raise Exception('config must be type `dict` not `%s`' % type(data)) |
| 69 |
|
| 70 |
# Configuration specific to modules/module_utils. |
| 71 |
modules = parse_modules_config(data.get('modules', {})) |
| 72 |
|
| 73 |
# Python versions supported by the controller, combined with Python versions supported by modules/module_utils. |
| 74 |
# Mainly used for display purposes and to limit the Python versions used for sanity tests. |
| 75 |
python_versions = tuple(version for version in SUPPORTED_PYTHON_VERSIONS |
| 76 |
if version in CONTROLLER_PYTHON_VERSIONS or version in modules.python_versions) |
| 77 |
|
| 78 |
return ContentConfig( |
| 79 |
modules=modules, |
| 80 |
python_versions=python_versions, |
| 81 |
) |
| 82 |
|
| 83 |
|
| 84 |
def load_config(path: str) -> t.Optional[ContentConfig]: |
| 85 |
"""Load and parse the specified config file and return the result or None if loading/parsing failed.""" |
| 86 |
if YAML_IMPORT_ERROR: |
| 87 |
raise ApplicationError('The "PyYAML" module is required to parse config: %s' % YAML_IMPORT_ERROR) |
| 88 |
|
| 89 |
if PACKAGING_IMPORT_ERROR: |
| 90 |
raise ApplicationError('The "packaging" module is required to parse config: %s' % PACKAGING_IMPORT_ERROR) |
| 91 |
|
| 92 |
value = read_text_file(path) |
| 93 |
|
| 94 |
try: |
| 95 |
yaml_value = yaml_load(value) |
| 96 |
except Exception as ex: # pylint: disable=broad-except |
| 97 |
display.warning('Ignoring config "%s" due to a YAML parsing error: %s' % (path, ex)) |
| 98 |
return None |
| 99 |
|
| 100 |
try: |
| 101 |
config = parse_content_config(yaml_value) |
| 102 |
except Exception as ex: # pylint: disable=broad-except |
| 103 |
display.warning('Ignoring config "%s" due a config parsing error: %s' % (path, ex)) |
| 104 |
return None |
| 105 |
|
| 106 |
display.info('Loaded configuration: %s' % path, verbosity=1) |
| 107 |
|
| 108 |
return config |
| 109 |
|
| 110 |
|
| 111 |
def get_content_config(args: EnvironmentConfig) -> ContentConfig: |
| 112 |
""" |
| 113 |
Parse and return the content configuration (if any) for the current collection. |
| 114 |
For ansible-core, a default configuration is used. |
| 115 |
Results are cached. |
| 116 |
""" |
| 117 |
if args.host_path: |
| 118 |
args.content_config = deserialize_content_config(os.path.join(args.host_path, 'config.dat')) |
| 119 |
|
| 120 |
if args.content_config: |
| 121 |
return args.content_config |
| 122 |
|
| 123 |
collection_config_path = 'tests/config.yml' |
| 124 |
|
| 125 |
config = None |
| 126 |
|
| 127 |
if data_context().content.collection and os.path.exists(collection_config_path): |
| 128 |
config = load_config(collection_config_path) |
| 129 |
|
| 130 |
if not config: |
| 131 |
config = parse_content_config(dict( |
| 132 |
modules=dict( |
| 133 |
python_requires='default', |
| 134 |
), |
| 135 |
)) |
| 136 |
|
| 137 |
if not config.modules.python_versions: |
| 138 |
raise ApplicationError('This collection does not declare support for modules/module_utils on any known Python version.\n' |
| 139 |
'Ansible supports modules/module_utils on Python versions: %s\n' |
| 140 |
'This collection provides the Python requirement: %s' % ( |
| 141 |
', '.join(SUPPORTED_PYTHON_VERSIONS), config.modules.python_requires)) |
| 142 |
|
| 143 |
args.content_config = config |
| 144 |
|
| 145 |
return config |
| 146 |
|
| 147 |
|
| 148 |
def parse_python_requires(value: t.Any) -> tuple[str, ...]: |
| 149 |
"""Parse the given 'python_requires' version specifier and return the matching Python versions.""" |
| 150 |
if not isinstance(value, str): |
| 151 |
raise ValueError('python_requires must must be of type `str` not type `%s`' % type(value)) |
| 152 |
|
| 153 |
versions: tuple[str, ...] |
| 154 |
|
| 155 |
if value == 'default': |
| 156 |
versions = SUPPORTED_PYTHON_VERSIONS |
| 157 |
elif value == 'controller': |
| 158 |
versions = CONTROLLER_PYTHON_VERSIONS |
| 159 |
else: |
| 160 |
specifier_set = SpecifierSet(value) |
| 161 |
versions = tuple(version for version in SUPPORTED_PYTHON_VERSIONS if specifier_set.contains(Version(version))) |
| 162 |
|
| 163 |
return versions |
| 164 |
|
| 165 |
|
| 166 |
def serialize_content_config(args: EnvironmentConfig, path: str) -> None: |
| 167 |
"""Serialize the content config to the given path. If the config has not been loaded, an empty config will be serialized.""" |
| 168 |
with open_binary_file(path, 'wb') as config_file: |
| 169 |
pickle.dump(args.content_config, config_file) |
| 170 |
|
| 171 |
|
| 172 |
def deserialize_content_config(path: str) -> ContentConfig: |
| 173 |
"""Deserialize content config from the path.""" |
| 174 |
with open_binary_file(path) as config_file: |
| 175 |
return pickle.load(config_file) |