pipx/pipx_info: multiple fixes (#9044)

* pipx_info: factored process_list out

* pipx_info: no need to pass param to _list

* pipx_info: minor adjustment

* pipx mod utils: make_process_list parameters

* fix test for state=install_all

* fix assertions

* pipx tests: fix detection of pipx 1.7.0

* pipx: use make_process_output

* add testcase

* pipx: remove import json

* pinned in pipx list is not always there

* Update plugins/modules/pipx_info.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* remove ensurepath and --user from pipx install

* add changelog frag

* Update changelogs/fragments/9044-pipx-fixes.yml

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update changelogs/fragments/9044-pipx-fixes.yml

* Update changelogs/fragments/9044-pipx-fixes.yml

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update changelogs/fragments/9044-pipx-fixes.yml

Co-authored-by: Felix Fontein <felix@fontein.de>

---------

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Alexei Znamensky 2024-11-03 05:50:24 +13:00 committed by GitHub
commit 2429e228a4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 165 additions and 176 deletions

View file

@ -6,6 +6,10 @@
from __future__ import absolute_import, division, print_function
__metaclass__ = type
import json
from ansible_collections.community.general.plugins.module_utils.cmd_runner import CmdRunner, cmd_runner_fmt as fmt
@ -51,6 +55,7 @@ def pipx_runner(module, command, **kwargs):
editable=fmt.as_bool("--editable"),
pip_args=fmt.as_opt_eq_val('--pip-args'),
suffix=fmt.as_opt_val('--suffix'),
spec_metadata=fmt.as_list(),
)
arg_formats["global"] = fmt.as_bool("--global")
@ -63,3 +68,38 @@ def pipx_runner(module, command, **kwargs):
**kwargs
)
return runner
def make_process_list(mod_helper, **kwargs):
def process_list(rc, out, err):
if not out:
return []
results = []
raw_data = json.loads(out)
if kwargs.get("include_raw"):
mod_helper.vars.raw_output = raw_data
if kwargs["name"]:
if kwargs["name"] in raw_data['venvs']:
data = {kwargs["name"]: raw_data['venvs'][kwargs["name"]]}
else:
data = {}
else:
data = raw_data['venvs']
for venv_name, venv in data.items():
entry = {
'name': venv_name,
'version': venv['metadata']['main_package']['package_version'],
'pinned': venv['metadata']['main_package'].get('pinned'),
}
if kwargs.get("include_injected"):
entry['injected'] = {k: v['package_version'] for k, v in venv['metadata']['injected_packages'].items()}
if kwargs.get("include_deps"):
entry['dependencies'] = list(venv['metadata']['main_package']['app_paths_of_dependencies'])
results.append(entry)
return results
return process_list

View file

@ -191,10 +191,8 @@ EXAMPLES = """
"""
import json
from ansible_collections.community.general.plugins.module_utils.module_helper import StateModuleHelper
from ansible_collections.community.general.plugins.module_utils.pipx import pipx_runner, pipx_common_argspec
from ansible_collections.community.general.plugins.module_utils.pipx import pipx_runner, pipx_common_argspec, make_process_list
from ansible.module_utils.facts.compat import ansible_facts
@ -251,26 +249,14 @@ class PipX(StateModuleHelper):
use_old_vardict = False
def _retrieve_installed(self):
def process_list(rc, out, err):
if not out:
return {}
name = _make_name(self.vars.name, self.vars.suffix)
output_process = make_process_list(self, include_injected=True, name=name)
installed = self.runner('_list global', output_process=output_process).run()
results = {}
raw_data = json.loads(out)
for venv_name, venv in raw_data['venvs'].items():
results[venv_name] = {
'version': venv['metadata']['main_package']['package_version'],
'injected': {k: v['package_version'] for k, v in venv['metadata']['injected_packages'].items()},
}
return results
installed = self.runner('_list', output_process=process_list).run(_list=1)
if self.vars.name is not None:
name = _make_name(self.vars.name, self.vars.suffix)
app_list = installed.get(name)
if name is not None:
app_list = [app for app in installed if app['name'] == name]
if app_list:
return {name: app_list}
return {name: app_list[0]}
else:
return {}

View file

@ -98,6 +98,15 @@ application:
type: dict
sample:
licenses: "0.6.1"
pinned:
description:
- Whether the installed application is pinned or not.
- When using C(pipx<=1.6.0), this returns C(null).
returned: success
type: bool
sample:
pinned: true
version_added: 10.0.0
raw_output:
description: The raw output of the C(pipx list) command, when O(include_raw=true). Used for debugging.
@ -112,10 +121,8 @@ cmd:
sample: ["/usr/bin/python3.10", "-m", "pipx", "list", "--include-injected", "--json"]
"""
import json
from ansible_collections.community.general.plugins.module_utils.module_helper import ModuleHelper
from ansible_collections.community.general.plugins.module_utils.pipx import pipx_runner, pipx_common_argspec
from ansible_collections.community.general.plugins.module_utils.pipx import pipx_runner, pipx_common_argspec, make_process_list
from ansible.module_utils.facts.compat import ansible_facts
@ -143,41 +150,10 @@ class PipXInfo(ModuleHelper):
self.command = [facts['python']['executable'], '-m', 'pipx']
self.runner = pipx_runner(self.module, self.command)
# self.vars.set('application', self._retrieve_installed(), change=True, diff=True)
def __run__(self):
def process_list(rc, out, err):
if not out:
return []
results = []
raw_data = json.loads(out)
if self.vars.include_raw:
self.vars.raw_output = raw_data
if self.vars.name:
if self.vars.name in raw_data['venvs']:
data = {self.vars.name: raw_data['venvs'][self.vars.name]}
else:
data = {}
else:
data = raw_data['venvs']
for venv_name, venv in data.items():
entry = {
'name': venv_name,
'version': venv['metadata']['main_package']['package_version']
}
if self.vars.include_injected:
entry['injected'] = {k: v['package_version'] for k, v in venv['metadata']['injected_packages'].items()}
if self.vars.include_deps:
entry['dependencies'] = list(venv['metadata']['main_package']['app_paths_of_dependencies'])
results.append(entry)
return results
with self.runner('_list global', output_process=process_list) as ctx:
self.vars.application = ctx.run(_list=1)
output_process = make_process_list(self, **self.vars.as_dict())
with self.runner('_list global', output_process=output_process) as ctx:
self.vars.application = ctx.run()
self._capture_results(ctx)
def _capture_results(self, ctx):