
    Vh5                         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dZdd	Zd
 Zd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_functionaZ  
module: xbps
short_description: Manage packages with XBPS
description:
  - Manage packages with the XBPS package manager.
author:
  - "Dino Occhialini (@dinoocch)"
  - "Michael Aldridge (@the-maldridge)"
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  name:
    description:
      - Name of the package to install, upgrade, or remove.
    aliases: [pkg, package]
    type: list
    elements: str
  state:
    description:
      - Desired state of the package.
    default: "present"
    choices: ["present", "absent", "latest", "installed", "removed"]
    type: str
  recurse:
    description:
      - When removing a package, also remove its dependencies, provided that they are not required by other packages and were
        not explicitly installed by a user.
    type: bool
    default: false
  update_cache:
    description:
      - Whether or not to refresh the master package lists. This can be run as part of a package installation or as a separate
        step.
    type: bool
    default: true
  upgrade:
    description:
      - Whether or not to upgrade whole system.
    type: bool
    default: false
  upgrade_xbps:
    description:
      - Whether or not to upgrade the C(xbps) package when necessary. Before installing new packages, C(xbps) requires the
        user to update itself. Thus when this option is set to V(false), upgrades and installations fail when C(xbps) is not
        up to date.
    type: bool
    default: true
    version_added: '0.2.0'
  root:
    description:
      - The full path for the target root directory.
    type: path
    version_added: '10.2.0'
  repositories:
    description:
      - Repository URL(s) to prepend to the repository list for the package installation. The URL can be remote repositories
        or paths for local repositories.
    type: list
    elements: str
    version_added: '10.2.0'
  accept_pubkey:
    description:
      - Whether or not repository signing keys should be automatically accepted.
    type: bool
    default: false
    version_added: '10.2.0'
a  
- name: Install package foo (automatically updating the xbps package if needed)
  community.general.xbps:
    name: foo
    state: present

- name: Upgrade package foo
  community.general.xbps:
    name: foo
    state: latest
    update_cache: true

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

- name: Recursively remove package foo
  community.general.xbps:
    name: foo
    state: absent
    recurse: true

- name: Update package cache
  community.general.xbps:
    update_cache: true

- name: Upgrade packages
  community.general.xbps:
    upgrade: true

- name: Install a package, failing if the xbps package is out of date
  community.general.xbps:
    name: foo
    state: present
    upgrade_xbps: false

- name: Find repository keys to install into a new void system on a mounted partition
  ansible.builtin.find:
    path: /var/db/xbps/keys
    pattern: "*.plist"
  register: xbps_keys

- name: Copy repository keys to into a new void system on a mounted partition
  ansible.builtin.copy:
    remote_src: true
    src: "{{ item }}"
    dest: "/mnt/{{ item }}"
    owner: root
    group: root
    mode: "0644"
  when: xbps_keys.matched > 0
  loop: "{{ xbps_keys.files | map(attribute='path') }}"

- name: Install a new void system on a mounted partition
  community.general.xbps:
    name: base-system
    state: present
    repositories: https://repo-default.voidlinux.org/current
    root: /mnt
z
msg:
  description: Message about results.
  returned: success
  type: str
  sample: "System Upgraded"
packages:
  description: Packages that are affected/would be affected.
  type: list
  sample: ["ansible"]
  returned: success
N)AnsibleModulec                 *    t        t        |             S )zReturns package install state)boollen)xbps_outputs    j/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/xbps.pyis_installedr      s    K !!    c                     | j                   d   r|d| j                   d   }| j                   d   r1|j                  |d         s|s| j                   d   D ]	  }|d|} |S )z-Appends the repository/root flags when neededrootz -r repositoriesremovez --repository=)params
startswith)module	xbps_pathcmd	skip_reporepos        r   append_flagsr      si    }}Vv!67}}^$S^^Ih<O-PYbMM.1 	5D*-t4C	5 Jr   c                    |dk(  rz|d   d|}t        | ||d      }| j                  |d      \  }}}t        |      syd	|d
   z  }t        | ||      }| j                  |d      \  }	}
}|	dk(  s|	dk(  r	 d||
vfS yy)zReturns Package infopresentquery Tr   Fcheck_rc)FFz%s -Suninstallr      N)r   run_commandr   )r   r   namestatelcmdlrclstdoutlstderrrcmdrrcrstdoutrstderrs               r   query_packager.      s    	#G,d3FIttD & 2 24% 2 HWgG$Ii01FIt4 & 2 24% 2 HWg!8sby% W,,,# r   c                     d|d   z  }t        | ||      }| j                  d   rd}nd}| j                  |d|      \  }}}d|v r| j                  d	
       |dk7  r| j                  d
       d|v ryy)z)Returns True if update_package_db changedz%s -Sr!   accept_pubkeyzy
zn
F)r    datazFailed to import pubkeyz&Failed to import pubkey for repositorymsgr   zCould not update package dbzavg rateT)r   r   r#   	fail_json)r   r   r   stdinrcstdoutstderrs          r   update_package_dbr9      s    
Yy)
*C
vy#
.C}}_%++C%e+LB F*EF	Qw:;Vr   c                     d|d   z  }t        | ||      }| j                  |d      \  }}}|dk7  r| j                  d       y y )Nz%s -uy xbpsr!   Fr   r   zCould not upgrade xbps itselfr2   )r   r#   r4   )r   r   exit_on_successcmdupgradexbpsr6   r7   r8   s          r   upgrade_xbpsr=      sY    "i	&:;N!&)^DN++NU+KB	Qw<= r   c                 R   d|d   z  }d|d   z  }t        | ||      }t        | ||      }| j                  |d      \  }}}|dk(  rt        |j                               dk(  r| j	                  dd       y| j
                  r| j	                  d	d
       y| j                  |d      \  }}}|dk(  r| j	                  d	d       y|dk(  r7| j                  d   r(t        | |       d| j                  d<   t        | |       y| j                  d       y| j                  d       y)z%Returns true is full upgrade succeedsz%s -uyr!   z%s -unFr   r   zNothing to upgradechangedr3   TzWould have performed upgradezSystem upgraded   r=   zCould not upgrader2   N)
r   r#   r	   
splitlines	exit_json
check_moder   r=   upgrader4   )r   r   
cmdupgradecmdneedupgrader6   r7   r8   s          r   rE   rE      s5   Yy12J9!56Nfi<J!&)^DN++NU+KB	Qwv  "#q(U0DET/MN!'!3!3J!3!OBQw  3D ErfmmN;VY/05n-	*  %8 901r   c                 t   g }|D ]p  }t        | ||      \  }}|s|d   d|}t        | ||d      }| j                  |d      \  }}	}
|dk7  r| j                  d|z  	       |j	                  |       r t        |      dkD  r | j                  dd
t        |      z  |       | j                  dd       y)z(Returns true if package removal succeedsr    -y Tr   Fr   r   zfailed to remove %sr2   zremoved %s package(s)r@   r3   packageszpackage(s) already absentr?   N)r.   r   r#   r4   appendr	   rC   )r   r   rK   changed_packagespackage	installedupdatedr   r6   r7   r8   s              r   remove_packagesrQ     s     )*69gF	7%h/969cTB#//e/DFF7!6'!BC() q +B-.,/9I 	 	K U(CDr   c                 d   g }t        |      D ]6  \  }}	 t        | ||      \  }}|r|dk(  s|dk(  r|r&|j                  |       8 t        |      dk(  r| j	                  dd       |d   dd	j                  |      }	t        | ||	      }	| j                  |	d
      \  }
}}|
dk(  r9| j                  d   r*t        | |       d| j                  d<   t        | |||       n.|
dk7  r)|dk(  r|
dk(  s| j                  dt        |      z  |       | j	                  ddt        |      z  |       y)z)Returns true if package install succeeds.r   latestr   FzNothing to Installr?   r!   rI   r   r   rA   r=   r"   z failed to install %s packages(s))r3   rK   Tzinstalled %s package(s)rJ   N)	enumerater.   rL   r	   rC   joinr   r#   r   r=   install_packagesr4   )r   r   r%   rK   	toInstallirN   rO   rP   r   r6   r7   r8   s                r   rV   rV   "  sZ   I) "
7	'*69gF	7%9, H,!" 9~,@A!),chhy.A
BC
vy#
.C++C%+@B	RxFMM.1VY'(-n%E8<	q%8+b?	N,"+ 	 	- T'@I(('  )r   c                 
   g }|D ]9  }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  g        y
)z Returns change status of commandr   rS   absentrS   removedTz package(s) would be rJ   Fzpackage(s) already %sN)r.   rL   rC   r	   )r   r   rK   r%   would_be_changedrN   rO   rP   s           r   check_packagesr^   E  s     -*69gF	7++I("y("7##G,- HE !5,*% 	 	' 	,Ce,K"$ 	 	&r   c                     | j                   r|ry| j                  dd       t        | |      }|s| j                  ||rdnd       yy)zUpdate package cacheNTz$Would have updated the package cacher?   z Updated the package master listszPackage list already up to date)rD   rC   r9   )r   r   upgrade_plannedr@   s       r   update_cachera   Y  s^    D 	 	
  	2G29.2 	 	 r   c                  r   t        t        t        dddgdd      t        dg d	      t        d
d      t        d
d      t        dd      t        dd      t        d      t        dd      t        d
d      	      g dgd      } t               }| j                  dd      |d<   | j                  dd      |d<   | j                  dd      |d<   t        j                  j                  |d         s| j                  d|d   z         | j                  }|d   dv rd|d<   n|d   dv rd|d<   |d    rt        | ||d!   xs |d"          |d"   rt        | |       |d!   rR|d!   }| j                  rt        | |||d          |d   d#v rt        | ||d   |       y|d   dk(  rt        | ||       yyy)$z$Returns, calling appropriate commandNpkgrN   liststr)defaultaliasestypeelementsr   )r   rO   rS   r[   r\   )rf   choicesFr   )rf   rh   Tpath)rh   )rh   ri   )	r$   r%   recurserE   ra   r=   r   r   r0   )r$   ra   rE   )argument_specrequired_one_ofsupports_check_modezxbps-installr!   z
xbps-queryr   zxbps-remover   zcannot find xbps, in path %sr2   r%   )r   rO   )r[   r\   r[   ra   r$   rE   rZ   )r   dictget_bin_pathosrk   existsr4   r   ra   rE   rD   r^   rV   rQ   )r   r   ppkgss       r   mainrv   i  s    dUI,>VV[\y 3> ? V4V4d8d86"6E:u6:
 == "F" I!..~tDIi,,\4@Ig --mTBIh77>>)I./;%i02 	3 	A 	z--'
	
7,	,'
VY6)Ba	lD|	"yy69dAgJ?W:..VY'
DAwZ8#FIt4 $ r   __main__)F)r   )
__future__r   r   r   rh   __metaclass__DOCUMENTATIONEXAMPLESRETURNrr   ansible.module_utils.basicr   r   r   r.   r9   r=   rE   rQ   rV   r^   ra   rv   __name__ r   r   <module>r      s    A @GR>@
 
 4"
,(>28E6 )F&( 45n zF r   