o
    mi*)                     @   s  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d dlmZ d dlmZmZ d dlmZ G dd deZejejejedZe ZeeZd	ee d
ee fddZ da!d
e"fddZ#d
e"fddZ$	dEdee dededee d
ef
ddZ%dee d
e"fddZ&dee d
efddZ'					dFdee de"de"d ee( d!e"d"eeeef  d
e
ee	eef f fd#d$Z)d%ee( d
efd&d'Z*dGd(e"d
efd)d*Z+d+ed
e	eee f fd,d-Z,d.ed
e	eeef fd/d0Z-d1ed2ed
eeef fd3d4Z.d.ed
ee fd5d6Z/d.ed
ee fd7d8Z0d.ed
e1fd9d:Z2d;ed<ed
ee fd=d>Z3d?ed@ed1ed
ee fdAdBZ4d.ed<ed
ee fdCd<Z5g dDZ6dS )H    N)AnyDictListOptionalTupleUnion)find_executable)authwww_authenticate)Errorc                       sH   e Zd ZdZ		d
dee dedee dee ddf
 fdd	Z	  Z
S )DockerErrorz3Raised when attempting to execute a docker command.Ncommand_launchedreturn_codestdoutstderrreturnc                    sx   d |}d| d| d}|d ur|d|  d7 }n|d7 }|d ur0|d|  d7 }n|d	7 }t | d S )
N z!The docker command executed was `z`.
It returned with code 
zThe content of stdout is 'z'
zNThe content of stdout can be found above the stacktrace (it wasn't captured).
zThe content of stderr is 'zMThe content of stderr can be found above the stacktrace (it wasn't captured).)joindecodesuper__init__)selfr   r   r   r   command_launched_str	error_msg	__class__ S/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/wandb/docker/__init__.pyr      s    
zDockerError.__init__)NN)__name__
__module____qualname____doc__r   strintr   bytesr   __classcell__r   r   r   r   r      s    r   zwandb-entrypoint.shcmdr   c              
   C   sT   zt jdg|  t jdd W S  t jy) } zt| W Y d}~dS d}~ww )z_Simple wrapper for calling docker,.

    returning None on error and the output on success
    docker)r   utf8N)
subprocesscheck_outputSTDOUTr   stripCalledProcessErrorprint)r'   er   r   r   shell5   s   r1   c                  C   s<   t durt S tdsda t S tddg} | duod| v a t S )z8Return `True` if docker buildx is installed and working.Nr(   Fbuildxz--help)_buildx_installedr   r1   )help_outputr   r   r   is_buildx_installedH   s   r5   c                  C   s>   zt jddgdd} | jdkrW dS W dS  ty   Y dS w )z?Return `True` if docker is installed and working, else `False`.r(   z	--versionT)capture_outputr   F)r*   run
returncodeFileNotFoundError)resultr   r   r   is_docker_installedU   s   
r;   tagsfilecontext_pathplatformc           
      C   s   t  }|r	ddgndg}|t|r|rdgng 7 }|r!|d|g7 }g }| D ]}|d|g7 }q%dg| | d||g }t|}	|	S )Nr2   buildz--loadz
--platformz-tr(   z-f)r5   should_add_load_argumentrun_command_live_output)
r<   r=   r>   r?   
use_buildxcommand
build_tagstagargsr   r   r   r   r@   f   s   r@   c                 C   s   | d u s
| rd| vrdS dS )N,TFr   )r?   r   r   r   rA   x   s   rA   rG   c                 C   s   t j| t jt jddd<}d}	 t|j d}|sn!|d}|dkr.t	|
 dd n||
 7 }t	|
 d	d qt	| W d    n1 sKw   Y  | }|d
kr`t| || |S )NT   )r   r   universal_newlinesbufsize i      )endr   )r*   PopenPIPEr,   osreadr   filenofindr/   r   waitr   encode)rG   processr   chunkindexr   r   r   r   rB      s2   

rB   TFcapture_stdoutcapture_stderrinputreturn_stderrenvc           
      C   s   dd | D } t tj}||pi  | d dkrd|d< |r"tjnd }|r)tjnd }tj| ||||d}	|	jdkrDt| |	j|	j	|	j
|rPt|	j	t|	j
fS t|	j	S )	Nc                 S   s   g | ]}t |qS r   )r#   ).0xr   r   r   
<listcomp>   s    zrun.<locals>.<listcomp>rI   r2   enabledDOCKER_CLI_EXPERIMENTAL)r^   r   r   r`   r   )dictrS   environupdater*   rR   r7   r8   r   r   r   _post_process_stream)
rG   r\   r]   r^   r_   r`   subprocess_envstdout_deststderr_destcompleted_processr   r   r   r7      s,   



r7   streamc                 C   s<   | d u rdS |   }t|dkr|d dkr|d d }|S )NrL   r   rN   r   )r   len)rn   decoded_streamr   r   r   ri      s   ri   gpuc                 C   s   d}| s|d7 }d |S )Nallz-cpuzwandb/deepo:{})format)rq   rF   r   r   r   default_image   s   
rt   	repo_namec                 C   sd   |  dd}t|dkr|d |d fS |  dd}t|dkr.d|d vr.|d |d fS | d fS )N@rI      r   :/)rsplitro   )ru   partsr   r   r   parse_repository_tag   s   r|   
image_namec                 C   s4   t | \}}t|\}}|dkrd}|||pdfS )Nz	docker.ioindex.docker.iolatest)r|   r	   resolve_repository_name)r}   
repositoryrF   registryru   r   r   r   parse   s
   r   r   repoc           	      C   s   t | }|rdd | D }|d|df}nd}tjd|  ddd	}|jd
rDz
t|jd
 }W n tyC   i }Y nw t	
d| d|   i }|drvtj|d d d|d d | |dd}|  | }|S i S )zMake a request to the root of a v2 docker registry to get the auth url.

    Always returns a dictionary, if there's no token key we couldn't authenticate
    c                 S   s   i | ]	\}}|  |qS r   )lower)ra   kvr   r   r   
<dictcomp>   s    zauth_token.<locals>.<dictcomp>usernamepasswordNhttps:///v2/   )timeoutzwww-authenticate	Received z& when attempting to authenticate with bearerrealmz$?service={}&scope=repository:{}:pullservice)r	   r   )auth_configresolve_authconfigitemsgetrequestsheadersr
   r   
ValueErrorlogerrorrs   raise_for_statusjson)	r   r   	auth_info
normalizednormalized_auth_inforesponseinforesresult_jsonr   r   r   
auth_token   s@   


r   c              	   C   s   t | \}}}d}z+t||d}|dkrd}tjd| d| d| d| d	d
dd}|  W n tjyJ   td| d|   Y dS w d	|d | |j
d gS )z4Get the docker id from a public or private registry.Ntokenr~   zregistry-1.docker.ior   r   z/manifests/zBearer z4application/vnd.docker.distribution.manifest.v2+json)AuthorizationAccept   )r   r   r   z# when attempting to get digest for rv   ry   zDocker-Content-Digest)r   r   r   r   headr   RequestExceptionr   r   r   r   )r}   r   r   rF   r   r   r   r   r   image_id_from_registry  s&   r   c              	   C   s\   d| v r| S t d| ddg}z|du rtt|d }|W S  ttfy-   t|  Y S w )zFRetrieve the image id from the local docker daemon or remote registry.z@sha256:inspectz--formatz{{json .RepoDigests}}Nr   )r1   r   r   loads
IndexErrorr   )r}   digestsim_idr   r   r   image_id$  s   r   c                 C   s    t d| ddg}|rt|S dS )z3Retrieve the image default uid through brute force.r7   idz-urN   )r1   r$   )r}   	image_uidr   r   r   get_image_uid3  s   r   imagerF   c                 C   s   t d|  d| gS )z#Push an image to a remote registry.pushrx   r1   )r   rF   r   r   r   r   9  s   r   r   r   c                 C   s   t dd| d||gS )zLogin to a registry.loginz
--usernamez
--passwordr   )r   r   r   r   r   r   r   >  s   r   c                 C   s   t d| |gS )zTag an image.rF   r   )r}   rF   r   r   r   rF   C  s   )r1   r@   r7   r   r   r;   r   r   r|   rt   r   r   r   rF   )N)TTNFN)F)7r   loggingrS   r*   typingr   r   r   r   r   r   r   dockerpycreds.utilsr   wandb.dockerr	   r
   wandb.errorsr   r   pathr   dirnameabspath__file__
entrypointload_configr   	getLoggerr   r   r#   r1   r3   boolr5   r;   r@   rA   rB   r%   r7   ri   rt   r|   r   r   r   r   r$   r   r   r   rF   __all__r   r   r   r   <module>   s      


$	
)