mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 13:52:54 +00:00
pipx, pipx_info: refactor (#11640)
* pipx, pipx_info: refactor * add changelog frag
This commit is contained in:
4
changelogs/fragments/11640-pipx-refactor.yml
Normal file
4
changelogs/fragments/11640-pipx-refactor.yml
Normal 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).
|
||||||
@@ -7,6 +7,8 @@ from __future__ import annotations
|
|||||||
import json
|
import json
|
||||||
import typing as t
|
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
|
from ansible_collections.community.general.plugins.module_utils.cmd_runner import CmdRunner, cmd_runner_fmt
|
||||||
|
|
||||||
if t.TYPE_CHECKING:
|
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(
|
arg_formats = dict(
|
||||||
state=cmd_runner_fmt.as_map(_state_map),
|
state=cmd_runner_fmt.as_map(_state_map),
|
||||||
name=cmd_runner_fmt.as_list(),
|
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"),
|
version=cmd_runner_fmt.as_fixed("--version"),
|
||||||
)
|
)
|
||||||
arg_formats["global"] = cmd_runner_fmt.as_bool("--global")
|
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(
|
runner = CmdRunner(
|
||||||
module,
|
module,
|
||||||
|
|||||||
@@ -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.module_helper import StateModuleHelper
|
||||||
from ansible_collections.community.general.plugins.module_utils.pipx import (
|
from ansible_collections.community.general.plugins.module_utils.pipx import (
|
||||||
make_process_dict,
|
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
|
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}"
|
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}
|
return {k: v for k, v in installed.items() if k == self.app_name}
|
||||||
|
|
||||||
def __init_module__(self):
|
def __init_module__(self):
|
||||||
if self.vars.executable:
|
self.runner = pipx_runner(self.module, 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)
|
|
||||||
|
|
||||||
pkg_req = PackageRequirement(self.module, self.vars.name)
|
pkg_req = PackageRequirement(self.module, self.vars.name)
|
||||||
self.parsed_name = pkg_req.parsed_name
|
self.parsed_name = pkg_req.parsed_name
|
||||||
self.parsed_req = pkg_req.requirement
|
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)
|
self.vars.set("application", self._retrieve_installed(), change=True, diff=True)
|
||||||
|
|
||||||
@@ -371,7 +364,7 @@ class PipX(StateModuleHelper):
|
|||||||
self._capture_results(ctx)
|
self._capture_results(ctx)
|
||||||
|
|
||||||
def state_upgrade(self):
|
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:
|
if not self.vars.application:
|
||||||
self.do_raise(f"Trying to upgrade a non-existent application: {name}")
|
self.do_raise(f"Trying to upgrade a non-existent application: {name}")
|
||||||
if self.vars.force:
|
if self.vars.force:
|
||||||
@@ -385,7 +378,7 @@ class PipX(StateModuleHelper):
|
|||||||
|
|
||||||
def state_uninstall(self):
|
def state_uninstall(self):
|
||||||
if self.vars.application:
|
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:
|
with self.runner("state global name", check_mode_skip=True) as ctx:
|
||||||
ctx.run(name=name)
|
ctx.run(name=name)
|
||||||
self._capture_results(ctx)
|
self._capture_results(ctx)
|
||||||
@@ -393,7 +386,7 @@ class PipX(StateModuleHelper):
|
|||||||
state_absent = state_uninstall
|
state_absent = state_uninstall
|
||||||
|
|
||||||
def state_reinstall(self):
|
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:
|
if not self.vars.application:
|
||||||
self.do_raise(f"Trying to reinstall a non-existent application: {name}")
|
self.do_raise(f"Trying to reinstall a non-existent application: {name}")
|
||||||
self.changed = True
|
self.changed = True
|
||||||
@@ -402,7 +395,7 @@ class PipX(StateModuleHelper):
|
|||||||
self._capture_results(ctx)
|
self._capture_results(ctx)
|
||||||
|
|
||||||
def state_inject(self):
|
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:
|
if not self.vars.application:
|
||||||
self.do_raise(f"Trying to inject packages into a non-existent application: {name}")
|
self.do_raise(f"Trying to inject packages into a non-existent application: {name}")
|
||||||
if self.vars.force:
|
if self.vars.force:
|
||||||
@@ -415,7 +408,7 @@ class PipX(StateModuleHelper):
|
|||||||
self._capture_results(ctx)
|
self._capture_results(ctx)
|
||||||
|
|
||||||
def state_uninject(self):
|
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:
|
if not self.vars.application:
|
||||||
self.do_raise(f"Trying to uninject packages into a non-existent application: {name}")
|
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:
|
with self.runner("state global name inject_packages", check_mode_skip=True) as ctx:
|
||||||
|
|||||||
@@ -130,8 +130,6 @@ version:
|
|||||||
version_added: 10.1.0
|
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.module_helper import ModuleHelper
|
||||||
from ansible_collections.community.general.plugins.module_utils.pipx import (
|
from ansible_collections.community.general.plugins.module_utils.pipx import (
|
||||||
make_process_dict,
|
make_process_dict,
|
||||||
@@ -156,12 +154,7 @@ class PipXInfo(ModuleHelper):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def __init_module__(self):
|
def __init_module__(self):
|
||||||
if self.vars.executable:
|
self.runner = pipx_runner(self.module, 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)
|
|
||||||
with self.runner("version") as ctx:
|
with self.runner("version") as ctx:
|
||||||
rc, out, err = ctx.run()
|
rc, out, err = ctx.run()
|
||||||
self.vars.version = out.strip()
|
self.vars.version = out.strip()
|
||||||
|
|||||||
Reference in New Issue
Block a user