
    Vh,                         d dl mZmZmZ eZdZdZd dlZd dl	m
Z
 d dlmZ d Zd Zd	 Zdd
Zd Zd Zd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functionaf  
module: pear
short_description: Manage pear/pecl packages
description:
  - Manage PHP packages with the pear package manager.
author:
  - Jonathan Lestrelin (@jle64) <jonathan.lestrelin@gmail.com>
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  name:
    type: str
    description:
      - Name of the package to install, upgrade, or remove.
    required: true
    aliases: [pkg]
  state:
    type: str
    description:
      - Desired state of the package.
    default: "present"
    choices: ["present", "installed", "latest", "absent", "removed"]
  executable:
    type: path
    description:
      - Path to the pear executable.
  prompts:
    description:
      - List of regular expressions that can be used to detect prompts during pear package installation to answer the expected
        question.
      - Prompts will be processed in the same order as the packages list.
      - You can optionally specify an answer to any question in the list.
      - If no answer is provided, the list item will only contain the regular expression.
      - "To specify an answer, the item will be a dict with the regular expression as key and the answer as value C(my_regular_expression:
        'an_answer')."
      - You can provide a list containing items with or without answer.
      - A prompt list can be shorter or longer than the packages list but will issue a warning.
      - If you want to specify that a package will not need prompts in the middle of a list, V(null).
    type: list
    elements: raw
    version_added: 0.2.0
a  
- name: Install pear package
  community.general.pear:
    name: Net_URL2
    state: present

- name: Install pecl package
  community.general.pear:
    name: pecl/json_post
    state: present

- name: Install pecl package with expected prompt
  community.general.pear:
    name: pecl/apcu
    state: present
    prompts:
      - (.*)Enable internal debugging in APCu \[no\]

- name: Install pecl package with expected prompt and an answer
  community.general.pear:
    name: pecl/apcu
    state: present
    prompts:
      - (.*)Enable internal debugging in APCu \[no\]: "yes"

- name: Install multiple pear/pecl packages at once with prompts. Prompts will be processed on the same order as the packages
    order. If there is more prompts than packages, packages without prompts will be installed without any prompt expected.
    If there is more packages than prompts, additional prompts will be ignored.
  community.general.pear:
    name: pecl/gnupg, pecl/apcu
    state: present
    prompts:
      - I am a test prompt because gnupg doesnt asks anything
      - (.*)Enable internal debugging in APCu \[no\]: "yes"

- name: Install multiple pear/pecl packages at once skipping the first prompt. Prompts will be processed on the same order
    as the packages order. If there is more prompts than packages, packages without prompts will be installed without any
    prompt expected. If there is more packages than prompts, additional prompts will be ignored.
  community.general.pear:
    name: pecl/gnupg, pecl/apcu
    state: present
    prompts:
      - null
      - (.*)Enable internal debugging in APCu \[no\]: "yes"

- name: Upgrade package
  community.general.pear:
    name: Net_URL2
    state: latest

- name: Remove packages
  community.general.pear:
    name: Net_URL2,pecl/json_post
    state: absent
N)to_text)AnsibleModulec                     | j                  d      }|D ]3  }d|v s|j                  dd      d   j                         }|dk(  r1|c S  y)z9Take pear remoteinfo output and get the installed version
z
Installed N   -splitrsplitstrip)pear_outputlinesline	installeds       j/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/pear.pyget_local_versionr   }   s]    d#E 4D!,R0668IC     c                     | j                   d   r=t        j                  j                  | j                   d         r| j                   d   }|S | j	                  dd| j                   d   g      }|S )N
executablepearT)paramsospathisfileget_bin_path)moduleresults     r   _get_pear_pathr"      sb    }}\"rww~~fmmL6Q'R|, M $$VTFMM,4O3PQMr   c                     | j                  d      }|D ]+  }d|v s|j                  dd      d   j                         c S  y)z7Take pear remote-info output and get the latest versionr	   zLatest Nr
   r   r   )r   r   r   s      r   get_repository_versionr$      sM    d#E 4;;tQ'+11334 r   c                     |dk(  rwt        |       d|}| j                  |d      \  }}}|dk7  ryt        |       d|}| j                  |d      \  }}	}
t        |	      }t        |	      }|dk(  rd||k(  fS yy	)
zQuery the package status in both the local system and the repository.
    Returns a boolean to indicate if the package is installed,
    and a second boolean to indicate if the package is up-to-date.presentz info Fcheck_rcr   )FFz remote-info TN)r"   run_commandr   r$   )r    namestatelcmdlrclstdoutlstderrrcmdrrcrstdoutrstderrlversionrversions                r   query_packager6      s     	-f5t< & 2 24% 2 HWg!8&4V&<dC & 2 24% 2 HWg %W- *'2!8 (h.//- r   c           
      @   d}|D ]i  }t        | |      \  }}|st        |       d|}| j                  |d      \  }}}	|dk7  r$| j                  d|dt	        ||	z                |dz  }k |dkD  r| j                  d	d
|z         | j                  dd       y )Nr   z uninstall Fr'   zfailed to remove : msgr
   Tzremoved %s package(s)changedr:   zpackage(s) already absent)r6   r"   r)   	fail_jsonr   	exit_json)
r    packagesremove_cpackager   updatedcmdrcstdoutstderrs
             r   remove_packagesrG      s    H *67;	7#1&#97C#//e/DFF7gwvX^G_!`aA !|+BX+MN
U(CDr   c           
         d}t        |      }d}|r	t        |      }t        |      }|dkD  rr||k7  rm||kD  r,||z
  }	t        |      dt        |      dt        |	      d}
n+||z
  }	t        |      dt        |      dt        |	      d}
| j                  |
       g }|D ]s  }t	        |t
              r8t        |j                               d   }||   dz   }|j                  ||f       K|s|j                  d |f       a|j                  ||f       u |}t        |      D ]  \  }}t        | |      \  }}|r|dk(  s|d	k(  r|r$|dk(  rd
}|d	k(  rd}|r|t        |      k  r||   d   }||   d   }nd }|}t        |       dd|}| j                  |d||d      \  }}}|dk7  r$| j                  d|dt        ||z                |dz  } |dkD  r| j                  dd|z         | j                  dd       y )Nr   r	   z packages to install but z prompts to expect. z prompts will be ignoredz packages to install but only z, packages won't be expected to have a promptr&   latestinstallupgrader
    FT)r(   prompt_regexdatabinary_datazfailed to install r8   r9   zinstalled %s package(s)r;   zpackage(s) already installed)boollenr   warn
isinstancedictlistkeysappend	enumerater6   r"   r)   r=   r>   )r    r+   r?   prompts	install_c
has_promptdefault_stdin
nb_promptsnb_packagesdiffr:   tmp_prompts_itemkeyanswerirA   r   rB   commandrM   rN   rC   rD   rE   rF   s                             r   install_packagesrf      sj   IgJM\
(m>z[8K'!K/gnozg{  ~E  FP  ~Q  SZ  [_  S`  a"Z/{+WZ-@'$-QKK  	;E %&5::<(+sd*""C=1""D-#89""E=#9:	; ) 
7 +67;	7%9,(1BwIGHG!c'l*"1:a=L1:a=DL D*62GWE#//eR^eiw{/|FF7wPVY_P_H`!abQ	36 1}+D	+RS
U(FGr   c                    g }|D ]8  }t        | |      \  }}|dv r|r|dk(  r|s	|dk(  s%|r(|j                  |       : |r)|dk(  rd}| j                  dt        |      d|       y | j                  dd	|z  
       y )Nr&   rI   absentrI   removedTz package(s) would be r;   Fzpackage(s) already %s)changer:   )r6   rW   r>   rQ   )r    r?   r+   would_be_changedrA   r   rB   s          r   check_packagesrm     s     -*67;	7++I("y("7##G,- HE !5,* 	+ 	+BU+JKr   c                      t        t        t        dgd      t        dg d      t        d dd	      t        d dd
d            d      } | j                  }|d   dv rd|d<   n|d   dv rd|d<   |d   r|d   j                  d      }g }t	        |      D ]  \  }}|j                  d         | j                  rt        | ||d          |d   dv rt        | |d   ||d          y |d   dk(  rt        | |       y y y )NpkgT)aliasesrequiredr&   )r&   r   rI   ri   rj   )defaultchoicesFr   )rr   rq   typerU   raw)rr   rq   rt   elements)r*   r+   r   rY   )argument_specsupports_check_moder+   )r&   r   )ri   rj   ri   r*   ,rh   rY   )
r   rT   r   r   rX   rW   
check_moderm   rf   rG   )r    ppkgs	pkg_filesrd   ro   s         r   mainr~   #  s$   ug5y2ijD5vFFUS	
 !"F 	A 	z--'
	
7,	,'
yys#	o 	#FAsT"	# 6474W:..VQwZq|DwZ8#FD) $ r   __main__)r&   )
__future__r   r   r   rt   __metaclass__DOCUMENTATIONEXAMPLESr   +ansible.module_utils.common.text.convertersr   ansible.module_utils.basicr   r   r"   r$   r6   rG   rf   rm   r~   __name__ r   r   <module>r      sp    A @.`6p 
 ? 4	:E0@HFL"*D zF r   