o
    ni@                     @   sd  d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
mZmZmZmZmZ ddlZddlZddlmZ ddlZddlmZ ddlmZ d	d
lmZ dZdZd	ZdZdZe  Z!dd Z"deee#ee# f  de$ddfddZ%de#de$fddZ&dede#de$fddZ'de#de#de#ddfdd Z(de#d!e#ddfd"d#Z)dede$fd$d%Z*d&ee#e	f d'ee#e	f d(ee# ddfd)d*Z+d+d,d-d,d(ee# ddfd.d/Z,	0dHd1e#d2ee# d3e$dd,fd4d5Z-d6d,d7e#d1e#d8e#d9ee# d:e$dee$ed, ee# f fd;d<Z.de$fd=d>Z/dede#dee$ee# f fd?d@Z0de$fdAdBZ1deddfdCdDZ2dEe
de	fdFdGZ3dS )IzUtilities for wandb verify.    N)partial)Path)AnyCallableDictListOptionalTupleUnion)gql)Artifact)runid   )Apiverify
   u   ✅u   ❌c                 C   s   t d |  S )N-)	ID_PREFIX)name r   U/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/wandb/sdk/verify/verify.pynice_id   s   r   failed_test_or_testswarningreturnc                    s   |rd nd t | trtt ttj|  dd d S t | tr;t| dkr;tt td fdd| D  d S tt	 d S )	NyellowredTfgboldr   
c                    s   g | ]
}t j| d dqS )Tr   )clickstyle).0fcolorr   r   
<listcomp>.   s    z!print_results.<locals>.<listcomp>)

isinstancestrprintRED_Xr!   r"   listlenjoin	CHECKMARK)r   r   r   r%   r   print_results    s   
r0   hostc                 C   s   | dv rt dd dS dS )N)zapi.wandb.aizhttp://api.wandb.aizhttps://api.wandb.aiz,Cannot run wandb verify against api.wandb.aiFT)r0   )r1   r   r   r   
check_host5   s   
r2   apic                 C   sj   t ddddd d}d }| jd u rdtj|dd	d
}n| j }|s,d| d}t|d |d u S )NzChecking if logged inH   . endz)https://docs.wandb.ai/ref/cli/wandb-loginzBNot logged in. Please log in using `wandb login`. See the docs: {}Tblue)	underliner   zPCould not get viewer with default API key. Please relogin using `WANDB_BASE_URL=z% wandb login --relogin` and try againF)	r*   ljustapi_keyformatr!   r"   r3   viewerr0   )r3   r1   login_doc_urlfail_stringresr   r   r   check_logged_in<   s    


rB   urltest_url_stringfailure_outputc                 C   s4   t |dddd d }| ds|}t|d d S )Nr4   r5   r6   r7   httpsT)r*   r;   
startswithr0   )rC   rD   rE   r@   r   r   r   check_secure_requestsT   s
   
rH   originc                 C   sT   t ddddd d }tj| |ddd}|jd	d u r#d
| }t|d d S )Nz)Checking CORs configuration of the bucketr4   r5   r6   r7   GET)OriginzAccess-Control-Request-Method)headerszAccess-Control-Allow-OriginzbYour object store does not have a valid CORs configuration, you must allow GET and PUT to Origin: T)r*   r;   requestsoptionsrL   getr0   )rC   rI   r@   res_getr   r   r   check_cors_configuration]   s   
rQ   c              	   C   s  t ddddd g }d}d}dd	d
}g d}||||d}d}t|d}|d |  tjtdd|tdf}	|	j	}
|	j
}d}z%tddD ]}|	jdd| i|d qJddd}|	jd|i|d d W n tyy   d}|d Y nw z|	dtdi W n ty   |d Y nw t| W d    n1 sw   Y  t }|| dt d|
 }|d u r|d  t|d dS |j D ]\}}|| |kr|d!  nq|r|jd" d d# d$ks|jd% dks|jd" d& d# dks|jd" d' d# dkr|d( |r%|jd d$kr%|d) ztt|j|}tt|jdd*}W n tyM   |d+ t|d Y dS w | }|dkr\|d, t|d t|d-kS ).Nz6Checking logged metrics, saving and downloading a filer4   r5   r6   r7      zA test config   zconfig string)
config_valconfig_string)r   one2)epochs
stringTestdictTestlistTestz"./test with_special-characters.txtwtest	check_runT)idreinitconfigproject      lossg      ?)step)val1val2dictFz5Failed to log values to run. Contact W&B for support.zHT%3ML z!<a href="https://mysite">Link</a>z0Failed to log to media. Contact W&B for support./z:Failed to access run through API. Contact W&B for support.zCRead config values don't match run config. Contact W&B for support.keyspreviousValueg?lastStepz	dict.val1z	dict.val2zCHistory metrics don't match logged values. Check database encoding.zdRead summary values don't match expected value. Check database encoding, or contact W&B for support.replacez]Unable to download file. Check SQS configuration, topic configuration and bucket permissions.zTContents of downloaded file do not match uploaded contents. Contact W&B for support.r   )r*   r;   openwriteclosewandbinitr   PROJECT_NAMEr_   entityrangelog	ExceptionappendHtmlsaver   runr0   ra   itemshistory_keyssummaryretry_fnr   filedownloadreadr-   )r3   failed_test_stringsn_epochsstring_test	dict_test	list_testra   filepathr$   r}   run_idrv   loggedilog_dict
public_apiprev_runkeyvalue	read_filecontentsr   r   r   r^   m   s   







r^   downloaded_manifestcomputed_manifest
fails_listc                 C   sl   z&|  D ]}|| d | | d ksJ || d | | d ks#J qW d S  ty5   |d Y d S w )NdigestsizezGArtifact manifest does not appear as expected. Contact W&B for support.)rk   AssertionErrorrz   )r   r   r   r   r   r   r   verify_manifest   s   
r   
downloadedr   computedc                 C   s   | j |j kr|d d S d S )NzEArtifact digest does not appear as expected. Contact W&B for support.)r   rz   )r   r   r   r   r   r   verify_digest   s
   r   Fr   
verify_dirsingularc                 C   s$  t jd| d}tdd}|d |  ||j W d    n1 s&w   Y  |r/|S |d u r5d}|d}|d W d    n1 sJw   Y  tj	
|sZt| t| d	d}|d
 W d    n1 srw   Y  || t|d }|d ||   |S )Nartsy)typer   verify_int_test.txtr\   ztest 1z./verify_a.txtztest 2z/verify_1.txt1verify_3.txt3)rs   r   rp   rq   rr   add_filer   new_fileospathexistsmakedirsadd_dirr   
write_textadd_referenceresolveas_uri)r   r   r   artr$   file3r   r   r   artifact_with_path_or_paths   s.   



r   artifactaliasdownload_dirr   add_extra_filec                 C   s  t jtddtddid^}|r4tdd}|d |  | |j W d    n1 s/w   Y  z	|j	| |d	 W n& t
yc } z|d
|  dd |fW  Y d }~W  d    S d }~ww W d    n1 snw   Y  t jtdtddid^}	z|	| d| }
W n& t
y } z|d|  dd |fW  Y d }~W  d    S d }~ww z|
j|d W n t
y   |d dd |f Y W  d    S w W d    n1 sw   Y  d|
|fS )Nlog_artifactTr]   zartifact logr_   r`   rb   ra   verify_2.txtr\   rW   )aliaseszUnable to log artifact. Fuse_artifactzartifact use)r_   rb   ra   :zUnable to use artifact. rootz6Unable to download artifact. Check bucket permissions.)rs   rt   r   ru   rp   rq   rr   r   r   r   ry   rz   r   r   )r   r   r   r   r   r   log_art_runr$   euse_art_runused_artr   r   r   log_use_download_artifact  sb   

r   c                  C   sf  t ddddd g } d}d}td}t|d	d
}t||||| d\}}} |r,|d u r3t| d dS z|j|d W n tyI   | d Y nw d}d}td}t|ddd
}t||||| d	\}}} |rk|d u rrt| d dS t	t
|h dkr| d tjddd}	|	| t||	|  |	j d }
|j d }t||
|  t| d t| dkS )Nz-Checking artifact save and download workflowsr4   r5   r6   r7   z./verify_sing_art	sing_art1z
sing-artysT)r   Fr   zEArtifact does not contain expected checksum. Contact W&B for support.z./verify_artart1zmy-artysz./verify_art_dir>   verify_1.txtr   r   r   r   z=Artifact directory is missing files. Contact W&B for support.r   dataset)r   r   r   )r*   r;   r   r   r   r0   r   
ValueErrorrz   setr   listdirrs   r   r   r   manifestto_manifest_jsonr   r-   )r   sing_art_dirr   r   singular_art	cont_testdownload_artifactmulti_art_dirr   r   r   r   r   r   r   check_artifacts6  sR   





r   c                 C   sh  t ddddd g }d}t|d}|d |  tjtd	d
tddid}t	| W d    n1 s9w   Y  t
 }||j dt d|j }|d u ra|d t|d dS ztt|j|}|j}	tt|jd
d}W n ty   |d t|d Y dS w | }
z|
dksJ W n ty   |d Y nw t|d t|dk|	fS )NzChecking signed URL uploadr4   r5   r6   r7   zgql_test_file.txtr\   test2graphql_putTr]   zput to graphqlr   rj   zJUnable to access previous run through public API. Contact W&B for support.F)FNrn   z}Unable to read file successfully saved through a put request. Check SQS configurations, bucket permissions and topic configs.zMRead file contents do not match saved file contents. Contact W&B for support.r   )r*   r;   rp   rq   rr   rs   rt   r   ru   r|   r   r}   rv   r_   rz   r0   r   r   r   rC   r   ry   r   r   r-   )r3   r1   r   gql_fpr$   r}   r   r   r   rC   r   r   r   r   check_graphql_putn  sX   




r   c               
   C   s   t ddddd dtd } t }g }td}t }|j}z|j	||t
d| d	d
d W n2 tyb } z&t|tjrO|jd urO|jjdkrO|d n	|d| d W Y d }~nd }~ww t|d t|dkS )Nz5Checking ability to send large payloads through proxyr4   r5   r6   r7   ai a6  
        query Project($entity: String!, $name: String!, $runName: String!, $desc: String!){
            project(entityName: $entity, name: $name) {
                run(name: $runName, desc: $desc) {
                    name
                    summaryMetrics
                }
            }
        }
        )rv   r   runNamedesc<   )variable_valuestimeouti  zYFailed to send a large payload. Check nginx.ingress.kubernetes.io/proxy-body-size is "0".z+Failed to send a large payload with error: Fr   )r*   r;   intgetpassgetuserr   rs   r   _base_client_get_resultru   ry   r(   rM   	HTTPErrorresponsestatus_coderz   r0   r-   )descyusernamer   queryr   clientr   r   r   r   check_large_post  sH   





r   c                 C   s   t ddddd |  \}}d }d}|di dd }|di d	d
}ddlm} |tj||k r=d|}n|tj||krNd| d}d}t	|| d S )Nz,Checking wandb package version is up to dater4   r5   r6   r7   FcliVersionInfomax_cli_versionmin_cli_versionz0.0.1r   )parse_versionzEwandb version out of date, please run pip install --upgrade wandb=={}zwandb version is not supported by your local installation. This could cause some issues. If you're having problems try: please run `pip install --upgrade wandb==`T)
r*   r;   viewer_server_inforO   
wandb.utilr   rs   __version__r=   r0   )r3   _server_infor@   r   r   r   r   r   r   r   check_wandb_version  s(   r   fnc                 C   s^   t   }d }d}|tk st   | tk r-|d7 }z|  }W |S  ty,   t d Y qw |S )Nr   rc   )time
MIN_RETRYSGET_RUN_MAX_TIMEry   sleep)r   ini_timerA   r   r   r   r   r     s   
r   )NF)4__doc__r   r   r   	functoolsr   pathlibr   typingr   r   r   r   r   r	   r
   r!   rM   	wandb_gqlr   rs   wandb.sdk.artifacts.artifactr   wandb.sdk.libr   apis.internalr   ru   r   r   r/   r+   generate_idr   r   r)   boolr0   r2   rB   rH   rQ   r^   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s    $
	c






/"8.3