|
4 | 4 | import logging
|
5 | 5 | import os
|
6 | 6 | import random
|
| 7 | +import re |
7 | 8 | import shutil
|
8 | 9 | import string
|
9 | 10 | import subprocess
|
@@ -126,14 +127,14 @@ def env_get_or_fail_with(env_name, err_msg):
|
126 | 127 | raise RuntimeError(err_msg)
|
127 | 128 |
|
128 | 129 |
|
129 |
| -def one_of_commands_exists(commands, exc_msg): |
| 130 | +def one_of_commands_exists(commands, exc_msg) -> str: |
130 | 131 | """
|
131 | 132 | Verify that the provided command exists. Raise CommandDoesNotExistException in case of an
|
132 | 133 | error or if the command does not exist.
|
133 | 134 |
|
134 | 135 | :param commands: str, command to check (python 3 only)
|
135 | 136 | :param exc_msg: str, message of exception when command does not exist
|
136 |
| - :return: bool, True if everything's all right (otherwise exception is thrown) |
| 137 | + :return: str, the command which exists |
137 | 138 | """
|
138 | 139 | found = False
|
139 | 140 | for command in commands:
|
@@ -233,3 +234,29 @@ def random_str(size=10):
|
233 | 234 | :return: the string
|
234 | 235 | """
|
235 | 236 | return ''.join(random.choice(string.ascii_lowercase) for _ in range(size))
|
| 237 | + |
| 238 | + |
| 239 | +def is_ansibles_python_2(ap_exe: str) -> bool: |
| 240 | + """ |
| 241 | + Discover whether ansible-playbook is using python 2. |
| 242 | +
|
| 243 | + :param ap_exe: path to the python executable |
| 244 | +
|
| 245 | + :return: True if it's 2 |
| 246 | + """ |
| 247 | + out = run_cmd([ap_exe, "--version"], log_stderr=True, return_output=True) |
| 248 | + |
| 249 | + # python version = 3.7.2 |
| 250 | + reg = r"python version = (\d)" |
| 251 | + |
| 252 | + reg_grp = re.findall(reg, out) |
| 253 | + try: |
| 254 | + py_version = reg_grp[0] |
| 255 | + except IndexError: |
| 256 | + logger.warning("could not figure out which python is %s using", ap_exe) |
| 257 | + return False # we don't know, fingers crossed |
| 258 | + if py_version == "2": |
| 259 | + logger.info("%s is using python 2", ap_exe) |
| 260 | + return True |
| 261 | + logger.debug("it seems that %s is not using python 2", ap_exe) |
| 262 | + return False |
0 commit comments