Debian Codegraph @

ansible_collections/community/general/plugins/modules/pipx_info.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2021, Alexei Znamensky <[email protected]>
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: pipx_info
11 short_description: Rretrieves information about applications installed with pipx
12 version_added: 5.6.0
13 description:
14 - Retrieve details about Python applications installed in isolated virtualenvs using pipx.
15 extends_documentation_fragment:
16 - community.general._attributes
17 - community.general._attributes.info_module
18 - community.general._pipx
19 options:
20 name:
21 description:
22 - Name of an application installed with C(pipx).
23 type: str
24 include_deps:
25 description:
26 - Include dependent packages in the output.
27 type: bool
28 default: false
29 include_injected:
30 description:
31 - Include injected packages in the output.
32 type: bool
33 default: false
34 include_raw:
35 description:
36 - Returns the raw output of C(pipx list --json).
37 - The raw output is not affected by O(include_deps) or O(include_injected).
38 type: bool
39 default: false
40 global:
41 version_added: 9.3.0
42 author:
43 - "Alexei Znamensky (@russoz)"
44 """
45
46 EXAMPLES = r"""
47 - name: retrieve all installed applications
48 community.general.pipx_info: {}
49
50 - name: retrieve all installed applications, include dependencies and injected packages
51 community.general.pipx_info:
52 include_deps: true
53 include_injected: true
54
55 - name: retrieve application tox
56 community.general.pipx_info:
57 name: tox
58 include_deps: true
59
60 - name: retrieve application ansible-lint, include dependencies
61 community.general.pipx_info:
62 name: ansible-lint
63 include_deps: true
64 """
65
66 RETURN = r"""
67 application:
68 description: The list of installed applications.
69 returned: success
70 type: list
71 elements: dict
72 contains:
73 name:
74 description: The name of the installed application.
75 returned: success
76 type: str
77 sample: "tox"
78 version:
79 description: The version of the installed application.
80 returned: success
81 type: str
82 sample: "3.24.0"
83 dependencies:
84 description: The dependencies of the installed application, when O(include_deps=true).
85 returned: success
86 type: list
87 elements: str
88 sample: ["virtualenv"]
89 injected:
90 description: The injected packages for the installed application, when O(include_injected=true).
91 returned: success
92 type: dict
93 sample:
94 licenses: "0.6.1"
95 pinned:
96 description:
97 - Whether the installed application is pinned or not.
98 - When using C(pipx<=1.6.0), this returns C(null).
99 returned: success
100 type: bool
101 sample:
102 pinned: true
103 version_added: 10.0.0
104
105 raw_output:
106 description: The raw output of the C(pipx list) command, when O(include_raw=true). Used for debugging.
107 returned: success
108 type: dict
109
110 cmd:
111 description: Command executed to obtain the list of installed applications.
112 returned: success
113 type: list
114 elements: str
115 sample:
116 [
117 "/usr/bin/python3.10",
118 "-m",
119 "pipx",
120 "list",
121 "--include-injected",
122 "--json"
123 ]
124
125 version:
126 description: Version of pipx.
127 type: str
128 returned: always
129 sample: "1.7.1"
130 version_added: 10.1.0
131 """
132
133 from ansible_collections.community.general.plugins.module_utils._module_helper import ModuleHelper
134 from ansible_collections.community.general.plugins.module_utils._pipx import (
135 make_process_dict,
136 pipx_common_argspec,
137 pipx_runner,
138 )
139 from ansible_collections.community.general.plugins.module_utils._version import LooseVersion
140
141
142 class PipXInfo(ModuleHelper):
143 output_params = ["name"]
144 argument_spec = dict(
145 name=dict(type="str"),
146 include_deps=dict(type="bool", default=False),
147 include_injected=dict(type="bool", default=False),
148 include_raw=dict(type="bool", default=False),
149 )
150 argument_spec.update(pipx_common_argspec)
151 module = dict(
152 argument_spec=argument_spec,
153 supports_check_mode=True,
154 )
155
156 def __init_module__(self):
157 self.runner = pipx_runner(self.module, self.vars.executable)
158 with self.runner("version") as ctx:
159 rc, out, err = ctx.run()
160 self.vars.version = out.strip()
161
162 if LooseVersion(self.vars.version) < LooseVersion("1.7.0"):
163 self.do_raise("The pipx tool must be at least at version 1.7.0")
164
165 def __run__(self):
166 output_process = make_process_dict(self.vars.include_injected, self.vars.include_deps)
167 with self.runner("_list global", output_process=output_process) as ctx:
168 applications, raw_data = ctx.run()
169 if self.vars.include_raw:
170 self.vars.raw_output = raw_data
171
172 if self.vars.name:
173 self.vars.application = [v for k, v in applications.items() if k == self.vars.name]
174 else:
175 self.vars.application = list(applications.values())
176 self._capture_results(ctx)
177
178 def _capture_results(self, ctx):
179 self.vars.cmd = ctx.cmd
180 if self.verbosity >= 4:
181 self.vars.run_info = ctx.run_info
182
183
184 def main():
185 PipXInfo.execute()
186
187
188 if __name__ == "__main__":
189 main()