o
    xi9&                     @  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 ddl	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
ZdZdZG dd dZG dd dZG dd dZd2ddZd3ddZd4d!d"Zd5d#d$Z			d6d7d+d,Z			d6d8d-d.Zd9d0d1Z dS ):a   The layer between launch sdk user code and the wandb internal process.

If there is an active run this communication is done through the wandb run's
backend interface.

If there is no active run, the messages are staged on the StagedLaunchInputs
singleton and sent when a run is created.
    )annotationsN)Any)LaunchError)META_SCHEMA)
get_module   )config_path_is_validoverride_file.\_wandb_configsc                   @  s<   e Zd ZdZdZdd Zdd Zedd Zed	d
 Z	dS )ConfigTmpDira  Singleton for managing temporary directories for configuration files.

    Any configuration files designated as inputs to a launch job are copied to
    a temporary directory. This singleton manages the temporary directory and
    provides paths to the configuration files.
    Nc                 C     | j d u rt| | _ | j S N	_instanceobject__new__cls r   T/home/ubuntu/.local/lib/python3.10/site-packages/wandb/sdk/launch/inputs/internal.pyr   )      
zConfigTmpDir.__new__c                 C  s:   t | dst | _tj| jt| _t	| j d S d S )N_tmp_dir)
hasattrtempfilemkdtempr   ospathjoinLAUNCH_MANAGED_CONFIGS_DIR_configs_dirmkdirselfr   r   r   __init__.   s
   

zConfigTmpDir.__init__c                 C     t | jS r   )pathlibPathr   r#   r   r   r   tmp_dir4      zConfigTmpDir.tmp_dirc                 C  r&   r   )r'   r(   r!   r#   r   r   r   configs_dir8   r*   zConfigTmpDir.configs_dir)
__name__
__module____qualname____doc__r   r   r%   propertyr)   r+   r   r   r   r   r      s    
r   c                   @  s&   e Zd ZdZ					ddddZdS )JobInputArgumentsz1Arguments for the publish_job_input of Interface.Nincludelist[str] | Noneexcludeschemadict | None	file_path
str | None
run_configbool | Nonec                 C  s"   || _ || _|| _|| _|| _d S r   r2   r4   r5   r7   r9   )r$   r2   r4   r5   r7   r9   r   r   r   r%   @   s
   
zJobInputArguments.__init__)NNNNN)
r2   r3   r4   r3   r5   r6   r7   r8   r9   r:   )r,   r-   r.   r/   r%   r   r   r   r   r1   =   s    r1   c                   @  s6   e Zd ZdZdd ZdddZdd
dZdddZdS )StagedLaunchInputsNc                 C  r   r   r   r   r   r   r   r   R   r   zStagedLaunchInputs.__new__returnNonec                 C  s   t | ds
g | _d S d S )N_staged_inputs)r   r?   r#   r   r   r   r%   W   s   

zStagedLaunchInputs.__init__input_argumentsr1   c                 C  s   | j | d S r   )r?   append)r$   r@   r   r   r   add_staged_input[   s   z#StagedLaunchInputs.add_staged_inputrun	wandb.Runc                 C  s   | j D ]}t|| qdS )z)Apply the staged inputs to the given run.N)r?   _publish_job_input)r$   rC   inputr   r   r   applya   s   
zStagedLaunchInputs.apply)r=   r>   )r@   r1   )rC   rD   )r,   r-   r.   r   r   r%   rB   rG   r   r   r   r   r<   O   s    

r<   rF   rC   rD   r=   r>   c                 C  s   |j dusJ |j jdusJ | jdusJ |j j}| jr1t }tj|j| j}|j	||j
d |j| jr>dd | jD ng | jrJdd | jD ng | j| j| jpSdd dS )zPublish a job input to the backend interface of the given run.

    Arguments:
        input (JobInputArguments): The arguments for the job input.
        run (wandb.Run): The run to publish the job input to.
    N)	base_pathc                 S     g | ]}t |qS r   _split_on_unesc_dot.0r   r   r   r   
<listcomp>{       z&_publish_job_input.<locals>.<listcomp>c                 S  rI   r   rJ   rL   r   r   r   rN   ~   rO    )include_pathsexclude_pathsinput_schemar9   r7   )_backend	interfacer9   r7   r   r   r   r   r+   saver)   publish_job_inputr2   r4   r5   )rF   rC   rU   
config_dirdestr   r   r   rE   g   s&   

rE   r5   dictdefsr6   c           	      C  s   i }d| v r|r|  ddd }t|| |}|  D ]T\}}t|tr0t||}|||< qt|trmg }|D ]}t|trI|t|| q9|| q9|dkrht|dkrht|d trh|	|d  q|||< q|||< q|S )zRecursively fix JSON schemas with common issues.

    1. Replaces any instances of $ref with their associated definition in defs
    2. Removes any "allOf" lists that only have one item, "lifting" the item up
    See test_internal.py for examples
    z$refz#/$defs/r   allOfr   )
popsplit_replace_refs_and_allofsitems
isinstancerZ   listrA   lenupdate)	r5   r[   retdef_keykeyvalnew_val_dictnew_val_listitemr   r   r   r_      s,   






r_   r   c                 C  sD   t | drt| jr|  } t| tstd| dd}t| |S )a  Prepare a schema for validation.

    This function prepares a schema for validation by:
    1. Converting a Pydantic model instance or class to a dict
    2. Replacing $ref with their associated definition in defs
    3. Removing any "allOf" lists that only have one item, "lifting" the item up

    We support both an instance of a pydantic BaseModel class (e.g. schema=MySchema(...))
    or the BaseModel class itself (e.g. schema=MySchema)
    model_json_schemazHschema must be a dict, Pydantic model instance, or Pydantic model class.z$defsN)r   callablerl   ra   rZ   r   r]   r_   )r5   r[   r   r   r   _prepare_schema   s   

rn   c                 C  sF   t dddd}|t}t|| td}|r!td|  d S d S )N
jsonschemaziSetting job schema requires the jsonschema package. Please install it with `pip install 'wandb[launch]'`.F)requiredlazy)rg   z5Schema includes unhandled or invalid configurations:
)r   Draft202012Validatorr   sortediter_errorsstrwandbtermwarn)r5   ro   	validatorerrsr   r   r   _validate_schema   s   
rz   r   ru   r2   r3   r4   
Any | Nonec           	      C  s   t |  t|  t }tj|j| }tj|}tj|s$t	| t
| | |r4t|}t| t|||| dd}tjdurJt|tj dS t }|| dS )a_  Declare an overridable configuration file for a launch job.

    The configuration file is copied to a temporary directory and the path to
    the copy is sent to the backend interface of the active run and used to
    configure the job builder.

    If there is no active run, the configuration file is staged and sent when a
    run is created.
    Fr;   N)r   r	   r   r   r   r   r+   dirnameexistsmakedirsshutilcopyrn   rz   r1   rv   rC   rE   r<   rB   )	r   r2   r4   r5   r)   rY   dest_dir	argumentsstaged_inputsr   r   r   handle_config_file_input   s2   

r   c                 C  sT   |r
t |}t| t| ||ddd}tjdur t|tj dS t }|| dS )a:  Declare wandb.config as an overridable configuration for a launch job.

    The include and exclude paths are sent to the backend interface of the
    active run and used to configure the job builder.

    If there is no active run, the include and exclude paths are staged and sent
    when a run is created.
    TN)r2   r4   r5   r9   r7   )rn   rz   r1   rv   rC   rE   r<   rB   )r2   r4   r5   r   stage_inputsr   r   r   handle_run_config_input   s   
r   	list[str]c                 C  s   g }d}d}|t | k rX| | tkr6|t | d kr%td|  dt d| |d  tkr5|t7 }|d7 }n| | tkrH|| d}|d7 }n
|| | 7 }|d7 }|t | k s|r_|| |S )zSplit a string on unescaped dots.

    Arguments:
        path (str): The string to split.

    Raises:
        ValueError: If the path has a trailing escape character.

    Returns:
        List[str]: The split string.
    rP   r   r   zInvalid config path z: trailing r
      )rc   	BACKSLASHr   PERIODrA   )r   partspartir   r   r   rK     s.   


rK   )rF   r1   rC   rD   r=   r>   )r5   rZ   r[   r6   r=   rZ   )r5   r   r=   rZ   )r5   rZ   r=   r>   )NNN)r   ru   r2   r3   r4   r3   r5   r{   )r2   r3   r4   r3   r5   r{   )r   ru   r=   r   )!r/   
__future__r   r   r'   r   r   typingr   rv   wandb.data_typeswandb.sdk.launch.errorsr   wandb.sdk.launch.inputs.schemar   
wandb.utilr   filesr   r	   r   r   r    r   r1   r<   rE   r_   rn   rz   r   r   rK   r   r   r   r   <module>   s@    	

 
)
,