From bc98b2aa3bcaf0484bd56b84febf45e90f5b1070 Mon Sep 17 00:00:00 2001 From: Alexei Znamensky <103110+russoz@users.noreply.github.com> Date: Tue, 31 Mar 2026 18:08:50 +1300 Subject: [PATCH] pipx, pipx_info: refactor (#11640) * pipx, pipx_info: refactor * add changelog frag --- changelogs/fragments/11640-pipx-refactor.yml | 4 ++++ plugins/module_utils/pipx.py | 9 +++++++- plugins/modules/pipx.py | 23 +++++++------------- plugins/modules/pipx_info.py | 9 +------- 4 files changed, 21 insertions(+), 24 deletions(-) create mode 100644 changelogs/fragments/11640-pipx-refactor.yml diff --git a/changelogs/fragments/11640-pipx-refactor.yml b/changelogs/fragments/11640-pipx-refactor.yml new file mode 100644 index 0000000000..b7d28a35df --- /dev/null +++ b/changelogs/fragments/11640-pipx-refactor.yml @@ -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). diff --git a/plugins/module_utils/pipx.py b/plugins/module_utils/pipx.py index 093a251ac2..5c8fad89e1 100644 --- a/plugins/module_utils/pipx.py +++ b/plugins/module_utils/pipx.py @@ -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, diff --git a/plugins/modules/pipx.py b/plugins/modules/pipx.py index 94919d00dd..108432d8b6 100644 --- a/plugins/modules/pipx.py +++ b/plugins/modules/pipx.py @@ -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: diff --git a/plugins/modules/pipx_info.py b/plugins/modules/pipx_info.py index b2ac1139ce..b315b2c00d 100644 --- a/plugins/modules/pipx_info.py +++ b/plugins/modules/pipx_info.py @@ -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()