1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/plugins/modules/python_requirements_info.py

216 lines
6.2 KiB
Python
Raw Normal View History

2020-03-09 10:11:07 +01:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
2020-03-09 10:11:07 +01:00
# Copyright (c) 2018 Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
2020-03-09 10:11:07 +01:00
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
DOCUMENTATION = '''
module: python_requirements_info
short_description: Show python path and assert dependency versions
description:
- Get info about available Python requirements on the target host, including listing required libraries and gathering versions.
- This module was called C(python_requirements_facts) before Ansible 2.9. The usage did not change.
extends_documentation_fragment:
- community.general.attributes
- community.general.attributes.info_module
2020-03-09 10:11:07 +01:00
options:
dependencies:
Enabling validation-modules for system modules (#1212) * fixed validation-modules for aix_devices.py * fixed validation-modules for aix_filesystem.py * fixed validation-modules for aix_inittab.py * fixed validation-modules for aix_lvg.py * fixed validation-modules for aix_lvol.py * fixed validation-modules for awall.py * fixed validation-modules for dconf.py * fixed validation-modules for gconftool2.py * fixed validation-modules for interfaces_file.py * fixed validation-modules for java_keystore.py * fixed validation-modules for kernel_blacklist.py * fixed validation-modules for plugins/modules/system/lbu.py * fixed validation-modules for plugins/modules/system/locale_gen.py * fixed validation-modules for plugins/modules/system/lvg.py * fixed validation-modules for plugins/modules/system/lvol.py * fixed validation-modules for plugins/modules/system/mksysb.py * fixed validation-modules for plugins/modules/system/modprobe.py * fixed validation-modules for plugins/modules/system/nosh.py * fixed validation-modules for plugins/modules/system/open_iscsi.py * fixed validation-modules for plugins/modules/system/openwrt_init.py * fixed validation-modules for plugins/modules/system/osx_defaults.py * fixed validation-modules for plugins/modules/system/pamd.py * fixed validation-modules for plugins/modules/system/pam_limits.py * fixed validation-modules for plugins/modules/system/parted.py * fixed validation-modules for plugins/modules/system/puppet.py * fixed validation-modules for plugins/modules/system/python_requirements_info.py * fixed validation-modules for plugins/modules/system/runit.py the parameter "dist" is not used anywhere in the module * fixed validation-modules for plugins/modules/system/sefcontext.py * fixed validation-modules for plugins/modules/system/selogin.py * fixed validation-modules for plugins/modules/system/seport.py * fixed validation-modules for plugins/modules/system/solaris_zone.py * fixed validation-modules for plugins/modules/system/syspatch.py * fixed validation-modules for plugins/modules/system/vdo.py * fixed validation-modules for plugins/modules/system/xfconf.py * removed ignore almost all validate-modules lines in system * removed unnecessary validations, per shippable test * kernel_blacklist: keeping blacklist_file as str instead of path * mksysb: keeping storage_path as str instead of path * pam_limits: keeping dest as str instead of path * rollback on adding doc for puppet.py legacy param * rolledback param seuser required in selogin module * rolledback changes in runit * rolledback changes in osx_defaults * rolledback changes in aix_defaults
2020-11-04 09:02:50 +01:00
type: list
elements: str
2020-03-09 10:11:07 +01:00
description: >
A list of version-likes or module names to check for installation.
Supported operators: <, >, <=, >=, or ==. The bare module name like
V(ansible), the module with a specific version like V(boto3==1.6.1), or a
partial version like V(requests>2) are all valid specifications.
default: []
2020-03-09 10:11:07 +01:00
author:
- Will Thames (@willthames)
- Ryan Scott Brown (@ryansb)
2020-03-09 10:11:07 +01:00
'''
EXAMPLES = '''
2020-05-16 15:07:51 +02:00
- name: Show python lib/site paths
community.general.python_requirements_info:
2020-05-16 15:07:51 +02:00
- name: Check for modern boto3 and botocore versions
community.general.python_requirements_info:
2020-03-09 10:11:07 +01:00
dependencies:
- boto3>1.6
- botocore<2
2020-03-09 10:11:07 +01:00
'''
RETURN = '''
python:
description: path to python version used
returned: always
type: str
sample: /usr/local/opt/python@2/bin/python2.7
python_version:
description: version of python
returned: always
type: str
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)]"
python_version_info:
description: breakdown version of python
returned: always
type: dict
contains:
major:
description: The C(major) component of the python interpreter version.
returned: always
type: int
sample: 3
minor:
description: The C(minor) component of the python interpreter version.
returned: always
type: int
sample: 8
micro:
description: The C(micro) component of the python interpreter version.
returned: always
type: int
sample: 10
releaselevel:
description: The C(releaselevel) component of the python interpreter version.
returned: always
type: str
sample: final
serial:
description: The C(serial) component of the python interpreter version.
returned: always
type: int
sample: 0
version_added: 4.2.0
2020-03-09 10:11:07 +01:00
python_system_path:
description: List of paths python is looking for modules in
returned: always
type: list
sample:
- /usr/local/opt/python@2/site-packages/
- /usr/lib/python/site-packages/
2020-03-09 10:11:07 +01:00
valid:
description: A dictionary of dependencies that matched their desired versions. If no version was specified, then RV(ignore:desired) will be null
2020-03-09 10:11:07 +01:00
returned: always
type: dict
sample:
boto3:
desired: null
installed: 1.7.60
botocore:
desired: botocore<2
installed: 1.10.60
mismatched:
description: A dictionary of dependencies that did not satisfy the desired version
returned: always
type: dict
sample:
botocore:
desired: botocore>2
installed: 1.10.60
not_found:
description: A list of packages that could not be imported at all, and are not installed
returned: always
type: list
sample:
- boto4
- requests
2020-03-09 10:11:07 +01:00
'''
import re
import sys
import operator
HAS_DISTUTILS = False
try:
import pkg_resources
from ansible_collections.community.general.plugins.module_utils.version import LooseVersion
2020-03-09 10:11:07 +01:00
HAS_DISTUTILS = True
except ImportError:
pass
from ansible.module_utils.basic import AnsibleModule
operations = {
'<=': operator.le,
'>=': operator.ge,
'<': operator.lt,
'>': operator.gt,
'==': operator.eq,
}
python_version_info = dict(
major=sys.version_info[0],
minor=sys.version_info[1],
micro=sys.version_info[2],
releaselevel=sys.version_info[3],
serial=sys.version_info[4],
)
2020-03-09 10:11:07 +01:00
def main():
module = AnsibleModule(
argument_spec=dict(
dependencies=dict(type='list', elements='str', default=[])
2020-03-09 10:11:07 +01:00
),
supports_check_mode=True,
)
if not HAS_DISTUTILS:
module.fail_json(
msg='Could not import "distutils" and "pkg_resources" libraries to introspect python environment.',
python=sys.executable,
python_version=sys.version,
python_version_info=python_version_info,
2020-03-09 10:11:07 +01:00
python_system_path=sys.path,
)
pkg_dep_re = re.compile(r'(^[a-zA-Z][a-zA-Z0-9_-]+)(?:(==|[><]=?)([0-9.]+))?$')
2020-03-09 10:11:07 +01:00
results = dict(
not_found=[],
mismatched={},
valid={},
)
for dep in module.params['dependencies']:
2020-03-09 10:11:07 +01:00
match = pkg_dep_re.match(dep)
if not match:
2020-03-09 10:11:07 +01:00
module.fail_json(msg="Failed to parse version requirement '{0}'. Must be formatted like 'ansible>2.6'".format(dep))
pkg, op, version = match.groups()
if op is not None and op not in operations:
module.fail_json(msg="Failed to parse version requirement '{0}'. Operator must be one of >, <, <=, >=, or ==".format(dep))
try:
existing = pkg_resources.get_distribution(pkg).version
except pkg_resources.DistributionNotFound:
# not there
results['not_found'].append(pkg)
continue
if op is None and version is None:
results['valid'][pkg] = {
'installed': existing,
'desired': None,
}
elif operations[op](LooseVersion(existing), LooseVersion(version)):
results['valid'][pkg] = {
'installed': existing,
'desired': dep,
}
else:
results['mismatched'][pkg] = {
2020-03-09 10:11:07 +01:00
'installed': existing,
'desired': dep,
}
module.exit_json(
python=sys.executable,
python_version=sys.version,
python_version_info=python_version_info,
2020-03-09 10:11:07 +01:00
python_system_path=sys.path,
**results
)
if __name__ == '__main__':
main()