Debian Codegraph @

ansible_collections/community/general/plugins/module_utils/_pipx.py

raw
1 # Copyright (c) 2022, Alexei Znamensky <[email protected]>
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 # Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time.
6 # Do not use this from other collections or standalone plugins/modules!
7
8 from __future__ import annotations
9
10 import json
11 import typing as t
12
13 from ansible.module_utils.facts.compat import ansible_facts
14
15 from ansible_collections.community.general.plugins.module_utils._cmd_runner import CmdRunner, cmd_runner_fmt
16
17 if t.TYPE_CHECKING:
18 from ansible.module_utils.basic import AnsibleModule
19
20
21 pipx_common_argspec = {
22 "global": dict(type="bool", default=False),
23 "executable": dict(type="path"),
24 }
25
26
27 _state_map = dict(
28 install="install",
29 install_all="install-all",
30 present="install",
31 uninstall="uninstall",
32 absent="uninstall",
33 uninstall_all="uninstall-all",
34 inject="inject",
35 uninject="uninject",
36 upgrade="upgrade",
37 upgrade_shared="upgrade-shared",
38 upgrade_all="upgrade-all",
39 reinstall="reinstall",
40 reinstall_all="reinstall-all",
41 pin="pin",
42 unpin="unpin",
43 )
44
45
46 def pipx_runner(module: AnsibleModule, executable, **kwargs) -> CmdRunner:
47 arg_formats = dict(
48 state=cmd_runner_fmt.as_map(_state_map),
49 name=cmd_runner_fmt.as_list(),
50 name_source=cmd_runner_fmt.as_func(cmd_runner_fmt.unpack_args(lambda n, s: [s] if s else [n])),
51 install_apps=cmd_runner_fmt.as_bool("--include-apps"),
52 install_deps=cmd_runner_fmt.as_bool("--include-deps"),
53 inject_packages=cmd_runner_fmt.as_list(),
54 force=cmd_runner_fmt.as_bool("--force"),
55 include_injected=cmd_runner_fmt.as_bool("--include-injected"),
56 index_url=cmd_runner_fmt.as_opt_val("--index-url"),
57 python=cmd_runner_fmt.as_opt_val("--python"),
58 system_site_packages=cmd_runner_fmt.as_bool("--system-site-packages"),
59 _list=cmd_runner_fmt.as_fixed(["list", "--include-injected", "--json"]),
60 editable=cmd_runner_fmt.as_bool("--editable"),
61 pip_args=cmd_runner_fmt.as_opt_eq_val("--pip-args"),
62 suffix=cmd_runner_fmt.as_opt_val("--suffix"),
63 spec_metadata=cmd_runner_fmt.as_list(),
64 version=cmd_runner_fmt.as_fixed("--version"),
65 )
66 arg_formats["global"] = cmd_runner_fmt.as_bool("--global")
67 if executable:
68 command = [executable]
69 else:
70 facts = ansible_facts(module, gather_subset=["python"])
71 command = [facts["python"]["executable"], "-m", "pipx"]
72
73 runner = CmdRunner(
74 module,
75 command=command,
76 arg_formats=arg_formats,
77 environ_update={"USE_EMOJI": "0", "PIPX_USE_EMOJI": "0"},
78 check_rc=True,
79 **kwargs,
80 )
81 return runner
82
83
84 def _make_entry(venv_name, venv, include_injected, include_deps):
85 entry = {
86 "name": venv_name,
87 "version": venv["metadata"]["main_package"]["package_version"],
88 "pinned": venv["metadata"]["main_package"].get("pinned"),
89 }
90 if include_injected:
91 entry["injected"] = {k: v["package_version"] for k, v in venv["metadata"]["injected_packages"].items()}
92 if include_deps:
93 entry["dependencies"] = list(venv["metadata"]["main_package"]["app_paths_of_dependencies"])
94 return entry
95
96
97 def make_process_dict(include_injected, include_deps=False):
98 def process_dict(rc, out, err):
99 if not out:
100 return {}
101
102 results = {}
103 raw_data = json.loads(out)
104 for venv_name, venv in raw_data["venvs"].items():
105 results[venv_name] = _make_entry(venv_name, venv, include_injected, include_deps)
106
107 return results, raw_data
108
109 return process_dict