
    VhR                    6   d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	 d dl
mZ d dlm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 d d
lmZmZ d dlmZ d dlmZ d dlm Z  d dl!m"Z" d dl#m$Z$m%Z%  e"       Z&ejN                  dd       Z( G d de)      Z*y)    )annotationsN)MutableSequence)rmtree)context)AnsibleErrorAnsibleParserError)	GalaxyAPI)
user_agent)	to_nativeto_text)	yaml_dump	yaml_load)LooseVersion)open_url)RoleRequirement)Display)
is_subpathunfrackpathc                     d} t        t        d      rLt        j                  d      }t        j                  |_        d|_        	 t        j                  |d       d} | S | S # t        j                  $ r Y | S w xY w)zj
    Check if tarfile.data_filter implementation is working
    for the current Python version or not
    Fdata_filterzdocs/README.mdz../README.mdz/fooT)hasattrtarfileTarInfoSYMTYPEtypelinknamer   LinkOutsideDestinationError)rettis     C/home/dcms/DCMS/lib/python3.12/site-packages/ansible/galaxy/role.py_check_working_data_filterr!   1   sz     Cw&__-.//$	F+ CJ3J	 22 	 J		s   A! !A87A8c                     e Zd Z eddg      Zej                  j                  dd      ej                  j                  dd      fZej                  j                  dd      Z	ej                  j                  dd      ej                  j                  dd      fZ
d	ZddZd Zd Zed        Zed        Zed        Zed        Zed        Zd Zd Zd Zd Zed        Zed        Zy
)
GalaxyRolegithgmetazmain.ymlz	main.yamlz.galaxy_install_infozrequirements.ymlzrequirements.yaml)defaultsfileshandlersr&   tasks	templatesvarstestsNc                   d | _         d | _        d | _        d | _        t        j
                  d    | _        t        j                  d| j                  z         || _	        || _
        || _        || _        |xs || _        d | _        || _        |j                   D cg c],  }t"        j$                  j'                  || j                        . c}| _        ||j+                  t"        j$                  j'                  t"        j$                  j,                  | j                              s2t"        j$                  j'                  || j                        }|| _        y | j.                  D ]s  }	t"        j$                  j1                  t"        j$                  j'                  |||	            sBt"        j$                  j'                  || j                        } || _        y  || _        y | j(                  d   | _        y c c}w )Nignore_certszValidate TLS certificates: %sr   )	_metadata_metadata_dependencies_requirements_install_infor   CLIARGS_validate_certsdisplaydebuggalaxy_apinameversionsrcdownload_urlscmroles_pathsospathjoinpathsendswithsep	META_MAINexists)
selfr8   apir:   r<   r;   r>   rA   x	meta_mains
             r    __init__zGalaxyRole.__init__S   st   &*#!!#*??>#BB58L8LLM		;$ :@:L:LMQbggll1dii0M
==bggkk499!EFww||D$))4 DI	 "& Iww~~bggll4y&IJ!ww||D$))<DI	 DI 

1DI% Ns   1G9c                j    | j                   r| j                  d| j                   dS | j                  S )zj
        Returns "rolename (version)" if version is not null
        Returns "rolename" otherwise
        z ())r;   r:   rH   s    r    __repr__zGalaxyRole.__repr__y   s(    
 << $		4<<8899    c                4    | j                   |j                   k(  S N)r:   )rH   others     r    __eq__zGalaxyRole.__eq__   s    yyEJJ&&rQ   c                z    t        | j                  t              s| j                  j                  S | j                  S rS   )
isinstancer9   r	   rI   rO   s    r    rI   zGalaxyRole.api   s(    $))Y/99== yyrQ   c                   | j                   | j                  D ]z  }| j                  D ]i  }t        j                  j                  ||      }t        j                  j                  |      sC	 t        |d      5 }t        |      | _         ddd        z | | j                   S # 1 sw Y   xY w# t        $ r' t        j                  d| j                  z         Y   yw xY w)z'
        Returns role metadata
        NrzUnable to load metadata for %sF)r0   rC   rF   r@   rA   rB   isfileopenr   	Exceptionr6   vvvvvr:   )rH   rA   rK   	meta_pathfs        r    metadatazGalaxyRole.metadata   s    
 >>!

 
!% 	I "T9 =Iww~~i0)!%i!5 >1:1>
 	
 ~~> >( )#MM*JTYY*VW#()s*   ,B.8B"	B."B+'B..+CCc                
   | j                   7g | _         | j                  $| j                  j                  d      xs g | _         t        | j                   t              st        d|  d| j                          | j                   S )zC
        Returns a list of dependencies from role metadata
        dependencies.Expected role dependencies to be a list. Role z% has meta/main.yml with dependencies )r1   r`   getrW   r   r   rO   s    r    metadata_dependenciesz GalaxyRole.metadata_dependencies   s    
 &&.*,D'}}(.2mm.?.?.O.USU+$55G$@Fklp  mH  mH  lI  J  ***rQ   c                   | j                   t        j                  j                  | j                  | j                        }t        j                  j                  |      r:	 t        |d      }t        |      | _         	 |j                          | j                   S | j                   S # t        $ r5 t        j                  d| j                  z         Y j                          yw xY w# j                          w xY w)z+
        Returns role install info
        rY   z)Unable to load Galaxy install info for %sF)r3   r@   rA   rB   META_INSTALLrZ   r[   r   r\   r6   r]   r:   close)rH   	info_pathr_   s      r    install_infozGalaxyRole.install_info   s    
 %TYY0A0ABIww~~i(Y,A)21D&
 GGI!!!t!!! ! !MM"MPTPYPY"YZ GGI	! GGIs$   !B& &+C$C' #C$$C' 'C9c                h    | j                   D ]#  }t        j                  j                  |      s# y y)NTF)rC   r@   rA   isdir)rH   rA   s     r    _existszGalaxyRole._exists   s-    JJ 	Dww}}T"	 rQ   c                   t        | j                  t        j                  j                  t        j                  j
                        j                  d            }t        j                  j                  t        j                  j                  | j                  d            s=t        j                  t        j                  j                  | j                  d             t        j                  j                  | j                  | j                        }t        |d      5 }	 t        ||      | _        	 ddd       y# t         $ r Y ddd       yw xY w# 1 sw Y   yxY w)z
        Writes a YAML-formatted file to the role's meta/ directory
        (named .galaxy_install_info) which contains some information
        we can use later for commands like 'list' and 'info'.
        z%c)r;   install_dater&   zw+NFT)dictr;   datetimenowtimezoneutcstrftimer@   rA   rG   rB   makedirsrg   r[   r   r3   r\   )rH   infori   r_   s       r    _write_galaxy_install_infoz%GalaxyRole._write_galaxy_install_info   s     LL!**..x/@/@/D/DENNtT
 ww~~bggll499f=>KKTYY78GGLLD,=,=>	)T" 	a%.tQ%7"	   		 		 s*    E"D==	EEEEEc                h    | j                   r	 t        | j                         yy# t        $ r Y yw xY w)z
        Removes the specified role from the roles path.
        There is a sanity check to make sure there's a meta/main.yml file at this
        path so the user doesn't blow away random directories.
        TF)r`   r   rA   r\   rO   s    r    removezGalaxyRole.remove   s;     ==tyy!   s   % 	11c                N   |r| j                   | j                   }n1d|v r!d|v rd|d   d|d   d| j                  d}n| j                  }t        j                  d|z         	 t	        || j
                  t               d	
      }t        j                  d      }|j                         }|r$|j                  |       |j                         }|r$|j                          |j                  S y# t        $ r+}t        j                  dt        |      z         Y d}~yd}~ww xY w)zS
        Downloads the archived role to a temp location based on role data
        Ngithub_usergithub_repozhttps://github.com//z	/archive/z.tar.gzz- downloading role from %s<   )validate_certs
http_agenttimeoutF)deletezfailed to download the file: %s)r=   r;   r<   r6   r   r5   r
   tempfileNamedTemporaryFilereadwriterh   r:   r\   errorr   )rH   	role_dataarchive_urlurl_file	temp_filedataes          r    fetchzGalaxyRole.fetch   s#       ,"//)+0JNWXeNfhqr  iA  CG  CO  CO  P"hhOO8;FG
O#K@T@Takamwyz$77uE	}}OOD)#==?D  ! ~~%   O@71:MNNOs   &A,C0 C0 0	D$9!DD$c           
        | j                   r4t        j                  d6dt        j                  d   i| j
                  }n| j                  rt        j                  j                  | j                        r| j                  }nSd| j                  v r| j                  }| j                  |      }n&| j                  j                  | j                        }|s0t        d| j                  d| j                  j                  d      |j                  d      dk(  r"t         j#                  d| j$                  z         | j                  j'                  d	|d
         }| j(                  st+        |      dkD  rf|D cg c]6  }t-               x}s|j/                  |j                  d      xs d      5|8 }}	 |j1                          t9        |d         | _        n|j                  dd       r|d   | _        nwd| _        no| j(                  dk7  r`|r^t9        | j(                        |D cg c]  }|j                  dd        c}vr)t        d| j(                  d| j$                  d|d      |D ]D  }|d   | j(                  k(  rd|v r
|d   | _        |d   | j(                  k(  s6d|v s;|d   | _        F | j                  |      }nt        d      |rt         j=                  d|z         t?        j@                  |      st        d      t?        jB                  |d      }d }	|jE                         }
|
D ]  }| jF                  D ]z  }||j$                  v st        j                  jI                  t        j                  jI                  |j$                              }|	s|}|}	_t+        |      t+              k  sw|}|}	|  |	st        d      	 tK        |jM                  |	            | _'        | jR                  }| j                  |d   k7  r| j                  |d d t+        |      }tU        |      D ]  \  }}|| _        t         j!                  d| j$                  d | j                         	 t        j                  jW                  | j                        rt        j                  jY                  | j                        st        d!      t        j                  j                  d"d#      st        d$| j$                  z        | j[                         s7t        d%| j                  z        t        j\                  | j                         t_        d#&      }|
D ]  }|ja                         s|jc                         s%d'D ]M  }te        ||d       x}s|d(k(  r+t        j                  jI                  te        |dd            }nN|jg                  |dd)      }t        j                  j4                  |ji                  t        jj                         }d}t        j                  j5                  |||      }tm        ||d*+      sd,| d-| d.| }t        |      t        j                  j5                  ||      }t        j                  j4                  |jg                  |dd)      ji                  t        jj                         }to        |||       P tq               r)|js                  |tu        | j                        d/0       |js                  |tu        | j                                | jw                           n t         j!                  d3t        |       z         | j                  r)t        j                  j                  | j                        s	 t        j                  |       y*y*y#c c}w # t2        $ r< t        ddj5                  |D cg c]  }|j6                   nc c}w c}      z        w xY wc c}w # tP        $ r t        d      w xY w# tx        $ rT}|jz                  tz        j|                  k(  r||d)z
  k  rY d }~t        d1| j                  d2tu        |            d }~ww xY w# tx        t        f$ r.}t         j#                  d4|d5t9        |             Y d }~y*d }~ww xY w)7Nkeep_scm_metaz://z	- sorry, z was not found on .	role_typeAPPzP%s is a Container App role, and should only be installed using Ansible Containerversionsidr   r:    zUnable to compare role versions (%s) to determine the most recent version due to incompatible version formats. Please contact the role author to resolve versioning conflicts, or specify an explicit role version to install.z, github_branchmasterz- the specified version (z) of z2 was not found in the list of available versions (z).sourcer=   zNo valid role data foundzinstalling from %sz>the downloaded file does not appear to be a valid tar archive.rY   z7this role does not appear to have a meta/main.yml file.z=this role does not appear to have a valid meta/main.yml file.z- extracting z to z7the specified roles path exists and is not a directory.forceFzJthe specified role %s appears to already exist. Use --force to replace it.zu%s doesn't appear to contain a role.
  please remove this directory manually if you really want to put the role here.)follow)r:   r   r      T)realzInvalid z for tarfile member: path z is not a subpath of the role r   )filterzCould not update files in z: z- %s was installed successfullyzUnable to remove tmp file (z):  )Br>   r   scm_archive_roler   r4   specr<   r@   rA   rZ   r   rI   lookup_role_by_namer   
api_serverrd   r6   warningr:   fetch_role_relatedr;   lenr   parsesort	TypeErrorrB   vstringr   r=   r7   r   
is_tarfiler[   
getmembersrF   dirnamer   extractfiler0   r\   rC   	enumeraterG   rl   rz   rv   r   isregissymgetattrreplacesplitrE   r   setattrr!   extractr   rx   OSErrorerrnoEACCESstrunlinkIOError)rH   tmp_filer   role_versionsavloose_versionsrole_versionrole_tar_file	meta_filemembersmemberrK   meta_parent_dirarchive_parent_dirrC   	paths_lenidxrA   resolved_archiveattr
attr_valuerelative_to	full_patherrrelative_path_dirrelative_pathr   s                               r    installzGalaxyRole.install  s   88&77tgooVeFftjnjsjstHXXww~~dhh'88$((" HH	::i0 HH88B	 &TXXW[W_W_WjWj'kll==-6OO %026))%< = !% ; ;J	RV X||
 =)A-5B  *FLNGZqGZ`a`g`ghihmhmnthuh{y{`|  aE!  *F  *F*//1 (/~b/A'B"='0'A'/\\X-$)>dqFr_`quuVUYGZFr)r*  JN  JV  JV JN  JS  JS JW,X Y Y
 %2 IL#F+t||;L@X#/#9#F+t||;R^@^,8,H)	I  ::i0 9::MM.9:%%h/"#cdd 'Xs ; 	'224% 7F%)^^ 7	$3 /1ggoobggoofkk>Z.[O#,5D 2,2	#&#7#>P:Q#Q9H$606I77 !&'`aal)2=3L3LY3W)X 

99a( !%		E"1IJ	!*5!1 @kIC $DIOOtyy$))$TU=k77>>$))4#%77==#;&23l&m m%,__%8%8%%H&23  CG  CL  CL  4L  'M  !M (,{{}*6 8TVZV_V_8` +a %a KK		2+67IRW+X(
 '. "TF %+LLNflln ((< E6=fdD6Q(Q
(Q$,#':#524''//'&RXZ\B]2^K 2<1C1CDVXZ\]1^J13z?O?OPRPVPV?W1XJ24K,.GGLL9I;Xb,c	'1)=MTX'Y,4TF:TU^T__}  O  ~P  +QC*6s*;$;46GGLLAQS^4_ 102i>O>OPacegh>i>o>oprpvpv>w0x 'm D)E,  :; - 5 5fi		>R[a 5 b !. 5 5fi		>R SE"TJ 779y@kF  ACI MNRWW^^DHH%=h		(+ g *F  ) ".!+-1YY>7Za		7Z7Z-[!\#  Gs` % l*+jkklL # k77ell2sY]7J$*QUQZQZ\efg\h+ijjk $W- hQY[bcd[e(fgghss   <^9$^93^9:^> ,`` 9K`#!b >`_0/`` #	b ,%a;$a;;b c $b;;c c                p    t        | j                  | j                  | j                  | j                        S )z
        Returns role spec info
        {
           'scm': 'git',
           'src': 'http://git.example.com/repos/repo.git',
           'version': 'v1.0',
           'name': 'repo'
        }
        )r>   r<   r;   r:   )rp   r>   r<   r;   r:   rO   s    r    r   zGalaxyRole.spec  s%     dhh499UUrQ   c                H   | j                   g | _         | j                  D ]z  }t        j                  j	                  | j                  |      }t        j                  j                  |      sM	 t        |d      }t        |      | _         j                           n t        | j                   t              st        d|  d| j                          | j                   S # t        $ r% t        j                  d| j                  z         Y w xY w# j                          w xY w)z+
        Returns role requirements
        rY   z"Unable to load requirements for %src   z has meta/requirements.yml )r2   META_REQUIREMENTSr@   rA   rB   rZ   r[   r   r\   r6   r]   r:   rh   rW   r   r   )rH   meta_requirementsr^   r_   s       r    requirementszGalaxyRole.requirements  s   
 %!#D%)%;%; !GGLL4EF	77>>)," C0-6q\* 	 $,,o>$'UVZU[[vw{  xJ  xJ  wK  &L  M  M!!! % X&JTYY&VWX 	s$   .C+D	DDDD!)NNNN)__name__
__module____qualname__setSUPPORTED_SCMSr@   rA   rB   rF   rg   r   	ROLE_DIRSrL   rP   rU   propertyrI   r`   re   rj   rm   rx   rz   r   r   r   r   r   rQ   r    r#   r#   K   s2   %'Nfj1277<<3TUI77<<(>?Lf.@A277<<PVXkClm`I$&L'  
  & + +" " "$  ,<ob 
V 
V " "rQ   r#   )returnbool)+
__future__r   r   rq   	functoolsr@   r   r   collections.abcr   shutilr   ansibler   ansible.errorsr   r   ansible.galaxy.apir	   ansible.galaxy.user_agentr
   +ansible.module_utils.common.text.convertersr   r    ansible.module_utils.common.yamlr   r   #ansible.module_utils.compat.versionr   ansible.module_utils.urlsr   !ansible.playbook.role.requirementr   ansible.utils.displayr   ansible.utils.pathr   r   r6   cacher!   objectr#   r   rQ   r    <module>r      sp   , #    	   +   ; ( 0 J A < . = ) 6
)  2Z" Z"rQ   