| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2018 Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
module: python_requirements_info |
| 10 |
short_description: Show python path and assert dependency versions |
| 11 |
description: |
| 12 |
- Get info about available Python requirements on the target host, including listing required libraries and gathering versions. |
| 13 |
extends_documentation_fragment: |
| 14 |
- community.general._attributes |
| 15 |
- community.general._attributes.info_module |
| 16 |
options: |
| 17 |
dependencies: |
| 18 |
type: list |
| 19 |
elements: str |
| 20 |
description: |
| 21 |
- 'A list of version-likes or module names to check for installation. Supported operators: C(<), C(>), C(<=), C(>=), |
| 22 |
or C(==).' |
| 23 |
- The bare module name like V(ansible), the module with a specific version like V(boto3==1.6.1), or a partial version |
| 24 |
like V(requests>2) are all valid specifications. |
| 25 |
default: [] |
| 26 |
author: |
| 27 |
- Will Thames (@willthames) |
| 28 |
- Ryan Scott Brown (@ryansb) |
| 29 |
""" |
| 30 |
|
| 31 |
EXAMPLES = r""" |
| 32 |
- name: Show python lib/site paths |
| 33 |
community.general.python_requirements_info: |
| 34 |
|
| 35 |
- name: Check for modern boto3 and botocore versions |
| 36 |
community.general.python_requirements_info: |
| 37 |
dependencies: |
| 38 |
- boto3>1.6 |
| 39 |
- botocore<2 |
| 40 |
""" |
| 41 |
|
| 42 |
RETURN = r""" |
| 43 |
python: |
| 44 |
description: Path to the Python interpreter used. |
| 45 |
returned: always |
| 46 |
type: str |
| 47 |
sample: /usr/local/opt/python@2/bin/python2.7 |
| 48 |
python_version: |
| 49 |
description: Version of Python. |
| 50 |
returned: always |
| 51 |
type: str |
| 52 |
sample: "2.7.15 (default, May 1 2018, 16:44:08)\n[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.1)]" |
| 53 |
python_version_info: |
| 54 |
description: Breakdown version of Python. |
| 55 |
returned: always |
| 56 |
type: dict |
| 57 |
contains: |
| 58 |
major: |
| 59 |
description: The C(major) component of the python interpreter version. |
| 60 |
returned: always |
| 61 |
type: int |
| 62 |
sample: 3 |
| 63 |
minor: |
| 64 |
description: The C(minor) component of the python interpreter version. |
| 65 |
returned: always |
| 66 |
type: int |
| 67 |
sample: 8 |
| 68 |
micro: |
| 69 |
description: The C(micro) component of the python interpreter version. |
| 70 |
returned: always |
| 71 |
type: int |
| 72 |
sample: 10 |
| 73 |
releaselevel: |
| 74 |
description: The C(releaselevel) component of the python interpreter version. |
| 75 |
returned: always |
| 76 |
type: str |
| 77 |
sample: final |
| 78 |
serial: |
| 79 |
description: The C(serial) component of the python interpreter version. |
| 80 |
returned: always |
| 81 |
type: int |
| 82 |
sample: 0 |
| 83 |
version_added: 4.2.0 |
| 84 |
python_system_path: |
| 85 |
description: List of paths Python is looking for modules in. |
| 86 |
returned: always |
| 87 |
type: list |
| 88 |
sample: |
| 89 |
- /usr/local/opt/python@2/site-packages/ |
| 90 |
- /usr/lib/python/site-packages/ |
| 91 |
valid: |
| 92 |
description: A dictionary of dependencies that matched their desired versions. If no version was specified, then RV(ignore:desired) |
| 93 |
is V(null). |
| 94 |
returned: always |
| 95 |
type: dict |
| 96 |
sample: |
| 97 |
boto3: |
| 98 |
desired: |
| 99 |
installed: 1.7.60 |
| 100 |
botocore: |
| 101 |
desired: botocore<2 |
| 102 |
installed: 1.10.60 |
| 103 |
mismatched: |
| 104 |
description: A dictionary of dependencies that did not satisfy the desired version. |
| 105 |
returned: always |
| 106 |
type: dict |
| 107 |
sample: |
| 108 |
botocore: |
| 109 |
desired: botocore>2 |
| 110 |
installed: 1.10.60 |
| 111 |
not_found: |
| 112 |
description: A list of packages that could not be imported at all, and are not installed. |
| 113 |
returned: always |
| 114 |
type: list |
| 115 |
sample: |
| 116 |
- boto4 |
| 117 |
- requests |
| 118 |
""" |
| 119 |
|
| 120 |
import operator |
| 121 |
import re |
| 122 |
import sys |
| 123 |
|
| 124 |
from ansible.module_utils.basic import AnsibleModule |
| 125 |
|
| 126 |
from ansible_collections.community.general.plugins.module_utils._version import LooseVersion |
| 127 |
|
| 128 |
HAS_IMPORTLIB_METADATA = False |
| 129 |
try: |
| 130 |
import importlib.metadata |
| 131 |
|
| 132 |
HAS_IMPORTLIB_METADATA = True |
| 133 |
except ImportError: |
| 134 |
pass |
| 135 |
|
| 136 |
HAS_DISTUTILS = False |
| 137 |
try: |
| 138 |
import pkg_resources |
| 139 |
|
| 140 |
HAS_DISTUTILS = True |
| 141 |
except ImportError: |
| 142 |
pass |
| 143 |
|
| 144 |
operations = { |
| 145 |
"<=": operator.le, |
| 146 |
">=": operator.ge, |
| 147 |
"<": operator.lt, |
| 148 |
">": operator.gt, |
| 149 |
"==": operator.eq, |
| 150 |
} |
| 151 |
|
| 152 |
python_version_info = dict( |
| 153 |
major=sys.version_info[0], |
| 154 |
minor=sys.version_info[1], |
| 155 |
micro=sys.version_info[2], |
| 156 |
releaselevel=sys.version_info[3], |
| 157 |
serial=sys.version_info[4], |
| 158 |
) |
| 159 |
|
| 160 |
|
| 161 |
def main(): |
| 162 |
module = AnsibleModule( |
| 163 |
argument_spec=dict(dependencies=dict(type="list", elements="str", default=[])), |
| 164 |
supports_check_mode=True, |
| 165 |
) |
| 166 |
if not HAS_DISTUTILS and not HAS_IMPORTLIB_METADATA: |
| 167 |
module.fail_json( |
| 168 |
msg='Could not import "pkg_resources" or "importlib.metadata" libraries to introspect Python environment.', |
| 169 |
python=sys.executable, |
| 170 |
python_version=sys.version, |
| 171 |
python_version_info=python_version_info, |
| 172 |
python_system_path=sys.path, |
| 173 |
) |
| 174 |
pkg_dep_re = re.compile(r"(^[a-zA-Z][a-zA-Z0-9_-]+)(?:(==|[><]=?)([0-9.]+))?$") |
| 175 |
|
| 176 |
results = dict( |
| 177 |
not_found=[], |
| 178 |
mismatched={}, |
| 179 |
valid={}, |
| 180 |
) |
| 181 |
|
| 182 |
for dep in module.params["dependencies"]: |
| 183 |
match = pkg_dep_re.match(dep) |
| 184 |
if not match: |
| 185 |
module.fail_json(msg=f"Failed to parse version requirement '{dep}'. Must be formatted like 'ansible>2.6'") |
| 186 |
pkg, op, version = match.groups() |
| 187 |
if op is not None and op not in operations: |
| 188 |
module.fail_json( |
| 189 |
msg=f"Failed to parse version requirement '{dep}'. Operator must be one of >, <, <=, >=, or ==" |
| 190 |
) |
| 191 |
if HAS_DISTUTILS: |
| 192 |
try: |
| 193 |
existing = pkg_resources.get_distribution(pkg).version |
| 194 |
except pkg_resources.DistributionNotFound: |
| 195 |
# not there |
| 196 |
results["not_found"].append(pkg) |
| 197 |
continue |
| 198 |
else: |
| 199 |
try: |
| 200 |
existing = importlib.metadata.version(pkg) |
| 201 |
except importlib.metadata.PackageNotFoundError: |
| 202 |
# not there |
| 203 |
results["not_found"].append(pkg) |
| 204 |
continue |
| 205 |
if op is None and version is None: |
| 206 |
results["valid"][pkg] = { |
| 207 |
"installed": existing, |
| 208 |
"desired": None, |
| 209 |
} |
| 210 |
elif operations[op](LooseVersion(existing), LooseVersion(version)): |
| 211 |
results["valid"][pkg] = { |
| 212 |
"installed": existing, |
| 213 |
"desired": dep, |
| 214 |
} |
| 215 |
else: |
| 216 |
results["mismatched"][pkg] = { |
| 217 |
"installed": existing, |
| 218 |
"desired": dep, |
| 219 |
} |
| 220 |
|
| 221 |
module.exit_json( |
| 222 |
python=sys.executable, |
| 223 |
python_version=sys.version, |
| 224 |
python_version_info=python_version_info, |
| 225 |
python_system_path=sys.path, |
| 226 |
**results, |
| 227 |
) |
| 228 |
|
| 229 |
|
| 230 |
if __name__ == "__main__": |
| 231 |
main() |