
    Vh`                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
Z
d dlZd dlZdZ	 d dlZdZd dlmZ d d	lmZmZ d d
lmZ  e       Zd Zd Z G d de      Zy# e$ r Y 4w xY w)    )absolute_importdivisionprint_functiona  
name: filetree
author: Dag Wieers (@dagwieers) <dag@wieers.com>
short_description: recursively match all files in a directory tree
description:
  - This lookup enables you to template a complete tree of files on a target system while retaining permissions and ownership.
  - Supports directories, files and symlinks, including SELinux and other file properties.
  - If you provide more than one path, it will implement a first_found logic, and will not process entries it already processed
    in previous paths. This enables merging different trees in order of importance, or add role_vars to specific paths to
    influence different instances of the same role.
options:
  _terms:
    description: Path(s) of files to read.
    required: true
    type: list
    elements: string
a1  
- name: Create directories
  ansible.builtin.file:
    path: /web/{{ item.path }}
    state: directory
    mode: '{{ item.mode }}'
  with_community.general.filetree: web/
  when: item.state == 'directory'

- name: Template files (explicitly skip directories in order to use the 'src' attribute)
  ansible.builtin.template:
    src: '{{ item.src }}'
    # Your template files should be stored with a .j2 file extension,
    # but should not be deployed with it. splitext|first removes it.
    dest: /web/{{ item.path | splitext | first }}
    mode: '{{ item.mode }}'
  with_community.general.filetree: web/
  when: item.state == 'file'

- name: Recreate symlinks
  ansible.builtin.file:
    src: '{{ item.src }}'
    dest: /web/{{ item.path }}
    state: link
    follow: false  # avoid corrupting target files if the link already exists
    force: true
    mode: '{{ item.mode }}'
  with_community.general.filetree: web/
  when: item.state == 'link'

- name: list all files under web/
  ansible.builtin.debug:
    msg: "{{ lookup('community.general.filetree', 'web/') }}"
a  
_raw:
  description: List of dictionaries with file information.
  type: list
  elements: dict
  contains:
    src:
      description:
        - Full path to file.
        - Not returned when RV(_raw[].state) is set to V(directory).
      type: path
    root:
      description: Allows filtering by original location.
      type: path
    path:
      description: Contains the relative path to root.
      type: path
    mode:
      description: The permissions the resulting file or directory.
      type: str
    state:
      description: TODO.
      type: str
    owner:
      description: Name of the user that owns the file/directory.
      type: raw
    group:
      description: Name of the group that owns the file/directory.
      type: raw
    seuser:
      description: The user part of the SELinux file context.
      type: raw
    serole:
      description: The role part of the SELinux file context.
      type: raw
    setype:
      description: The type part of the SELinux file context.
      type: raw
    selevel:
      description: The level part of the SELinux file context.
      type: raw
    uid:
      description: Owner ID of the file/directory.
      type: int
    gid:
      description: Group ID of the file/directory.
      type: int
    size:
      description: Size of the target.
      type: int
    mtime:
      description: Time of last modification.
      type: float
    ctime:
      description: Time of last metadata update or creation (depends on OS).
      type: float
NFT)
LookupBase)	to_nativeto_text)Displayc                     g d}t         rPt        j                         r<	 t        j                  t	        |             }|d   dk7  r|d   j                  dd      }|S # t
        $ r |cY S w xY w)N)NNNNr      :   )HAVE_SELINUXselinuxis_selinux_enabledlgetfilecon_rawr   OSErrorsplit)pathcontextrets      m/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/lookup/filetree.pyselinux_contextr      sn    &G224	 )))D/:C q6R< !fll3*GN  	N	s   A A+*A+c                    t         j                  j                  | |      }	 t        j                  |      }t        | |      }t        j                  |j                        rd|d<   t        j                  |      |d<   nit        j                  |j                        rd	|d<   nDt        j                  |j                        rd
|d<   ||d<   nt
        j                  d| d       y|j                  |d<   |j                  |d<   	 t!        j"                  |j                        j$                  |d<   	 t)        t+        j,                  |j                        j.                        |d<   dt        j0                  |j                        d|d<   |j2                  |d<   |j4                  |d<   |j6                  |d<   t8        rBt;        j<                         dk(  r+t?        |      }|d   |d<   |d   |d<   |d   |d<   |d   |d<   |S # t        $ r&}t
        j                  d| d| d       Y d}~yd}~ww xY w# t&        $ r |j                  |d<   Y  w xY w# t&        $ r |j                  |d<   Y 	w xY w)zD Returns dictionary with file properties, or return None on failure z%filetree: Error using stat() on path z ()N)rootr   linkstatesrc	directoryfilezfiletree: Error file type of z is not supporteduidgidownergroup003omodesizemtimectimer   r   seuserserole   setyper   selevel) osr   joinlstatr   displaywarningdictstatS_ISLNKst_modereadlinkS_ISDIRS_ISREGst_uidst_gidpwdgetpwuidpw_nameKeyErrorr   grpgetgrgidgr_nameS_IMODEst_sizest_mtimest_ctimer   r   r   r   )r   r   abspathster   r   s          r   
file_propsrM      s#   ggll4&GXXg
 Dt
$C||BJJG[[)E
	bjj	!"G	bjj	!GE
7y@QRSCJCJ!||BII.66G!s||BII6>>?G dll2::.s34CK**CK;;CL;;CL2249!'*
H
H
H IJQ  ?y1#QOP,  !yyG!  !yyG!s;   H 	,H: 65I 	H7H22H7:III54I5c                       e Zd ZddZy)LookupModuleNc           
      <   | j                  ||       | j                  |      }g }|D ]j  }t        j                  j	                  |      }| j
                  j                  |dt        j                  j                  |            }t        j                  j                  ||      }	t        j                  d|	 d       t        j                  |	d      D ]  \  }
}}||z   D ]  }t        j                  j                  t        j                  j                  |
|      |	      }||D cg c]  }|d   	 c}vsWt        |	|      }|ft        j                  dt        j                  j                  |	|       d       |j                  |         m |S c c}w )	N)var_optionsdirectfilesz	Walking ''T)topdownr   z	  found ')set_optionsget_basedirr1   r   basename_loaderpath_dwim_relativedirnamer2   r4   debugwalkrelpathrM   append)selfterms	variableskwargsbasedirr   term	term_filedwimmed_pathr   r   dirsrS   entryr^   propss                   r   runzLookupModule.run   s]   Yv>""9- 	.D((.I<<::7GRWW__]aMbcL77<<i8DMMIdV1-.%'WWT4%@ 	.!dE!E\ .E ggoobggll4.GNG #&FuV}&FF *4 9 ,#MMIbggll46Q5RRS*TUJJu-.	.	.  
 'Gs   (F)N)__name__
__module____qualname__rk        r   rO   rO      s    rp   rO   )
__future__r   r   r   type__metaclass__DOCUMENTATIONEXAMPLESRETURNr1   r?   rC   r7   r   r   ImportErroransible.plugins.lookupr   +ansible.module_utils.common.text.convertersr   r   ansible.utils.displayr	   r4   r   rM   rO   ro   rp   r   <module>r{      s    C B$!F8
r 
 
 
 	L . J )
) .b: Y  		s   A A! A!