
    VhA                    0   d dl mZ dZdZdZd dlZd dlmZmZ d dl	m
Z
 d dlmZ d d	lmZmZmZ g d
ddgdgdZ G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zd Zedk(  r e        yy)    )annotationsa&	  
module: package_facts
short_description: Package information as facts
description:
  - Return information about installed packages as facts.
options:
  manager:
    description:
      - The package manager(s) used by the system so we can query the package information.
        This is a list and can support multiple package managers per system, since version 2.8.
      - The V(portage) and V(pkg) options were added in version 2.8.
      - The V(apk) option was added in version 2.11.
      - The V(pkg_info)' option was added in version 2.13.
      - Aliases were added in 2.18, to support using C(manager={{ansible_facts['pkg_mgr']}})
    default: ['auto']
    choices:
        auto: Depending on O(strategy), will match the first or all package managers provided, in order
        rpm: For RPM based distros, requires RPM Python bindings, not installed by default on Suse (python3-rpm)
        yum: Alias to rpm
        dnf: Alias to rpm
        dnf5: Alias to rpm
        zypper: Alias to rpm
        apt: For DEB based distros, C(python-apt) package must be installed on targeted hosts
        portage: Handles ebuild packages, it requires the C(qlist) utility, which is part of 'app-portage/portage-utils'
        pkg: libpkg front end (FreeBSD)
        pkg5: Alias to pkg
        pkgng: Alias to pkg
        pacman: Archlinux package manager/builder
        apk: Alpine Linux package manager
        pkg_info: OpenBSD package manager
        openbsd_pkg: Alias to pkg_info
    type: list
    elements: str
  strategy:
    description:
      - This option controls how the module queries the package managers on the system.
    choices:
        first: returns only information for the first supported package manager available.
        all: returns information for all supported and available package managers on the system.
    default: 'first'
    type: str
    version_added: "2.8"
version_added: "2.5"
requirements:
    - See details per package manager in the O(manager) option.
author:
  - Matthew Jones (@matburt)
  - Brian Coca (@bcoca)
  - Adam Miller (@maxamillion)
extends_documentation_fragment:
  -  action_common_attributes
  -  action_common_attributes.facts
attributes:
    check_mode:
        support: full
    diff_mode:
        support: none
    facts:
        support: full
    platform:
        platforms: posix
a  
- name: Gather the package facts
  ansible.builtin.package_facts:
    manager: auto

- name: Print the package facts
  ansible.builtin.debug:
    var: ansible_facts.packages

- name: Check whether a package called foobar is installed
  ansible.builtin.debug:
    msg: "{{ ansible_facts.packages['foobar'] | length }} versions of foobar are installed!"
  when: "'foobar' in ansible_facts.packages"

a  
ansible_facts:
  description: Facts to add to ansible_facts.
  returned: always
  type: complex
  contains:
    packages:
      description:
        - Maps the package name to a non-empty list of dicts with package information.
        - Every dict in the list corresponds to one installed version of the package.
        - The fields described below are present for all package managers. Depending on the
          package manager, there might be more fields for a package.
      returned: when operating system level package manager is specified or auto detected manager
      type: dict
      contains:
        name:
          description: The package's name.
          returned: always
          type: str
        version:
          description: The package's version.
          returned: always
          type: str
        source:
          description: Where information on the package came from.
          returned: always
          type: str
      sample: |-
        {
          "packages": {
            "kernel": [
              {
                "name": "kernel",
                "source": "rpm",
                "version": "3.10.0",
                ...
              },
              {
                "name": "kernel",
                "source": "rpm",
                "version": "3.10.0",
                ...
              },
              ...
            ],
            "kernel-tools": [
              {
                "name": "kernel-tools",
                "source": "rpm",
                "version": "3.10.0",
                ...
              }
            ],
            ...
          }
        }
        # Sample rpm
        {
          "packages": {
            "kernel": [
              {
                "arch": "x86_64",
                "epoch": null,
                "name": "kernel",
                "release": "514.26.2.el7",
                "source": "rpm",
                "version": "3.10.0"
              },
              {
                "arch": "x86_64",
                "epoch": null,
                "name": "kernel",
                "release": "514.16.1.el7",
                "source": "rpm",
                "version": "3.10.0"
              },
              {
                "arch": "x86_64",
                "epoch": null,
                "name": "kernel",
                "release": "514.10.2.el7",
                "source": "rpm",
                "version": "3.10.0"
              },
              {
                "arch": "x86_64",
                "epoch": null,
                "name": "kernel",
                "release": "514.21.1.el7",
                "source": "rpm",
                "version": "3.10.0"
              },
              {
                "arch": "x86_64",
                "epoch": null,
                "name": "kernel",
                "release": "693.2.2.el7",
                "source": "rpm",
                "version": "3.10.0"
              }
            ],
            "kernel-tools": [
              {
                "arch": "x86_64",
                "epoch": null,
                "name": "kernel-tools",
                "release": "693.2.2.el7",
                "source": "rpm",
                "version": "3.10.0"
              }
            ],
            "kernel-tools-libs": [
              {
                "arch": "x86_64",
                "epoch": null,
                "name": "kernel-tools-libs",
                "release": "693.2.2.el7",
                "source": "rpm",
                "version": "3.10.0"
              }
            ],
          }
        }
        # Sample deb
        {
          "packages": {
            "libbz2-1.0": [
              {
                "version": "1.0.6-5",
                "source": "apt",
                "arch": "amd64",
                "name": "libbz2-1.0"
              }
            ],
            "patch": [
              {
                "version": "2.7.1-4ubuntu1",
                "source": "apt",
                "arch": "amd64",
                "name": "patch"
              }
            ],
          }
        }
        # Sample pkg_info
        {
          "packages": {
            "curl": [
              {
                  "name": "curl",
                  "source": "pkg_info",
                  "version": "7.79.0"
              }
            ],
            "intel-firmware": [
              {
                  "name": "intel-firmware",
                  "source": "pkg_info",
                  "version": "20210608v0"
              }
            ],
          }
        }
N)	to_nativeto_text)AnsibleModule)get_best_parsable_locale)CLIMgrRespawningLibMgrget_all_pkg_managers)dnfdnf5yumzypperpkg5pkgngopenbsd_pkg)rpmpkgpkg_infoc                  *    e Zd ZdZdgZddgZd Zd Zy)RPMr   z/usr/libexec/platform-pythonz/usr/bin/python3c                R    | j                   j                         j                         S N)_libTransactionSetdbMatchselfs    M/home/dcms/DCMS/lib/python3.12/site-packages/ansible/modules/package_facts.pylist_installedzRPM.list_installed  s    yy'')1133    c                   t        || j                  j                     || j                  j                     || j                  j                     || j                  j
                     || j                  j                           S )N)nameversionreleaseepocharch)dictr   RPMTAG_NAMERPMTAG_VERSIONRPMTAG_RELEASERPMTAG_EPOCHRPMTAG_ARCHr   packages     r   get_package_detailszRPM.get_package_details  sh    !6!67#DII$<$<=#DII$<$<=!$))"8"89 !6!67	: 	:r    N)__name__
__module____qualname__LIBCLI_BINARIESINTERPRETERSr   r/    r    r   r   r     s%    
C7L&L
4:r    r   c                  F     e Zd ZdZg dZ fdZed        Zd Zd Z	 xZ
S )APTapt)r9   zapt-getaptitudec                8    d | _         t        t        |           y r   )_cachesuperr8   __init__)r   	__class__s    r   r>   zAPT.__init__%  s    c4!#r    c                    | j                   | j                   S | j                  j                         | _         | j                   S r   )r<   r   Cacher   s    r   	pkg_cachezAPT.pkg_cache)  s2    ;;";;iioo'{{r    c                    | j                   }|j                         D cg c]  }||   j                  s| c}S c c}w r   )rB   keysis_installed)r   cachepks      r   r   zAPT.list_installed1  s1    "ZZ\DrU2Y-C-CDDDs   ;;c                    | j                   |   j                  }t        ||j                  |j                  |j
                  |j                  d   j                        S )Nr   )r"   r#   r&   categoryorigin)rB   	installedr'   r#   architecturesectionoriginsrJ   )r   r.   ac_pkgs      r   r/   zAPT.get_package_details6  sk    (22&..v?R?R]c]k]ktz  uC  uC  DE  uF  uM  uM  N  	Nr    )r0   r1   r2   r3   r4   r>   propertyrB   r   r/   __classcell__)r?   s   @r   r8   r8      s2    
C1L$  E
Nr    r8   c                      e Zd ZdZd Zd Zy)PACMANpacmanc                    t        t              }t        j                  | j                  dgt	        |            \  }}}|dk7  s|rt        d|d|      |j                  d      d d S )	Nz-Qi)LC_ALL)environ_updater   Unable to list packages rc= : z

)r   modulerun_command_clir'   	Exceptionsplit)r   localercouterrs        r   r   zPACMAN.list_installed?  se    )&1))499e*<TY_M`)aC7cBLMMyy "%%r    c                   i }d }|j                         D ]\  }t        j                  d|      }|r&|j                  d      }|j                  d      ||<   A||   dz   |j	                         z   ||<   ^ d }|d   dk7  r3|d   j                  d      D cg c]  }|j                  d      d    }}|d	   |d
   |d   |dS c c}w )Nz([\w ]*[\w]) +: (.*)      z  ProvidesNone=r   NameVersionArchitecture)r"   r#   r&   provides)
splitlinesrematchgrouplstripr_   )r   r.   raw_pkg_detailslast_detaillinemrm   ps           r   r/   zPACMAN.get_package_detailsF  s    &&( 	cD0$7Aggaj/0wwqz, 0?{/Kd/RUYU`U`Ub/b,	c :&&0 )4::4@ QH  $F+&y1#N3 	
 	
s   CNr0   r1   r2   CLIr   r/   r6   r    r   rS   rS   ;  s    
C&
r    rS   c                  $    e Zd ZdZg dZd Zd Zy)PKGr   )	r"   r#   rJ   rK   	automaticr&   rI   prefixvitalc           	         t         j                  | j                  dddj                  g d      z  g      \  }}}|dk7  s|rt	        d|d|      |j                         S )Nqueryz%%%sz	%)	nvRtaqorw   Vr   rX   rY   )r[   r\   r]   joinr^   rn   r   ra   rb   rc   s       r   r   zPKG.list_installedg  sj    ))499gv

  TA  IB  @B  +C  DC7cBLMM~~r    c                (   t        t        | j                  |j                  d                  }d|v r	 |d   j                  d      d   |d<   d|v rt        t        |d               |d<   d|v r|d   j                  dd      d	   |d<   d
|v rVd|d
   v r|d
   j                  dd      \  |d
<   |d<   nd	|d<   d|d
   v r|d
   j                  dd      \  |d
<   |d<   nd|d<   d|v rt        t        |d               |d<   |S # t        $ r Y w xY w)N	r&   :rf   r|   rI   /re   r   r#   ,
port_epoch_revision0r~   )r'   zipatomsr_   
IndexErrorboolint)r   r.   r   s      r   r/   zPKG.get_package_detailsm  sD   3tzz7==#678S=!&k//4Q7F ##CK(8$9:C!*o33C;A>C
Oc)n$47	N4H4Ha4P1IL 1$%L!c)n$25i.2F2FsA2N/IJ"%Jc>CL 12CL
/  s   D 	DDNr0   r1   r2   ry   r   r   r/   r6   r    r   r{   r{   b  s    
CjE r    r{   c                  $    e Zd ZdZg dZd Zd Zy)PORTAGEqlist)rI   r"   r#   ebuild_revisionslotsprefixessufixesc                    t         j                  dj                  | j                  ddddddg      d	      \  }}}|d
k7  rt	        d|dt        |            |j                         S )N z-Iv|xargsz-n1024qatomT)use_unsafe_shellr   rX   rY   )r[   r\   r   r]   RuntimeErrorr   rn   r   s       r   r   zPORTAGE.list_installed  sm    ))#((DIIuc7TXZ`bi3j*k  C)  DC7r9UX>Z[[~~r    c                \    t        t        | j                  |j                                     S r   )r'   r   r   r_   r-   s     r   r/   zPORTAGE.get_package_details  s    C

GMMO455r    Nr   r6   r    r   r   r     s    
C^E 6r    r   c                      e Zd ZdZd Zd Zy)APKapkc                    t         j                  | j                  ddg      \  }}}|dk7  rt        d|d|      |j	                         S )Ninfoz-vr   rX   rY   r[   r\   r]   r^   rn   r   s       r   r   zAPK.list_installed  sG    ))499fd*CDC7BLMM~~r    c                t    |ddd}|j                  dd      }	 |d   |d   |d   dS # t        $ r |cY S w xY w)N )r"   r#   r$   -rf   r   re   )rsplitr   )r   r.   rs   nvrs       r   r/   zAPK.get_package_details  sY    #*rbInnS!$	#Aq6q6 
  	#""	#s   ) 77Nrx   r6   r    r   r   r     s    
C 
#r    r   c                      e Zd ZdZd Zd Zy)PKG_INFOr   c                    t         j                  | j                  dg      \  }}}|dk7  s|rt        d|d|      |j	                         S )Nz-ar   rX   rY   r   r   s       r   r   zPKG_INFO.list_installed  sG    ))499d*;<C7cBLMM~~r    c                    |dd}|j                  d      d   j                  dd      }	 |d   |d   dS # t        $ r |cY S w xY w)Nr   )r"   r#   re   )maxsplitr   r   )r_   r   r   )r   r.   rs   detailss       r   r/   zPKG_INFO.get_package_details  se    #*r:---+A.55cA5F	#
"1:   	#""	#s   
8 AANrx   r6   r    r   r   r     s    
C 
#r    r   c            
        t               } | j                         D cg c]  }|j                          }}|j                  t        j                         D cg c]  }|D ]  }|  c}}       t        t        dddgdddgdd      d	
      ai }di i}t        j                  d   D cg c]  }|j                          }}t        j                  d   }d|v r"|j                  |       |j                  d       t        |      j                  |      }	|	rBdt        j                  d   v rd}
nddj                  |	      z  }
t        j                  |
       d}t               }|D ]  }|dk(  r|r nt        j                         D ]  }|t        |   v s|} n ||v r9|j                  |        | |          }	 |j!                  d      r%|dz  }	 |j#                  |j%                                 |dk(  rd|z  }
t        j                  |
       ||d   d<   t        j,                  di | y c c}w c c}}w c c}w # t&        $ r.}t        j)                  d|dt+        |             Y d }~d }~ww xY w# t&        $ rD}|t        j                  d   v r$t        j)                  d|dt+        |             Y d }~^d }~ww xY w)Nliststrauto)typeelementsdefaultfirstall)choicesr   )managerstrategyT)argument_specsupports_check_modeansible_factsr   r   zKCould not auto detect a usable package manager, check warnings for details.z*Unsupported package managers requested: %sz, )msgr   F)handle_exceptionsre   z!Failed to retrieve packages with z: zRequested package manager z  was not usable by this module: zCould not detect a supported package manager from the following list: %s, or the required Python library is not installed. Check warnings for details.packagesr6   )r
   rD   lowerextendALIASESvaluesr   r'   r[   paramsremoveset
differencer   	fail_jsonaddis_availableupdateget_packagesr^   warnr   	exit_json)PKG_MANAGERSxPKG_MANAGER_NAMESalistaliasr   resultsmanagersr   unsupportedr   foundseenpkgmgraliasedr   es                    r   mainr     s    ()L,8,=,=,?@q@@1AUuUeeUeUV vSXflem6nDKUCS`g7h*j/35F H#G#)==#;<a	<H<}}Z(H)*h-**+<=KV]]9--_C>$))KBXYCS!E5D uw5 ||~ 	G)) 	 T>&,v&(		u##e#<
bOOG$8$8$:;-u: z^`hiS! ,4GOZ(
wK AU =R ! bKKVU\]^U_ `aab 	uy11`fhopqhrst	usM   H7H<
-I JI	I>$I94J9I>>J	K
9K		K__main__)
__future__r   DOCUMENTATIONEXAMPLESRETURNro   +ansible.module_utils.common.text.convertersr   r   ansible.module_utils.basicr   "ansible.module_utils.common.localer   #ansible.module_utils.facts.packagesr   r	   r
   r   r   r8   rS   r{   r   r   r   r   r0   r6   r    r   <module>r      s    #=~ c
J 
 J 4 G ^ ^ -G:
 :(N
 N6$
V $
N)& )X6f 6#& #.#v #.I X zF r    