o
    xiD                     @  s  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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 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"										dMdNd d!Z#													dOdPd*d+Z$dQd/d0Z%dRd1d2Z&		dSdTd3d4Z'	dUdVd5d6Z(dWd:d;Z)dXd?d@Z*dYdAdBZ+dZdHdIZ,d[dKdLZ-dS )\    )annotationsN)Any)Api)Artifact)
JobBuilder)GitReference)_validate_schema)_is_git_uriget_current_python_versionget_entrypoint_file)
filesystem)make_artifact_name_safe)streamlevelwandbz.gitpathstrjob_typeentity
str | Noneprojectnamedescriptionaliaseslist[str] | Noneruntime
entrypointgit_hashbuild_context
dockerfilereturnArtifact | Nonec                 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'   O/home/ubuntu/.local/lib/python3.10/site-packages/wandb/sdk/launch/create_job.py
create_job    s"   5
r)   r#   r   
base_imageservicesdict[str, str] | Noneschemadict[str, Any] | None&tuple[Artifact | None, str, list[str]]c                   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_|pi |_|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 }dd
i}|r
t| dd|ii}| jd |j|j|j|||j||d
 fdd|D d\}}d}|di ds7d}n
|d d!krAd"}|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_partialinput_schemasz@wandb.configjobc                      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   r2   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	_services_make_code_artifactbuildr#   _loggerdebugr&   r   create_artifactrO   
_client_id_sequence_client_ididgetlog_artifactwaitfinishr   r>   delete)r#   r   r   r   r   r   r   r   r   r   r   r   r   r*   r+   r-   r1   r2   r4   er>   r=   ri   artifactres_action_runr'   rJ   r(   r"   j   s   












 
r"   r1   tempfile.TemporaryDirectory.tuple[dict[str, Any] | None, list[str] | None]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   r1   r   r   r   NNr<   )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 argumentr0   )pythondockerzInvalid job type: )_create_repo_metadatar   cleanupupdate_create_artifact_metadatar   termwarnr^   )
r   r1   r   r   r   r   r2   repo_metadataartifact_metadatar4   r'   r'   r(   rc      sJ   	


rc   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_executable4  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 hashr0   zruntime.txtz.python-versionr   )commitremoter   F)r   r   r   notebook)r   r^   r   r	   r   fetchcommit_hashosr   joinexistsopenreadstrip
splitlinesr
   _clean_python_versionurlr   )r   r1   r   r   r   refr   	local_dirpython_versionfr{   r2   r'   r'   r(   r   <  sR   







r   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'   )rH   rr'   r'   r(   rK     s    z-_create_artifact_metadata.<locals>.<listcomp>z)wandb is not present in requirements.txt.)r   codePathr   )r   r   isdirr   r^   r   r   r   r   r   r   r   r   anyr   r   r
   )r   r   r   r   entrypoint_filer4   depspathr   r   r{   r2   r'   r'   r(   r   w  s.   





r   tmpdirr;   r   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.r   repor<   ry   r:   T)r9   verbose	files_dirF)r   Settingsr   r@   _is_notebook_run
set_configset_summary)r   r;   r9   r=   r'   r'   r(   rg     s   

rg   r=   r>   	wandb.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   r<   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                   rC   rD   r'   rG   artifact_namer'   r(   rK     s    
z'_make_code_artifact.<locals>.<listcomp>)r?   rL   rB   :r   )r   _make_code_artifact_namer   r   r   r   r   add_dirrd   islinkr^   CODE_ARTIFACT_EXCLUDE_PATHSremoveFileNotFoundErrorro   rO   rp   rq   rr   rt   ru   _handle_server_artifactr   replace)r#   r=   r>   r   r   r   r   r   r   r   code_artifactrx   itemrz   r{   r'   r   r(   rk     s^   
	


rk   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./r0   r   r\   r   Nr   )r   r   )r   r   
clean_path	path_namer'   r'   r(   r     s   
r   r3   r2   dict[str, Any]r4   Nonec                 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)r3   r2   r4   r   r'   r'   r(   re   
  s   
"re   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   rm   rn   )r   r'   r'   r(   r     s   r   )
NNNNNNNNNN)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!   )NNNNNNNNNNNNN)"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   r+   r,   r-   r.   r    r/   )r   r   r1   r~   r   r   r   r   r   r   r   r   r    r   )r   r   r   )r   r   r1   r   r   r   r   r   r   r   r    r.   )N)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   r   r   r    r   )r   r   r   r   r    r   )r3   r   r2   r   r4   r   r    r   )r   r   r    r   ).
__future__r   r   loggingr   r_   sysra   typingr   r   wandb.apis.internalr   wandb.sdk.artifacts.artifactr   wandb.sdk.internal.job_builderr   wandb.sdk.launch.git_referencer    wandb.sdk.launch.inputs.internalr   wandb.sdk.launch.utilsr	   r
   r   wandb.sdk.libr   
wandb.utilr   basicConfigstdoutINFO	getLoggerrm   r   r)   r"   rc   r   r   r   rg   rk   r   re   r   r'   r'   r'   r(   <module>   st    
N 

4<
$

G
