2021-10-06 07:01:54 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 22:12:10 +02:00
|
|
|
# Copyright (c) 2021, Alexei Znamensky <russoz@gmail.com>
|
2022-08-05 12:28:29 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2021-10-06 07:01:54 +02:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: pipx
|
|
|
|
short_description: Manages applications installed with pipx
|
|
|
|
version_added: 3.8.0
|
|
|
|
description:
|
|
|
|
- Manage Python applications installed in isolated virtualenvs using pipx.
|
2023-02-24 09:25:20 +01:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.attributes
|
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: full
|
|
|
|
diff_mode:
|
|
|
|
support: full
|
2021-10-06 07:01:54 +02:00
|
|
|
options:
|
|
|
|
state:
|
|
|
|
type: str
|
2022-08-18 06:44:18 +02:00
|
|
|
choices: [present, absent, install, uninstall, uninstall_all, inject, upgrade, upgrade_all, reinstall, reinstall_all, latest]
|
2021-10-06 07:01:54 +02:00
|
|
|
default: install
|
|
|
|
description:
|
|
|
|
- Desired state for the application.
|
|
|
|
- The states C(present) and C(absent) are aliases to C(install) and C(uninstall), respectively.
|
2022-08-18 06:44:18 +02:00
|
|
|
- The state C(latest) is equivalent to executing the task twice, with state C(install) and then C(upgrade).
|
|
|
|
It was added in community.general 5.5.0.
|
2021-10-06 07:01:54 +02:00
|
|
|
name:
|
|
|
|
type: str
|
|
|
|
description:
|
|
|
|
- >
|
|
|
|
The name of the application to be installed. It must to be a simple package name.
|
|
|
|
For passing package specifications or installing from URLs or directories,
|
|
|
|
please use the I(source) option.
|
|
|
|
source:
|
|
|
|
type: str
|
|
|
|
description:
|
|
|
|
- >
|
|
|
|
If the application source, such as a package with version specifier, or an URL,
|
|
|
|
directory or any other accepted specification. See C(pipx) documentation for more details.
|
|
|
|
- When specified, the C(pipx) command will use I(source) instead of I(name).
|
2023-03-26 09:17:55 +02:00
|
|
|
install_apps:
|
|
|
|
description:
|
|
|
|
- Add apps from the injected packages.
|
|
|
|
- Only used when I(state=inject).
|
|
|
|
type: bool
|
|
|
|
default: false
|
|
|
|
version_added: 6.5.0
|
2021-10-06 07:01:54 +02:00
|
|
|
install_deps:
|
|
|
|
description:
|
|
|
|
- Include applications of dependent packages.
|
2023-04-20 06:43:49 +02:00
|
|
|
- Only used when I(state=install), I(state=latest), or I(state=inject).
|
2021-10-06 07:01:54 +02:00
|
|
|
type: bool
|
|
|
|
default: false
|
|
|
|
inject_packages:
|
|
|
|
description:
|
|
|
|
- Packages to be injected into an existing virtual environment.
|
|
|
|
- Only used when I(state=inject).
|
|
|
|
type: list
|
|
|
|
elements: str
|
|
|
|
force:
|
|
|
|
description:
|
|
|
|
- Force modification of the application's virtual environment. See C(pipx) for details.
|
2023-03-30 07:28:24 +02:00
|
|
|
- Only used when I(state=install), I(state=upgrade), I(state=upgrade_all), I(state=latest), or I(state=inject).
|
2021-10-06 07:01:54 +02:00
|
|
|
type: bool
|
|
|
|
default: false
|
|
|
|
include_injected:
|
|
|
|
description:
|
|
|
|
- Upgrade the injected packages along with the application.
|
2023-03-30 07:28:24 +02:00
|
|
|
- Only used when I(state=upgrade), I(state=upgrade_all), or I(state=latest).
|
|
|
|
- This is used with I(state=upgrade) and I(state=latest) since community.general 6.6.0.
|
2021-10-06 07:01:54 +02:00
|
|
|
type: bool
|
|
|
|
default: false
|
|
|
|
index_url:
|
|
|
|
description:
|
|
|
|
- Base URL of Python Package Index.
|
2023-03-30 07:28:24 +02:00
|
|
|
- Only used when I(state=install), I(state=upgrade), I(state=latest), or I(state=inject).
|
2021-10-06 07:01:54 +02:00
|
|
|
type: str
|
|
|
|
python:
|
|
|
|
description:
|
|
|
|
- Python version to be used when creating the application virtual environment. Must be 3.6+.
|
2023-03-30 07:28:24 +02:00
|
|
|
- Only used when I(state=install), I(state=latest), I(state=reinstall), or I(state=reinstall_all).
|
2021-10-06 07:01:54 +02:00
|
|
|
type: str
|
2023-04-10 09:10:32 +02:00
|
|
|
system_site_packages:
|
|
|
|
description:
|
|
|
|
- Give application virtual environment access to the system site-packages directory.
|
|
|
|
- Only used when I(state=install) or I(state=latest).
|
|
|
|
type: bool
|
|
|
|
default: false
|
|
|
|
version_added: 6.6.0
|
2021-10-06 07:01:54 +02:00
|
|
|
executable:
|
|
|
|
description:
|
|
|
|
- Path to the C(pipx) installed in the system.
|
|
|
|
- >
|
|
|
|
If not specified, the module will use C(python -m pipx) to run the tool,
|
|
|
|
using the same Python interpreter as ansible itself.
|
|
|
|
type: path
|
2022-03-02 22:16:47 +01:00
|
|
|
editable:
|
|
|
|
description:
|
|
|
|
- Install the project in editable mode.
|
|
|
|
type: bool
|
|
|
|
default: false
|
|
|
|
version_added: 4.6.0
|
|
|
|
pip_args:
|
|
|
|
description:
|
|
|
|
- Arbitrary arguments to pass directly to C(pip).
|
|
|
|
type: str
|
|
|
|
version_added: 4.6.0
|
2021-10-06 07:01:54 +02:00
|
|
|
notes:
|
|
|
|
- This module does not install the C(pipx) python package, however that can be easily done with the module M(ansible.builtin.pip).
|
|
|
|
- This module does not require C(pipx) to be in the shell C(PATH), but it must be loadable by Python as a module.
|
2023-01-18 21:28:16 +01:00
|
|
|
- >
|
|
|
|
This module will honor C(pipx) environment variables such as but not limited to C(PIPX_HOME) and C(PIPX_BIN_DIR)
|
|
|
|
passed using the R(environment Ansible keyword, playbooks_environment).
|
2023-04-20 06:43:49 +02:00
|
|
|
- This module requires C(pipx) version 0.16.2.1 or above.
|
2021-10-06 07:01:54 +02:00
|
|
|
- Please note that C(pipx) requires Python 3.6 or above.
|
|
|
|
- >
|
|
|
|
This first implementation does not verify whether a specified version constraint has been installed or not.
|
|
|
|
Hence, when using version operators, C(pipx) module will always try to execute the operation,
|
|
|
|
even when the application was previously installed.
|
|
|
|
This feature will be added in the future.
|
|
|
|
- See also the C(pipx) documentation at U(https://pypa.github.io/pipx/).
|
|
|
|
author:
|
|
|
|
- "Alexei Znamensky (@russoz)"
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: Install tox
|
|
|
|
community.general.pipx:
|
|
|
|
name: tox
|
|
|
|
|
|
|
|
- name: Install tox from git repository
|
|
|
|
community.general.pipx:
|
|
|
|
name: tox
|
|
|
|
source: git+https://github.com/tox-dev/tox.git
|
|
|
|
|
|
|
|
- name: Upgrade tox
|
|
|
|
community.general.pipx:
|
|
|
|
name: tox
|
|
|
|
state: upgrade
|
|
|
|
|
|
|
|
- name: Reinstall black with specific Python version
|
|
|
|
community.general.pipx:
|
|
|
|
name: black
|
|
|
|
state: reinstall
|
|
|
|
python: 3.7
|
|
|
|
|
|
|
|
- name: Uninstall pycowsay
|
|
|
|
community.general.pipx:
|
|
|
|
name: pycowsay
|
|
|
|
state: absent
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
2022-08-08 08:02:56 +02:00
|
|
|
from ansible_collections.community.general.plugins.module_utils.module_helper import StateModuleHelper
|
|
|
|
from ansible_collections.community.general.plugins.module_utils.pipx import pipx_runner
|
2021-10-06 07:01:54 +02:00
|
|
|
|
2022-08-08 08:02:56 +02:00
|
|
|
from ansible.module_utils.facts.compat import ansible_facts
|
2021-10-06 07:01:54 +02:00
|
|
|
|
|
|
|
|
2022-08-08 08:02:56 +02:00
|
|
|
class PipX(StateModuleHelper):
|
2021-10-06 07:01:54 +02:00
|
|
|
output_params = ['name', 'source', 'index_url', 'force', 'installdeps']
|
|
|
|
module = dict(
|
|
|
|
argument_spec=dict(
|
|
|
|
state=dict(type='str', default='install',
|
2022-05-23 07:19:24 +02:00
|
|
|
choices=['present', 'absent', 'install', 'uninstall', 'uninstall_all',
|
2022-08-18 06:44:18 +02:00
|
|
|
'inject', 'upgrade', 'upgrade_all', 'reinstall', 'reinstall_all', 'latest']),
|
2021-10-06 07:01:54 +02:00
|
|
|
name=dict(type='str'),
|
|
|
|
source=dict(type='str'),
|
2023-03-26 09:17:55 +02:00
|
|
|
install_apps=dict(type='bool', default=False),
|
2021-10-06 07:01:54 +02:00
|
|
|
install_deps=dict(type='bool', default=False),
|
|
|
|
inject_packages=dict(type='list', elements='str'),
|
|
|
|
force=dict(type='bool', default=False),
|
|
|
|
include_injected=dict(type='bool', default=False),
|
|
|
|
index_url=dict(type='str'),
|
|
|
|
python=dict(type='str'),
|
2023-04-10 09:10:32 +02:00
|
|
|
system_site_packages=dict(type='bool', default=False),
|
2022-03-02 22:16:47 +01:00
|
|
|
executable=dict(type='path'),
|
|
|
|
editable=dict(type='bool', default=False),
|
|
|
|
pip_args=dict(type='str'),
|
2021-10-06 07:01:54 +02:00
|
|
|
),
|
|
|
|
required_if=[
|
|
|
|
('state', 'present', ['name']),
|
|
|
|
('state', 'install', ['name']),
|
|
|
|
('state', 'absent', ['name']),
|
|
|
|
('state', 'uninstall', ['name']),
|
2022-08-09 10:15:51 +02:00
|
|
|
('state', 'upgrade', ['name']),
|
|
|
|
('state', 'reinstall', ['name']),
|
2022-08-18 06:44:18 +02:00
|
|
|
('state', 'latest', ['name']),
|
2021-10-06 07:01:54 +02:00
|
|
|
('state', 'inject', ['name', 'inject_packages']),
|
|
|
|
],
|
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
def _retrieve_installed(self):
|
|
|
|
def process_list(rc, out, err):
|
|
|
|
if not out:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
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': dict(
|
2021-10-30 08:11:57 +02:00
|
|
|
(k, v['package_version']) for k, v in venv['metadata']['injected_packages'].items()
|
2021-10-06 07:01:54 +02:00
|
|
|
),
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
|
2022-08-08 08:02:56 +02:00
|
|
|
installed = self.runner('_list', output_process=process_list).run(_list=1)
|
2021-10-06 07:01:54 +02:00
|
|
|
|
|
|
|
if self.vars.name is not None:
|
|
|
|
app_list = installed.get(self.vars.name)
|
|
|
|
if app_list:
|
|
|
|
return {self.vars.name: app_list}
|
|
|
|
else:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
return installed
|
|
|
|
|
|
|
|
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']
|
2022-08-08 08:02:56 +02:00
|
|
|
self.runner = pipx_runner(self.module, self.command)
|
2021-10-06 07:01:54 +02:00
|
|
|
|
|
|
|
self.vars.set('application', self._retrieve_installed(), change=True, diff=True)
|
|
|
|
|
|
|
|
def __quit_module__(self):
|
|
|
|
self.vars.application = self._retrieve_installed()
|
|
|
|
|
2022-08-08 08:02:56 +02:00
|
|
|
def _capture_results(self, ctx):
|
|
|
|
self.vars.stdout = ctx.results_out
|
|
|
|
self.vars.stderr = ctx.results_err
|
|
|
|
self.vars.cmd = ctx.cmd
|
|
|
|
if self.verbosity >= 4:
|
|
|
|
self.vars.run_info = ctx.run_info
|
|
|
|
|
2021-10-06 07:01:54 +02:00
|
|
|
def state_install(self):
|
|
|
|
if not self.vars.application or self.vars.force:
|
2021-11-01 07:59:15 +01:00
|
|
|
self.changed = True
|
2023-04-10 09:10:32 +02:00
|
|
|
with self.runner('state index_url install_deps force python system_site_packages editable pip_args name_source', check_mode_skip=True) as ctx:
|
2022-08-08 08:02:56 +02:00
|
|
|
ctx.run(name_source=[self.vars.name, self.vars.source])
|
|
|
|
self._capture_results(ctx)
|
2021-10-06 07:01:54 +02:00
|
|
|
|
|
|
|
state_present = state_install
|
|
|
|
|
|
|
|
def state_upgrade(self):
|
|
|
|
if not self.vars.application:
|
2022-05-23 07:19:24 +02:00
|
|
|
self.do_raise("Trying to upgrade a non-existent application: {0}".format(self.vars.name))
|
2021-10-06 07:01:54 +02:00
|
|
|
if self.vars.force:
|
2021-11-01 07:59:15 +01:00
|
|
|
self.changed = True
|
2022-08-08 08:02:56 +02:00
|
|
|
|
2023-04-20 06:43:49 +02:00
|
|
|
with self.runner('state include_injected index_url force editable pip_args name', check_mode_skip=True) as ctx:
|
2022-08-08 08:02:56 +02:00
|
|
|
ctx.run()
|
|
|
|
self._capture_results(ctx)
|
2021-10-06 07:01:54 +02:00
|
|
|
|
|
|
|
def state_uninstall(self):
|
2022-08-08 08:02:56 +02:00
|
|
|
if self.vars.application:
|
|
|
|
with self.runner('state name', check_mode_skip=True) as ctx:
|
|
|
|
ctx.run()
|
|
|
|
self._capture_results(ctx)
|
2021-10-06 07:01:54 +02:00
|
|
|
|
|
|
|
state_absent = state_uninstall
|
|
|
|
|
|
|
|
def state_reinstall(self):
|
|
|
|
if not self.vars.application:
|
2022-05-23 07:19:24 +02:00
|
|
|
self.do_raise("Trying to reinstall a non-existent application: {0}".format(self.vars.name))
|
2021-11-01 07:59:15 +01:00
|
|
|
self.changed = True
|
2022-08-08 08:02:56 +02:00
|
|
|
with self.runner('state name python', check_mode_skip=True) as ctx:
|
|
|
|
ctx.run()
|
|
|
|
self._capture_results(ctx)
|
2021-10-06 07:01:54 +02:00
|
|
|
|
|
|
|
def state_inject(self):
|
|
|
|
if not self.vars.application:
|
2022-05-23 07:19:24 +02:00
|
|
|
self.do_raise("Trying to inject packages into a non-existent application: {0}".format(self.vars.name))
|
2021-10-06 07:01:54 +02:00
|
|
|
if self.vars.force:
|
2021-11-01 07:59:15 +01:00
|
|
|
self.changed = True
|
2023-03-26 09:17:55 +02:00
|
|
|
with self.runner('state index_url install_apps install_deps force editable pip_args name inject_packages', check_mode_skip=True) as ctx:
|
2022-08-08 08:02:56 +02:00
|
|
|
ctx.run()
|
|
|
|
self._capture_results(ctx)
|
2021-10-06 07:01:54 +02:00
|
|
|
|
|
|
|
def state_uninstall_all(self):
|
2022-08-08 08:02:56 +02:00
|
|
|
with self.runner('state', check_mode_skip=True) as ctx:
|
|
|
|
ctx.run()
|
|
|
|
self._capture_results(ctx)
|
2021-10-06 07:01:54 +02:00
|
|
|
|
|
|
|
def state_reinstall_all(self):
|
2022-08-08 08:02:56 +02:00
|
|
|
with self.runner('state python', check_mode_skip=True) as ctx:
|
|
|
|
ctx.run()
|
|
|
|
self._capture_results(ctx)
|
2021-10-06 07:01:54 +02:00
|
|
|
|
|
|
|
def state_upgrade_all(self):
|
|
|
|
if self.vars.force:
|
2021-11-01 07:59:15 +01:00
|
|
|
self.changed = True
|
2022-08-08 08:02:56 +02:00
|
|
|
with self.runner('state include_injected force', check_mode_skip=True) as ctx:
|
|
|
|
ctx.run()
|
|
|
|
self._capture_results(ctx)
|
2021-10-06 07:01:54 +02:00
|
|
|
|
2022-08-18 06:44:18 +02:00
|
|
|
def state_latest(self):
|
|
|
|
if not self.vars.application or self.vars.force:
|
|
|
|
self.changed = True
|
2023-04-10 09:10:32 +02:00
|
|
|
with self.runner('state index_url install_deps force python system_site_packages editable pip_args name_source', check_mode_skip=True) as ctx:
|
2022-08-18 06:44:18 +02:00
|
|
|
ctx.run(state='install', name_source=[self.vars.name, self.vars.source])
|
|
|
|
self._capture_results(ctx)
|
|
|
|
|
2023-04-20 06:43:49 +02:00
|
|
|
with self.runner('state include_injected index_url force editable pip_args name', check_mode_skip=True) as ctx:
|
2022-08-18 06:44:18 +02:00
|
|
|
ctx.run(state='upgrade')
|
|
|
|
self._capture_results(ctx)
|
|
|
|
|
2021-10-06 07:01:54 +02:00
|
|
|
|
|
|
|
def main():
|
|
|
|
PipX.execute()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|