
    Vhd1                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZ d Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona	  
module: apk
short_description: Manages apk packages
description:
  - Manages C(apk) packages for Alpine Linux.
author: "Kevin Brebanov (@kbrebanov)"
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  available:
    description:
      - During upgrade, reset versioned world dependencies and change logic to prefer replacing or downgrading packages (instead
        of holding them) if the currently installed package is no longer available from any repository.
    type: bool
    default: false
  name:
    description:
      - A package name, like V(foo), or multiple packages, like V(foo,bar).
      - Do not include additional whitespace when specifying multiple packages as a string. Prefer YAML lists over comma-separating
        multiple package names.
    type: list
    elements: str
  no_cache:
    description:
      - Do not use any local cache path.
    type: bool
    default: false
    version_added: 1.0.0
  repository:
    description:
      - A package repository or multiple repositories. Unlike with the underlying apk command, this list will override the
        system repositories rather than supplement them.
    type: list
    elements: str
  state:
    description:
      - Indicates the desired package(s) state.
      - V(present) ensures the package(s) is/are present. V(installed) can be used as an alias.
      - V(absent) ensures the package(s) is/are absent. V(removed) can be used as an alias.
      - V(latest) ensures the package(s) is/are present and the latest version(s).
    default: present
    choices: ["present", "absent", "latest", "installed", "removed"]
    type: str
  update_cache:
    description:
      - Update repository indexes. Can be run with other steps or on its own.
    type: bool
    default: false
  upgrade:
    description:
      - Upgrade all installed packages to their latest version.
    type: bool
    default: false
  world:
    description:
      - Use a custom world file when checking for explicitly installed packages. The file is used only when a value is provided
        for O(name), and O(state) is set to V(present) or V(latest).
    type: str
    default: /etc/apk/world
    version_added: 5.4.0
notes:
  - O(name) and O(upgrade) are mutually exclusive.
  - When used with a C(loop:) each package will be processed individually, it is much more efficient to pass the list directly
    to the O(name) option.
a  
- name: Update repositories and install foo package
  community.general.apk:
    name: foo
    update_cache: true

- name: Update repositories and install foo and bar packages
  community.general.apk:
    name: foo,bar
    update_cache: true

- name: Remove foo package
  community.general.apk:
    name: foo
    state: absent

- name: Remove foo and bar packages
  community.general.apk:
    name: foo,bar
    state: absent

- name: Install the package foo
  community.general.apk:
    name: foo
    state: present

- name: Install the packages foo and bar
  community.general.apk:
    name: foo,bar
    state: present

- name: Update repositories and update package foo to latest version
  community.general.apk:
    name: foo
    state: latest
    update_cache: true

- name: Update repositories and update packages foo and bar to latest versions
  community.general.apk:
    name: foo,bar
    state: latest
    update_cache: true

- name: Update all installed packages to the latest versions
  community.general.apk:
    upgrade: true

- name: Upgrade / replace / downgrade / uninstall all installed packages to the latest versions available
  community.general.apk:
    available: true
    upgrade: true

- name: Update repositories as a separate step
  community.general.apk:
    update_cache: true

- name: Install package from a specific repository
  community.general.apk:
    name: foo
    state: latest
    update_cache: true
    repository: http://dl-3.alpinelinux.org/alpine/edge/main

- name: Install package without using cache
  community.general.apk:
    name: foo
    state: latest
    no_cache: true

- name: Install package checking a custom world
  community.general.apk:
    name: foo
    state: latest
    world: /etc/apk/world.custom
z
packages:
  description: A list of packages that have been changed.
  returned: when packages have changed
  type: list
  sample: ['package', 'other-package']
N)AnsibleModulec                     g }| j                  d      }t        j                  d      }|D ]6  }|j                  |      }|s|j	                  |j                  d             8 |S )N
z^\(\d+/\d+\)\s+\S+\s+(\S+)   )splitrecompilesearchappendgroup)stdoutpackagesdataregexlps         i/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/apk.pyparse_for_packagesr      s^    H<<DJJ45E (LLOOOAGGAJ'( O    c                     dt         z  }| j                  |d      \  }}}|dk7  r| j                  d||       y |r| j                  dd||	       y y)
Nz	%s updateFcheck_rcr   zcould not update package db)msgr   stderrTzupdated repository indexes)changedr   r   r   )APK_PATHrun_command	fail_json	exit_json)moduleexitcmdrcr   r   s         r   update_package_dbr'      sf    

"C++C%+@B	Qw:6RXY	+GPV_efr   c                 "   t        j                  dt        j                  |      z   dz         }t        |      5 }|j	                         j                         }|D ]  }|j                  |      s d d d        y 	 d d d        y# 1 sw Y   yxY w)N^z([@=<>~].+)?$TF)r   r   escapeopenreadr
   r   )r#   nameworldr   fcontentr   s          r   query_toplevelr1      s     JJtbiio-0@@AE	e &&(.." 	A||A	 	
 
 s   5B0B:BBc                 T    t         d|}| j                  |d      \  }}}|dk(  ryy)Nz -v info --installed Fr   r   T)r   r    )r#   r-   r%   r&   r   r   s         r   query_packager3      s5    )14
8C++C%+@B	Qwr   c                     t         d|}| j                  |d      \  }}}dt        j                  |      z  }t        j                  ||      }|r|j                  d      dk(  ryy)Nz	 version Fr   z3(%s)-[\d\.\w]+-[\d\w]+\s+(.)\s+[\d\.\w]+-[\d\w]+\s+   <T)r   r    r   r*   r   r   )r#   r-   r%   r&   r   r   search_patternmatchs           r   query_latestr9      sc    %t
,C++C%+@BKryyY]_NIInf-EQ3&r   c                     t         d|}| j                  |d      \  }}}dt        j                  |      z  }t        j                  ||      ryy)Nz -v info --description Fr   z^%s: virtual meta packageT)r   r    r   r*   r   )r#   r-   r%   r&   r   r   r7   s          r   query_virtualr;      sN    +3T
:C++C%+@B1RYYt_EN	yy(r   c                     t         d|}| j                  |d      \  }}}|j                         }t        |      dkD  r|dd  S g S )Nz -v info --depends Fr   r	   )r   r    r
   len)r#   r-   r%   r&   r   r   dependenciess          r   get_dependenciesr?      sR    '/
6C++C%+@B<<>L
<1AB	r   c                 L   | j                   r
dt        z  }n	dt        z  }|rd|z  }| j                  |d      \  }}}t        |      }|dk7  r| j	                  d|||       t        j                  d	|      r| j                  dd
|||       | j                  dd|||       y )Nz%s upgrade --simulatez
%s upgradez%s --availableFr   r   zfailed to upgrade packagesr   r   r   r   z^OKzpackages already upgradedr   r   r   r   r   Tzupgraded packages)
check_moder   r    r   r!   r   r   r"   )r#   	availabler%   r&   r   r   packagelists          r   upgrade_packagesrF      s    %2h'$++C%+@B$V,K	Qw9&QWbmn	yy ,GPV_ep{|
T':6RXcnor   c                    d}g }g }|D ]  }t        | |      r8t        | |      }|D ]&  }	|dk(  s	t        | |	      r|j                  |	       ( Gt	        | ||      s|j                  |       f|dk(  slt        | |      ry|j                  |        |rd}|s|s| j                  dd       dj                  ||z         }
|r$| j                  rt        d|
}n/t        d|
}n#| j                  rt        d	|
}nt        d
|
}| j                  |d      \  }}}t        |      }|dk7  r| j                  d|
z  |||       | j                  dd|
z  |||       y )NFlatestTzpackage(s) already installedr   r    z add --upgrade --simulate z add --upgrade z add --simulate z add r   r   zfailed to install %srA   zinstalled %s package(s)rB   )r;   r?   r9   r   r1   r"   joinrC   r   r    r   r!   )r#   namesstater.   upgrade
to_install
to_upgrader-   r>   
dependencyr   r%   r&   r   r   rE   s                   r   install_packagesrR     s   GJJ (&+FD9L* 2
H$\&*-M%%j12 "&$6!!$'("<+E!!$'( g,JKxx
Z/0H6>IC+3X>C,4h?C!)84C++C%+@B$V,K	Qw3x@X^itu
T'@H'MV\ek  wB  Cr   c                    g }|D ]   }t        | |      s|j                  |       " |s| j                  dd       dj                  |      }| j                  rt
        d|}nt
        d|}| j                  |d      \  }}}t        |      }|D ]  }t        | |      sd} n |d	k7  r| j                  d
|z  |||       | j                  dd|z  |||       y )NFzpackage(s) already removedrI   rJ   z del --purge --simulate z del --purge r   r	   r   zfailed to remove %s package(s)rA   Tzremoved %s package(s)rB   )	r3   r   r"   rK   rC   r   r    r   r!   )	r#   rL   	installedr-   r%   r&   r   r   rE   s	            r   remove_packagesrU   .  s   I #&T"# ,HIHHYE08%@%-u5++C%+@B$V,K &B 
Qw=GPV_ep{|
T'>%'HQW`fq|}r   c                     t        t        t        dg d      t        dd      t        dd	      t        dd      t        dd	      t        dd	      t        dd	      t        d
d	            g dgddggd      } t        dddd      | _        | j                  dd      a| j
                  }|d   r	t        da|d   r|d   D ]  }t        d|da |d   dv rd|d<   |d   dv rd|d<   |d   rt        | |d    xr |d           |d   rt        | |d          |d   d v rt        | |d   |d   |d!          y |d   dk(  rt        | |d          y y )"Npresent)rW   rT   absentremovedrH   )defaultchoicesliststr)typeelementsFbool)rZ   r^   z/etc/apk/world)rM   r-   no_cache
repositoryupdate_cacherN   rD   r.   )r-   rc   rN   r-   rN   T)argument_specrequired_one_ofmutually_exclusivesupports_check_modeC)LANGLC_ALLLC_MESSAGESLC_CTYPEapk)requiredra   z --no-cacherb   z --repository z --repositories-file /dev/nullrM   )rW   rT   )rX   rY   rX   rc   rD   )rW   rH   r.   )
r   dictrun_command_environ_updateget_bin_pathr   paramsr'   rF   rR   rU   )r#   r   rs      r   mainrt   I  s   y2ij6E2%f5%8e&9V45v6/e<	
 ==#Y/0 F" )-#cs]`(aF% ""54"8HA}&.1 	< 	ZAMUWXYH	Z 	z--'
z**'
&ai-"D)4DE|;0z**6AgJ'
C	
7x	&	* 
 r   __main__)
__future__r   r   r   r^   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   ansible.module_utils.basicr   r   r'   r1   r3   r9   r;   r?   rF   rR   rU   rt   __name__ r   r   <module>r~      s    A @ENJX
 
 4	p $CN~60+f zF r   