o
    -wi=*                     @   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Zd dlmZm	Z	m
Z
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mZ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mZ ddl m!Z!m"Z"m#Z#m$Z$m%Z% e&e'Z(dZ)d.ddZ*de
e+ de+fddZ,dedede	e+ef de+fddZ-dede+de+fddZ.dede+dee+e/f fddZ0dede+d e+de+fd!d"Z1ded#e+d$e+de+fd%d&Z2d'e+d(e/de+de+fd)d*Z3d+ede+fd,d-Z4dS )/    N)AnyDictListTuple)docker)Api)builder_from_configenvironment_from_configregistry_from_config)
get_module   )
EntryPointLaunchProject)ExecutionErrorLaunchError)
LOG_PREFIXevent_loop_thread_exec   )ACCELERATOR_SETUP_TEMPLATEENTRYPOINT_TEMPLATEPIP_TEMPLATEPYTHON_SETUP_TEMPLATEUSER_CREATE_TEMPLATEzDockerfile.wandbreturnc                     s&   t tj} | dI dH stddS )z.Verify if Docker is installed on host machine.r   NzCould not find Docker executable. Ensure Docker is installed as per the instructions at https://docs.docker.com/install/overview/.)r   shutilwhichr   )	find_exec r   [/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/wandb/sdk/launch/builder/build.pyvalidate_docker_installation&   s   
r   split_commandc                 C   s   d dd | D S )zReturn a shell-escaped string from *split_command*.

    Also remove quotes from double quoted strings. Ex:
    "'local container queue'" --> "local container queue"
     c                 s   s"    | ]}t |d dV  qdS )' N)shlexquotereplace).0argr   r   r   	<genexpr>7   s     zjoin.<locals>.<genexpr>)join)r    r   r   r   r*   1   s   r*   launch_projectapilaunch_configc                    s8  | j sJ d|pi }|di }t|ts't|j}td| d| dt|}|di }t|tsFt|j}td| d| dt||}|di }t|tsft|j}td	| d| dt	|||}	|	srtd
| 
  |  p|| j}
|
dusJ tt d |	| |
I dH }|std|S )aQ  Construct a docker image from a project and returns the URI of the image.

    Arguments:
        launch_project: The project to build an image from.
        api: The API object to use for fetching the project.
        launch_config: The launch config to use for building the image.

    Returns:
        The URI of the built image.
    z-To build an image on queue a URI must be set.environmentzInvalid environment config: z	 of type z* loaded from launch config. Expected dict.registryzInvalid registry config: builderzInvalid builder config: z(Unable to build image. No builder found.Nz%Building docker image from uri sourcezError building image uri)uriget
isinstancedicttype__name__r   r	   r
   r   fetch_and_validate_projectget_job_entry_pointoverride_entrypointwandbtermlogr   build_image)r+   r,   r-   
env_config
wrong_typer.   registry_configr/   builder_configr0   entry_point	image_urir   r   r   build_image_from_project:   sF   






rC   dockerfile_contentsc                 C   s0   |   }|| }t|d dd }|S )z<Hashes the source and dockerfile contents into a unique tag.zutf-8N   )get_image_source_stringhashlibsha256encode	hexdigest)r+   rD   image_source_stringunique_id_string	image_tagr   r   r   $image_tag_from_dockerfile_and_sourcew   s   rN   runner_typec                 C   s<   dd l }| }|dkr| js|dfS | jpt }||fS )Nr   	sagemaker)getpassgetuserdocker_imagedocker_user_idosgeteuid)r+   rO   rQ   usernameuseridr   r   r   get_docker_user   s   rY   
py_versionpy_majorc                 C   s   t |dd }|dk rd| d}nd| d}| jr@td| j  d| d	| d
dg}tj| jd||d}|S ddg}tj|d}|S )zFill in the Dockerfile templates for stage 2 of build.

    CPU version is built on python, Accelerator version is built on user provided.
    .r      zpython:z-busterz	-bookwormzUsing accelerator base image: python	libpythonzpython3-pipzpython3-setuptoolsz \
)accelerator_base_imagepython_packagesrZ   zpython3-devgcc)py_base_image)	intsplitr`   _loggerinfor   formatr*   r   )r+   rZ   r[   minorpython_base_imagera   
base_setupr   r   r   get_base_setup   s0   
rl   build_context_dirbuilder_typec                 C   s  |dkrt  }|std d}n|dkrd}d}|rd}g }d }t|}|d d  rn|d	g7 }d
}t|d d }| }	W d    n1 sMw   Y  t	dd |	D sctt
 d tj|d||dS |d d  rtd}
|
d u rtd nst|d d d}|
|}W d    n1 sw   Y  dd |di dg D }|rt	dd |D stt
 d t|d d d}|d| W d    n1 sw   Y  |d	g7 }d
}tj|d||dS |sS|d d  rS|ddg7 }|  d }|std|  t|d d }| }	W d    n	1 s1w   Y  t	d d |	D sHtt
 d! tj|d||dS d"}td# |S )$Nr   zmDocker BuildX is not installed, for faster builds upgrade docker: https://github.com/docker/buildx#installingzRUN WANDB_DISABLE_CACHE=truekanikoFz8RUN --mount=type=cache,mode=0777,target=/root/.cache/pipsrczrequirements.txtzsrc/requirements.txtz4pip install uv && uv pip install -r requirements.txtc                 S      g | ]}d |v qS r:   r   r'   rr   r   r   
<listcomp>       z,get_requirements_section.<locals>.<listcomp>z)wandb is not present in requirements.txt.r!   )buildx_optional_prefixrequirements_filespip_installzpyproject.tomltomlizpyproject.toml found but tomli could not be loaded. To install dependencies from pyproject.toml please run `pip install tomli` and try again.rbc                 S   s   g | ]}t |qS r   )strr'   dr   r   r   ru      s    projectdependenciesc                 S   rq   rr   r   r}   r   r   r   ru      rv   z7wandb is not present as a dependency in pyproject.toml.w
zrequirements.frozen.txtzsrc/requirements.frozen.txtz_wandb_bootstrap.pyzpython _wandb_bootstrap.pyz No dependency sources found for c                 S   rq   rr   r   rs   r   r   r   ru   
  rv   z0wandb is not present in requirements.frozen.txt.zRUN mkdir -p env/z:No requirements file found. No packages will be installed.)r   is_buildx_installedr:   termwarnpathlibPathexistsopen	readlinesanyr   r   rh   r*   r   loadr2   writeparse_existing_requirementsr   )r+   rm   rn   buildx_installedprefixrx   deps_install_line	base_pathfrequirementsrz   contentsproject_depsrequirements_liner   r   r   get_requirements_section   s   






r   rW   rX   c                 C   s,   |dkrdS t j|| d}|d|  7 }|S )NrP   z	USER root)uiduserz
USER )r   rh   )rW   rX   rO   user_creater   r   r   get_user_setup  s
   r   rA   c                 C   s   t jt| jdS )N)
entrypoint)r   rh   jsondumpscommand)rA   r   r   r   get_entrypoint_setup%  s   r   )r   N)5rG   r   loggingrU   r   r$   r   typingr   r   r   r   r:   	wandb.envr   wandb.apis.internalr   wandb.sdk.launch.loaderr   r	   r
   
wandb.utilr   _project_specr   r   errorsr   r   utilsr   r   templates.dockerfiler   r   r   r   r   	getLoggerr6   rf   _WANDB_DOCKERFILE_NAMEr   r|   r*   rC   rN   rd   rY   rl   r   r   r   r   r   r   r   <module>   s~    

	

=


%
i	