|
| 1 | +# Author: Felix Fontein <[email protected]> |
| 2 | +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or |
| 3 | +# https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 | +# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 | +# SPDX-FileCopyrightText: 2022, Ansible Project |
| 6 | +"""Environment variable handling.""" |
| 7 | + |
| 8 | +import os |
| 9 | +import os.path |
| 10 | +import typing as t |
| 11 | + |
| 12 | +from antsibull_core import yaml |
| 13 | + |
| 14 | +from .docs_parsing import AnsibleCollectionMetadata |
| 15 | + |
| 16 | + |
| 17 | +class EnvironmentVariableInfo: |
| 18 | + name: str |
| 19 | + description: t.Optional[t.List[str]] |
| 20 | + plugins: t.Dict[str, t.List[str]] # maps plugin_type to lists of plugin FQCNs |
| 21 | + |
| 22 | + def __init__(self, |
| 23 | + name: str, |
| 24 | + description: t.Optional[t.List[str]] = None, |
| 25 | + plugins: t.Optional[t.Dict[str, t.List[str]]] = None): |
| 26 | + self.name = name |
| 27 | + self.description = description |
| 28 | + self.plugins = plugins or {} |
| 29 | + |
| 30 | + def __repr__(self): |
| 31 | + return f'E({self.name}, description={repr(self.description)}, plugins={self.plugins})' |
| 32 | + |
| 33 | + |
| 34 | +def load_ansible_config(ansible_builtin_metadata: AnsibleCollectionMetadata |
| 35 | + ) -> t.Mapping[str, t.Mapping[str, t.Any]]: |
| 36 | + """ |
| 37 | + Load Ansible base configuration (``lib/ansible/config/base.yml``). |
| 38 | +
|
| 39 | + :arg ansible_builtin_metadata: Metadata for the ansible.builtin collection. |
| 40 | + :returns: A Mapping of configuration options to information on these options. |
| 41 | + """ |
| 42 | + return yaml.load_yaml_file(os.path.join(ansible_builtin_metadata.path, 'config', 'base.yml')) |
| 43 | + |
| 44 | + |
| 45 | +def _find_env_vars(options: t.Mapping[str, t.Mapping[str, t.Any]] |
| 46 | + ) -> t.Generator[t.Tuple[str, t.Optional[t.List[str]]], None, None]: |
| 47 | + for _, option_data in options.items(): |
| 48 | + if isinstance(option_data.get('env'), list): |
| 49 | + description = option_data.get('description') |
| 50 | + if isinstance(description, str): |
| 51 | + description = [description] |
| 52 | + if isinstance(description, list): |
| 53 | + description = [str(desc) for desc in description] |
| 54 | + else: |
| 55 | + description = None |
| 56 | + for env_var in option_data['env']: |
| 57 | + if isinstance(env_var.get('name'), str): |
| 58 | + yield (env_var['name'], description) |
| 59 | + if isinstance(option_data.get('suboptions'), dict): |
| 60 | + yield from _find_env_vars(option_data['suboptions']) |
| 61 | + |
| 62 | + |
| 63 | +def _collect_env_vars_and_descriptions(plugin_info: t.Mapping[str, t.Mapping[str, t.Any]], |
| 64 | + core_envs: t.Set[str], |
| 65 | + ) -> t.Tuple[t.Mapping[str, EnvironmentVariableInfo], |
| 66 | + t.Mapping[str, t.List[t.List[str]]]]: |
| 67 | + other_variables: t.Dict[str, EnvironmentVariableInfo] = {} |
| 68 | + other_variable_description: t.Dict[str, t.List[t.List[str]]] = {} |
| 69 | + for plugin_type, plugins in plugin_info.items(): |
| 70 | + for plugin_name, plugin_data in plugins.items(): |
| 71 | + plugin_options: t.Mapping[str, t.Mapping[str, t.Any]] = ( |
| 72 | + (plugin_data.get('doc') or {}).get('options') or {} |
| 73 | + ) |
| 74 | + for env_var, env_var_description in _find_env_vars(plugin_options): |
| 75 | + if env_var in core_envs: |
| 76 | + continue |
| 77 | + if env_var not in other_variables: |
| 78 | + other_variables[env_var] = EnvironmentVariableInfo(env_var) |
| 79 | + other_variable_description[env_var] = [] |
| 80 | + if plugin_type not in other_variables[env_var].plugins: |
| 81 | + other_variables[env_var].plugins[plugin_type] = [] |
| 82 | + other_variables[env_var].plugins[plugin_type].append(plugin_name) |
| 83 | + if env_var_description is not None: |
| 84 | + other_variable_description[env_var].append(env_var_description) |
| 85 | + return other_variables, other_variable_description |
| 86 | + |
| 87 | + |
| 88 | +def _augment_env_var_descriptions(other_variables: t.Mapping[str, EnvironmentVariableInfo], |
| 89 | + other_variable_description: t.Mapping[str, t.List[t.List[str]]], |
| 90 | + ) -> None: |
| 91 | + for variable, variable_info in other_variables.items(): |
| 92 | + if other_variable_description[variable]: |
| 93 | + value: t.Optional[t.List[str]] = other_variable_description[variable][0] |
| 94 | + for other_value in other_variable_description[variable]: |
| 95 | + if value != other_value: |
| 96 | + value = [ |
| 97 | + 'See the documentations for the options where this environment variable' |
| 98 | + ' is used.' |
| 99 | + ] |
| 100 | + break |
| 101 | + variable_info.description = value |
| 102 | + |
| 103 | + |
| 104 | +def collect_referenced_environment_variables(plugin_info: t.Mapping[str, t.Mapping[str, t.Any]], |
| 105 | + ansible_config: t.Mapping[str, t.Mapping[str, t.Any]], |
| 106 | + ) -> t.Mapping[str, EnvironmentVariableInfo]: |
| 107 | + """ |
| 108 | + Collect referenced environment variables that are not defined in the ansible-core |
| 109 | + configuration. |
| 110 | +
|
| 111 | + :arg plugin_info: Mapping of plugin type to a mapping of plugin name to plugin record. |
| 112 | + :arg ansible_config: The Ansible base configuration (``lib/ansible/config/base.yml``). |
| 113 | + :returns: A Mapping of environment variable name to an environment variable infomation object. |
| 114 | + """ |
| 115 | + core_envs = {'ANSIBLE_CONFIG'} |
| 116 | + for config in ansible_config.values(): |
| 117 | + if config.get('env'): |
| 118 | + for env in config['env']: |
| 119 | + core_envs.add(env['name']) |
| 120 | + |
| 121 | + other_variables, other_variable_description = _collect_env_vars_and_descriptions( |
| 122 | + plugin_info, core_envs) |
| 123 | + _augment_env_var_descriptions(other_variables, other_variable_description) |
| 124 | + return other_variables |
0 commit comments