o
    mi:                     @   s~  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mZmZm	Z	m
Z
mZ d dlZe dkZejddZdZdZde d	Zd
ZeeZG dd deZG dd deZG dd deZd=de	e de	e fddZde	e fddZdefddZ d=de	e defddZ!dede
eef fddZ"dedefdd Z#dede
eef fd!d"Z$d#ed$ede	e fd%d&Z%G d'd( d(e&Z'		d>d#ed$e	e d)e	e de	eeef  fd*d+Z(d,edefd-d.Z)d/eee*f de
eef fd0d1Z+	2d?d3ed4e,deeeeef f fd5d6Z-			d@de	e d7e	eeef  d)e	e de'fd8d9Z.d:edeeeeeeeeef f f f fd;d<Z/dS )A    N)AnyDictMappingOptionalTupleUnionWindowsz.dockerzconfig.jsonz
.dockercfgz	docker.iozhttps://index.z/v1/z<token>c                   @   s   e Zd ZdZdS )DockerErrorzBase class from which all other exceptions inherit.

    If you want to catch all errors that the Docker SDK might raise,
    catch this base exception.
    N)__name__
__module____qualname____doc__ r   r   O/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/wandb/docker/auth.pyr	      s    r	   c                   @      e Zd ZdS )InvalidConfigFileErrorNr
   r   r   r   r   r   r   r          r   c                   @   r   )InvalidRepositoryErrorNr   r   r   r   r   r   !   r   r   config_pathreturnc              
   C   s   t td | t tjt ttjt tg}t	
dt|  |D ]}tj|r8t	
d|  |  S q$t	
d d S )NzTrying paths: zFound file at path: zNo config file found)listfilterconfig_path_from_environmentospathjoinhome_dirDOCKER_CONFIG_FILENAMELEGACY_DOCKER_CONFIG_FILENAMElogdebugreprexists)r   pathsr   r   r   r   find_config_file%   s$   
r%   c                  C   s*   t jd} | s
d S t j| t jtS )NDOCKER_CONFIG)r   environgetr   r   basenamer   )
config_dirr   r   r   r   >   s   r   c                   C   s   t r	tjddS tjdS )zGet the user's home directory.

    Uses the same logic as the Docker Engine client - use %USERPROFILE% on Windows,
    $HOME/getuid on POSIX.
    USERPROFILE ~)IS_WINDOWS_PLATFORMr   r'   r(   r   
expanduserr   r   r   r   r   E   s   r   c              
   C   s   t | }|si S zt|}t|}|W  d    W S 1 s w   Y  W n ttfy? } zt| W Y d }~nd }~ww td i S )N4All parsing attempts failed - returning empty config)r%   openjsonloadOSError
ValueErrorr    r!   )r   config_filefconfer   r   r   load_general_configQ   s   

&
r:   	repo_namec                 C   sX   d| v rt d|  dt| \}}|d dks|d dkr&t d| dt||fS )	Nz://z)Repository name cannot contain a scheme ()r   -zInvalid index name (z%). Cannot begin or end with a hyphen.)r   split_repo_nameresolve_index_name)r;   
index_nameremote_namer   r   r   resolve_repository_named   s   

rC   rA   c                 C   s   t | } | dt krt} | S )Nzindex.)convert_to_hostname
INDEX_NAME)rA   r   r   r   r@   r   s   r@   c                 C   sT   |  dd}t|dksd|d vr"d|d vr"|d dkr"t| fS |d |d fS )N/   .r   :	localhost)splitlenrE   )r;   partsr   r   r   r?   y   s
   $r?   
authconfigregistryc                 C   s   t | ts	t| } | |S N)
isinstance
AuthConfigget_credential_store)rN   rO   r   r   r   rS      s   

rS   c                       s  e Zd Zd*dedee ddf fddZe	d+deeeee	f f d	e
deeeee	f f fd
dZe	d*dee deeee	f  dee dd fddZedeeeee	f f fddZedee fddZedefddZede
fddZ	d*dee deeee	f  fddZdee dedeeee	f  fddZdeddfd d!Zdee dee fd"d#Zdeeeee	f f fd$d%Zd&ed'eee	f ddfd(d)Z  ZS ),rR   Ndctcredstore_envr   c                    s8   t  | d|vri |d< | | || _t | _d S Nauths)super__init__update_credstore_envdict_stores)selfrT   rU   	__class__r   r   rY      s   
zAuthConfig.__init__Fentriesraise_on_errorc                 C   s   i }|  D ]j\}}t|ts%td| d |r!td| i   S d|v r:td|  d|d i||< qd|vrLtd| d	 i ||< qt|d \}}td
t| dt| d |||d|d||< q|S )a  Parse authentication entries.

        Args:
          entries:        Dict of authentication entries.
          raise_on_error: If set to true, an invalid format will raise
                          InvalidConfigFileError
        Returns:
          Authentication registry.
        zConfig entry for key z is not auth configz#Invalid configuration for registry identitytokenz*Found an IdentityToken entry for registry IdentityTokenauthzAuth data for z> is absent. Client might be using a credentials store instead.zFound entry (registry=z, username=r<   emailusernamepasswordrf   serveraddress)	itemsrQ   r\   r    r!   r   decode_authr"   r(   )clsra   rb   r8   rO   entryrh   ri   r   r   r   
parse_auth   s:   

zAuthConfig.parse_authr   config_dictc              
   C   sT  |sJt |}|s| i |S zt|}t|}W d   n1 s"w   Y  W n! tttfyI } zt| | t	||W  Y d}~S d}~ww i }t
|tsSJ |drltd |d| j|dddi |drtd |d|di |drtd	 |d|di |r| ||S td
 | d| |i|S )aO  Load authentication data from a Docker configuration file.

        If the config_path is not passed in it looks for a configuration file in the
        root directory.

        Lookup priority:
            explicit config_path parameter > DOCKER_CONFIG environment
            variable > ~/.docker/config.json > ~/.dockercfg.
        NrW   zFound 'auths' sectionT)rb   
credsStorezFound 'credsStore' sectioncredHelperszFound 'credHelpers' sectionzNCouldn't find auth-related section ; attempting to interpret as auth-only file)r%   r1   r2   r3   r4   KeyErrorr5   r    r!   _load_legacy_configrQ   r   r(   rZ   ro   pop)rm   r   rp   rU   r6   r7   r9   resr   r   r   load_config   sB   









zAuthConfig.load_configc                 C      |  di S rV   r(   r^   r   r   r   rW         zAuthConfig.authsc                 C   s   |  dd S )Nrq   ry   rz   r   r   r   creds_store  r{   zAuthConfig.creds_storec                 C   rx   )Nrr   ry   rz   r   r   r   cred_helpers  r{   zAuthConfig.cred_helpersc                 C   s   | j  o| j o| j S rP   )rW   r|   r}   rz   r   r   r   is_empty  s   zAuthConfig.is_emptyrO   c                 C   s   | j s| jr(| |}|dur(td| | ||}|dur#|S td |r.t|nt}tdt|  || j	v rNtdt|  | j	| S | j	
 D ]\}}t||krktdt|  |  S qStd dS )a   Return the authentication data for a specific registry.

        As with the Docker client, legacy entries in the config with full URLs are
        stripped down to hostnames before checking for a match. Returns None if no match
        was found.
        NzUsing credentials store z/No entry in credstore - fetching from auth dictLooking for auth entry for zFound No entry found)r|   r}   rS   r    r!   _resolve_authconfig_credstorer@   rE   r"   rW   rk   )r^   rO   
store_namecfgkeyr8   r   r   r   resolve_authconfig  s(   	




zAuthConfig.resolve_authconfigcredstore_namec              
   C   s   |r|t krt}tdt|  | |}z'||}d|i}|d tkr0|d |d< |W S ||d |d d |W S  t	j
tfyP   td Y d S  t	jye } z	tdt| d }~ww )	Nr   ServerAddressUsernameSecretrd   )r   Passwordr   zCredentials store error: )rE   	INDEX_URLr    r!   r"   _get_store_instancer(   TOKEN_USERNAMErZ   dockerpycredsCredentialsNotFoundr5   
StoreErrorr	   )r^   rO   r   storedatarv   r9   r   r   r   r   2  s(   


z(AuthConfig._resolve_authconfig_credstorenamezdockerpycreds.Storec                 C   s*   || j vrtj|| jd| j |< | j | S )N)environment)r]   r   Storer[   )r^   r   r   r   r   r   K  s
   

zAuthConfig._get_store_instancec                 C   s"   |r|t krt}| j|p| jS rP   )rE   r   r}   r(   r|   )r^   rO   r   r   r   rS   R  s   zAuthConfig.get_credential_storec                 C   sh   | j  }| jr | | j}|  D ]}| || j||< q| j D ]\}}| ||||< q%|S rP   )	rW   copyr|   r   r   keysr   r}   rk   )r^   	auth_datar   kregr   r   r   r   get_all_credentialsX  s   
zAuthConfig.get_all_credentialsr   r   c                 C   s   || d |< d S rV   r   )r^   r   r   r   r   r   add_authf  s   zAuthConfig.add_authrP   F)r
   r   r   r   r   r   rY   classmethodstrr   boolro   rw   propertyrW   r|   r}   r~   r   r   r   rS   r   r   __classcell__r   r   r_   r   rR      s^     84 
"
&rR   rU   c                 C   s   t | ts
t| |} | |S rP   )rQ   rR   r   )rN   rO   rU   r   r   r   r   j  s   


r   urlc                 C   s    |  dd ddddd S )Nzhttp://r,   zhttps://rF   rG   r   )replacerK   )r   r   r   r   rD   t  s    rD   re   c                 C   sB   t | tr
| d} t| }|dd\}}|d|dfS )Nascii   :rG   utf8)rQ   r   encodebase64	b64decoderK   decode)re   sloginpwdr   r   r   rl   x  s
   


rl   Fra   rb   c                 C   s   t | |S )zParse authentication entries.

    Args:
      entries:        Dict of authentication entries.
      raise_on_error: If set to true, an invalid format will raise
                      InvalidConfigFileError
    Returns:
      Authentication registry.
    )rR   ro   )ra   rb   r   r   r   ro     s   ro   rp   c                 C   s   t | ||S rP   )rR   rw   )r   rp   rU   r   r   r   rw     s   rw   r6   c              
   C   s   t d zIg }t| %}| D ]}|| dd  qt|dk r*tdW d    n1 s4w   Y  t	|d \}}dt
|||d tdiiW S  tye } zt | W Y d }~nd }~ww t d	 i S )
Nz+Attempting to parse legacy auth file formatz = rG      z$Invalid or empty configuration file!r   rW   rg   r0   )r    r!   r1   	readlinesappendstriprK   rL   r   rl   rE   r   	Exception)r6   r   r7   linerh   ri   r9   r   r   r   rt     s4   



rt   rP   )NNr   )NNN)0r   r2   loggingr   platformtypingr   r   r   r   r   r   r   systemr.   r   r   r   r   rE   r   r   	getLoggerr
   r    r   r	   r   r   r   r%   r   r   r:   rC   r@   r?   rS   r\   rR   r   rD   bytesrl   r   ro   rw   rt   r   r   r   r   <module>   s    

 d

"	

"