| 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 |
# started out with AWX's scan_packages module |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
DOCUMENTATION = r""" |
| 11 |
module: pip_package_info |
| 12 |
short_description: Pip package information |
| 13 |
description: |
| 14 |
- Return information about installed pip packages. |
| 15 |
extends_documentation_fragment: |
| 16 |
- community.general._attributes |
| 17 |
- community.general._attributes.info_module |
| 18 |
options: |
| 19 |
clients: |
| 20 |
description: |
| 21 |
- A list of the pip executables that are used to get the packages. They can be supplied with the full path or just the |
| 22 |
executable name, for example V(pip3.7). |
| 23 |
default: ['pip'] |
| 24 |
type: list |
| 25 |
elements: path |
| 26 |
requirements: |
| 27 |
- pip >= 20.3b1 (necessary for the C(--format) option) |
| 28 |
- The requested C(pip) executables must be installed on the target. |
| 29 |
author: |
| 30 |
- Matthew Jones (@matburt) |
| 31 |
- Brian Coca (@bcoca) |
| 32 |
- Adam Miller (@maxamillion) |
| 33 |
""" |
| 34 |
|
| 35 |
EXAMPLES = r""" |
| 36 |
- name: Just get the list from default pip |
| 37 |
community.general.pip_package_info: |
| 38 |
|
| 39 |
- name: Get the facts for default pip, pip2 and pip3.6 |
| 40 |
community.general.pip_package_info: |
| 41 |
clients: ['pip', 'pip2', 'pip3.6'] |
| 42 |
|
| 43 |
- name: Get from specific paths (virtualenvs?) |
| 44 |
community.general.pip_package_info: |
| 45 |
clients: '/home/me/projec42/python/pip3.5' |
| 46 |
""" |
| 47 |
|
| 48 |
RETURN = r""" |
| 49 |
packages: |
| 50 |
description: A dictionary of installed package data. |
| 51 |
returned: always |
| 52 |
type: dict |
| 53 |
contains: |
| 54 |
python: |
| 55 |
description: A dictionary with each pip client which then contains a list of dicts with python package information. |
| 56 |
returned: always |
| 57 |
type: dict |
| 58 |
sample: |
| 59 |
{ |
| 60 |
"packages": { |
| 61 |
"pip": { |
| 62 |
"Babel": [ |
| 63 |
{ |
| 64 |
"name": "Babel", |
| 65 |
"source": "pip", |
| 66 |
"version": "2.6.0" |
| 67 |
} |
| 68 |
], |
| 69 |
"Flask": [ |
| 70 |
{ |
| 71 |
"name": "Flask", |
| 72 |
"source": "pip", |
| 73 |
"version": "1.0.2" |
| 74 |
} |
| 75 |
], |
| 76 |
"Flask-SQLAlchemy": [ |
| 77 |
{ |
| 78 |
"name": "Flask-SQLAlchemy", |
| 79 |
"source": "pip", |
| 80 |
"version": "2.3.2" |
| 81 |
} |
| 82 |
], |
| 83 |
"Jinja2": [ |
| 84 |
{ |
| 85 |
"name": "Jinja2", |
| 86 |
"source": "pip", |
| 87 |
"version": "2.10" |
| 88 |
} |
| 89 |
] |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
""" |
| 94 |
|
| 95 |
import json |
| 96 |
import os |
| 97 |
|
| 98 |
from ansible.module_utils.basic import AnsibleModule |
| 99 |
from ansible.module_utils.facts.packages import CLIMgr |
| 100 |
|
| 101 |
|
| 102 |
class PIP(CLIMgr): |
| 103 |
def __init__(self, pip, module): |
| 104 |
self.CLI = pip |
| 105 |
self.module = module |
| 106 |
|
| 107 |
def list_installed(self): |
| 108 |
rc, out, err = self.module.run_command([self._cli, "list", "-l", "--format=json"]) |
| 109 |
if rc != 0: |
| 110 |
raise Exception(f"Unable to list packages rc={rc} : {err}") |
| 111 |
return json.loads(out) |
| 112 |
|
| 113 |
def get_package_details(self, package): |
| 114 |
package["source"] = self.CLI |
| 115 |
return package |
| 116 |
|
| 117 |
|
| 118 |
def main(): |
| 119 |
# start work |
| 120 |
module = AnsibleModule( |
| 121 |
argument_spec=dict( |
| 122 |
clients=dict(type="list", elements="path", default=["pip"]), |
| 123 |
), |
| 124 |
supports_check_mode=True, |
| 125 |
) |
| 126 |
module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"} |
| 127 |
packages = {} |
| 128 |
results = {"packages": {}} |
| 129 |
clients = module.params["clients"] |
| 130 |
|
| 131 |
found = 0 |
| 132 |
for pip in clients: |
| 133 |
if not os.path.basename(pip).startswith("pip"): |
| 134 |
module.warn(f"Skipping invalid pip client: {pip}") |
| 135 |
continue |
| 136 |
try: |
| 137 |
pip_mgr = PIP(pip, module) |
| 138 |
if pip_mgr.is_available(): |
| 139 |
found += 1 |
| 140 |
packages[pip] = pip_mgr.get_packages() |
| 141 |
except Exception as e: |
| 142 |
module.warn(f"Failed to retrieve packages with {pip}: {e}") |
| 143 |
continue |
| 144 |
|
| 145 |
if found == 0: |
| 146 |
module.fail_json(msg=f"Unable to use any of the supplied pip clients: {clients}") |
| 147 |
|
| 148 |
# return info |
| 149 |
results["packages"] = packages |
| 150 |
module.exit_json(**results) |
| 151 |
|
| 152 |
|
| 153 |
if __name__ == "__main__": |
| 154 |
main() |