o
    xi(                     @  s,  d Z ddlmZ ddlZddlZddlZddlZddlmZm	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d	lmZmZ dd
lmZmZ ddlmZ ddlmZ ddlmZ dZdZ ej!Z"dDd!d"Z#dEd$d%Z$dFd(d)Z%dGd4d5Z&G d6d7 d7Z'dHd:d;Z(dId<d=Z)dJd>d?Z*dKdBdCZ+dS )Lz)Implements `wandb sync` using wandb-core.    )annotationsN)IterableIterator)filterfalse)term)ServerSyncResponse)wandb_setup)asyncio_compatwbauth)Printernew_printer)progress_printer)ServiceConnection)MailboxHandle   g?pathslist[pathlib.Path]liveboolentitystrprojectrun_idjob_typereplace_tagsdry_runskip_syncedverboseparallelismintreturnNonec       
           s>  t |t 	ztj  W n ty   d Y nw d}| s)t	jjg} d}t	
fdd| D |	ds?t
d dS |rUt
dt d	 t|	 d
 dS t
dt d	 t|	 d
 |rpt
dspdS tj	jjdddst
d dS 	 t 	j 	fdd dS )a  Replay one or more .wandb files.

    Args:
        live: Whether to enable 'live' mode, which indefinitely retries reading
            incomplete transaction logs.
        entity: The entity override for all paths, or an empty string.
        project: The project override for all paths, or an empty string.
        run_id: The run ID override for all paths, or an empty string.
        job_type: An override for the job type for all runs, or an empty string.
        replace_tags: A string in the form 'old1=new1,old2=new2' that defines
            how to rename run tags.
        paths: One or more .wandb files, run directories containing
            .wandb files, and wandb directories containing run directories.
        dry_run: If true, just prints what it would do and exits.
        skip_synced: If true, skips files that have already been synced
            as indicated by a .wandb.synced marker file in the same directory.
        verbose: Verbose mode for printing more info.
        parallelism: Max number of runs to sync at a time.
    NFTc                 3  s&    | ]}t | d D ]}|V  q
qdS )r   N)_find_wandb_files).0path
wandb_filer"    G/home/ubuntu/.local/lib/python3.10/site-packages/wandb/cli/beta_sync.py	<genexpr>K   s    
zsync.<locals>.<genexpr>)r   zNo runs to sync.zWould sync z run(s):)r   rootzSyncing zSync the listed runs?z
wandb sync)hostsource
no_offlinezNot authenticated.c                     s"   t  
	jdS )N)cwdr   servicer   r   r   r   tag_replacementssettingsprinterr   )_do_syncr1   r'   )r.   r   r   r   r   r2   r   r   r/   	singletonr0   wandb_filesr'   r(   <lambda>n   s    zsync.<locals>.<lambda>)_parse_replace_tagsr   r4   pathlibPathr.   OSErrorr1   	wandb_dir_to_unique_filesr   termloglen_print_sorted_pathsconfirmr
   authenticate_sessionbase_urlensure_servicer   asyncerrun)r   r   r   r   r   r   r   r   r   r   r   ask_for_confirmationr'   )r.   r   r   r   r   r2   r   r   r/   r4   r   r0   r5   r(   sync   sP   !
	

 rG   dict[str, str]c                 C  sX   | si S i }|  dD ]}d|vrtd| d| dd\}}| || < q|S )z0Parse the --replace-tags argument to wandb sync.,=zInvalid --replace-tags format: z. Expected 'old=new'.   )split
ValueErrorstrip)r   r0   pairold_tagnew_tagr'   r'   r(   r7      s   
r7   Iterator[pathlib.Path]set[pathlib.Path]c                C  s   t  }t| ddD ]C}z| }W n ty. } ztd| d|  W Y d}~q	d}~ww |j|jf}|rH|| }rHt	| d|  |||< q	t
| S )zmReturns paths with duplicates removed.

    Determines file equality the same way as os.path.samefile().
    T)reversezFailed to stat z: Nz is the same as )dictsortedstatr:   r   	termerrorst_inost_devgetr=   setvalues)r   r   
id_to_pathr%   rW   eid
other_pathr'   r'   r(   r<      s   	
r<   r5   r.   pathlib.Path | Noner/   r   r0   r1   wandb.Settingsr2   r   c                  sj   |j | |	|||||||d	I dH }|jddI dH }|j|j|dI dH }t|j||
|I dH  dS )zaSync the specified files.

    This is factored out to make the progress animation testable.
    )r.   r   r   r   r   r   r0   N   timeout)r   )	init_sync
wait_asyncrG   r`   _SyncStatusLoopwait_with_progress)r5   r.   r   r/   r   r   r   r   r0   r1   r2   r   init_handleinit_resultsync_handler'   r'   r(   r3      s*   r3   c                   @  sB   e Zd ZdZdd
dZdddZdddZdddZdddZdS )ri   z6Displays a sync operation's status until it completes.r`   r   r/   r   r2   r   r    r!   c                 C  s&   || _ || _|| _d | _t | _d S )N)_id_service_printer_rate_limit_last_timeasyncioEvent_done)selfr`   r/   r2   r'   r'   r(   __init__   s
   z_SyncStatusLoop.__init__handle!MailboxHandle[ServerSyncResponse]c              	     sb   t  4 I dH }|| | ||   W d  I dH  dS 1 I dH s*w   Y  dS )z2Display status updates until the handle completes.N)r	   open_task_group
start_soon_wait_then_mark_done_show_progress_until_done)ru   rw   groupr'   r'   r(   rj      s
   .z"_SyncStatusLoop.wait_with_progressc                   sB   |j d dI d H }|jD ]}| jj|j|jd q| j  d S )Nre   level)rh   messagesrp   displaycontentseverityrt   r\   )ru   rw   responsemsgr'   r'   r(   r{      s
   
z$_SyncStatusLoop._wait_then_mark_donec                   s   t | jdJ}|  I dH sI| j| jI dH }|jddI dH }|jD ]}| jj|j	|j
d q%|t|j |  I dH rW d   dS W d   dS 1 sTw   Y  dS )z4Show rate-limited status updates until _done is set.z
Syncing...Nre   r~   )r   rp   _rate_limit_check_donero   sync_statusrn   rh   new_messagesr   r   r   updateliststats)ru   progressrw   r   r   r'   r'   r(   r|      s   
"z)_SyncStatusLoop._show_progress_until_doner   c                   sT   t  }| j}|| _|r%||  }tk r%ttt| | j I dH  | j	 S )z4Wait for rate limit and return whether _done is set.N)
time	monotonicrq   _POLL_WAIT_SECONDSr	   race_SLEEPrt   waitis_set)ru   now	last_timetime_since_lastr'   r'   r(   r     s   


z&_SyncStatusLoop._rate_limit_check_doneN)r`   r   r/   r   r2   r   r    r!   )rw   rx   r    r!   )r    r!   )r    r   )	__name__
__module____qualname____doc__rv   rj   r{   r|   r   r'   r'   r'   r(   ri      s    


	
	ri   r%   pathlib.Pathc                c  s0    |rt tt| E dH  dS t| E dH  dS )z*Returns paths to the .wandb files to sync.N)r   
_is_synced_expand_wandb_files)r%   r   r'   r'   r(   r#     s   r#   c                 c  sh    | j dkr| V  dS | d}zt|}W n	 ty   Y nw |V  |E dH  dS | dE dH  dS )z/Iterate over .wandb files selected by the path.z.wandbNz*.wandbz	*/*.wandb)suffixglobnextStopIteration)r%   files_in_run_directory
first_filer'   r'   r(   r   !  s   


r   c                 C  s   |  d S )z*Returns whether the .wandb file is synced.z.wandb.synced)with_suffixexists)r%   r'   r'   r(   r   6  s   r   Iterable[pathlib.Path]r*   c          
   	   C  s   g }| D ])}t |}|r(tt t ||}W d   n1 s#w   Y  || qt|}|r8t|nt}t	t
t||D ]}td||   qCt||krht|| }	td|	dd dS dS )a  Print file paths, sorting them and truncating the list if needed.

    Args:
        paths: Paths to print. Must be absolute with symlinks resolved.
        verbose: If true, doesn't truncate paths.
        root: A root directory for making paths relative.
    Nz  z  +z,dz! more (pass --verbose to see all))r   
contextlibsuppressrM   relative_toappendrV   r>   _MAX_LIST_LINESrangeminr   r=   )
r   r   r*   formatted_pathsr%   formatted_pathsorted_paths	max_linesi	remainingr'   r'   r(   r?   ;  s    r?   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   )r   r   r    rH   )r   rR   r   r   r    rS   )r5   rS   r.   rb   r   r   r/   r   r   r   r   r   r   r   r   r   r0   rH   r1   rc   r2   r   r   r   r    r!   )r%   r   r   r   r    rR   )r%   r   r    rR   )r%   r   r    r   )r   r   r   r   r*   rb   r    r!   ),r   
__future__r   rr   r   r8   r   collections.abcr   r   	itertoolsr   wandbwandb.errorsr   wandb.proto.wandb_sync_pb2r   	wandb.sdkr   wandb.sdk.libr	   r
   wandb.sdk.lib.printerr   r   wandb.sdk.lib.progressr   (wandb.sdk.lib.service.service_connectionr    wandb.sdk.mailbox.mailbox_handler   r   r   sleepr   rG   r7   r<   r3   ri   r#   r   r   r?   r'   r'   r'   r(   <module>   s:    

d

)
<

