Debian Codegraph @

test/lib/ansible_test/_internal/cli/parsers/helpers.py

raw
1 """Helper functions for composite parsers."""
2
3 from __future__ import annotations
4
5 from ...constants import (
6 CONTROLLER_PYTHON_VERSIONS,
7 SUPPORTED_PYTHON_VERSIONS,
8 )
9
10 from ...completion import (
11 docker_completion,
12 remote_completion,
13 filter_completion,
14 )
15
16 from ...host_configs import (
17 DockerConfig,
18 HostConfig,
19 PosixRemoteConfig,
20 )
21
22
23 def get_docker_pythons(name: str, controller: bool, strict: bool) -> list[str]:
24 """Return a list of docker instance Python versions supported by the specified host config."""
25 image_config = filter_completion(docker_completion()).get(name)
26 available_pythons = CONTROLLER_PYTHON_VERSIONS if controller else SUPPORTED_PYTHON_VERSIONS
27
28 if not image_config:
29 return [] if strict else list(available_pythons)
30
31 supported_pythons = [python for python in image_config.supported_pythons if python in available_pythons]
32
33 return supported_pythons
34
35
36 def get_remote_pythons(name: str, controller: bool, strict: bool) -> list[str]:
37 """Return a list of remote instance Python versions supported by the specified host config."""
38 platform_config = filter_completion(remote_completion()).get(name)
39 available_pythons = CONTROLLER_PYTHON_VERSIONS if controller else SUPPORTED_PYTHON_VERSIONS
40
41 if not platform_config:
42 return [] if strict else list(available_pythons)
43
44 supported_pythons = [python for python in platform_config.supported_pythons if python in available_pythons]
45
46 return supported_pythons
47
48
49 def get_controller_pythons(controller_config: HostConfig, strict: bool) -> list[str]:
50 """Return a list of controller Python versions supported by the specified host config."""
51 if isinstance(controller_config, DockerConfig):
52 pythons = get_docker_pythons(controller_config.name, False, strict)
53 elif isinstance(controller_config, PosixRemoteConfig):
54 pythons = get_remote_pythons(controller_config.name, False, strict)
55 else:
56 pythons = list(SUPPORTED_PYTHON_VERSIONS)
57
58 return pythons