summaryrefslogtreecommitdiffstats
path: root/src/ansible_compat/config.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-03 13:38:37 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-03 13:38:37 +0000
commitedb762c8af1ace09545c11f1342e8fe1abdae3b8 (patch)
tree642cd160bb22cc28e6672c1baea60a49476d71b2 /src/ansible_compat/config.py
parentReleasing progress-linux version 24.5.1-1~progress7.99u1. (diff)
downloadpython-ansible-compat-edb762c8af1ace09545c11f1342e8fe1abdae3b8.tar.xz
python-ansible-compat-edb762c8af1ace09545c11f1342e8fe1abdae3b8.zip
Merging upstream version 24.6.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/ansible_compat/config.py')
-rw-r--r--src/ansible_compat/config.py63
1 files changed, 40 insertions, 23 deletions
diff --git a/src/ansible_compat/config.py b/src/ansible_compat/config.py
index 6bed01b..9435540 100644
--- a/src/ansible_compat/config.py
+++ b/src/ansible_compat/config.py
@@ -8,7 +8,7 @@ import os
import re
import subprocess
from collections import UserDict
-from typing import Literal
+from typing import TYPE_CHECKING, Literal
from packaging.version import Version
@@ -16,6 +16,9 @@ from ansible_compat.constants import ANSIBLE_MIN_VERSION
from ansible_compat.errors import InvalidPrerequisiteError, MissingAnsibleError
from ansible_compat.ports import cache
+if TYPE_CHECKING:
+ from pathlib import Path
+
# do not use lru_cache here, as environment can change between calls
def ansible_collections_path() -> str:
@@ -397,35 +400,49 @@ class AnsibleConfig(UserDict[str, object]): # pylint: disable=too-many-ancestor
self,
config_dump: str | None = None,
data: dict[str, object] | None = None,
+ cache_dir: Path | None = None,
) -> None:
"""Load config dictionary."""
super().__init__()
+ self.cache_dir = cache_dir
if data:
self.data = copy.deepcopy(data)
- return
-
- if not config_dump:
- env = os.environ.copy()
- # Avoid possible ANSI garbage
- env["ANSIBLE_FORCE_COLOR"] = "0"
- config_dump = subprocess.check_output(
- ["ansible-config", "dump"], # noqa: S603
- universal_newlines=True,
- env=env,
- )
+ else:
+ if not config_dump:
+ env = os.environ.copy()
+ # Avoid possible ANSI garbage
+ env["ANSIBLE_FORCE_COLOR"] = "0"
+ config_dump = subprocess.check_output(
+ ["ansible-config", "dump"], # noqa: S603
+ universal_newlines=True,
+ env=env,
+ )
- for match in re.finditer(
- r"^(?P<key>[A-Za-z0-9_]+).* = (?P<value>.*)$",
- config_dump,
- re.MULTILINE,
- ):
- key = match.groupdict()["key"]
- value = match.groupdict()["value"]
- try:
- self[key] = ast.literal_eval(value)
- except (NameError, SyntaxError, ValueError):
- self[key] = value
+ for match in re.finditer(
+ r"^(?P<key>[A-Za-z0-9_]+).* = (?P<value>.*)$",
+ config_dump,
+ re.MULTILINE,
+ ):
+ key = match.groupdict()["key"]
+ value = match.groupdict()["value"]
+ try:
+ self[key] = ast.literal_eval(value)
+ except (NameError, SyntaxError, ValueError):
+ self[key] = value
+ # inject isolation collections paths into the config
+ if self.cache_dir:
+ cpaths = self.data["COLLECTIONS_PATHS"]
+ if cpaths and isinstance(cpaths, list):
+ cpaths.insert(
+ 0,
+ f"{self.cache_dir}/collections",
+ )
+ else: # pragma: no cover
+ msg = f"Unexpected data type for COLLECTIONS_PATHS: {cpaths}"
+ raise RuntimeError(msg)
+ if data:
+ return
def __getattribute__(self, attr_name: str) -> object:
"""Allow access of config options as attributes."""