o
    -wiC                  #   @   s~  d dl 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jejejd
 e dZ!ddgZ"										d<de#de#de
e# de
e# de
e# de
e# de
e	e#  de
e# de
e# de
e# de
e# de
e# de
e fddZ$											d=dede#de#de
e# de
e# de
e# de
e# de
e	e#  de
e# de
e# de
e# de
e# de
e# de
e# dee
e e#e	e# f fddZ%de#d ej&de
e# de
e# de#de
e# dee
ee#ef  e
e	e#  f fd!d"Z'de#fd#d$Z(		d>de#d e#de#de
e# de
e# de
ee#ef  fd%d&Z)	d?de#de#de
e# dee
ee#ef  e
e	e#  f fd'd(Z*d)e#d*e#defd+d,Z+ded-ed.d/de#de#de
e# de
e# de
e# de
e# fd0d1Z,de#de
e# de#fd2d3Z-d4e#d5ee#ef d6e
e	e#  ddfd7d8Z.d9e#de#fd:d;Z/dS )@    N)AnyDictListOptionalTuple)Api)Artifact)
JobBuilder)GitReference)_is_git_uriget_current_python_versionget_entrypoint_file)
filesystem)make_artifact_name_safe)streamlevelwandbz.gitpathjob_typeentityprojectnamedescriptionaliasesruntime
entrypointgit_hashbuild_context
dockerfilereturnc                 C   s0   t  }t||| ||||||||	|
|\}}}|S )a  Create a job from a path, not as the output of a run.

    Arguments:
        path (str): Path to the job directory.
        job_type (str): Type of the job. One of "git", "code", or "image".
        entity (Optional[str]): Entity to create the job under.
        project (Optional[str]): Project to create the job under.
        name (Optional[str]): Name of the job.
        description (Optional[str]): Description of the job.
        aliases (Optional[List[str]]): Aliases for the job.
        runtime (Optional[str]): Python runtime of the job, like 3.9.
        entrypoint (Optional[str]): Entrypoint of the job. If build_context is
            provided, path is relative to build_context.
        git_hash (Optional[str]): Git hash of a specific commit, when using git type jobs.
        build_context (Optional[str]): Path to the build context, when using image type jobs.
        dockerfile (Optional[str]): Path to the Dockerfile, when using image type jobs.
            If build_context is provided, path is relative to build_context.

    Returns:
        Optional[Artifact]: The artifact created by the job, the action (for printing), and job aliases.
                            None if job creation failed.

    Example:
        ```python
        artifact_job = wandb.create_job(
            job_type="code",
            path=".",
            entity="wandb",
            project="jobs",
            name="my-train-job",
            description="My training job",
            aliases=["train"],
            runtime="3.9",
            entrypoint="train.py",
        )
        # then run the newly created job
        artifact_job.call()
        ```
    )r   _create_job)r   r   r   r   r   r   r   r   r   r   r   r   apiartifact_job_action_aliases r%   X/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/wandb/sdk/launch/create_job.py
create_job   s"   5
r'   r!   
base_imagec                    s  t d| d  r  t kr t dt   d dg fS |d ur7td|s7t d|  d dg fS |p:g }t }zt|||
|||	d\}}|sTd dg fW S W n t	yt } zt d|  d dg fW  Y d }~S d }~ww t
||j|d	 zt j|jd
d
d||dd}W n t	y   d dg f Y S w t|j|d} |j_|dkr|	d usJ t| |||	||| d}|sd dg fS | |j| j|||d}|st d td d dg fS  s|j ||j7 }d|vr|dg7 }| jd |j|j|j|||j|dd
id
 fdd|D d\}}d}|di ds#d}n
|ddkr-d }|j||d! |  |  t   | d"| d"|j }|!  |||fS )#NzCreating launch job of type: z...zfArtifact names may only contain alphanumeric characters, dashes, underscores, and dots. Did you mean:  z^3\.\d+$zXRuntime (-r, --runtime) must be a minor version of Python 3, e.g. 3.9 or 3.10, received )r   tempdirr   r   r   r   zError creating job: )metadatatmp_pathrequirementsT)silentdisable_job_creationcli_create_job)dirsettingsr   r   r   )
job_sourcecode)r!   job_builderr   r   runr   r   r   )r   r   r(   z JobBuilder failed to build a jobz2Failed to build job, check job source and metadatalatestjob_partialc                       g | ]} |d qS )artifactCollectionNamealiasr%   .0ar   r%   r&   
<listcomp>   s    z_create_job.<locals>.<listcomp>artifact_type_nameartifact_collection_namedigest	client_idsequence_client_identity_nameproject_namerun_namer   r+   is_user_createdr   zNo changes detected forartifactSequencelatestArtifactCreatedstatePENDINGUpdated)r   /)"r   termlogr   	termerrorrematchtempfileTemporaryDirectory_make_metadata_for_partial_job	Exception_dump_metadata_and_requirementsr   init"_configure_job_builder_for_partial	_settingsjob_name_make_code_artifactbuildr!   _loggerdebugr$   create_artifactrF   
_client_id_sequence_client_ididgetlog_artifactwaitfinishr   r6   delete)r!   r   r   r   r   r   r   r   r   r   r   r   r   r(   r*   r+   r-   er6   r5   r`   artifactres_action_runr%   rA   r&   r    g   s   











 
r    r*   c           
      C   s   i }| dkr'|dusJ t ||j|||d}|s|  dS || |dfS | dkrG|dus1J t|||d\}}	|s>dS || ||	fS | dkrg|rRtd |rYtd	 ||p^d
|d |dfS td|   dS )zCCreate metadata for partial jobs, return metadata and requirements.gitN)r   r*   r   r   r   NNr4   )r   r   r   imagezASetting runtime is not supported for image jobs, ignoring runtimez]Setting an entrypoint is not currently supported for image jobs, ignoring entrypoint argumentr)   )pythondockerzInvalid job type: )_create_repo_metadatar   cleanupupdate_create_artifact_metadatar   termwarnrU   )
r   r*   r   r   r   r   r+   repo_metadataartifact_metadatar-   r%   r%   r&   rZ      sJ   	


rZ   c                 C   sH   |  d}t|dkr |d dr"td|  d|  d d S d S d S )N    r   z.pyzEntrypoint zE is a python file without an executable, you may want to use `python z` as the entrypoint instead.)splitlenendswithr   r}   )r   entrypoint_listr%   r%   r&    _maybe_warn_python_no_executable&  s   
r   c                 C   s  |rd|v rt d d S t| t| st d d S t| |}|s*t d d S || |j}|sA|js>t d d S |j}tj	||jpId}|}|stj
tj	|drzttj	|d}	|	  }W d    n1 stw   Y  n4tj
tj	|drttj	|d}	|	   d	 }W d    n1 sw   Y  nt \}}
t|}||jd
|d|dd}|S )Nz..z1Entrypoint cannot contain backward path traversalzPath must be a git URIzCould not parse git URIzCould not find git commit hashr)   zruntime.txtz.python-versionr   )commitremoter   F)rt   r   rw   notebook)r   rU   r   r   r
   fetchcommit_hashosr   joinexistsopenreadstrip
splitlinesr   _clean_python_versionurlr   )r   r*   r   r   r   refr   	local_dirpython_versionfrq   r+   r%   r%   r&   ry   .  sR   







ry   c                 C   s   t j| std i g fS t| |d}t|}g }t j| d}t j	|rEt
|}|  }W d    n1 s@w   Y  tdd |D sStd |rZt|}n	t \}}	t|}|||d}
|
|fS )Nz&Path must be a valid file or directoryr   requirements.txtc                 S   s   g | ]}d |v qS )r   r%   )r?   rr%   r%   r&   rB   |  s    z-_create_artifact_metadata.<locals>.<listcomp>z)wandb is not present in requirements.txt.)rw   codePathr   )r   r   isdirr   rU   r   r   r   r   r   r   r   r   anyr}   r   r   )r   r   r   r   entrypoint_filer-   depspathr   r   rq   r+   r%   r%   r&   r|   i  s.   





r|   tmpdirr3   c                 C   sV   |dkrd}|dkrd}t j| |d}t|dd}d|_d|_|i  |i  |S )	z3Configure job builder with temp dir and job source.rt   repor4   ro   )x_files_dirr3   T)r2   verboseF)r   Settingsr	   r9   _is_notebook_run
set_configset_summary)r   r3   r2   r5   r%   r%   r&   r^     s   

r^   r5   r6   zwandb.sdk.wandb_run.Runc                    s<  | d}|d }	ttj||	| tj ddd}
z|
| W n& tyH } ztj	|r5t
d t
d|  W Y d}~dS d}~ww tD ]}z|
| W qK ty]   Y qKw | jd |
j|
j|
j|||jd||	d	d
 fdddD d\}}||
 |
  |||
 |s|
jdd dd }|S )z_Helper for creating and logging code artifacts.

    Returns the name of the eventual job.
    r   r4   zCode artifact for job)r   typer   zfSymlinks are not supported for code artifact jobs, please copy the code into a directory and try againzError adding to code artifact: N)r   r   Tc                    r:   r;   r%   r>   artifact_namer%   r&   rB     s    
z'_make_code_artifact.<locals>.<listcomp>)r7   rC   r8   :r   )r   _make_code_artifact_namer   r   r   r   r   add_dirr[   islinkrU   CODE_ARTIFACT_EXCLUDE_PATHSremoveFileNotFoundErrorre   rF   rf   rg   rh   rj   rk   _handle_server_artifactr   replace)r!   r5   r6   r   r   r   r   r   r   r   code_artifactrn   itemrp   rq   r%   r   r&   ra     s^   
	


ra   c                 C   s\   |rd| S |  dd}|d dkr|dd }|d dkr%|dd }dt| }|S )	z=Make a code artifact name from a path and user provided name.zcode-z./r)   r   rS   r   Nr   )r   r   )r   r   
clean_path	path_namer%   r%   r&   r     s   
r   r,   r+   r-   c                 C   s   t |  ttj| dd}t|| W d   n1 s w   Y  |p(g }ttj| dd}|d| W d   dS 1 sGw   Y  dS )zDump manufactured metadata and requirements.txt.

    File used by the job_builder to create a job from provided metadata.
    zwandb-metadata.jsonwNr   
)	r   mkdir_exists_okr   r   r   r   jsondumpwrite)r,   r+   r-   r   r%   r%   r&   r\     s   
"r\   r   c                 C   s:   |  ddkrd| dd d } td|   | S )N.r      z$micro python version stripped. Now: )countr   r   rc   rd   )r   r%   r%   r&   r     s   r   )
NNNNNNNNNN)NNNNNNNNNNNru   )N)0r   loggingr   rV   sysrX   typingr   r   r   r   r   r   wandb.apis.internalr   wandb.sdk.artifacts.artifactr   wandb.sdk.internal.job_builderr	   wandb.sdk.launch.git_referencer
   wandb.sdk.launch.utilsr   r   r   wandb.sdk.libr   
wandb.utilr   basicConfigstdoutINFO	getLoggerrc   r   strr'   r    rY   rZ   r   ry   r|   r^   ra   r   r\   r   r%   r%   r%   r&   <module>   sT   

	

N
	

 
4
<
$	
G


