2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
2021-08-08 10:40:22 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2022-08-05 22:45:15 +02:00
|
|
|
# Copyright (c) 2018, Ansible Project
|
2022-08-05 13:17:19 +02:00
|
|
|
# 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
|
|
|
|
|
|
|
# started out with AWX's scan_packages module
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
module: pip_package_info
|
2022-11-09 14:08:55 +01:00
|
|
|
short_description: Pip package information
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Return information about installed pip packages
|
|
|
|
options:
|
|
|
|
clients:
|
|
|
|
description:
|
|
|
|
- A list of the pip executables that will be used to get the packages.
|
2022-06-22 22:54:08 +02:00
|
|
|
They can be supplied with the full path or just the executable name, for example C(pip3.7).
|
2020-03-09 10:11:07 +01:00
|
|
|
default: ['pip']
|
2022-09-06 21:07:46 +02:00
|
|
|
required: false
|
2020-03-09 10:11:07 +01:00
|
|
|
type: list
|
2021-02-26 11:37:23 +01:00
|
|
|
elements: path
|
2020-03-09 10:11:07 +01:00
|
|
|
requirements:
|
|
|
|
- The requested pip executables must be installed on the target.
|
|
|
|
author:
|
|
|
|
- Matthew Jones (@matburt)
|
|
|
|
- Brian Coca (@bcoca)
|
|
|
|
- Adam Miller (@maxamillion)
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: Just get the list from default pip
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.pip_package_info:
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-05-16 15:07:51 +02:00
|
|
|
- name: Get the facts for default pip, pip2 and pip3.6
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.pip_package_info:
|
2020-03-09 10:11:07 +01:00
|
|
|
clients: ['pip', 'pip2', 'pip3.6']
|
|
|
|
|
2020-05-16 15:07:51 +02:00
|
|
|
- name: Get from specific paths (virtualenvs?)
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.pip_package_info:
|
2020-03-09 10:11:07 +01:00
|
|
|
clients: '/home/me/projec42/python/pip3.5'
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
packages:
|
|
|
|
description: a dictionary of installed package data
|
|
|
|
returned: always
|
|
|
|
type: dict
|
|
|
|
contains:
|
|
|
|
python:
|
|
|
|
description: A dictionary with each pip client which then contains a list of dicts with python package information
|
|
|
|
returned: always
|
|
|
|
type: dict
|
|
|
|
sample:
|
|
|
|
"packages": {
|
|
|
|
"pip": {
|
|
|
|
"Babel": [
|
|
|
|
{
|
|
|
|
"name": "Babel",
|
|
|
|
"source": "pip",
|
|
|
|
"version": "2.6.0"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"Flask": [
|
|
|
|
{
|
|
|
|
"name": "Flask",
|
|
|
|
"source": "pip",
|
|
|
|
"version": "1.0.2"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"Flask-SQLAlchemy": [
|
|
|
|
{
|
|
|
|
"name": "Flask-SQLAlchemy",
|
|
|
|
"source": "pip",
|
|
|
|
"version": "2.3.2"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"Jinja2": [
|
|
|
|
{
|
|
|
|
"name": "Jinja2",
|
|
|
|
"source": "pip",
|
|
|
|
"version": "2.10"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
'''
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_text
|
2020-03-09 10:11:07 +01:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils.facts.packages import CLIMgr
|
|
|
|
|
|
|
|
|
|
|
|
class PIP(CLIMgr):
|
|
|
|
|
2022-08-12 12:33:02 +02:00
|
|
|
def __init__(self, pip, module):
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
self.CLI = pip
|
2022-08-12 12:33:02 +02:00
|
|
|
self.module = module
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
def list_installed(self):
|
2022-08-12 12:33:02 +02:00
|
|
|
rc, out, err = self.module.run_command([self._cli, 'list', '-l', '--format=json'])
|
2020-03-09 10:11:07 +01:00
|
|
|
if rc != 0:
|
|
|
|
raise Exception("Unable to list packages rc=%s : %s" % (rc, err))
|
|
|
|
return json.loads(out)
|
|
|
|
|
|
|
|
def get_package_details(self, package):
|
|
|
|
package['source'] = self.CLI
|
|
|
|
return package
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
# start work
|
2021-02-26 11:37:23 +01:00
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
clients=dict(type='list', elements='path', default=['pip']),
|
|
|
|
),
|
|
|
|
supports_check_mode=True)
|
2020-03-09 10:11:07 +01:00
|
|
|
packages = {}
|
|
|
|
results = {'packages': {}}
|
|
|
|
clients = module.params['clients']
|
|
|
|
|
|
|
|
found = 0
|
|
|
|
for pip in clients:
|
|
|
|
|
|
|
|
if not os.path.basename(pip).startswith('pip'):
|
|
|
|
module.warn('Skipping invalid pip client: %s' % (pip))
|
|
|
|
continue
|
|
|
|
try:
|
2022-08-12 12:33:02 +02:00
|
|
|
pip_mgr = PIP(pip, module)
|
2020-03-09 10:11:07 +01:00
|
|
|
if pip_mgr.is_available():
|
|
|
|
found += 1
|
|
|
|
packages[pip] = pip_mgr.get_packages()
|
|
|
|
except Exception as e:
|
|
|
|
module.warn('Failed to retrieve packages with %s: %s' % (pip, to_text(e)))
|
|
|
|
continue
|
|
|
|
|
|
|
|
if found == 0:
|
|
|
|
module.fail_json(msg='Unable to use any of the supplied pip clients: %s' % clients)
|
|
|
|
|
|
|
|
# return info
|
|
|
|
results['packages'] = packages
|
|
|
|
module.exit_json(**results)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|