o
    xiZ                     @  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Zddlm	Z	m
Z
mZmZ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 dd	lmZmZmZ dd
lmZ ddlmZ ee Z!e	riddl"m#Z# dZ$dZ%dZ&ed Z'G dd dZ(e(ddde(ddddZ)G dd deZ*G dd deZ+G dd deZ,G dd deZ-G dd ded d!Z.G d"d# d#eZ/d,d(d)Z0G d*d+ d+Z1dS )-zjob builder.    )annotationsN)TYPE_CHECKINGAnyCallableLiteral	TypedDict)InternalArtifact)Artifact)TypeRegistry)Api)
DIFF_FNAMEMETADATA_FNAMEREQUIREMENTS_FNAME)make_artifact_name_safe   )SettingsStatic)ArtifactRecordzrequirements.frozen.txtwandb-job.jsonjob)logwarnerrorc                   @  s4   e Zd ZdddZdd	d
ZdddZdddZdS )Versionmajorintminorpatchc                 C  s   || _ || _|| _d S N_major_minor_patch)selfr   r   r    r#   R/home/ubuntu/.local/lib/python3.10/site-packages/wandb/sdk/internal/job_builder.py__init__#   s   
zVersion.__init__returnstrc                 C  s   | j  d| j d| j S )N.r   r"   r#   r#   r$   __repr__(   s   zVersion.__repr__otherboolc                 C  sL   | j |j k rdS | j |j kr$| j|jk rdS | j|jkr$| j|jk r$dS dS )NTFr   r"   r+   r#   r#   r$   __lt__+   s   zVersion.__lt__objectc                 C  s2   t |tstS | j|jko| j|jko| j|jkS r   )
isinstancer   NotImplementedr   r    r!   r-   r#   r#   r$   __eq__5   s   


zVersion.__eq__N)r   r   r   r   r   r   )r&   r'   )r+   r   r&   r,   )r+   r/   r&   r,   )__name__
__module____qualname__r%   r*   r.   r2   r#   r#   r#   r$   r   "   s
    



r      )
dockerfilebuild_contextc                   @     e Zd ZU ded< ded< dS )GitInfor'   remotecommitNr3   r4   r5   __annotations__r#   r#   r#   r$   r:   F      
 r:   c                   @  6   e Zd ZU ded< ded< ded< ded< ded	< d
S )GitSourceDictr:   git	list[str]
entrypointr,   notebook
str | Noner8   r7   Nr=   r#   r#   r#   r$   rA   K      
 rA   c                   @  r@   )ArtifactSourceDictr'   artifactrC   rD   r,   rE   rF   r8   r7   Nr=   r#   r#   r#   r$   rH   S   rG   rH   c                   @  s   e Zd ZU ded< dS )ImageSourceDictr'   imageNr=   r#   r#   r#   r$   rJ   [   s   
 rJ   c                   @  sF   e Zd ZU ded< ded< ded< ded< ded< d	ed
< ded< dS )JobSourceDictr'   _versionsource_type4GitSourceDict | ArtifactSourceDict | ImageSourceDictsourcedict[str, Any]input_typesoutput_typesrF   runtimedict[str, str]servicesNr=   r#   r#   r#   r$   rL   _   s   
 rL   F)totalc                   @  r9   )ArtifactInfoForJobr'   idnameNr=   r#   r#   r#   r$   rX   i   r?   rX   rP   rO   r&   Version | Nonec                 C  s4   d}| D ]}t |}|r|du s||k r|}q|S )zJGet the minimum supported wandb version the source dict of wandb-job.json.N)!SOURCE_KEYS_MIN_SUPPORTED_VERSIONget)rP   min_seenkeynew_verr#   r#   r$   !get_min_supported_for_source_dictn   s   
ra   c                   @  s  e Zd ZU ded< ded< ded< ded< ded	< ded
< ded< ded< ded< ded< ded< ded< ded< ded< ded< 	djdkddZdld"d#Zdmd%d&Zednd'd(Zej	dod*d(Zedpd+d,Z
edpd-d.Zdqd0d1Zdrd6d7Zdsd;d<Zdtd@dAZdudCdDZdvdFdGZdwdIdJZdxdKdLZdndMdNZdndOdPZdydSdTZ	U	U	Udzd{d\d]Zd|d^d_Zd}d`daZd~dbdcZddddeZdndfdgZddhdiZdUS )
JobBuilderr   	_settingsr'   
_files_dirrF   _metadatafile_path_requirements_pathzdict[str, Any] | None_config_summaryzArtifactInfoForJob | None_logged_code_artifactr,   _disable_partial_source_idrC   _aliases_job_seq_id_job_version_alias_is_notebook_run_verboserU   	_servicesFsettingsverbose	files_dirc                C  sx   || _ || _d| _d| _d| _d| _d| _d| _d| _|j	p |j
| _d| _g | _|j| _|  | _|| _d| _i | _dS )a  Instantiate a JobBuilder.

        Args:
            settings: Parameters for the job builder.
                In a run, this is the run's settings.
                Otherwise, this is a set of undocumented parameters,
                all of which should be made explicit like files_dir.
            files_dir: The directory where to write files.
                In a run, this should be the run's files directory.
        NF)rc   rd   re   rf   rg   rh   ri   rm   rn   disable_job_creationx_disable_machine_inforj   rk   rl   
job_source_source_type_get_is_notebook_runro   rp   _partialrq   )r"   rr   rs   rt   r#   r#   r$   r%      s$   

zJobBuilder.__init__configrQ   r&   Nonec                 C  
   || _ d S r   )rg   )r"   r{   r#   r#   r$   
set_config      
zJobBuilder.set_configsummaryc                 C  r}   r   )rh   )r"   r   r#   r#   r$   set_summary   r   zJobBuilder.set_summaryc                 C  s   | j S r   rj   r)   r#   r#   r$   disable   s   zJobBuilder.disablevalc                 C  r}   r   r   )r"   r   r#   r#   r$   r      s   
c                 C     t | j S r   )r
   type_ofrg   to_jsonr)   r#   r#   r$   rR         zJobBuilder.input_typesc                 C  r   r   )r
   r   rh   r   r)   r#   r#   r$   rS      r   zJobBuilder.output_types	source_idc                 C  r}   r   )rk   )r"   r   r#   r#   r$   set_partial_source_id   r   z JobBuilder.set_partial_source_idresdict | NonerI   r   c              
   C  s   |j dkr`|d ur`z<|d d d u rd| _n'|d d d |d kr/d|d d d  | _nd|d d d d  | _|d d | _W n ty_ } ztd	|  W Y d }~nd }~ww |j d
krv|d urxt|d |jd| _d S d S d S )Nr   artifactSequencelatestArtifactv0rY   vversionIndexr   z+Malformed response from ArtifactSaver.save code)rY   rZ   )	typern   rm   KeyError_loggerinforX   rZ   ri   )r"   r   rI   er#   r#   r$   _handle_server_artifact   s(   
z"JobBuilder._handle_server_artifactprogram_relpathmetadata'tuple[GitSourceDict | None, str | None]c                 C  sf  | di }| d}| d}| d}|d usJ |d us!J | jrtjtjt tj|s7dS |d u sA| jj	d u rHt
d dS | jj	d usPJ tjtjt| jj	||}tj|}|dr|d}d	}	|D ]
}
|
dkr~|	d
7 }	qtd|d|	 d  }n|}| ||}||d|| j| d| dd}| | d| }||fS )NrB   r;   r<   rootNN#target path does not exist, exitingz../r   r      )r;   r<   r8   r7   )rB   rD   rE   r8   r7   _)r]   ro   ospathexistsjoingetcwdbasenamerc   x_jupyter_rootr   r   relpathr'   normpath
startswithsplit_get_entrypoint_make_job_name)r"   r   r   git_infor;   r<   r   full_program_path
split_path
count_dotsprD   rP   rZ   r#   r#   r$   _build_repo_job_source   sN   





z!JobBuilder._build_repo_job_sourcemessagelevel	LOG_LEVELc                 C  sr   d }|dkrt | tj}n|dkrt | tj}n|dkr(t | tj}| jr5|d ur7|| d S d S d S )Nr   r   r   )	r   r   wandbtermlogwarningtermwarnr   	termerrorrp   )r"   r   r   log_funcr#   r#   r$   _log_if_verbose  s   


zJobBuilder._log_if_verbose,tuple[ArtifactSourceDict | None, str | None]c           	      C  s   t | jtsJ | jr<|  s<tj|t }tj	|s;tj	tj
|s5td | dd dS tj
|}n|}| ||}|| jd| jd  |d|dd	}| jd
 d^}}| |}||fS )Nr   zNo program path found when generating artifact job source for a non-colab notebook run. See https://docs.wandb.ai/guides/launch/create-jobr   r   zwandb-artifact://_id/rY   r8   r7   )rD   rE   rI   r8   r7   rZ   :)r0   ri   dictro   _is_colab_runr   r   r   r   r   r   r   r   r   r   r]   r   r   )	r"   r   r   full_program_relpathrD   rP   artifact_basenamer   rZ   r#   r#   r$   _build_artifact_job_source'  s0   

z%JobBuilder._build_artifact_job_sourcetuple[ImageSourceDict, str]c                 C  sz   | d}t|tsJ |}d|v r0|dd }td|r0|d| d}|  j|g7  _d|i}| |}||fS )Ndockerr   z([a-zA-Z0-9_\-\.]+) rK   )	r]   r0   r'   r   re	fullmatchreplacerl   r   )r"   r   
image_nameraw_image_nametagrP   rZ   r#   r#   r$   _build_image_job_sourceM  s   

z"JobBuilder._build_image_job_source	input_strc                 C  s   | j jr| j jS td| S )zCUse job name from settings if provided, else use programmatic name.zjob-)rc   job_namer   )r"   r   r#   r#   r$   r   d  s   zJobBuilder._make_job_namec                 C  s2   | j r|dr|d }|S tjtj|g}|S )NrD   )rz   r]   r   r   r   sys
executable)r"   r   r   rD   r#   r#   r$   r   k  s
   zJobBuilder._get_entrypointc                 C     t | jdot| jjS )N_jupyter)hasattrrc   r,   r   r)   r#   r#   r$   ry   z     zJobBuilder._get_is_notebook_runc                 C  r   )N_colab)r   rc   r,   r   r)   r#   r#   r$   r   }  r   zJobBuilder._is_colab_runrN   Ntuple[GitSourceDict | ArtifactSourceDict | ImageSourceDict | None, str | None]c                 C  s   d}|dkr|  |pd|\}}n"|dkr | |pd|\}}n|dkr1| |r1| |\}}nd}|du rE|rC| d| dd d	S ||fS )
zConstruct a job source dict and name from the current run.

        Args:
            source_type (str): The type of source to build the job from. One of
                "repo", "artifact", or "image".
        Nrepor   rI   rK   zSource type is set to 'z' but some required information is missing from the environment. A job will not be created from this run. See https://docs.wandb.ai/guides/launch/create-jobr   r   )r   r   _has_image_job_ingredientsr   r   )r"   rN   r   r   rP   rZ   r#   r#   r$   _build_job_source  s,   


zJobBuilder._build_job_sourceNapir   r8   r7   
base_imageArtifact | Nonec                 C  s  t d | jdurd| ji| jd}|| j| dS tjtj	| j
ts0| dd dS |  }|du rE| d| j
 dd dS |d	}|du rV| d
d dS t| j }t| j }	d}
d}| |}|s| jjs|| jjs|| jr| dd dS | ||}| js|dkr|s| dd dS | |||\}}
|du rdS |r||d< |r||d< |r||d< t| D ]}|| du r|| qtt |pd||||	|d}| j!r| j!|d< |dusJ |
dusJ t"|
t#}t d |$d}|%t&j'|dd W d   n	1 sw   Y  |j(tj	| j
tt)d |dkrItjtj	| j
t*rI|j(tj	| j
t*t*d |S )a  Build a job artifact from the current run.

        Args:
            api (Api): The API object to use to create the job artifact.
            build_context (Optional[str]): Path within the job source code to
                the image build context. Saved as part of the job for future
                builds.
            dockerfile (Optional[str]): Path within the build context the
                Dockerfile. Saved as part of the job for future builds.
            base_image (Optional[str]): The base image used to run the job code.

        Returns:
            Optional[Artifact]: The job artifact if it was successfully built,
            otherwise None.
        z Attempting to build job artifactNz@wandb.config)rR   rS   zhNo requirements.txt found, not creating job artifact. See https://docs.wandb.ai/guides/launch/create-jobr   z/Ensure read and write access to run files dir: zf, control this via the WANDB_DIR env var. See https://docs.wandb.ai/guides/track/environment-variablespythonzrNo python version found in metadata, not creating job artifact. See https://docs.wandb.ai/guides/launch/create-jobz/No source type found, not creating job artifactrK   zdNo program path found, not creating job artifact. See https://docs.wandb.ai/guides/launch/create-jobr8   r7   r   r   )rM   rN   rP   rR   rS   rT   rV   zadding wandb-job metadata filer      )indent)rZ   r   )+r   r   rk   rR   rS   update_artifact_metadatar   r   r   r   rd   r   r   _handle_metadata_filer]   r
   r   rg   r   rh   _get_source_typerc   r   rw   rx   _get_program_relpathrz   r   listkeyspopr'   ra   rq   r   JOB_ARTIFACT_TYPEnew_filewritejsondumpsadd_fileFROZEN_REQUIREMENTS_FNAMEr   )r"   r   r8   r7   r   new_metadatar   rT   rR   rS   rZ   source_inforN   r   rP   r_   rI   fr#   r#   r$   build  s   




	


zJobBuilder.buildc                 C  s`   | j r| j S | |rtd dS |  rtd dS | |r)td dS td d S )Nzis repo sourced jobr   zis artifact sourced jobrI   zis image sourced jobrK   zno source found)rx   _has_git_job_ingredientsr   r   _has_artifact_job_ingredientsr   r"   r   r#   r#   r$   r   B  s   





zJobBuilder._get_source_typec                 C  s`   | j rtd |d}|s| dd |S |dks!| jjdkr+|dp*|dS |dS )Nzrun is notebook based runprogramzaNotebook 'program' path not found in metadata. See https://docs.wandb.ai/guides/launch/create-jobr   rI   codePathLocalcodePath)ro   r   r   r]   r   rc   rw   )r"   rN   r   r   r#   r#   r$   r   U  s   


zJobBuilder._get_program_relpathc                 C  s`   t jt j| jtr.tt j| jt}t|}W d    |S 1 s'w   Y  |S d S r   )	r   r   r   r   rd   r   openr   load)r"   r   r   r#   r#   r$   r   l  s   
z JobBuilder._handle_metadata_filec                 C  s@   | di }| jr| dd u rdS | dd uo| dd uS )NrB   r   Fr;   r<   )r]   ro   )r"   r   r   r#   r#   r$   r   v  s   z#JobBuilder._has_git_job_ingredientsc                 C  s
   | j d uS r   )ri   r)   r#   r#   r$   r   |  r   z(JobBuilder._has_artifact_job_ingredientsc                 C  s   | dd uS )Nr   )r]   r   r#   r#   r$   r     s   z%JobBuilder._has_image_job_ingredients)F)rr   r   rs   r,   rt   r'   )r{   rQ   r&   r|   )r   rQ   r&   r|   )r&   r,   )r   r,   r&   r|   )r&   rQ   )r   r'   r&   r|   )r   r   rI   r   r&   r|   )r   r'   r   rQ   r&   r   )r   r'   r   r   r&   r|   )r   r'   r   rQ   r&   r   )r   rQ   r&   r   )r   r'   r&   r'   )r   r'   r   rQ   r&   rC   )rN   r'   r   rF   r   rQ   r&   r   )NNN)
r   r   r8   rF   r7   rF   r   rF   r&   r   )r   rQ   r&   rF   )rN   r'   r   rQ   r&   rF   )r&   r   )r   rQ   r&   r,   )r3   r4   r5   r>   r%   r~   r   propertyr   setterrR   rS   r   r   r   r   r   r   r   r   ry   r   r   r   r   r   r   r   r   r   r#   r#   r#   r$   rb   z   sd   
 
&




6

&




/ 





rb   )rP   rO   r&   r[   )2__doc__
__future__r   r   loggingr   r   r   typingr   r   r   r   r   r   &wandb.sdk.artifacts._internal_artifactr   wandb.sdk.artifacts.artifactr	   wandb.sdk.data_types._dtypesr
   wandb.sdk.internal.internal_apir   wandb.sdk.lib.filenamesr   r   r   
wandb.utilr   settings_staticr   	getLoggerr3   r   wandb.proto.wandb_internal_pb2r   r   	JOB_FNAMEr   r   r   r\   r:   rA   rH   rJ   rL   rX   ra   rb   r#   r#   r#   r$   <module>   sF    




