
    Vh@                         d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlmZmZmZ d dlmZ d dlmZ d	 Z G d
 de
      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: pipx
short_description: Manages applications installed with pipx
version_added: 3.8.0
description:
  - Manage Python applications installed in isolated virtualenvs using pipx.
extends_documentation_fragment:
  - community.general.attributes
  - community.general.pipx
attributes:
  check_mode:
    support: full
  diff_mode:
    support: full
options:
  state:
    type: str
    choices:
      - present
      - absent
      - install
      - install_all
      - uninstall
      - uninstall_all
      - inject
      - uninject
      - upgrade
      - upgrade_shared
      - upgrade_all
      - reinstall
      - reinstall_all
      - latest
      - pin
      - unpin
    default: install
    description:
      - Desired state for the application.
      - The states V(present) and V(absent) are aliases to V(install) and V(uninstall), respectively.
      - The state V(latest) is equivalent to executing the task twice, with state V(install) and then V(upgrade). It was added
        in community.general 5.5.0.
      - The states V(install_all), V(uninject), V(upgrade_shared), V(pin) and V(unpin) are only available in C(pipx>=1.6.0),
        make sure to have a compatible version when using this option. These states have been added in community.general 9.4.0.
  name:
    type: str
    description:
      - The name of the application and also the name of the Python package being installed.
      - In C(pipx) documentation it is also referred to as the name of the virtual environment where the application is installed.
      - If O(name) is a simple package name without version specifiers, then that name is used as the Python package name
        to be installed.
      - Starting in community.general 10.7.0, you can use package specifiers when O(state=present) or O(state=install). For
        example, O(name=tox<4.0.0) or O(name=tox>3.0.27).
      - Please note that when you use O(state=present) and O(name) with version specifiers, contrary to the behavior of C(pipx),
        this module honors the version specifier and installs a version of the application that satisfies it. If you want
        to ensure the reinstallation of the application even when the version specifier is met, then you must use O(force=true),
        or perhaps use O(state=upgrade) instead.
      - Use O(source) for installing from URLs or directories.
  source:
    type: str
    description:
      - Source for the package. This option is used when O(state=install) or O(state=latest), and it is ignored with other
        states.
      - Use O(source) when installing a Python package with version specifier, or from a local path, from a VCS URL or compressed
        file.
      - The value of this option is passed as-is to C(pipx).
      - O(name) is still required when using O(source) to establish the application name without fetching the package from
        a remote source.
      - The module is not idempotent when using O(source).
  install_apps:
    description:
      - Add apps from the injected packages.
      - Only used when O(state=inject).
    type: bool
    default: false
    version_added: 6.5.0
  install_deps:
    description:
      - Include applications of dependent packages.
      - Only used when O(state=install), O(state=latest), or O(state=inject).
    type: bool
    default: false
  inject_packages:
    description:
      - Packages to be injected into an existing virtual environment.
      - Only used when O(state=inject).
    type: list
    elements: str
  force:
    description:
      - Force modification of the application's virtual environment. See C(pipx) for details.
      - Only used when O(state=install), O(state=upgrade), O(state=upgrade_all), O(state=latest), or O(state=inject).
      - The module is not idempotent when O(force=true).
    type: bool
    default: false
  include_injected:
    description:
      - Upgrade the injected packages along with the application.
      - Only used when O(state=upgrade), O(state=upgrade_all), or O(state=latest).
      - This is used with O(state=upgrade) and O(state=latest) since community.general 6.6.0.
    type: bool
    default: false
  index_url:
    description:
      - Base URL of Python Package Index.
      - Only used when O(state=install), O(state=upgrade), O(state=latest), or O(state=inject).
    type: str
  python:
    description:
      - Python version to be used when creating the application virtual environment. Must be 3.6+.
      - Only used when O(state=install), O(state=latest), O(state=reinstall), or O(state=reinstall_all).
    type: str
  system_site_packages:
    description:
      - Give application virtual environment access to the system site-packages directory.
      - Only used when O(state=install) or O(state=latest).
    type: bool
    default: false
    version_added: 6.6.0
  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
  suffix:
    description:
      - Optional suffix for virtual environment and executable names.
      - B(Warning:) C(pipx) documentation states this is an B(experimental) feature subject to change.
    type: str
    version_added: 9.3.0
  global:
    version_added: 9.4.0
  spec_metadata:
    description:
      - Spec metadata file for O(state=install_all).
      - This content of the file is usually generated with C(pipx list --json), and it can be obtained with M(community.general.pipx_info)
        with O(community.general.pipx_info#module:include_raw=true) and obtaining the content from the RV(community.general.pipx_info#module:raw_output).
    type: path
    version_added: 9.4.0
requirements:
  - When using O(name) with version specifiers, the Python package C(packaging) is required.
  - If the package C(packaging) is at a version lesser than C(22.0.0), it will fail silently when processing invalid specifiers,
    like C(tox<<<<4.0).
author:
  - "Alexei Znamensky (@russoz)"
aX  
- 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: Install or upgrade tox with dependency group 'docs'
  community.general.pipx:
    name: tox
    source: tox[docs]
    state: latest

- 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

- name: Install multiple packages from list
  vars:
    pipx_packages:
      - pycowsay
      - black
      - tox
  community.general.pipx:
    name: "{{ item }}"
    state: latest
  with_items: "{{ pipx_packages }}"
zs
version:
  description: Version of pipx.
  type: str
  returned: always
  sample: "1.7.1"
  version_added: 10.1.0
)StateModuleHelper)pipx_runnerpipx_common_argspecmake_process_dict)PackageRequirement)ansible_factsc                 .    || S dj                  | |      S )Nz{0}{1})format)namesuffixs     j/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/pipx.py
_make_namer      s    >4DxtV'DD    c                   Z   e Zd Zg dZ e eddg d       ed       ed       edd	       edd	       ed
d       edd	       edd	       ed       ed       edd	       edd	       ed       ed       ed            Zej                  e        eedddgfdddgfdddgfdddgfdddgfdddgfdddgfdddgfddddgfdddgfdddgfg ed      d      ZdZ	d Z
d  Zd! Zd" Zd# ZeZd$ Zd% Zd& ZeZd' Zd( Zd) Zd* Zd+ Zd, Zd- Zd. Zd/ Zd0 Zy1)2PipX)r   source	index_urlforceinstalldepsstrinstall)presentabsentr   install_all	uninstalluninstall_allinjectuninjectupgradeupgrade_sharedupgrade_all	reinstallreinstall_alllatestpinunpin)typedefaultchoices)r*   boolF)r*   r+   list)r*   elementspath)stater   r   install_appsinstall_depsinject_packagesr   include_injectedr   pythonsystem_site_packageseditablepip_argsr   spec_metadatar1   r   r   r   r:   r   r   r"   r%   r'   r    r4   r(   r)   )r   T)argument_specrequired_ifrequired_bysupports_check_modec                     t        d      }| j                  d|      j                         \  }}| j                  |S |j	                         D ci c]  \  }}|| j                  k(  s|| c}}S c c}}w )NT)r5   z_list global)output_process)r	   runnerrunapp_nameitems)selfr@   	installeddummykvs         r   _retrieve_installedzPipX._retrieve_installed  sj    *DA;;~n;UYY[	5== !*!2IAa4==6H1IIIs   A3*A3c                    | j                   j                  r| j                   j                  g| _        n(t        | j                  dg      }|d   d   ddg| _        t        | j                  | j                        | _        t        | j                  | j                   j                        }|j                  | _	        |j                  | _        t        | j                  | j                   j                        | _        | j                   j                  d| j!                         dd       | j                  d	      5 }|j#                         \  }}}|j%                         | j                   _        d d d        y # 1 sw Y   y xY w)
Nr6   )gather_subset
executablez-mpipxapplicationT)changediffversion)varsrM   commandr   moduler   rA   r
   r   parsed_namerequirement
parsed_reqr   r   rC   setrJ   rB   striprR   )rE   factspkg_reqctxrcouterrs          r   __init_module__zPipX.__init_module__  s   99 II001DL!$++hZHE!(OL94HDL!$++t||<$T[[$))..A"..!--"4#3#3TYY5E5EF		mT%=%=%?SWX[[# 	,s779LBS #		DII	, 	, 	,s   :4E77F c                 B    | j                         | j                  _        y N)rJ   rS   rO   )rE   s    r   __quit_module__zPipX.__quit_module__/  s     $ 8 8 :		r   c                     |j                   | j                  _        |j                  | j                  _        |j
                  | j                  _        | j                  j                  d|j                  d       y )Nrun_info   )	verbosity)results_outrS   stdoutresults_errstderrcmdrY   rf   rE   r]   s     r   _capture_resultszPipX._capture_results2  sK    ??		??						j#,,!<r   c                    | j                   r;| j                  j                  s%| j                  j                  | j                  _        | j                  j                  j                  | j                        rhd}| j                   r<| j                  j                  | j                     d   | j                   j                  v nd}| j                  j                  xs | }nd}d}| j                  j                  }|r|r|sy d| _	        d}| j                  |d      5 }|j                  | j                  | j                  j                  g|       | j                  |       d d d        y # 1 sw Y   y xY w)NTrR   Fjstate global index_url install_deps force python system_site_packages editable pip_args suffix name_sourcecheck_mode_skip)name_sourcer   )rX   rS   r   r   rO   getrC   	specifierr   changedrA   rB   rV   ro   )rE   is_installedversion_matchr   
args_orderr]   s         r   state_installzPipX.state_install8  s$   ??499#3#3#yy~~DII99  $$T]]3Llpl{l{DII11$--@KtOhOhh  BFMIIOO:M(9E L!MIIOOEM% B
[[T[: 	'cGG!1!14993C3C DEGR!!#&	' 	' 	's   AE**E3c                     d| _         | j                  dd      5 }|j                          | j                  |       d d d        y # 1 sw Y   y xY w)NTzXstate global index_url force python system_site_packages editable pip_args spec_metadatarr   )rw   rA   rB   ro   rn   s     r   state_install_allzPipX.state_install_allQ  sU    [[s  FJ[  K 	'  ORGGI!!#&	' 	' 	's   "AAc                    t        | j                  j                  | j                  j                        }| j                  j                  s | j                  dj                  |             | j                  j                  rd| _        | j                  dd      5 }|j                  |       | j                  |       d d d        y # 1 sw Y   y xY w)Nz1Trying to upgrade a non-existent application: {0}TDstate global include_injected index_url force editable pip_args namerr   r   r   rS   r   r   rO   do_raiser   r   rw   rA   rB   ro   rE   r   r]   s      r   state_upgradezPipX.state_upgradeW  s    $))..$))*:*:;yy$$MMMTTUYZ[99??DL[[_qu[v 	'z}GGG!!#&	' 	' 	'   $CCc                 0   | j                   j                  rtt        | j                   j                  | j                   j                        }| j                  dd      5 }|j                  |       | j                  |       d d d        y y # 1 sw Y   y xY w)Nstate global nameTrr   r   )rS   rO   r   r   r   rA   rB   ro   r   s      r   state_uninstallzPipX.state_uninstallb  s{    99  diinndii.>.>?D0$G +3T"%%c*+ + !+ +s   $BBc                 |   t        | j                  j                  | j                  j                        }| j                  j                  s | j                  dj                  |             d| _        | j                  dd      5 }|j                  |       | j                  |       d d d        y # 1 sw Y   y xY w)Nz3Trying to reinstall a non-existent application: {0}Tzstate global name pythonrr   r   )r   rS   r   r   rO   r   r   rw   rA   rB   ro   r   s      r   state_reinstallzPipX.state_reinstallk  s    $))..$))*:*:;yy$$MMOVVW[\][[3T[J 	'cGGG!!#&	' 	' 	's   $B22B;c                    t        | j                  j                  | j                  j                        }| j                  j                  s | j                  dj                  |             | j                  j                  rd| _        | j                  dd      5 }|j                  |       | j                  |       d d d        y # 1 sw Y   y xY w)Nz>Trying to inject packages into a non-existent application: {0}Tz]state global index_url install_apps install_deps force editable pip_args name inject_packagesrr   r   r   r   s      r   state_injectzPipX.state_injectt  s    $))..$))*:*:;yy$$MMZaabfgh99??DL[[x  KO[  P 	'  TWGGG!!#&	' 	' 	'r   c                 n   t        | j                  j                  | j                  j                        }| j                  j                  s | j                  dj                  |             | j                  dd      5 }|j                  |       | j                  |       d d d        y # 1 sw Y   y xY w)Nz@Trying to uninject packages into a non-existent application: {0}z!state global name inject_packagesTrr   r   )
r   rS   r   r   rO   r   r   rA   rB   ro   r   s      r   state_uninjectzPipX.state_uninject~  s    $))..$))*:*:;yy$$MM\ccdhij[[<d[S 	'WZGGG!!#&	' 	' 	's   >$B++B4c                     | j                  dd      5 }|j                          | j                  |       d d d        y # 1 sw Y   y xY w)Nzstate globalTrr   rA   rB   ro   rn   s     r   state_uninstall_allzPipX.state_uninstall_all  s@    [[[> 	'#GGI!!#&	' 	' 	'	   "?Ac                     | j                  dd      5 }|j                          | j                  |       d d d        y # 1 sw Y   y xY w)Nzstate global pythonTrr   r   rn   s     r   state_reinstall_allzPipX.state_reinstall_all  sA    [[.[E 	'GGI!!#&	' 	' 	'r   c                     | j                   j                  rd| _        | j                  dd      5 }|j	                          | j                  |       d d d        y # 1 sw Y   y xY w)NTz#state global include_injected forcerr   )rS   r   rw   rA   rB   ro   rn   s     r   state_upgrade_allzPipX.state_upgrade_all  sU    99??DL[[>PT[U 	'Y\GGI!!#&	' 	' 	's   "AA%c                     | j                  dd      5 }|j                          | j                  |       d d d        y # 1 sw Y   y xY w)Nzstate global pip_argsTrr   r   rn   s     r   state_upgrade_sharedzPipX.state_upgrade_shared  sA    [[0$[G 	'3GGI!!#&	' 	' 	'r   c                    | j                   j                  r| j                   j                  rsd| _        d}| j	                  |d      5 }|j                  d| j                   j                  | j                   j                  g       | j                  |       d d d        | j	                  dd      5 }|j                  d       | j                  |       d d d        y # 1 sw Y   IxY w# 1 sw Y   y xY w)	NTrq   rr   r   )r1   rt   r   r"   )r1   )	rS   rO   r   rw   rA   rB   r   r   ro   )rE   rz   r]   s      r   state_latestzPipX.state_latest  s    yy$$		DL FJZ> +#idiinndiiFVFV5WX%%c*+ [[_qu[v 	'z}GG)G$!!#&	' 	'	+ +	' 	's   	AC 3$C, C),C5c                     | j                  dd      5 }|j                          | j                  |       d d d        y # 1 sw Y   y xY wNr   Trr   r   rn   s     r   	state_pinzPipX.state_pin  A    [[,d[C 	'sGGI!!#&	' 	' 	'r   c                     | j                  dd      5 }|j                          | j                  |       d d d        y # 1 sw Y   y xY wr   r   rn   s     r   state_unpinzPipX.state_unpin  r   r   N)__name__
__module____qualname__output_paramsdictr;   updater   rU   use_old_vardictrJ   ra   rd   ro   r{   state_presentr}   r   r   state_absentr   r   r   r   r   r   r   r   r   r    r   r   r   r      s   KMy
 uvu5vu5&59.659E"!vu=6515!''M* ,-#i&*i&*mo%67h)kF8,i&*kF8,h)h): ;<efX&gx(
 
 !%F( OJ,&;='. "M'	'+ #L''''
'
''

''
'r   r   c                  ,    t         j                          y rc   )r   executer   r   r   mainr     s    LLNr   __main__N)
__future__r   r   r   r*   __metaclass__DOCUMENTATIONEXAMPLESRETURNHansible_collections.community.general.plugins.module_utils.module_helperr   ?ansible_collections.community.general.plugins.module_utils.pipxr   r   r	   Bansible_collections.community.general.plugins.module_utils.pkg_reqr
   !ansible.module_utils.facts.compatr   r   r   r   r   r   r   r   <module>r      sl    A @Un*X
 g   a ;EL' L'^ zF r   