
    Vh+                         d dl mZmZmZ eZdZdZd dlZd dl	Z	d dl
Z
d dlmZmZ d dlmZ 	 d dlZdZdZd	Zd
ZdZdZdZd Zd Zd ZddZd Zd Zd Z d Z!d Z"d Z#ddZ$d Z%e&dk(  r e%        yy# e$ r dZ e
j(                         ZY Xw xY w)    )absolute_importdivisionprint_functionaO	  
module: apt_rpm
short_description: APT-RPM package manager
description:
  - Manages packages with C(apt-rpm). Both low-level (C(rpm)) and high-level (C(apt-get)) package manager binaries required.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
options:
  package:
    description:
      - List of packages to install, upgrade, or remove.
      - Since community.general 8.0.0, may include paths to local C(.rpm) files if O(state=installed) or O(state=present),
        requires C(rpm) Python module.
    aliases: [name, pkg]
    type: list
    elements: str
  state:
    description:
      - Indicates the desired package state.
      - Please note that V(present) and V(installed) are equivalent to V(latest) right now. This will change in the future.
        To simply ensure that a package is installed, without upgrading it, use the V(present_not_latest) state.
      - The states V(latest) and V(present_not_latest) have been added in community.general 8.6.0.
    choices:
      - absent
      - present
      - present_not_latest
      - installed
      - removed
      - latest
    default: present
    type: str
  update_cache:
    description:
      - Run the equivalent of C(apt-get update) before the operation. Can be run as part of the package installation or as
        a separate step.
      - Default is not to update the cache.
    type: bool
    default: false
  clean:
    description:
      - Run the equivalent of C(apt-get clean) to clear out the local repository of retrieved package files. It removes everything
        but the lock file from C(/var/cache/apt/archives/) and C(/var/cache/apt/archives/partial/).
      - Can be run as part of the package installation (clean runs before install) or as a separate step.
    type: bool
    default: false
    version_added: 6.5.0
  dist_upgrade:
    description:
      - If true performs an C(apt-get dist-upgrade) to upgrade system.
    type: bool
    default: false
    version_added: 6.5.0
  update_kernel:
    description:
      - If true performs an C(update-kernel) to upgrade kernel packages.
    type: bool
    default: false
    version_added: 6.5.0
requirements:
  - C(rpm) Python package (rpm bindings), optional. Required if O(package) option includes local files.
author:
  - Evgenii Terechkov (@evgkrsk)
aj  
- name: Install package foo
  community.general.apt_rpm:
    pkg: foo
    state: present

- name: Install packages foo and bar
  community.general.apt_rpm:
    pkg:
      - foo
      - bar
    state: present

- name: Remove package foo
  community.general.apt_rpm:
    pkg: foo
    state: absent

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

# bar will be the updated if a newer version exists
- name: Update the package database and install bar
  community.general.apt_rpm:
    name: bar
    state: present
    update_cache: true

- name: Run the equivalent of "apt-get clean" as a separate step
  community.general.apt_rpm:
    clean: true

- name: Perform cache update and complete system upgrade (includes kernel)
  community.general.apt_rpm:
    update_cache: true
    dist_upgrade: true
    update_kernel: true
N)AnsibleModulemissing_required_lib)	to_nativeTFz/usr/bin/apt-cachez/usr/bin/apt-getz/usr/bin/rpmz
0 upgraded, 0 newly installedz
Try to install new kernel c                    t        j                         }|j                  t         j                         t	        j
                  | t        j                        }	 |j                  |      }	 t	        j                  |       t        |t         j                           S # t         j                  $ r}Y d}~t	        j                  |       yd}~ww xY w# t	        j                  |       w xY w)zQreturn package name of a local rpm passed in.
    Inspired by ansible.builtin.yumN)rpmTransactionSet
setVSFlags_RPMVSF_NOSIGNATURESosopenO_RDONLYhdrFromFdnoerrorcloser   RPMTAG_NAME)pathtsfdheaderes        m/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/apt_rpm.pylocal_rpm_package_namer      s     
			BMM#**+	r{{	#B# 	VCOO,-- 99 
 	s$   B C/C 	CC C(c                 H    | j                  t        d|g      \  }}}|dk(  ryy)N-qr   TF)run_commandRPM_PATH)modulenamercouterrs        r   query_packager%      s.     %%xt&<=LBS	Qw    c                     | j                  t        d|gddi      \  }}}t        j                  d|      d   }t        j                  d|      d   }||k\  ryy	)
NpolicyLANGCenviron_updatez
 |:       TF)r   	APT_CACHEresplit)r    r!   r"   r#   r$   	installed	candidates          r   check_package_versionr4      se    
 %%y(D&ASY[^R_%`LBS3'*I3'*IIr&   c                     |j                  d      r1t        s | j                  t        d      t               t        |      }| j                  t        dd|g      \  }}}|dk(  r|syt        | |      ryy)	Nz.rpmr
   )msg	exceptionr   z
--providesr   TF)	endswithHAS_RPM_PYTHON	fail_jsonr   RPM_PYTHON_IMPORT_ERRORr   r   r   r4   )r    r!   allow_upgrader"   r#   r$   s         r   query_package_providesr=      sy     }}V(/1  
 &d+%%x|T&JKLBS	Qw .r&   c                 J    | j                  t        dgdddi      \  }}}d|fS )NupdateTr)   r*   check_rcr,   F)r   APT_PATH)r    r"   
update_outr$   s       r   update_package_dbrD      s7     ,,h-ADbhjman,oB
C:r&   c           	          d}t        j                  |      D ]M  \  }}}|D ]B  }|t         j                  j                  t         j                  j	                  ||            z  }D O |S )Nr   )r   walkr   getsizejoin)r    r   
total_sizedirsfilesfs         r   dir_sizerM      sf    JWWT] AdE 	AA"''//"'',,tQ*?@@J	AA r&   c                 v    t        | d      }| j                  t        dgd      \  }}}|t        | d      k7  |fS )Nz/var/cache/apt/archivescleanT)rA   )rM   r   rB   )r    tr"   r#   r$   s        r   rO   rO      sG    23A%%x&9D%ILBS&";<<cBBr&   c                 X    | j                  t        ddgdddi      \  }}}t        |v|fS )N-yzdist-upgradeTr)   r*   r@   )r   rB   APT_GET_ZEROr    r"   r#   r$   s       r   dist_upgraderU      s>    %%x~&FQUgmorfs%tLBS#S))r&   c                 N    | j                  ddgdddi      \  }}}t        |v|fS )Nz/usr/sbin/update-kernelrR   Tr)   r*   r@   )r   UPDATE_KERNEL_ZEROrT   s       r   update_kernelrX      s=    %%'@$&GRVhnpsgt%uLBSc)3//r&   c                     |yd}|D ]R  }t        | |      s| j                  t        dd|gddi      \  }}}|dk7  r| j                  d|d	|
       |dz  }T |dkD  rdd|z  fS y)NFzEmpty package listr   rR   remover)   r*   r+   zfailed to remove z: r6      Tzremoved %s package(s))Fzpackage(s) already absent)r%   r   rB   r:   )r    packagesremove_cpackager"   r#   r$   s          r   remove_packagesra      s    ,H 
VW-))8T8W*M_egj^k)lC7gs!KLA
 !|-899/r&   c                 D   |yg }|D ]"  }t        | ||      r|j                  |       $ |rrt        ddg|z   }| j                  |ddi      \  }}}d}	|D ]  }t        | |d	      rd	}	 |s|	s(| j	                  d
dj                  |      d|       y dd|z  fS y)NrZ   r<   rR   installr)   r*   r+   TF' z
' failed: r\   z%s present(s))FzNothing to install)r=   appendrB   r   r:   rH   )
r    pkgspecr<   r^   r`   commandr"   r#   r$   r2   s
             r   install_packagesrj   
  s    ,H %%fg]SOOG$% T9-8))'63-)PC	 	"G)&'O!		"
 Ychhw6G!MN/H455,r&   c                  x   t        t        t        ddg d      t        dd      t        dd      t        dd      t        dd      t        ddd	d
g                  } t        j                  j	                  t
              r#t        j                  j	                  t              s| j                  d       | j                  }|d   dv r!| j                  d|d   d|d   ddd       d}d}|d   rt        |        |d   rt        |       \  }}|xs |}|d   rt        |       \  }}|xs |}||z  }|d   rt        |       \  }}|xs |}||z  }|d   }|d   dv r"t        | ||d   dk7         \  }}|xs |}||z  }|d   d!v rt        | |      \  }}|xs |}||z  }| j!                  ||"       y )#Nstrpresent)absentr2   rm   removedpresent_not_latestlatest)typedefaultchoicesboolF)rr   rs   listr!   pkg)rr   elementsaliases)stateupdate_cacherO   rU   rX   r`   )argument_specz0cannot find /usr/bin/apt-get and/or /usr/bin/rpmr\   rz   )r2   rm   zstate=a?   currently behaves unexpectedly by always upgrading to the latest version if the package is already installed. This behavior is deprecated and will change in community.general 11.0.0. You can use state=latest to explicitly request this behavior or state=present_not_latest to explicitly request the behavior that state=zp will have in community.general 11.0.0, namely that the package will not be upgraded if it is already installed.z11.0.0zcommunity.general)versioncollection_name r{   rO   rU   rX   r`   )r2   rm   rp   rq   rp   rc   )rn   ro   )changedr6   )r   dictr   r   existsrB   r   r:   params	deprecaterD   rO   rU   rX   rj   ra   	exit_json)r    pmodifiedoutputmr#   r^   s          r   mainr   &  s   E9  ?L  M659FE2659FE:fuvuoN
	F 77>>(#277>>(+COPAz-- &'wZ7= / 	 		
 HF&!z=C=q'C=q# (C=q#|HzMM#FHAgJRfDfgC=q#z**"684C=q# X62r&   __main__)F)'
__future__r   r   r   rr   __metaclass__DOCUMENTATIONEXAMPLESr   r0   	tracebackansible.module_utils.basicr   r   +ansible.module_utils.common.text.convertersr   r
   r9   r;   ImportError
format_excr/   rB   r   rS   rW   r   r%   r4   r=   rD   rM   rO   rU   rX   ra   rj   r   __name__ r&   r   <module>r      s    A @CJ'R 
 	  B#
 N" 	03 ."
,
C*
0
02-8<3~ zF {  5N2i2245s   A2 2BB