
    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
mZ dZ	 d dlmZ d dlmZ dZd d
lmZmZ d dlmZ dZ G d de      ZddZd ZddZd Zd Z d Z!d Z"e#dk(  r e"        yy# e$ r  e	j&                         Zd	ZY Zw xY w)    )absolute_importdivisionprint_functionam  
module: layman
author: "Jakub Jirutka (@jirutka)"
short_description: Manage Gentoo overlays
description:
  - Uses Layman to manage an additional repositories for the Portage package manager on Gentoo Linux. Please note that Layman
    must be installed on a managed node prior using this module.
requirements:
  - layman python module
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  name:
    description:
      - The overlay ID to install, synchronize, or uninstall. Use V(ALL) to sync all of the installed overlays (can be used
        only when O(state=updated)).
    required: true
    type: str
  list_url:
    description:
      - An URL of the alternative overlays list that defines the overlay to install. This list will be fetched and saved under
        C(${overlay_defs}/${name}.xml), where C(overlay_defs) is read from the Layman's configuration.
    aliases: [url]
    type: str
  state:
    description:
      - Whether to install (V(present)), sync (V(updated)), or uninstall (V(absent)) the overlay.
    default: present
    choices: [present, absent, updated]
    type: str
  validate_certs:
    description:
      - If V(false), SSL certificates will not be validated. This should only be set to V(false) when no other option exists.
    type: bool
    default: true
a  
- name: Install the overlay mozilla which is on the central overlays list
  community.general.layman:
    name: mozilla

- name: Install the overlay cvut from the specified alternative list
  community.general.layman:
    name: cvut
    list_url: 'http://raw.github.com/cvut/gentoo-overlay/master/overlay.xml'

- name: Update (sync) the overlay cvut or install if not installed yet
  community.general.layman:
    name: cvut
    list_url: 'http://raw.github.com/cvut/gentoo-overlay/master/overlay.xml'
    state: updated

- name: Update (sync) all of the installed overlays
  community.general.layman:
    name: ALL
    state: updated

- name: Uninstall the overlay cvut
  community.general.layman:
    name: cvut
    state: absent
N)path)	LaymanAPI)
BareConfigTF)AnsibleModulemissing_required_lib)	fetch_urlzansible-httpgetc                       e Zd Zy)ModuleErrorN)__name__
__module____qualname__     l/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/layman.pyr   r   g   s    r   r   c                 6    | t        dd      } t        |       S )zlReturns the initialized ``LaymanAPI``.

    :param config: the layman's configuration to use (optional)
    T   )read_configfile	quietness)r   r   )configs    r   init_laymanr   k   s     
 ~DA>Vr   c                 @   t         | j                  d<   t        | |      \  }}|d   dk7  rt        d|d|d         	 t	        |d      5 }t        j                  ||       ddd       y# 1 sw Y   yxY w# t        $ r}t        d	t        |      z        d}~ww xY w)
z
    :param url: the URL to download
    :param dest: the absolute path of where to save the downloaded content to;
        it must be writable and not a directory

    :raises ModuleError
    
http_agentstatus   zFailed to get z: msgwNzFailed to write: %s)		USERAGENTparamsr   r   openshutilcopyfileobjIOErrorstr)moduleurldestresponseinfofes          r   download_urlr.   u   s     #,FMM,vs+NHdH~CeEFF:$_ 	,x+	, 	, 	, :/#a&899:s6    A8 A,#A8 ,A51A8 5A8 8	BBBc                    t        d      }t        |      }|j                  |      ry| j                  rd|z   dz   }| j	                  d|       |j                  |      sQ|st        d|z        |j                  d      }t        j                  ||d	z         }t        | ||       t               }|j                  |      st        |j                               y)
aN  Installs the overlay repository. If not on the central overlays list,
    then :list_url of an alternative list must be provided. The list will be
    fetched and saved under ``%(overlay_defs)/%(name.xml)`` (location of the
    ``overlay_defs`` is read from the Layman's configuration).

    :param name: the overlay id
    :param list_url: the URL of the remote repositories list to look for the overlay
        definition (optional, default: None)

    :returns: True if the overlay was installed, or False if already exists
        (i.e. nothing has changed)
    :raises ModuleError
    T)r   FzWould add layman repo ''changedr   z^Overlay '%s' is not on the list of known overlays and URL of the remote list was not provided.overlay_defsz.xml)r   r   is_installed
check_mode	exit_jsonis_repor   
get_optionr   joinr.   	add_repos
get_errors)r'   namelist_urllayman_conflaymanmymsgr3   r)   s           r   install_overlayrA      s     T2K%F4 *T1D851>>$ VX\] ^ ^ #--n=yytf}5VXt, D!&++-..r   c                     t               }|j                  |      sy| j                  rd|z   dz   }| j                  d|       |j	                  |       |j                         rt        |j                               y)zUninstalls the given overlay repository from the system.

    :param name: the overlay id to uninstall

    :returns: True if the overlay was uninstalled, or False if doesn't exist
        (i.e. nothing has changed)
    :raises ModuleError
    FzWould remove layman repo 'r0   Tr1   )r   r4   r5   r6   delete_reposr;   r   )r'   r<   r?   r@   s       r   uninstall_overlayrD      sx     ]Ft$-4t;51
&++-..r   c                     t               }|j                  |       s3|j                  d   D cg c]  }t        |d          }}t	        |      yc c}w )zSynchronizes the specified overlay repository.

    :param name: the overlay repository id to sync
    :raises ModuleError
       r   N)r   syncsync_resultsr&   r   )r<   r?   itemmessagess       r   sync_overlayrK      sP     ]F;;t-3-@-@-CDTCQLDD(## Ds   Ac                  X    t               } | j                         D ]  }t        |        y)zHSynchronize all of the installed overlays.

    :raises ModuleError
    N)r   get_installedrK   )r?   r<   s     r   sync_overlaysrN      s+    
 ]F$$& Tr   c                  0   t        t        t        d      t        dg      t        dg d      t        ddd	
            d      t        s j                  t	        d      t
               fddD        \  } }}d}	 | dk(  rt        ||      }n=| dk(  r,|dk(  rt                n(t        ||      rd}nt        |       nt        |      }j                  ||       y # t        $ r&}j                  |j                         Y d }~y d }~ww xY w)NT)requiredr(   )aliasespresent)rR   absentupdated)defaultchoicesFbool)rP   rU   type)r<   r=   statevalidate_certs)argument_specsupports_check_modeLayman)r   	exceptionc              3   <   K   | ]  }j                   |     y wN)r!   ).0keyr'   s     r   	<genexpr>zmain.<locals>.<genexpr>   s     Tsc*Ts   )rY   r<   r=   rT   ALL)r2   r<   )r   )r	   dictHAS_LAYMAN_API	fail_jsonr
   LAYMAN_IMP_ERRrA   rN   rK   rD   r6   r   message)rY   r<   r(   r2   r-   r'   s        @r   mainrj      s   t$5'*y2RS6J	
 !F 1(;~VT6STE4G5I%fdC8Giu} s3T"'5G
 	t4  (QYY''(s   AC& &	D/DD__main__r`   )$
__future__r   r   r   rX   __metaclass__DOCUMENTATIONEXAMPLESr#   	tracebackosr   rh   
layman.apir   layman.configr   rf   ImportError
format_excansible.module_utils.basicr	   r
   ansible.module_utils.urlsr   r    	Exceptionr   r   r.   rA   rD   rK   rN   rj   r   r   r   r   <module>ry      s    A @(T6   $(N
 K / 		) 	:,)X2
$#5L zF e  )Y))+NNs   A0 0B
B