10
10
import re
11
11
import typing as t
12
12
13
- import sh
14
13
from antsibull_core .logging import log
14
+ from antsibull_core .subprocess_util import CalledProcessError
15
15
from antsibull_core .vendored .json_utils import _filter_non_json_lines
16
16
from packaging .version import Version as PypiVer
17
17
@@ -64,38 +64,37 @@ def parse_ansible_galaxy_collection_list(json_output: t.Mapping[str, t.Any],
64
64
return result
65
65
66
66
67
- def _call_ansible_version (
67
+ async def _call_ansible_version (
68
68
venv : t .Union ['VenvRunner' , 'FakeVenvRunner' ],
69
- env : t .Dict [str , str ],
69
+ env : t .Optional [ t . Dict [str , str ] ],
70
70
) -> str :
71
- venv_ansible = venv .get_command ('ansible' )
72
- ansible_version_cmd = venv_ansible ('--version' , _env = env )
73
- return ansible_version_cmd .stdout .decode ('utf-8' , errors = 'surrogateescape' )
71
+ p = await venv .async_log_run (['ansible' , '--version' ], env = env )
72
+ return p .stdout
74
73
75
74
76
- def _call_ansible_galaxy_collection_list (
75
+ async def _call_ansible_galaxy_collection_list (
77
76
venv : t .Union ['VenvRunner' , 'FakeVenvRunner' ],
78
77
env : t .Dict [str , str ],
79
78
) -> t .Mapping [str , t .Any ]:
80
- venv_ansible_galaxy = venv .get_command ( 'ansible-galaxy' )
81
- ansible_collection_list_cmd = venv_ansible_galaxy (
82
- 'collection' , 'list' , '--format' , 'json' , _env = env )
83
- stdout = ansible_collection_list_cmd . stdout . decode ( 'utf-8' , errors = 'surrogateescape' )
84
- return json .loads (_filter_non_json_lines (stdout )[0 ])
79
+ p = await venv .async_log_run (
80
+ [ 'ansible-galaxy' , 'collection' , 'list' , '--format' , 'json' ],
81
+ env = env ,
82
+ )
83
+ return json .loads (_filter_non_json_lines (p . stdout )[0 ])
85
84
86
85
87
- def get_collection_metadata (venv : t .Union ['VenvRunner' , 'FakeVenvRunner' ],
88
- env : t .Dict [str , str ],
89
- collection_names : t .Optional [t .List [str ]] = None ,
90
- ) -> t .Dict [str , AnsibleCollectionMetadata ]:
86
+ async def get_collection_metadata (venv : t .Union ['VenvRunner' , 'FakeVenvRunner' ],
87
+ env : t .Dict [str , str ],
88
+ collection_names : t .Optional [t .List [str ]] = None ,
89
+ ) -> t .Dict [str , AnsibleCollectionMetadata ]:
91
90
collection_metadata = {}
92
91
93
92
# Obtain ansible.builtin version and path
94
- raw_result = _call_ansible_version (venv , env )
93
+ raw_result = await _call_ansible_version (venv , env )
95
94
collection_metadata ['ansible.builtin' ] = _extract_ansible_builtin_metadata (raw_result )
96
95
97
96
# Obtain collection versions
98
- json_result = _call_ansible_galaxy_collection_list (venv , env )
97
+ json_result = await _call_ansible_galaxy_collection_list (venv , env )
99
98
collection_list = parse_ansible_galaxy_collection_list (json_result , collection_names )
100
99
for namespace , name , path , version in collection_list :
101
100
collection_name = f'{ namespace } .{ name } '
@@ -105,28 +104,26 @@ def get_collection_metadata(venv: t.Union['VenvRunner', 'FakeVenvRunner'],
105
104
return collection_metadata
106
105
107
106
108
- def get_ansible_core_version (venv : t .Union ['VenvRunner' , 'FakeVenvRunner' ],
109
- env : t .Optional [t .Dict [str , str ]] = None ,
110
- ) -> PypiVer :
111
- try :
112
- venv_python = venv .get_command ('python' )
113
- ansible_version_cmd = venv_python (
114
- '-c' , 'import ansible.release; print(ansible.release.__version__)' , _env = env )
115
- output = ansible_version_cmd .stdout .decode ('utf-8' , errors = 'surrogateescape' ).strip ()
107
+ async def get_ansible_core_version (venv : t .Union ['VenvRunner' , 'FakeVenvRunner' ],
108
+ env : t .Optional [t .Dict [str , str ]] = None ,
109
+ ) -> PypiVer :
110
+ p = await venv .async_log_run (
111
+ ['python' , '-c' , 'import ansible.release; print(ansible.release.__version__)' ],
112
+ env = env ,
113
+ check = False ,
114
+ )
115
+ output = p .stdout .strip ()
116
+ if p .returncode == 0 and output :
116
117
return PypiVer (output )
117
- except sh .ErrorReturnCode :
118
- pass
119
118
120
119
try :
121
120
# Fallback: use `ansible --version`
122
- venv_ansible = venv .get_command ('ansible' )
123
- ansible_version_cmd = venv_ansible ('--version' , _env = env )
124
- raw_result = ansible_version_cmd .stdout .decode ('utf-8' , errors = 'surrogateescape' )
121
+ raw_result = await _call_ansible_version (venv , env )
125
122
metadata = _extract_ansible_builtin_metadata (raw_result )
126
123
if metadata .version is None :
127
124
raise ValueError ('Cannot retrieve ansible-core version from `ansible --version`' )
128
125
return PypiVer (metadata .version )
129
- except sh . ErrorReturnCode as exc :
126
+ except CalledProcessError as exc :
130
127
raise ValueError (
131
128
f'Cannot retrieve ansible-core version from `ansible --version`: { exc } '
132
129
) from exc
0 commit comments