
    Vh                       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mZ d dl	m
Z
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 	 	 d dlmZ 	 d dlmZmZ 	 d dlm Z  dZ!	 d dl"Z"	 ddl#m$Z$ e%jL                  Z'dZ(dZ) G d d      Z* G d de      Z+ G d d      Z, G d d      Z- G d d      Z. G d de.      Z/ G d  d!e.      Z0 G d" d#e.      Z1 G d$ d%e.      Z2 G d& d'      Z3 G d( d)      Z4d* Z5d+ Z6d4d,Z7d4d-Z8d. Z9d/ Z:d0 Z;d1 Z<d5d2Z=d3 Z>y# e$ r d
 ZY w xY w# e$ r	 d dlmZ Y w xY w# e$ r eZY w xY w# e$ r Y w xY w# e$ r dZ!Y w xY w# e$ r Y w xY w# e$ r dZ$Y w xY w)6    )annotationsN)	iskeyword)	to_nativeto_textto_bytes   )AnsibleCollectionConfig)contextmanager)
ModuleType)import_modulec                >    t        |        t        j                  |    S N)
__import__sysmodules)names    b/home/dcms/DCMS/lib/python3.12/site-packages/ansible/utils/collection_loader/_collection_finder.pyr   r      s    4{{4      )reload)TraversableResources)	find_specspec_from_loader)
FileFinderTF)_meta_yml_to_dict)z.ymlz.yamlz&<ansible_synthetic_collection_package>c                  J    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
e
xZxZxZZy	)
_AnsibleNSTraversablea  Class that implements the ``importlib.resources.abc.Traversable``
    interface for the following ``ansible_collections`` namespace packages::

    * ``ansible_collections``
    * ``ansible_collections.<namespace>``

    These namespace packages operate differently from a normal Python
    namespace package, in that the same namespace can be distributed across
    multiple directories on the filesystem and still function as a single
    namespace, such as::

    * ``/usr/share/ansible/collections/ansible_collections/ansible/posix/``
    * ``/home/user/.ansible/collections/ansible_collections/ansible/windows/``

    This class will mimic the behavior of various ``pathlib.Path`` methods,
    by combining the results of multiple root paths into the output.

    This class does not do anything to remove duplicate collections from the
    list, so when traversing either namespace patterns supported by this class,
    it is possible to have the same collection located in multiple root paths,
    but precedence rules only use one. When iterating or traversing these
    package roots, there is the potential to see the same collection in
    multiple places without indication of which would be used. In such a
    circumstance, it is best to then call ``importlib.resources.files`` for an
    individual collection package rather than continuing to traverse from the
    namespace package.

    Several methods will raise ``NotImplementedError`` as they do not make
    sense for these namespace packages.
    c                ^    |D cg c]  }t        j                  |       c}| _        y c c}w r   )pathlibPath_paths)selfpathsps      r   __init__z_AnsibleNSTraversable.__init__n   s    0561w||A66s   *c                Z    ddj                  t        t        | j                              z  S )Nz_AnsibleNSTraversable('%s')z', ')joinmapr   r    r!   s    r   __repr__z_AnsibleNSTraversable.__repr__q   s!    ,v{{3w;T/UUUr   c                b    t         j                  j                  d | j                  D              S )Nc              3  ^   K   | ]%  }|j                         s|j                          ' y wr   )is_diriterdir.0r#   s     r   	<genexpr>z0_AnsibleNSTraversable.iterdir.<locals>.<genexpr>u   s"     ,\QQRQYQYQ[QYY[,\s   --	itertoolschainfrom_iterabler    r(   s    r   r-   z_AnsibleNSTraversable.iterdirt   s!    ,,,\$++,\\\r   c                :    t        d | j                  D              S )Nc              3  <   K   | ]  }|j                           y wr   )r,   r.   s     r   r0   z/_AnsibleNSTraversable.is_dir.<locals>.<genexpr>x   s     3!188:3s   )anyr    r(   s    r   r,   z_AnsibleNSTraversable.is_dirw   s    3t{{333r   c                     y)NF r(   s    r   is_filez_AnsibleNSTraversable.is_filez   s    r   c                h    t         j                  j                  fd| j                  D              S )Nc              3  b   K   | ]&  }|j                         s|j                         ( y wr   )r,   glob)r/   r#   patterns     r   r0   z-_AnsibleNSTraversable.glob.<locals>.<genexpr>~   s%     ,`UVU]U]U_QVVG_,`s   //r1   )r!   r>   s    `r   r=   z_AnsibleNSTraversable.glob}   s"    ,,,`dkk,```r   c                    t        d      )Nznot usable on namespaces)NotImplementedError)r!   argskwargss      r   _not_implementedz&_AnsibleNSTraversable._not_implemented   s    !"<==r   N)__name__
__module____qualname____doc__r$   r)   r-   r,   r:   r=   rC   joinpath__truediv__
read_bytes	read_textr9   r   r   r   r   O   sD    <7V]4a> 7GFHF{FZ)r   r   c                  :    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
y	)
_AnsibleTraversableResourcesa  Implements ``importlib.resources.abc.TraversableResources`` for the
    collection Python loaders.

    The result of ``files`` will depend on whether a particular collection, or
    a sub package of a collection was referenced, as opposed to
    ``ansible_collections`` or a particular namespace. For a collection and
    its subpackages, a ``pathlib.Path`` instance will be returned, whereas
    for the higher level namespace packages, ``_AnsibleNSTraversable``
    will be returned.
    c                     || _         || _        y r   )_package_loader)r!   packageloaders      r   r$   z%_AnsibleTraversableResources.__init__   s    r   c                R    	 |j                   S # t        $ r |j                  cY S w xY wr   )r   AttributeErrorrD   r!   rQ   s     r   	_get_namez&_AnsibleTraversableResources._get_name   s,    	$<< 	$###	$    &&c                R    	 |j                   S # t        $ r |j                  cY S w xY wr   )
__parent__rT   __package__rU   s     r   _get_packagez)_AnsibleTraversableResources._get_package   s.    	'%%% 	'&&&	'rW   c                R    	 |j                   S # t        $ r |j                  cY S w xY wr   )originrT   __file__rU   s     r   	_get_pathz&_AnsibleTraversableResources._get_path   s,    	$>>! 	$###	$rW   c                |    t        |dd       }|sy|t        k(  ryt        j                  j	                  |      }|dv S )Nr]   FT>   __synthetic____init__.py)getattrSYNTHETIC_PACKAGE_NAMEospathbasename)r!   rQ   r]   module_filenames       r   _is_ansible_ns_packagez3_AnsibleTraversableResources._is_ansible_ns_package   sB    (D1++''**62"BBBr   c                    | j                  |      ry | j                  |      |j                  k7  rt        d|j                  z        y )Nz%r is not a package)ri   r[   rD   	TypeErrorrU   s     r   _ensure_packagez,_AnsibleTraversableResources._ensure_package   sH    &&w/W%)9)991G4D4DDEE :r   c                   | j                   }|j                  d      }|d   dk(  xr t        |      dk  }t        |t              r%|rt        |      }nIt        || j                        }n2t        |t              s"t        d|j                  j                  z        | j                  |       |rt        |j                   S t        j                   | j#                  |            j$                  S )N.r   ansible_collections   z!Expected string or module, got %r)rO   splitlen
isinstancestrr   r   rP   r   rk   	__class__rD   rl   r   submodule_search_locationsr   r   r_   parent)r!   rQ   partsis_nss       r   filesz"_AnsibleTraversableResources.files   s    --c"a11Dc%j1ngs# $G,*7DLLAGZ0?'BSBSB\B\\]]W%('*L*LMM||DNN734;;;r   N)rD   rE   rF   rG   r$   rV   r[   r_   ri   rl   rz   r9   r   r   rM   rM      s,    	$'$	CF<r   rM   c                  d    e Zd ZddZed        Zd Zd Zed        Z	d Z
d Zdd	Zdd
ZddZy)_AnsibleCollectionFinderNc           	        t        t        j                  j                  t	        t
        j                  d   j                                    | _        t        |t              r|g}n|g }|D cg c],  }t        j                  j                  t        |d            . }}|r|j                  t
        j                         g }|D ]  }t        j                  j                  |      dk(  rt        j                  j                  |      }||vsIt        j                  j                  t	        t        j                  j                  |d      d            s|j!                  |        || _        d | _        d | _        g | _        y c c}w )Nansiblesurrogate_or_stricterrorsro   )r   re   rf   dirnamer   r   r   r^   _ansible_pkg_pathrs   rt   
expanduserextendrg   isdirr&   append_n_configured_paths_n_cached_collection_paths$_n_cached_collection_qualified_paths_n_playbook_paths)r!   r"   scan_sys_pathsr#   
good_pathss        r   r$   z!_AnsibleCollectionFinder.__init__   s?   !*277??8CKKPYDZDcDc;d+e!feS!GE]E Z__TU##Ia8M$NO__ LL"
 	%A ww"&;;GGOOA&
"rww}}Xbggll1Nc>d  nC  6D  (E!!!$	% $. *.'481!#+ `s   .1Fc                   t         j                  D ]2  }t        |t              st         j                  j	                  |       4 t         j
                  D ]I  }t        |d      st        |j                  t              s+t         j
                  j	                  |       K t         j                  j                          d t        _        t        j                  t        d      y )N__self__zW_AnsibleCollectionFinder remove did not reset AnsibleCollectionConfig.collection_finder)r   	meta_pathrs   r|   remove
path_hookshasattrr   path_importer_cacheclearr	   _collection_findercollection_finderAssertionError)clsmpsphs      r   _removez _AnsibleCollectionFinder._remove   s    == 	*C#78$$S)	*
 .. 	*Br:&:bkkC[+\%%b)	*
 	%%'592 #44@ !z{{ Ar   c                    | j                          t        j                  j                  d|        t        j                  j                  d| j
                         | t        _        y )Nr   )r   r   r   insertr   _ansible_collection_path_hookr	   r   r(   s    r   _installz!_AnsibleCollectionFinder._install  sB    Q%a!C!CD481r   c                   t              | j                  }|sg }| j                  D ]Z  }t        j                  j                  |      dk7  r t        j                  j                  |d      }||vsJ|j                  |       \ |j                  d| j                         || _        t        fd|D              rt        |       S t        d      )Nro   r   c              3  @   K   | ]  }j                  |        y wr   )
startswith)r/   r#   rf   s     r   r0   zI_AnsibleCollectionFinder._ansible_collection_path_hook.<locals>.<genexpr>"  s     =atq!=s   not interested)r   r   _n_collection_pathsre   rf   rg   r&   r   r   r   r7   _AnsiblePathHookFinderImportError)r!   rf   interesting_pathsr#   s    `  r   r   z6_AnsibleCollectionFinder._ansible_collection_path_hook  s     EE  "-- 077##A&*??Q(=>A--%,,Q/0 $$Q(>(>?8ID5=+<==)$55*++r   c                b    | j                   }|s | j                  | j                  z   x| _         }|S r   )r   r   r   )r!   r"   s     r   r   z,_AnsibleCollectionFinder._n_collection_paths'  s3    //6:6L6LtOgOg6ggD+er   c                ,   t        |t              r|g}t               }|D cg c]B  }||v r|j                  |      rt        j
                  j                  t        |      d      D c}| _        d | _	        dD ]  }| j                  |        y c c}w )Ncollections)ro   zansible_collections.ansible)rs   rt   setaddre   rf   r&   r   r   r   _reload_hack)r!   playbook_pathsadded_pathsr#   pkgs        r   set_playbook_pathsz+_AnsibleCollectionFinder.set_playbook_paths.  s    nc*,-N e Vd  "UPQlmq|l|  AL  AP  AP  QR  AS"'',,y|]"K  "U*.' J 	#Cc"	# "Us   	BB+Bc                ^    t         j                  j                  |      }|sy t        |       y r   )r   r   getreload_module)r!   fullnamems      r   r   z%_AnsibleCollectionFinder._reload_hack>  s"    KKOOH%ar   c                   |j                  d      }|d   }|d   }t        |      }|dvry |dk(  r(|rt        dj                  |            | j                  }|dkD  r|t        dj                  |            |dk(  rt
        }n*|dk(  rt        }n|d	k(  rt        }n|d
k(  rt        }nt        }	  |||      S # t        $ r Y y w xY w)Nrn   r   )r~   ro   r   zHpath should not be specified for top-level packages (trying to find {0})z;path must be specified for subpackages (trying to find {0})r~      rp   )r   	path_list)rq   rr   
ValueErrorformatr   _AnsibleInternalRedirectLoader_AnsibleCollectionRootPkgLoader_AnsibleCollectionNSPkgLoader_AnsibleCollectionPkgLoader_AnsibleCollectionLoaderr   )r!   r   rf   
split_nametoplevel_pkgmodule_to_find
part_countinitialize_loaders           r   _get_loaderz$_AnsibleCollectionFinder._get_loaderD  s    ^^C(
!!}#B_
AA ? !k!r!rs{!|}} //>dlZaabjkll9$ >1_ ?1_ =1_ ; !9	$h$GG 		s   1	B; ;	CCc                &    | j                  ||      S r   )r   )r!   r   rf   s      r   find_modulez$_AnsibleCollectionFinder.find_modulem  s    $//r   c                    | j                  ||      }|y t        ||      }|t        |d      r|j                  |_        |S )N_subpackage_search_paths)r   r   r   r   rv   )r!   r   rf   targetrR   specs         r   r   z"_AnsibleCollectionFinder.find_specq  sK    !!(D1>&10J K.4.M.MD+r   )NTr   )rD   rE   rF   r$   classmethodr   r   r   propertyr   r   r   r   r   r   r9   r   r   r|   r|      sV    $B | |&9,(  # 'R0	r   r|   c                  J    e Zd Zd Zd	dZ e       Zd Zd	dZd	dZd Z	d Z
y)
r   c                @    t        |      | _        || _        d | _        y r   )r   _pathctxr   _file_finder)r!   r   pathctxs      r   r$   z_AnsiblePathHookFinder.__init__  s    !'*"3 r   Nc                    d }t         j                  D cg c]  }dt        |      v s| }}t        |      dk7  r#t	        dj                  t        |                  |d   }|S c c}w )Nr   r   z3need exactly one FileFinder import hook (found {0})r   )r   r   reprrr   	Exceptionr   )r!   _file_finder_hookr   s      r   _get_filefinder_path_hookz0_AnsiblePathHookFinder._get_filefinder_path_hook  so     *-..UBLDQSH<TRUU !Q&QXXY\]nYopqq-a0   Vs
   A&A&c                    |j                  d      }|d   }|dk(  r| j                  S | j                  s1	 t        j	                  | j
                        | _        | j                  S | j                  S # t        $ r Y y w xY w)Nrn   r   ro   )rq   r   r   r   _filefinder_path_hookr   r   )r!   r   r   r   s       r   _get_finderz"_AnsiblePathHookFinder._get_finder  s    ^^C(
!!}00*** $$ (>(T(TUYUbUb(cD% $$$4$$$ #     s   $A1 1	A=<A=c                    | j                  |      }|y t        r!t        |t              r|j	                  |      S |j	                  || j
                  g      S )Nrf   )r   HAS_FILE_FINDERrs   r   r   r   )r!   r   rf   finders       r   r   z"_AnsiblePathHookFinder.find_module  sU    !!(+>FJ!? %%h//%%hdmm_%EEr   c                    |j                  d      }|d   }| j                  |      }|y |dk(  r|j                  || j                  g      S |j                  |      S )Nrn   r   ro   r   )rq   r   r   r   )r!   r   r   r   r   r   s         r   r   z _AnsiblePathHookFinder.find_spec  sg    ^^C(
!!}!!(+>22##HDMM?#CC##H--r   c                0    t        | j                  g|      S r   )_iter_modules_implr   r!   prefixs     r   iter_modulesz#_AnsiblePathHookFinder.iter_modules  s    !4==/6::r   c                b    dj                  | j                  j                  | j                        S )Nz{0}(path='{1}'))r   ru   rD   r   r(   s    r   r)   z_AnsiblePathHookFinder.__repr__  s#     ''(?(?OOr   r   )rD   rE   rF   r$   r   r   r   r   r   r   r)   r9   r   r   r   r     s2    !! 67%4F.;Pr   r   c                      e Zd ZdZddZd Zd Zd Zd Ze	e
d               Ze	d	        Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)_AnsibleCollectionPkgLoaderBaseFNc                   || _         d | _        |j                  d      | _        |j	                  d      | _        | j
                  d   | _        | j
                  d   | _        d | _        d | _	        d | _
        | j                          | j                  |D cg c]  }t        |       c}      | _        | j                  | j                        | _        | j#                          y c c}w )Nrn   r   r   )	_fullname_redirect_modulerq   _split_name
rpartition_rpart_name_parent_package_name_package_to_load_source_code_path_decoded_source_compiled_code_validate_args_get_candidate_pathsr   _candidate_paths_get_subpackage_search_pathsr   _validate_final)r!   r   r   r#   s       r   r$   z(_AnsibleCollectionPkgLoaderBase.__init__  s    ! $#>>#.#..s3$($4$4Q$7! $ 0 0 3!%#" $ 9 9QZ:[A9Q<:[ \(,(I(I$J_J_(`% ;\s   C&c                p    | j                   d   dk7  r$t        dj                  | j                              y )Nr   ro   zPthis loader can only load packages from the ansible_collections package, not {0})r   r   r   r   r(   s    r   r   z._AnsibleCollectionPkgLoaderBase._validate_args  sJ    A"77pwwx|  yG  yG  H  I  I 8r   c                |    |D cg c],  }t         j                  j                  || j                        . c}S c c}w r   )re   rf   r&   r   )r!   r   r#   s      r   r   z4_AnsibleCollectionPkgLoaderBase._get_candidate_paths  s+    @IJ1Q 5 56JJJs   19c                ~    |D cg c]-  }t         j                  j                  t        |            s,|/ c}S c c}w r   )re   rf   r   r   )r!   candidate_pathsr#   s      r   r   z<_AnsibleCollectionPkgLoaderBase._get_subpackage_search_paths  s)    *IabggmmHQK.HIIIs   -::c                     y r   r9   r(   s    r   r   z/_AnsibleCollectionPkgLoaderBase._validate_final  s    r   c              +    K   d}t         j                  j                  |       }	 |s t        |       }d}|t         j                  | <   |j	                         D ]  \  }}t        |||        | y # t        $ rB |r>t         j                  j                  |       rt         j                  j                  |         w xY ww)NFT)r   r   r   r   itemssetattrr   pop)r   rB   created_modulemoduleattrvalues         r   _new_or_existing_modulez7_AnsibleCollectionPkgLoaderBase._new_or_existing_module  s      &	#D)!%$*D!%||~ -ee,-L 	;;??4(KKOOD)		s   "B?AA1 0B?1AB<<B?c                2   d}t         j                  j                  t        |      t        |             }d }t         j                  j	                  t        |            rkt         j                  j                  |d      }t         j                  j                  t        |            smt         j                  j                  |d      }d}nJ|dz   }d }t         j                  j                  t        |            st        dj                  | |            |||fS )NTrb   ra   Fz.pyz{0} not found at {1})	re   rf   r&   r   r   r   isfiler   r   )	leaf_namerf   has_codepackage_pathmodule_paths        r   _module_file_from_pathz6_AnsibleCollectionPkgLoaderBase._module_file_from_path  s    ww||IdOYy5IJ 77==,/0'',,|]CK77>>(;"78 ggll<I &.KL77>>(;"78!"8"?"?	4"PQQHl22r   c                    t        ||       S r   rM   r!   r   s     r   get_resource_readerz3_AnsibleCollectionPkgLoaderBase.get_resource_reader&      +Hd;;r   c                    | j                   ry | j                  | j                        }|t        ||j                         y y r   )r   get_coder   exec__dict__)r!   r  code_objs      r   exec_modulez+_AnsibleCollectionPkgLoaderBase.exec_module)  s;       ==06??+  r   c                4    | j                   r| j                   S y r   )r   r!   r   s     r   create_modulez-_AnsibleCollectionPkgLoaderBase.create_module3  s      (((r   c                   | j                   r3| j                   t        j                  | j                  <   | j                   S t	        | | j                  |      | j                        }| j                  | j                  |d<   ||d<    | j                  |fi |5 }| j                  |      }|t        ||j                         |cd d d        S # 1 sw Y   y xY w)N)
__loader__r^   rZ   __path__rZ   )r   r   r   r   dictget_filenamer   r   r  r  r  r  )r!   r   module_attrsr  r  s        r   load_modulez+_AnsibleCollectionPkgLoaderBase.load_module:  s      *.*?*?CKK'((( &&x011
 ((4'+'D'DL$*2L')T))(ClC 	v}}X.H#Xv/	 	 	s   +CCc                    || j                   k7  r%t        dj                  || j                               | j                  d uS )Nz6this loader cannot answer is_package for {0}, only {1})r   r   r   r   r  s     r   
is_packagez*_AnsibleCollectionPkgLoaderBase.is_packageT  s@    t~~%U\\]egkguguvww,,D88r   c                   | j                   r| j                   S || j                  k7  r%t        dj                  || j                              | j                  sy | j                  | j                        | _         | j                   S )Nz0this loader cannot load source for {0}, only {1})r   r   r   r   r   get_datar  s     r   
get_sourcez*_AnsibleCollectionPkgLoaderBase.get_sourceY  ss    '''t~~%OVVW_aeaoaopqq%%#}}T-C-CD###r   c                   |st        d      |d   dk(  st        d      |g}|D ]  }t        |      }t        j                  j	                  |      r(t        |d      5 }|j                         cd d d        c S |j                  d      sgt        j                  j                  t        j                  j                  |            s y y # 1 sw Y   xY w)Nza path must be specifiedr   /z%relative resource paths not supportedrb   __init__.py )
r   r   re   rf   r
  openreadendswithr   r   )r!   rf   r   r#   b_pathfds         r   r(  z(_AnsibleCollectionPkgLoaderBase.get_datad  s    788
 Aw#~ DEE#fO  	Aa[Fww~~f%&$' %2779% % 0RWW]]277??SYCZ5[	 % %s   CC	c                    t         S r   )rd   r  s     r   _synthetic_filenamez3_AnsibleCollectionPkgLoaderBase._synthetic_filename~  s    %%r   c                \   || j                   k7  r%t        dj                  || j                               | j                  }|si| j	                  |      rXt        | j                        dk(  r/t        j                  j                  | j                  d   d      }|S | j                  |      }|S )Nz/this loader cannot find files for {0}, only {1}r   r   ra   )r   r   r   r   r&  rr   r   re   rf   r&   r5  )r!   r   filenames      r   r"  z,_AnsibleCollectionPkgLoaderBase.get_filename  s    t~~%NUUV^`d`n`nopp))DOOH54001Q677<<(E(Ea(H/Z   33H=r   c                    | j                   r| j                   S | j                  |      }|sd}| j                  |      }|y t        ||ddd      | _         | j                   S )Nz<string>r  r   T)sourcer7  modeflagsdont_inherit)r   r"  r)  compile)r!   r   r7  source_codes       r   r  z(_AnsibleCollectionPkgLoaderBase.get_code  sn    &&& $$X.!Hooh/ %[8RX`aptu"""r   c                .    t        | j                  |      S r   )r   r   r   s     r   r   z,_AnsibleCollectionPkgLoaderBase.iter_modules  s    !$"?"?HHr   c                ~    dj                  | j                  j                  | j                  xs | j                        S )Nz{0}(path={1}))r   ru   rD   r   r   r(   s    r   r)   z(_AnsibleCollectionPkgLoaderBase.__repr__  s1    %%dnn&=&=t?\?\?v`d`v`vwwr   r   )rD   rE   rF   _allows_package_coder$   r   r   r   r   staticmethodr
   r  r  r  r  r  r$  r&  r)  r(  r5  r"  r  r   r)   r9   r   r   r   r     s     (I
KJ
   * 3 3(<,49
	$4&#(Ixr   r   c                       e Zd Z fdZ xZS )r   c                    t         t        |           t        | j                        dk7  r$t        dj                  | j                              y )Nr   zKthis loader can only load the ansible_collections toplevel package, not {0})superr   r   rr   r   r   r   r   r!   ru   s    r   r   z._AnsibleCollectionRootPkgLoader._validate_args  sX    -tCEt A%krrsw  tB  tB  C  D  D &r   )rD   rE   rF   r   __classcell__ru   s   @r   r   r     s    D Dr   r   c                  $     e Zd Z fdZd Z xZS )r   c                    t         t        |           t        | j                        dk7  r$t        dj                  | j                              y )Nr   zAthis loader can only load collections namespace packages, not {0})rE  r   r   rr   r   r   r   r   rF  s    r   r   z,_AnsibleCollectionNSPkgLoader._validate_args  sG    +TACt A%ahhimiwiwxyy &r   c                    | j                   s?| j                  dk7  r/t        dj                  | j                  | j                              y y )Nr~   no {0} found in {1})r   r   r   r   r   r(   s    r   r   z-_AnsibleCollectionNSPkgLoader._validate_final  sG    ,,1F1F)1S3::4;P;PRVRgRghii 2T,r   )rD   rE   rF   r   r   rG  rH  s   @r   r   r     s    z
jr   r   c                  J     e Zd Z fdZd Zd Z fdZd Z fdZd Z	 xZ
S )r   c                    t         t        |           t        | j                        dk7  r$t        dj                  | j                              y )Nrp   z6this loader can only load collection packages, not {0})rE  r   r   rr   r   r   r   r   rF  s    r   r   z*_AnsibleCollectionPkgLoader._validate_args  sG    )4?At A%V]]^b^l^lmnn &r   c                    | j                   dd ddgk(  rg | _        y | j                  s/t        dj                  | j                  | j
                              | j                  d   g| _        y )Nr   rp   r~   builtinrL  r   )r   r   r   r   r   r   r(   s    r   r   z+_AnsibleCollectionPkgLoader._validate_final  sk    Aa Y	$::,.D)..3::4;P;PRVRgRghii .2-J-J1-M,ND)r   c                   t         st        d      i |_        dj                  | j                  dd       }|dk(  rt
        j                  j                  t        d      j                        }t
        j                  j                  |d      }t        t        |      d      5 }|j                         }d d d        n}t        t
        j                  j                  |j                  d	   d
            }t
        j                  j                  |      r&t        |d      5 }|j                         }d d d        nd}	 r$t        ||df      }| j                  |      |_        t&        j(                  j+                  |t
        j                  j                  |j                               |S # 1 sw Y   zxY w# 1 sw Y   xY w# t         $ r(}	t        dj#                  t%        |	                  d }	~	ww xY w)Nz<ansible.utils.collection_loader._meta_yml_to_dict is not setrn   r   rp   ansible.builtinr~   z"config/ansible_builtin_runtime.ymlr,  r   zmeta/runtime.ymlr.  zruntime.ymlz&error parsing collection metadata: {0})collection_namecollection_path)r   r   _collection_metar&   r   re   rf   r   r   r^   r/  r   r0  r   r
  _canonicalize_metar   r   r   r	   on_collection_loadfire)
r!   r  rS  ansible_pkg_pathmetadata_pathr3  raw_routingb_routing_meta_pathrouting_dictexs
             r   _load_modulez(_AnsibleCollectionPkgLoader._load_module  s    [\\"$ ((4#3#3Aa#89//!ww}Y/G/P/PQGGLL)9;_`Mh}-t4 ( ggi( ( #+277<<8JL^+_"`ww~~12-t4 ,"$'')K, , !	]0P]>^_*.*A*A,*O' 	 2277ikipipixixy  zI  zI  jJ7  	K%( (
, ,  	]ELLYWY][\\	]s0   #F+F7;&G +F47G 	G4#G//G4c                N    t         t        |   |       | j                  |       y r   )rE  r   r  r_  )r!   r  ru   s     r   r  z'_AnsibleCollectionPkgLoader.exec_module  s!    )4<VD&!r   c                     y r   r9   r  s     r   r  z)_AnsibleCollectionPkgLoader.create_module      r   c                L    t         t        |   |      }| j                  |      S r   )rE  r   r$  r_  )r!   r   r  ru   s      r   r$  z'_AnsibleCollectionPkgLoader.load_module  s&    2DEhO  ((r   c                    |S r   r9   )r!   	meta_dicts     r   rV  z._AnsibleCollectionPkgLoader._canonicalize_meta   s
      r   )rD   rE   rF   r   r   r_  r  r  r$  rV  rG  rH  s   @r   r   r     s)    o
OB")r   r   c                  2     e Zd Zi ZdZ fdZd Zd Z xZS )r   Tc                    t         t        |           t        | j                        dk  r$t        dj                  | j                              y )N   z@this loader is only for sub-collection modules/packages, not {0})rE  r   r   rr   r   r   r   r   rF  s    r   r   z'_AnsibleCollectionLoader._validate_args  sG    &<>t 1$_ffgkguguvww %r   c                `    t        |      dk7  r| j                  dd ddgk7  rt        d      |S )Nr   rp   r~   rP  z/this loader requires exactly one path to search)rr   r   r   )r!   r   s     r   r   z-_AnsibleCollectionLoader._get_candidate_paths  s9    y>Q4#3#3Aa#8Y	<R#RNOOr   c                .   dj                  | j                  dd       }t        |      }d }d}t        |d| j                  g      }|r|j                  d      }|rd}n t        | j                  | j                        }|rXt        |      | _	        |rEt        | j                  d      r/| j                  j                  r|| j                  | j                  <   y |st        d	      | j                  | j                  |d
         \  }}}	|r|| _        |	r|	gS y )Nrn   r   rp   Fimport_redirectionredirectTr   zpackage has no pathsr   )r&   r   _get_collection_metadata_nested_dict_getr   r   _get_ancestor_redirect_redirected_package_mapr   r   r   r   r   r  r   r   )
r!   r   rS  collection_metarl  explicit_redirectrouting_entry
found_pathr  r  s
             r   r   z5_AnsibleCollectionLoader._get_subpackage_search_paths$  s   ((4#3#3Aa#892?C !(;OQUQ_Q_:`a$((4H $-d.J.JDNN[H $1($;D! WT-B-BJ%OTXTiTiTrTr?G,,T^^< 
 455-1-H-HI^I^`opq`r-s*
Hl %/D" >!r   )	rD   rE   rF   rp  rA  r   r   r   rG  rH  s   @r   r   r     s     x
1r   r   c                  *    e Zd Zd Zd Zd Zd Zd Zy)r   c                    d | _         |j                  d      }|d   }|d   }|dk7  rt        d      t        d      }t	        |d|g      }|r|j                  d      | _         | j                   st        d	      y )
Nrn   r   r   r~   r   rR  rk  rl  z not redirected, go ask path_hook)	_redirectrq   r   rm  rn  r   )r!   r   r   r   r   module_to_loadbuiltin_metars  s           r   r$   z'_AnsibleInternalRedirectLoader.__init__[  s    ^^C(
!!}#B9$.///0AB(8Lh7WX*..z:DN~~@AA r   c                    t        ||       S r   r  r  s     r   r  z2_AnsibleInternalRedirectLoader.get_resource_readern  r  r   c                    | j                   s.t        dj                  |j                  j                              t        | j                         t        j                  |j                  j                  <   y Nzno redirect found for {0})rw  r   r   __spec__r   r   r   r   )r!   r  s     r   r  z*_AnsibleInternalRedirectLoader.exec_moduleq  sM    ~~8??@T@TUVV -:$..,IFOO(()r   c                     y r   r9   r  s     r   r  z,_AnsibleInternalRedirectLoader.create_moduley  rb  r   c                    | j                   st        dj                  |            t        | j                         }|t        j
                  |<   |S r|  )rw  r   r   r   r   r   )r!   r   mods      r   r$  z*_AnsibleInternalRedirectLoader.load_module|  sE     ~~8??IJJ DNN+ #H
r   N)rD   rE   rF   r$   r  r  r  r$  r9   r   r   r   r   Z  s    B&<Jr   r   c                      e Zd Z ed dD              Z ej                   ed            Z ej                   ed            Z	d Z
d Zed        Zed        Zed	        Zed
        Zedd       Zed        Zy)AnsibleCollectionRefc              #  2   K   | ]  }t        |        y wr   )r   )r/   rs     r   r0   zAnsibleCollectionRef.<genexpr>  s       ^q
  ^s   )actionbecomecachecallbackcliconf
connectiondoc_fragmentsfilterhttpapi	inventorylookupmodule_utilsr   netconfroleshellstrategyterminaltestvarsplaybookz^\w+(\.\w+)*$z^\w+(\.\w+){2,}$c                   t        |d      }|t        |d      }t        |d      }t        |d      }| j                  |      s#t        dj                  t	        |                  || j
                  vrt        dj                  |            || _        |rKt        j                  | j                  |      s#t        dj                  t	        |                  || _
        nd| _
        || _        || _        d| j                  g}| j                  g}t	        d	j                  |            | _        | j                  d
k(  r|j                  d       n2| j                  dk(  r|j                  d       n|d| j                  gz  }| j                  r6|j                  | j                         |j                  | j                         | j                  dv r|j                  | j                         |j                  | j                         t	        d	j                  |            | _        d	j                  |      | _        y)a  
        Create an AnsibleCollectionRef from components
        :param collection_name: a collection name of the form 'namespace.collectionname'
        :param subdirs: optional subdir segments to be appended below the plugin type (eg, 'subdir1.subdir2')
        :param resource: the name of the resource being references (eg, 'mymodule', 'someaction', 'a_role')
        :param ref_type: the type of the reference, eg 'module', 'role', 'doc_fragment'
        strictr   NzGinvalid collection name (must be of the form namespace.collection): {0}z invalid collection ref_type: {0}zNinvalid subdirs entry: {0} (must be empty/None or of the form subdir1.subdir2)r.  ro   rn   r  rolesr  	playbooksplugins)r  r  )r   is_valid_collection_namer   r   r   VALID_REF_TYPES
collectionrematchVALID_SUBDIRS_REsubdirsresourceref_typer&    n_python_collection_package_namer   n_python_package_name_fqcr)r!   rS  r  r  r  package_componentsfqcr_componentss          r   r$   zAnsibleCollectionRef.__init__  s    "/(Cgh7G8H58H5,,_=fmmnw  yH  oI  J  K  K4///?FFxPQQ)88D117; !q!x!x  zC  DK  zL  "M  N  N"DLDL  4dooF??+09#((CU:V0W-==G#%%h/]]k)%%l3 :t}}"==<<%%dll3""4<<0==22%%dmm4t}}-%.sxx8J/K%L"YY/
r   c                d    dj                  | j                  | j                  | j                        S )NzEAnsibleCollectionRef(collection={0!r}, subdirs={1!r}, resource={2!r}))r   r  r  r  r(   s    r   r)   zAnsibleCollectionRef.__repr__  s>    V]]^b^m^moso{o{  ~B  ~K  ~K  L  	Lr   c                    | j                   S r   )r  r(   s    r   fqcrzAnsibleCollectionRef.fqcr  s    zzr   c                   t         j                  |       s#t        dj                  t	        |                   t        | d      } t        |d      }d}|dk(  r:| j                  t              r%| j                  dd      }|d   }|d	   }d|d   z   }n| j                  dd	      }|d   }|d	   }|j                  dd      }t        |      d
k(  r|d   }nd}dj                  |dd       }t        ||||z   |      S )a  
        Parse a string as a fully-qualified collection reference, raises ValueError if invalid
        :param ref: collection reference to parse (a valid ref is of the form 'ns.coll.resource' or 'ns.coll.subdir1.subdir2.resource')
        :param ref_type: the type of the reference, eg 'module', 'role', 'doc_fragment'
        :return: a populated AnsibleCollectionRef object
        z'{0} is not a valid collection referencer  r   r.  r  rn   r   r   r   rp   )r  is_valid_fqcrr   r   r   r   r1  PB_EXTENSIONSrsplitrq   rr   r&   )	refr  extresource_splitnamepackage_remnantr  package_splitnamer  rS  s	            r   	from_fqcrzAnsibleCollectionRef.from_fqcr  s    $11#6FMMiX[n]^^c(+8H5{"s||M'B!$D!!403O)!,H*1--C!$D!!403O)!,H ,11$: !Q&'*GG))$5a$:;#OWhnhWWr   c                N    	 t         j                  | |      S # t        $ r Y yw xY w)a  
        Attempt to parse a string as a fully-qualified collection reference, returning None on failure (instead of raising an error)
        :param ref: collection reference to parse (a valid ref is of the form 'ns.coll.resource' or 'ns.coll.subdir1.subdir2.resource')
        :param ref_type: the type of the reference, eg 'module', 'role', 'doc_fragment'
        :return: a populated AnsibleCollectionRef object on successful parsing, else None
        N)r  r  r   r  r  s     r   try_parse_fqcrz#AnsibleCollectionRef.try_parse_fqcr  s,    	'11#x@@ 		s    	$$c                    t        |       } | j                  d      }|dk(  rd}|t        j                  vr#t	        dj                  t        |                   |S )a  
        Utility method to convert from a PluginLoader dir name to a plugin ref_type
        :param legacy_plugin_dir_name: PluginLoader dir name (eg, 'action_plugins', 'library')
        :return: the corresponding plugin ref_type (eg, 'action', 'role')
        _pluginslibraryr   z3{0} cannot be mapped to a valid collection ref type)r   removesuffixr  r  r   r   r   )legacy_plugin_dir_nameplugin_types     r    legacy_plugin_dir_to_plugin_typez5AnsibleCollectionRef.legacy_plugin_dir_to_plugin_type  s`     "))?!@,99+F*$$K2BBBRYYZcdzZ{|}}r   Nc                    t        |       } |s-t        t        j                  t        j
                  |             S t        t        j                  | |            S )a  
        Validates if is string is a well-formed fully-qualified collection reference (does not look up the collection itself)
        :param ref: candidate collection reference to validate (a valid ref is of the form 'ns.coll.resource' or 'ns.coll.subdir1.subdir2.resource')
        :param ref_type: optional reference type to enable deeper validation, eg 'module', 'role', 'doc_fragment'
        :return: True if the collection ref passed is well-formed, False otherwise
        )r   boolr  r  r  VALID_FQCR_REr  r  s     r   r  z"AnsibleCollectionRef.is_valid_fqcr!  sF     cl!5!C!CSIJJ(77XFGGr   c                    t        |       } | j                  d      dk7  ryt        d | j                  d      D              S )a?  
        Validates if the given string is a well-formed collection name (does not look up the collection itself)
        :param collection_name: candidate collection name to validate (a valid name is of the form 'ns.collname')
        :return: True if the collection name passed is well-formed, False otherwise
        rn   r   Fc              3  N   K   | ]  }t        |       xr t        |        y wr   )r   is_python_identifier)r/   
ns_or_names     r   r0   z@AnsibleCollectionRef.is_valid_collection_name.<locals>.<genexpr>>  s0      
  *%%J*>z*JJ
s   #%)r   countallrq   )rS  s    r   r  z-AnsibleCollectionRef.is_valid_collection_name1  sK     "/2  &!+ 
 .33D9
 
 	
r   r   )rD   rE   rF   	frozensetr  r  r=  r   r  r  r$   r)   r   r  rB  r  r  r  r  r  r9   r   r   r  r    s      ^ 5]  ^ ^O "rzz'*:";<BJJw':;<M70rL   (X (XT 
 
  $ H H 
 
r   r  c                n   t        |       } | r-t        | t              rt        | j	                  d            dk7  rt        d      	 t        d| z         }t        t        j                  j                  t        |j                                    S # t        $ r t        dj                  |             w xY w)Nrn   r   Kcollection_name must be a non-empty string of the form namespace.collectionansible_collections.unable to locate collection {0})r   rs   rt   rr   rq   r   r   r   r   re   rf   r   r   r^   )rS  collection_pkgs     r   _get_collection_pathr  E  s    0O*_c"Bc/J_J_`cJdFeijFjfggT&'='OP RWW__Xn.E.E%FGHH  T:AA/RSSTs   B $B4c                \   t         j                  | d      }|r	 t        |j                        }|rt        j                  j                  t        j                  |j                     j                  j                  dd            }|j                  r_|j                  j                  d      D cg c]  }t        |       }}|j!                  d|       t        j                  j                  | }t        j                  j                  |t        |j"                              }t        j                  j%                  t'        |            r|j"                  ||j(                  fS |j"                  j+                  t,              st,        D ]{  }t        j                  j                  |t        |j"                  |z               }t        j                  j%                  t'        |            sb|j"                  ||j(                  fc S  y # t        t
        f$ r}d }Y d }~d }~ww xY wc c}w )Nr  ra   r  rn   r   )r  r  r   r  IOErrorModuleNotFoundErrorre   rf   r&   r   r   r^   replacer  rq   r   r   r  existsr   r  r1  r  )	r  acrr   ecpathxr"   rf   r  s	            r   _get_collection_playbook_pathr  Q  s   

-
-h
DC
	 D DEC
 GGLLS-Q-Q!R![![!c!cds  vA  "B  CE{{/2{{/@/@/FG!1GGQ&e,77<<y'>?Dww~~htn-||T3>>99\\**=9( BC77<<y9K/LMDww~~htn5"||T3>>AAB ) ,- 	C	 Hs   H
 0H)
H&H!!H&c                    t        | d|      S )Nr  )_get_collection_resource_path)	role_namecollection_lists     r   _get_collection_role_pathr  o  s    (G_MMr   c                &   |dk(  rt        |       S t        j                  | |      }|r&|j                  g}|j                  }|j
                  }n|sy | }d}|D ]  }	 t        ||||      }t        |j                        }|at        j                  j                  t        t        j                  |j                     j                  d            }|t        |d      |fc S  y # t         t"        f$ r
}	Y d }	~	d }	~	wt$        $ r
}
Y d }
~
d }
~
ww xY w)Nr  r.  )rS  r  r  r  r   r   )r  r  r  r  r  r  r   r  re   rf   r   r   r   r   r^   r   r  r  r   )r   r  r  r  r  r  rS  r   rf   r  r^  s              r   r  r  s  s	   ;,T22

-
-dH
=C
>>*++<<* 	&PWbju}~C 9 9:CwwxC<U<U0V0_0_h}'~6K!Lo]] "  ,- 	 		s   BC&&D:DDc           
        t        t        j                  j                  t	        |                   } | j                  d      }|j                  d      dk7  ry|j                  d      }t        |      |dz   k  rydj                  ||dz   |dz          }	 t        t        j                  j                  t	        t        d|z         j                                    }t        j                  j                  dg|d|dz     }t        t        j                  j                  t	        |                  }||k7  ry|S # t        $ r Y yw xY w)	an  
    Return the containing collection name for a given path, or None if the path is not below a configured collection, or
    the collection cannot be loaded (eg, the collection is masked by another of the same name higher in the configured
    collection roots).
    :param path: path to evaluate for collection containment
    :return: collection name or None
    r+  ro   r   Nrp   rn   r  r   )r   re   rf   abspathr   rq   r  indexrr   r&   r   r   r^   r   )rf   
path_partsac_poscandidate_collection_nameimported_pkg_pathoriginal_path_prefixs         r   _get_collection_name_from_pathr    s9    RWW__Xd^45DCJ-.!334F :!# #FQJvz)J K%bggooh}McfM  @A  @J  @J  7K  'L  M 77<<Gj6A:.FG!"''//(;L2M"NO00$$  s   AD< <	EEc                &    | sy t        | d|dg      S )Nrk  rl  )rn  )collection_meta_dictr   s     r   _get_import_redirectr    s    03GS]2^__r   c                    |}|r=|j                  d      d   }| j                  |      }|r||t        |      d  z   }|S |r=y )Nrn   r   )r   r   rr   )redirected_package_mapr   cur_pkgancestor_redirectrl  s        r   ro  ro    sW    G
$$S)!,266w?(8CLM+BBHO  r   c                B    | }|D ]  }|j                  |      }|r y  |S r   )r   )	root_dictkey_list	cur_valuekeys       r   rn  rn    s2    I MM#&	
 r   c              #  (  K   |sd}nt        |      }t        t        |       D ]  }t        j                  j                  |      s#t        t        j                  |            D ]  }t        j                  j                  ||      }t        j                  j                  |      rd|v s|dk(  rL|t        |      z   df _|j                  d      sq|dk7  sw|t        t        j                  j                  |      d         z   df   y w)	Nr.     .s   __pycache__Ts   .pyr-  r   F)r   r'   r   re   rf   r   sortedlistdirr&   r1  splitext)r"   r   r2  
b_basenameb_candidate_module_paths        r   r   r     s     6" h& Uww}}V$ F!34 	UJ&(ggll6:&F#ww}}45 :%~)E y44d:: &&v.:3O 9RWW-=-=j-I!-L#MMuTT	UUs   CDD7Dc                N   t        |       } | r-t        | t              rt        | j	                  d            dk7  rt        d      	 t        d| z         }t        |dd       }|t        dj                  |             |S # t        $ r t        dj                  |             w xY w)Nrn   r   r  r  r  rU  z5collection metadata was not loaded for collection {0})
r   rs   rt   rr   rq   r   r   r   r   rc   )rS  r  rU  s      r   rm  rm    s    0O*_c"Bc/J_J_`cJdFeijFjfggT&'='OP ~/A4HPWWXghii  T:AA/RSSTs   B   $B$r   )r.  )?
__future__r   r2   re   os.pathr  r   keywordr   +ansible.module_utils.common.text.convertersr   r   r   _collection_configr	   
contextlibr
   typesr   	importlibr   r   r   r   importlib.resources.abcr   importlib.abcobjectimportlib.utilr   r   importlib.machineryr   r   r   rU  r   rt   isidentifierr  r  rd   r   rM   r|   r   r   r   r   r   r   r   r  r  r  r  r  r  r  ro  rn  r   rm  r9   r   r   <module>r     s   #  	  	 
 
 U T 7 % !' ."	7 	A	:. O	
3 '' !A 4G 4GnL<#7 L<^e eVOP OPdUx UxpD&E D	j$C 	jL"A L`A> AL. .bw
 w
t	I<N#L'%T`
U6C'  !!!  7 	7	7
  " ""  		
  O  		  s   D D# E E %E *E$ D D #D1.D4 0D11D4 4D>=D>E	E	EEE! E!$E.-E.