pipx, pipx_info: refactor (#11640)

* pipx, pipx_info: refactor

* add changelog frag
This commit is contained in:
Alexei Znamensky
2026-03-31 18:08:50 +13:00
committed by GitHub
parent 8568594453
commit bc98b2aa3b
4 changed files with 21 additions and 24 deletions

View File

@@ -0,0 +1,4 @@
minor_changes:
- pipx module utils - small refactor, no behavior affected (https://github.com/ansible-collections/community.general/pull/11640).
- pipx - small refactor, no behavior affected (https://github.com/ansible-collections/community.general/pull/11640).
- pipx_info - small refactor, no behavior affected (https://github.com/ansible-collections/community.general/pull/11640).

View File

@@ -7,6 +7,8 @@ from __future__ import annotations
import json
import typing as t
from ansible.module_utils.facts.compat import ansible_facts
from ansible_collections.community.general.plugins.module_utils.cmd_runner import CmdRunner, cmd_runner_fmt
if t.TYPE_CHECKING:
@@ -38,7 +40,7 @@ _state_map = dict(
)
def pipx_runner(module: AnsibleModule, command, **kwargs) -> CmdRunner:
def pipx_runner(module: AnsibleModule, executable, **kwargs) -> CmdRunner:
arg_formats = dict(
state=cmd_runner_fmt.as_map(_state_map),
name=cmd_runner_fmt.as_list(),
@@ -59,6 +61,11 @@ def pipx_runner(module: AnsibleModule, command, **kwargs) -> CmdRunner:
version=cmd_runner_fmt.as_fixed("--version"),
)
arg_formats["global"] = cmd_runner_fmt.as_bool("--global")
if executable:
command = [executable]
else:
facts = ansible_facts(module, gather_subset=["python"])
command = [facts["python"]["executable"], "-m", "pipx"]
runner = CmdRunner(
module,

View File

@@ -211,8 +211,6 @@ version:
"""
from ansible.module_utils.facts.compat import ansible_facts
from ansible_collections.community.general.plugins.module_utils.module_helper import StateModuleHelper
from ansible_collections.community.general.plugins.module_utils.pipx import (
make_process_dict,
@@ -223,7 +221,7 @@ from ansible_collections.community.general.plugins.module_utils.pkg_req import P
from ansible_collections.community.general.plugins.module_utils.version import LooseVersion
def _make_name(name, suffix):
def make_installed_name(name, suffix):
return name if suffix is None else f"{name}{suffix}"
@@ -300,17 +298,12 @@ class PipX(StateModuleHelper):
return {k: v for k, v in installed.items() if k == self.app_name}
def __init_module__(self):
if self.vars.executable:
self.command = [self.vars.executable]
else:
facts = ansible_facts(self.module, gather_subset=["python"])
self.command = [facts["python"]["executable"], "-m", "pipx"]
self.runner = pipx_runner(self.module, self.command)
self.runner = pipx_runner(self.module, self.vars.executable)
pkg_req = PackageRequirement(self.module, self.vars.name)
self.parsed_name = pkg_req.parsed_name
self.parsed_req = pkg_req.requirement
self.app_name = _make_name(self.parsed_name, self.vars.suffix)
self.app_name = make_installed_name(self.parsed_name, self.vars.suffix)
self.vars.set("application", self._retrieve_installed(), change=True, diff=True)
@@ -371,7 +364,7 @@ class PipX(StateModuleHelper):
self._capture_results(ctx)
def state_upgrade(self):
name = _make_name(self.vars.name, self.vars.suffix)
name = make_installed_name(self.vars.name, self.vars.suffix)
if not self.vars.application:
self.do_raise(f"Trying to upgrade a non-existent application: {name}")
if self.vars.force:
@@ -385,7 +378,7 @@ class PipX(StateModuleHelper):
def state_uninstall(self):
if self.vars.application:
name = _make_name(self.vars.name, self.vars.suffix)
name = make_installed_name(self.vars.name, self.vars.suffix)
with self.runner("state global name", check_mode_skip=True) as ctx:
ctx.run(name=name)
self._capture_results(ctx)
@@ -393,7 +386,7 @@ class PipX(StateModuleHelper):
state_absent = state_uninstall
def state_reinstall(self):
name = _make_name(self.vars.name, self.vars.suffix)
name = make_installed_name(self.vars.name, self.vars.suffix)
if not self.vars.application:
self.do_raise(f"Trying to reinstall a non-existent application: {name}")
self.changed = True
@@ -402,7 +395,7 @@ class PipX(StateModuleHelper):
self._capture_results(ctx)
def state_inject(self):
name = _make_name(self.vars.name, self.vars.suffix)
name = make_installed_name(self.vars.name, self.vars.suffix)
if not self.vars.application:
self.do_raise(f"Trying to inject packages into a non-existent application: {name}")
if self.vars.force:
@@ -415,7 +408,7 @@ class PipX(StateModuleHelper):
self._capture_results(ctx)
def state_uninject(self):
name = _make_name(self.vars.name, self.vars.suffix)
name = make_installed_name(self.vars.name, self.vars.suffix)
if not self.vars.application:
self.do_raise(f"Trying to uninject packages into a non-existent application: {name}")
with self.runner("state global name inject_packages", check_mode_skip=True) as ctx:

View File

@@ -130,8 +130,6 @@ version:
version_added: 10.1.0
"""
from ansible.module_utils.facts.compat import ansible_facts
from ansible_collections.community.general.plugins.module_utils.module_helper import ModuleHelper
from ansible_collections.community.general.plugins.module_utils.pipx import (
make_process_dict,
@@ -156,12 +154,7 @@ class PipXInfo(ModuleHelper):
)
def __init_module__(self):
if self.vars.executable:
self.command = [self.vars.executable]
else:
facts = ansible_facts(self.module, gather_subset=["python"])
self.command = [facts["python"]["executable"], "-m", "pipx"]
self.runner = pipx_runner(self.module, self.command)
self.runner = pipx_runner(self.module, self.vars.executable)
with self.runner("version") as ctx:
rc, out, err = ctx.run()
self.vars.version = out.strip()