
    Vh                    R   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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 d dlmZmZ d dlmZmZ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)m*Z* d dl+m,Z,m-Z- d dl.m/Z/ d dl0m1Z1 d dl2m3Z3  e/       Z4 ejj                         Z6dZ7ejp                  dejr                  hZ:d Z;d Z<d Z=d Z>e;d        Z?d Z@ G d de      ZA ej                  dg d      ZC G d d      ZDej                   G d  d!             ZFy)"    )annotationsN)
HTTPStatus)BadStatusLineIncompleteRead)	HTTPErrorURLError)quote	urlencodeurlparseparse_qsurljoin)	constants)AnsibleError)
user_agent)retry_with_delays_and_condition)generate_jittered_backoff)string_types)to_bytes	to_nativeto_text)open_urlprepare_multipart)Display)secure_hash_s)makedirs_safed   i  c                      fd}|S )Nc                 L    t         5   | i |cd d d        S # 1 sw Y   y xY wN)_CACHE_LOCK)argskwargsfuncs     B/home/dcms/DCMS/lib/python3.12/site-packages/ansible/galaxy/api.pywrappedzcache_lock.<locals>.wrapped.   s(     	)((	) 	) 	)s   # )r#   r%   s   ` r$   
cache_lockr'   -   s    ) N    c                    t        | t              r| j                  t        v ryt        | t              rGt        | dd       x}r8t        |t              r|j                  }t        |t        t        t        f      ryy)NTorig_excF)
isinstanceGalaxyError	http_codeRETRY_HTTP_ERROR_CODESr   getattrr   reasonTimeoutErrorr   r   )	exceptionr*   s     r$   should_retry_errorr3   5   si     )[)i.A.AE[.[)\*GIz[_<`0`0`h)H h}n MNr(   c                      fd}|S )z
    Wrapper to lazily initialize connection info to Galaxy and verify the API versions required are available on the
    endpoint.

    :param versions: A list of API versions that the function supports.
    c                      fd}|S )Nc                   | j                   s*t        j                  d| j                  z         | j                  }d| j                  d|d}| j                  dk(  s| j                  dk(  rd}	 | j                  |d|d	
      }d|vrt        d|d| j                        || _        |j                  dddi      }t        |j                               dgk(  rd|d<   || _         t        j                  ddj!                  |j                               d| j                  d| j                  d       t#        | j                   j                               }t#              j%                  |      }	|	sVt        d
j&                  ddj!                        ddj!                  |      d| j                  d| j                  
      |	dhk(  rt        j)                  dd        
| g|i |S # t        t        t        t        f$ rt}|j                  d      s|j                  d      r t        |d      }	 | j                  |d|d	
      }n## t        $ r}|j                  dk(  r| d }~ww xY wY d }~d }~ww xY w) Nz'Initial connection to galaxy_server: %sz/Error when finding available api versions from  ()zhttps://galaxy.ansible.comzhttps://galaxy.ansible.com/zhttps://galaxy.ansible.com/api/GETT)methoderror_context_msgcachez/apiz/api/  available_versionsz!Tried to find galaxy API root at z. but no 'available_versions' are available on v1zv1/zv2/v2zFound API version ', z' with Galaxy server zGalaxy action z requires API versions 'z' but only 'z' are available on  zThe v2 Ansible Galaxy API is deprecated and no longer supported. Ensure that you have configured the ansible-galaxy CLI to utilize an updated and supported version of Ansible Galaxy.z2.20)version)_available_api_versionsdisplayvvvv
api_servername_call_galaxyr   r,   
ValueErrorKeyErrorendswith_urljoinr-   getlistkeysjoinsetintersection__name__
deprecated)selfr!   r"   n_urlr;   dataerrnew_errr>   common_versionsr:   versionss             r$   r%   z-g_connect.<locals>.decorator.<locals>.wrappedP   sp   //FXY `d`i`ikp$q!??&BBdooYvFv=E,,U5Temq,rD  (t3&27(J K K
 #( &*XX.BUFO%T"*//12ug=06&u-/A, $		*<*A*A*C DdiiQUQ`Q`b c "%T%A%A%F%F%H!I!(m889KLO""&,ootyy7JDIIVhLi&*ii$B C C
 4&(""G #	 #  $0000e %k:xH  ~~f-1H %UG4E#00uXiqu0v& ",,3"%I s<   1G7 7J/I? II?	I6I11I66I??Jr&   )r:   r%   r\   s   ` r$   	decoratorzg_connect.<locals>.decoratorO   s    @	1B r(   r&   )r\   r]   s   ` r$   	g_connectr^   H   s    BF r(   c                ~    t        |       }d}	 |j                  }|j                  d|xs dS # t        $ r Y  w xY w)z* Gets the cache ID for the URL specified. N: )r   portrJ   hostname)urlurl_inforb   s      r$   get_cache_idrf      sK    }HD}}
 ''44	  s   0 	<<c                   d}t         j                  j                  |       sLt        j	                  dt        |       z         t        | d      5  t        j                  | d       ddd       t        j                  |       j                  }|t        j                  z  r"t        j                  dt        |       z         yt        | d      5 }t        |j                         d	
      }ddd       	 t        j                        }t!        |t"              r|j%                  dd      |k7  rlt        j	                  dt        |       z         d|i}t        | d      5 }|j'                  t)        t        j*                  |      d	
             ddd       |S |S # 1 sw Y   8xY w# 1 sw Y   xY w# t        $ r d}Y w xY w# 1 sw Y   |S xY w)zR Loads the cache file requested if possible. The file must not be world writable.    z/Creating Galaxy API response cache file at '%s'wi  NzKGalaxy cache has world writable access (%s), ignoring it as a cache source.rbmodesurrogate_or_stricterrorsrC   z:Galaxy cache file at '%s' has an invalid version, clearingwb)ospathisfilerE   rF   r   openchmodstatst_modeS_IWOTHwarningreadjsonloadsrJ   r+   dictrN   writer   dumps)b_cache_pathcache_version
cache_modefdjson_valr<   s         r$   _load_cacher      s    M77>>,'FQ]I^^_,$ 	*HH\5)	* &..JDLL e!,/0 	1	l	& D"2779-BCD

8$ eT"eii	4&@M&QQT[\hTiijM* ,T* 	PbHHXdjj/8MNO	P L5L3	* 	*D D
  	P Ls6   FF)4F5  0GF&)F25GGGc                 8    dj                  d | dz   D              S )N/c              3  Z   K   | ]#  }|st        |d       j                  d       % yw)rm   rn   r   N)r   strip).0as     r$   	<genexpr>z_urljoin.<locals>.<genexpr>   s&     eacdIa(=>DDSIes   +!+)ra   )rQ   )r!   s    r$   rM   rM      s    88eSWZ_S_eeer(   c                  "     e Zd ZdZ fdZ xZS )r,   z( Error for bad Galaxy server responses. c                   t         t        |   |       |j                  | _        |j                         | _        	 t        |j                               }t        j                  |      }| j                  j                  d      }d|v rB|j                  d|j                        }|j                  dd      }d|| j                  ||fz  }nd|v r|j                  dg       }	|	si g}	g }
|	D ]l  }|j                  d	      xs |j                  d
      xs |j                  }|j                  d      xs d}d| j                  ||fz  }|
j!                  |       n |ddj#                  |
      }n.|j                  d|j                        }d|| j                  |fz  }t%        |      | _        y # t        t        f$ r i }Y Yw xY w)Nr   r@   messagecodeUnknownz(%s (HTTP Code: %d, Message: %s Code: %s)v3ro   detailtitlez%(HTTP Code: %d, Message: %s Code: %s)rB   rA   defaultz%s (HTTP Code: %d, Message: %s))superr,   __init__r   r-   geturlrd   r   rz   r{   r|   AttributeErrorrJ   splitrN   r0   appendrQ   r   r   )rV   
http_errorr   http_msgerr_info	url_split
galaxy_msgr   full_error_msgro   message_lineserror	error_msg
error_codemessage_line	__class__s                  r$   r   zGalaxyError.__init__   s   k4)'2#$$&	z01Hzz(+H HHNN3'	9!i1B1BCJ<<	2DHGUYUcUceoquKvvNY\\(B/FM 3!IIh/Z599W3EZIZIZ	"YYv.;)
G4>>[dfpJqq$$\2	3 )0=1IJN "i1B1BCJ?7DNN\fBggN 05 
+ 	H	s   .F/ /GG)rT   
__module____qualname____doc__r   __classcell__)r   s   @r$   r,   r,      s    2"1 "1r(   r,   CollectionMetadata)	namespacerH   created_strmodified_strc                      e Zd Zd Zy)CollectionVersionMetadatac	                t    || _         || _        || _        || _        || _        || _        || _        || _        y)a  
        Contains common information about a collection on a Galaxy server to smooth through API differences for
        Collection and define a standard meta info for a collection.

        :param namespace: The namespace name.
        :param name: The collection name.
        :param version: The version that the metadata refers to.
        :param download_url: The URL to download the collection.
        :param artifact_sha256: The SHA256 of the collection artifact for later verification.
        :param dependencies: A dict of dependencies of the collection.
        :param signatures_url: The URL to the specific version of the collection.
        :param signatures: The list of signatures found at the signatures_url.
        N)r   rH   rC   download_urlartifact_sha256dependenciessignatures_url
signatures)	rV   r   rH   rC   r   r   r   r   r   s	            r$   r   z"CollectionVersionMetadata.__init__   s@     #	(.(,$r(   N)rT   r   r   r   r&   r(   r$   r   r      s    %r(   r   c            
         e Zd ZdZddddddd ed      df	dZd Zd	 Zd
 Zd Z	e
 eg d      d               Z e eddd      e      	 	 d*d       Zd+dZed        Z edg      d        Z edg      d,d       Z edg      d-d       Z edg      d.d       Z edg      d        Z edg      d        Z edg      d        Z edg      d        Z edg      d        Z edg      d         Z edg      d!        Z ed"d#g      d$        Z ed"d#g      d/d%       Z  ed"d#g      d&        Z! ed"d#g      d'        Z" ed"d#g      d(        Z# ed"d#g      d)        Z$y)0	GalaxyAPIzM This class is meant to be used as a API client for an Ansible Galaxy server NTFinf<   c                   || _         || _        || _        || _        || _        || _        || _        || _        |xs i | _        || _	        || _
        t        t        j                  d      }t        |d       t        j                   j#                  |d      | _        |	rt&        5  t        j                   j)                  | j$                        rJt*        j-                  dt/        | j$                        z         t        j0                  | j$                         d d d        d | _        |
st5        | j$                        | _        t*        j7                  d| j
                  d| j                         y # 1 sw Y   \xY w)	Nrm   rn   i  rk   s   api.jsonzClearing cache file (%s)zValidate TLS certificates for z: )galaxyrH   usernamepasswordtokenrG   validate_certstimeoutrD   	_priority_server_timeoutr   CGALAXY_CACHE_DIRr   rq   rr   rQ   _b_cache_pathr    existsrE   rF   r   remove_cacher   debug)rV   r   rH   rd   r   r   r   r   available_api_versionsclear_response_cacheno_cachepriorityr   b_cache_dirs                 r$   r   zGalaxyAPI.__init__  s'    	  
,'='C$!&q11:OPk.WW\\+{C 277>>$"4"45LL!;gdFXFX>Y!YZIId0012
 %d&8&89DKQUQdQdef2 2s   'A4E66E?c                ,    t        | j                        S )z3Render GalaxyAPI as a native string representation.)r   rH   rV   s    r$   __str__zGalaxyAPI.__str__9  s     ##r(   c                ,    t        | j                        S )z9Render GalaxyAPI as a unicode/text string representation.)r   rH   r   s    r$   __unicode__zGalaxyAPI.__unicode__>  s     tyy!!r(   c                h    dj                  | | j                  | j                  | j                        S )z9Render GalaxyAPI as an inspectable string representation.z><{instance!s} "{name!s}" @ {url!s} with priority {priority!s}>)instancerH   r   rd   )formatrH   r   rG   r   s    r$   __repr__zGalaxyAPI.__repr__C  s2     MFDIIT__  	
r(   c                    t        || j                        st        S | j                  |j                  kD  xs | j                  | j                  k  S )z:Return whether the instance priority is higher than other.)r+   r   NotImplementedr   rH   )rV   other_galaxy_apis     r$   __lt__zGalaxyAPI.__lt__N  sH     *DNN;!! NN-777 "II		!	
r(   )r?   r@   r   c                    | j                   S r   )rD   r   s    r$   r   z GalaxyAPI.available_api_versionsY  s     +++r(         (   )retries
delay_basedelay_threshold)backoff_iteratorr3   c	           
        t        |      }	t        |      }
|s|	j                  }t        |	j                        }|r| j
                  rv| j
                  j                  |
i       }d}d}||v rt        j                  j                  ||   d   |      }|j                  t        j                  j                        }t        j                  j                  t        j                  j                        |k  }d|v xs d|v }|rS|sQ||   }|j                  d      r4d|v rd	d
d ii}nd
d i}g |d<   |d   D ]  }|d   j                  |        |S |d   }|S |sgt        j                  j                  t        j                  j                        }|t        j                  d      z  }|j!                  |      dd||<   |xs i }| j#                  |||       	 t$        j'                  d|z         t)        t+        |      || j,                  ||| j.                  t1               d      }t;        |j=                         d      }	 t?        j@                  |      }|rj| j
                  r^| j
                  |
   |   }d }dD ]
  }||v s|} n |r4d|d<   |j                  dg       }||   D ]  }|j                  |        |S ||d<   |S # t2        $ r}t5        ||      d }~wt6        $ r!}t9        d|dt+        |      |      d }~ww xY w# tB        $ r% t9        d|jD                  dt+        |            w xY w)Nz%Y-%m-%dT%H:%M:%SZFexpires)tzinfopageoffset	paginatedz/v3/linksnextresultsrh   )days)r   r   )requiredzCalling Galaxy at %ssafe)rX   r   headersr:   r   
http_agentfollow_redirectsz1Unknown error when attempting to call Galaxy at 'z': r*   rm   rn   z&Failed to parse Galaxy response from 'z' as JSON:
)rX   r   T)#r   rf   rr   r   queryr   
setdefaultdatetimestrptimereplacetimezoneutcnowrN   r   	timedeltastrftime_add_auth_tokenrE   rF   r   r   r   r   r   r   r,   	Exceptionr   r   rz   r{   r|   rJ   rd   )rV   rd   r!   r   r:   auth_requiredr;   r<   	cache_keyre   cache_idr   server_cacheiso_datetime_formatvalidr   is_paginated_url
path_cacheresresultrespe	resp_datarX   paginated_keykeyr   s                              r$   rI   zGalaxyAPI._call_galaxy_  se    C=$ I(T[[;;11(B?L"6EL("++44\)5LY5WYlm!//1B1B1F1F/G ))--h.?.?.C.CDwN%C(e2C-))4
>>+.*&7%tn &(C	N",Y"7 6I--f56 
 %Y/C
%"++//0A0A0E0EF8--155&//0CD!&+Y'
 -RWcMB
	LL/#56IcNdFYFYcj#)43G3GT^T`sy{D DIIK0EF		C::i(D
 T[[X.y9J !M* $;$'M
 *.
;'$//	2>"=1 +FNN6*+  )-
9%E  	4a!233 	NQS\]^S_` 	  	C"&((Ii,@ B C C	Cs1   AK
 L 
	LKL+LL.L=c                    d|v ry | j                   s3|r1t        dj                  t        t        j
                                    | j                   r*|j                  | j                   j                                y y )NAuthorizationzMNo access token or username set. A token can be set with --api-key or at {0}.)r   r   r   r   r   GALAXY_TOKEN_PATHupdater   )rV   r   rd   
token_typer   s        r$   r   zGalaxyAPI._add_auth_token  sf    g%zzh  ,,2F9Q=P=P3Q,RT T ::NN4::--/0 r(   c                    t        | j                  d      5 }|j                  t        t	        j
                  | j                        d             d d d        y # 1 sw Y   y xY w)Nrp   rk   rm   rn   )rt   r   r~   r   r{   r   r   )rV   r   s     r$   
_set_cachezGalaxyAPI._set_cache  sN    $$$40 	VBHHXdjj5>STU	V 	V 	Vs   :AA$r?   c                   t        | j                  | j                  d   d      dz   }t        d|i      }	 t	        ||| j
                  dt               | j                        }t        j                  t        |j!                         d            }|S # t        $ r}t        |d      d}~wt        $ r}t        d	t        |      z  |
      d}~ww xY w)z2
        Retrieve an authentication token
        r?   tokensr   github_tokenPOST)rX   r   r:   r   r   z$Attempting to authenticate to galaxyNz$Unable to authenticate to galaxy: %sr   rm   rn   )rM   rG   r   r
   r   r   r   r   r   r,   r  r   r   r{   r|   r   rz   )rV   r  rd   r!   r  r  rX   s          r$   authenticatezGalaxyAPI.authenticate  s    
 t(C(CD(I8TWZZ.,78	bCd4;N;NW]jtjv  AE  AU  AU  VD zz'$))+6KLM  	Ia!GHH 	bE	RST_`aa	bs#   -B 	CB((C4CCc                    t        | j                  | j                  d   d      dz   }|||r|ndd}|r||d<   | j                  |t	        |      d      }|j                  d	d
      r|d	   S |S )z(
        Post an import request
        r?   importsr   ra   )github_usergithub_repogithub_referencealternate_role_namer  r!   r:   r   N)rM   rG   r   rI   r
   rN   )rV   r  r   	reference	role_namerd   r!   rX   s           r$   create_import_taskzGalaxyAPI.create_import_task  s    
 t(C(CD(I9UX[[&&-6	B

 *3D&'  9T?6 J88It$	?"r(   c                    t        | j                  | j                  d   d      }|d||fz  }n|||d|d|}nt        d      | j	                  |      }|d   S )z5
        Check the status of an import task.
        r?   r  z%s?id=%d?github_user=&github_repo=z/Expected task_id or github_user and github_repor   )rM   rG   r   r   rI   )rV   task_idr  r   rd   rX   s         r$   get_import_taskzGalaxyAPI.get_import_task  sr    
 t(C(CD(I9UW~-C$)@8;[+VCPQQ  %Ir(   c           	        t        t        t        |                  }	 |j                  d      }dj	                  |dd       }|d   }|rt
        j                  d|d|       t        | j                  | j                  d   dd	|d
|      }| j                  |      }t        |d         dk7  r|d   d   S y# t        $ r t        d|z        w xY w)z&
        Find a role by name.
        .r   z- downloading role 'z', owned by zAInvalid role name (%s). Specify role as format: username.rolenamer?   rolesz?owner__username=z&name=r   N)r   urlquoter   r   rQ   rE   r  r   rM   rG   r   rI   len)rV   r%  notifyparts	user_namerd   rX   s          r$   lookup_role_by_namezGalaxyAPI.lookup_role_by_name  s    
 HXi%89:		pOOC(Eq-Ib	I)U^ _` t(C(CD(I78A9MO  %tI1$	?1%%  	pbennoo	ps   AB< <Cc                   g }	 t        | j                  | j                  d   d||d      }| j                  |      }|d   }|j	                  dd      du }t        | j                        }|j                  d|j                  d}|s?t        ||d         }| j                  |      }||d   z  }|j	                  dd      du }|s?|S # t        $ r2}	t        j                  d	|d
|dt        |	             Y d}	~	|S d}	~	ww xY w)z
        Fetch the list of related items for the given role.
        The url comes from the 'related' field of the role.
        r?   r/  z?page_size=50r   	next_linkNz://r   zUnable to retrieve role (id=z) data (z)), but this is not fatal so we continue: )rM   rG   r   rI   rN   r   schemenetlocr  rE   ry   r   )
rV   relatedrole_idr   rd   rX   donere   base_urlr  s
             r$   fetch_role_relatedzGalaxyAPI.fetch_role_related  s    	>4??D,G,G,MwX_ah*,C$$S)D9oGHH[$/47D  0H%-__hooFHxk):;((-4	?*d3t;	    	>OO&= > >	>s   CC	 		D'C??Dc           	        	 t        | j                  | j                  d   |d      }| j                  |      }d|v r|d   }n|}d}d|v r|j	                  dd      du }|sIt        | j                  |d         }| j                  |      }||d   z  }|j	                  dd      du }|sI|S # t
        $ r}t        d|d	t        |            d}~ww xY w)
z4
        Fetch the list of items specified.
        r?   z
?page_sizer   Tr   r7  NzFailed to download the z list: )rM   rG   r   rI   rN   r  r   r   )rV   whatrd   rX   r   r<  r   s          r$   get_listzGalaxyAPI.get_list7  s    
	`4??D,G,G,MtUabC$$S)DD y/D~d3t;t[0AB((-4	?*d3t;	 
 N 	`tYW\M]^__	`s   B&B+ )B+ +	C4CCc           	     ^   t        | j                  | j                  d   ddd      }|r#|dt        t	        t        |                  z   z  }|j                  dd       }|j                  dd       }|j                  dd       }|j                  d	d       }|r8t        |t              r(|j                  d
      }|ddj                  |      z   z  }|r8t        |t              r(|j                  d
      }|ddj                  |      z   z  }|r|d|z  z  }|r|d|z  z  }| j                  |      }|S )Nr?   searchr/  ?z&autocomplete=tags	platforms	page_sizeauthor,z&tags_autocomplete=+z&platforms_autocomplete=z&page_size=%sz&username_autocomplete=%s)rM   rG   r   r   r0  r   rN   r+   r   r   rQ   rI   )	rV   rC  r"   
search_urlrE  rF  rG  rH  rX   s	            r$   search_roleszGalaxyAPI.search_rolesO  s(    doot/J/J4/PRZ\cehi
*WXhv>N5O-PPPJzz&$'JJ{D1	JJ{D1	Hd+Jt\2::c?D/#((4.@@JI|<!,I4sxx	7JJJJ/I55J5>>J  ,r(   c                    t        | j                  | j                  d   d      dz   }t        ||||d      }| j	                  ||d      }|S )Nr?   notification_secretsr   )sourcer  r   secretr  r#  )rM   rG   r   r
   rI   )rV   rO  r  r   rP  rd   r!   rX   s           r$   
add_secretzGalaxyAPI.add_secretm  s_    t(C(CD(IKabehh&&	
    4 ?r(   c                t    t        | j                  | j                  d   d      }| j                  |d      }|S )Nr?   rN  T)r  rM   rG   r   rI   )rV   rd   rX   s      r$   list_secretszGalaxyAPI.list_secretsy  s:    t(C(CD(IKab  D 9r(   c                ~    t        | j                  | j                  d   d|      dz   }| j                  |dd      }|S )Nr?   rN  r   TDELETEr  r:   rS  )rV   	secret_idrd   rX   s       r$   remove_secretzGalaxyAPI.remove_secret  sE    t(C(CD(IKaclmpss  D Jr(   c           	         t        | j                  | j                  d   dd|d|      }| j                  |dd      }|S )Nr?   
removeroler(  r)  TrV  rW  rS  )rV   r  r   rd   rX   s        r$   delete_rolezGalaxyAPI.delete_role  sF    t(C(CD(I<;FTV  D Jr(   r@   r   c                F   t         j                  d|d| j                  d| j                         t        |d      }t        j
                  j                  |      st        dt        |      z        t        j                  |      st        dt        |      z        t        |d      5 }t        |j                         t        j                  	      }d
d
d
       t!        |ddd      \  }}|t#        |      d}d| j$                  v r)t'        | j                  | j$                  d   dd      dz   }n't'        | j                  | j$                  d   d      dz   }| j)                  |||ddd| j                  d| j                  d      }	|	d   S # 1 sw Y   xY w)z
        Publishes a collection to a Galaxy server and returns the import task URI.

        :param collection_path: The path to the collection tarball to publish.
        :return: The import task URI that contains the import results.
        z Publishing collection artifact 'z' to rB   rm   rn   z2The collection path specified '%s' does not exist.zThe collection path specified '%s' is not a tarball, use 'ansible-galaxy collection build' to create a proper release artifact.rj   )	hash_funcNzapplication/octet-stream)filename	mime_type)sha256file)zContent-typezContent-lengthr   	artifactscollectionsr   r@   r  Tz$Error when publishing collection to r7   r8   )r!   r   r:   r  r;   task)rE   rH   rG   r   rq   rr   r   r   r   tarfile
is_tarfilert   r   rz   hashlibra  r   r1  r   rM   rI   )
rV   collection_pathb_collection_pathcollection_tarra  content_typeb_form_datar   rW   r  s
             r$   publish_collectionzGalaxyAPI.publish_collection  s    	/[_[d[dfjfufuvw$_=RSww~~/0SV_`oVppqq##$56  MOXYhOi j k k #T* 	Tn">#6#6#8GNNSF	T %6  1!;%
!k )!+.

 4...T__d.I.I$.OQ\^klorrET__d.I.I$.OQ^_beeE  ['RXhl6:ii4R ! S F|7	T 	Ts   .*FF c                   d}d}d| j                   v r't        | j                  | j                   d   d|d      }n&t        | j                  | j                   d   d|d      }t        j                  d|z         t	        j                         }t
        j                  }|d	k(  st	        j                         |z
  |k  r	 | j                  |d
dd|z        }|j                  dd      }|j                  dd      rnmt        j                  d||fz         t	        j                  |       t        d|t
        j                  z        }|d	k(  rt	        j                         |z
  |k  r|dk(  rt        dt!        |      z        |j                  dg       D ]  }	|	d   }
|
j#                         dk(  rt        j%                  d|	d   z         7|
j#                         dk(  rt        j'                  d|	d   z         ft        j                  d|
d|	d           |dk(  rQt!        |d   j                  dd             }t!        |d   j                  d!d"|z              }t        d#|d$|d%      y# t        $ rH}|j                  dk7  r t        j                  d|z         t	        j                  |       Y d}~!d}~ww xY w)&aH  
        Waits until the import process on the Galaxy server has completed or the timeout is reached.

        :param task_id: The id of the import task to wait for. This can be parsed out of the return
            value for GalaxyAPI.publish_collection.
        :param timeout: The timeout in seconds, 0 is no timeout.
        waitingNr   zimports/collectionsr   r@   zcollection-importsz1Waiting until Galaxy import task %s has completedr   r9   Tz,Error when getting import task results at %s)r:   r  r;   r=   zJGalaxy import process has not started, wait %s seconds before trying againstatefinished_atzMGalaxy import process has a status of %s, wait %d seconds before trying again   zUTimeout while waiting for the Galaxy import process to finish, check progress at '%s'messageslevelr   zGalaxy import error message: %sr   ry   z!Galaxy import warning message: %szGalaxy import message: z - failedr   UNKNOWNdescriptionz&Unknown error, see %s for more detailszGalaxy import process failed: z (Code: r8   )r   rM   rG   rE   timer   &GALAXY_COLLECTION_IMPORT_POLL_INTERVALrI   r,   r-   vvvsleeprN   min$GALAXY_COLLECTION_IMPORT_POLL_FACTORr   r   lowerr   ry   )rV   r*  r   rq  rX   full_urlstartwaitr  r   ru  r   rx  s                r$   wait_import_taskzGalaxyAPI.wait_import_task  s     4...1L1LT1R 5wEH  1L1LT1R 4gsDH 	KhVW		77ltyy{U2g=	((%t;ilt;t ) v HHWi0Exxt,KKg $-( )JJt r4!"H"HHID- ltyy{U2g=. Iv!*8!4 5 6 6 xx
B/ 	\GG$E{{}'?')BTTU)+ CgiFX XYwyGYZ[	\ HT']..vyABD#W!!-1Y\d1degKP[]abcc	 A  ;;#%hkoop

4 s   <I1 1	K:=J==Kc           	     h   d| j                   v r| j                   d   }ddg}n| j                   d   }ddg}t        | j                  |d||d      }d	|d
|d| j                  d| j                  d	}| j	                  ||      }i }|D ]  \  }}	|j                  |	d      ||<    t        ||fi |S )z
        Gets the collection information from the Galaxy server about a specific Collection.

        :param namespace: The collection namespace.
        :param name: The collection name.
        return: CollectionMetadata about the collection.
        r   )r   
created_at)r   
updated_atr@   )r   created)r   modifiedrd  r   z+Error when getting the collection info for r-   from r7   r8   )r;   N)r   rM   rG   rH   rI   rN   r   )
rV   r   rH   api_path	field_mapinfo_urlr;   rX   metadata	api_fields
             r$   get_collection_metadataz!GalaxyAPI.get_collection_metadata  s     4...2248H-.I
 2248H*,I
 DOOX}iQUWZ[($		4??L  =N O( 	7OD)!XXi6HTN	7 ")T>X>>r(   c                   | j                   j                  d| j                   j                  d            }| j                  |d||d|dg}t        | }d|d|d|d	| j                  d
| j                  d}| j                  ||d      }| j                          |j                  d      xs g }	t        |d   d   |d   d   |d   |d   |d   d   |d   d   |d   |	      S )at  
        Gets the collection information from the Galaxy server about a specific Collection version.

        :param namespace: The collection namespace.
        :param name: The collection name.
        :param version: Version of the collection to get the information for.
        :return: CollectionVersionMetadata about the collection at the version requested.
        r   r@   rd  r\   r   3Error when getting collection version metadata for r-  r`   r  r7   r8   Tr;   r<   r   r   rH   
collectionrC   r   artifactra  r  r   href)r   rN   rG   rM   rH   rI   r  r   )
rV   r   rH   rC   r  	url_pathsn_collection_urlr;   rX   r   s
             r$   get_collection_version_metadataz)GalaxyAPI.get_collection_version_metadata  s    ..2249T9T9X9XY]9^___hy$PZ\cehi	#Y/($DOOU  !1EV^b cXXl+1r
(k):6)BDDVW]D^`den`o)-n)=tJ?OPX?Y)-j)9.)I4PV<Yce 	er(   c                   d}d| j                   v r| j                   d   }ddg}d}n| j                   d   }dg}d| j                   v rdnd}t        | j                  |d	||d
d|t        fz        }t	        |      }|j
                  }	| j                  r| j                  j                  t        |      i       }
|
j                  di       }	 | j                  ||      j                  }|j                  |d|d      }||k7  r+|||d|<   |j
                  |
v r|
|	= | j                          d|d|d| j                  d| j                  d	}	 | j!                  ||d|	      }d|v rd}nd}g }	 |||   D cg c]  }|d   	 c}z  }|}|D ]  }|j                  |i       } |sn^|r;t#        |t	        |      j
                        }|j%                  |j
                  |      }| j!                  t'        |d      |d|	      }| j                          |S # t        $ r}|j                  dk7  r g cY d}~S d}~ww xY w# t        $ r}|j                  dk7  r g cY d}~S d}~ww xY wc c}w )z
        Gets a list of available versions for a collection on a Galaxy server.

        :param namespace: The collection namespace.
        :param name: The collection name.
        :return: A list of versions that are available.
        Fr   r   r   Tr@   limitrG  rd  r\   z/?%s=%dr  r=   Nr-  z5Error when getting available collection versions for r  r7   r8   )r;   r<   r  rX   r   rC   rm   rn   )r   rM   rG   COLLECTION_PAGE_SIZEr   rr   r   r   rf   r  r   r,   r-   rN   r  rH   rI   r   r   r   )rV   r   rH   relative_linkr  pagination_pathpage_size_nameversions_urlversions_url_infor  r  modified_cachemodified_daterY   cached_modified_dater;   rX   results_keyr\   vr7  rr   s                         r$   get_collection_versionsz!GalaxyAPI.get_collection_versions8  s    4...2248H&/O M2248H%hO$(D,G,G$G[=)UY[egp  uC  EY  tZ  hZ  [$\2%**	 ;;;;11,|2LbQL)44ZDN $ < <Y M Z Z $2#5#5D6QSW#X #}4>K)T:;$))\9$Y/!  )$		4??L	$$\EV^bnw$xD T> !K#KtK/@A!9AAHI' 4%MM$3	4 
  '|Xl5K5P5PQ(001B1G1GS	$$YyAV%W7HPT`i % kD# & 	o  ==C'		&  	}}#I		  BsB   
H I :I)	H>"H93H>9H>	I&
I!I&!I&c                   | j                   j                  d| j                   j                  d            }| j                  |d||d|dg}t        | }d|d|d|d	| j                  d
| j                  d}| j                  ||d      }| j                          |j                  d      xs g D 	cg c]  }	|	d   	 }
}	|
s+t        j                  d| j                   d| d| d|        |
S c c}	w )aG  
        Gets the collection signatures from the Galaxy server about a specific Collection version.

        :param namespace: The collection namespace.
        :param name: The collection name.
        :param version: Version of the collection to get the information for.
        :return: A list of signature strings.
        r   r@   rd  r\   r   r  r-  r`   r  r7   r8   Tr  r   	signaturezServer z has not signed )	r   rN   rG   rM   rH   rI   r  rE   rF   )rV   r   rH   rC   r  r  r  r;   rX   signature_infor   s              r$   get_collection_signaturesz#GalaxyAPI.get_collection_signatures  s     ..2249T9T9X9XY]9^___hy$PZ\cehi	#Y/($DOOU  !1EV^b cHLQ]H^Hdbdenn[1e
eLL74??"33CI;aPTvUVW^V_`a fs   3C0)NNNFNFN)NF)NN)NNN)T)r   )%rT   r   r   r   floatr   r   r   r   r   propertyr^   r   r   r   r3   rI   r   r'   r  r  r&  r+  r5  r>  rA  rL  rQ  rT  rY  r\  rn  r  r  r  r  r  r&   r(   r$   r   r     s   W DT#'!&5\"gH$
"
	
	
 !", # , %21\^_- nr,0X	Xt
1 V V v " v " v  v , v : v` `. v : v	 	 v 
 v 
 v  d|+ +Z d|?d ?dB d|? ?@ d|e e0 d|U Un d| r(   r   )G
__future__r   rd  r   	functoolsrh  r{   rq   rv   rf  ry  	threadinghttpr   http.clientr   r   urllib.errorr   r   urllib.parser	   r0  r
   r   r   r   ansibler   r   ansible.errorsr   ansible.galaxy.user_agentr   ansible.module_utils.apir   r   ansible.module_utils.sixr   +ansible.module_utils.common.text.convertersr   r   r   ansible.module_utils.urlsr   r   ansible.utils.displayr   ansible.utils.hashingr   ansible.utils.pathr   rE   Lockr    r  TOO_MANY_REQUESTSBAD_GATEWAYr.   r'   r3   r^   rf   r   rM   r,   
namedtupler   r   total_orderingr   r&   r(   r$   <module>r     s  
 #      	      5 , R R " ' 0 D > 1 T T A ) / ,
)inn    &JZ5  Df%1, %1T ,[++,@Bvw % %4 T
 T
 T
r(   