o
    xi**                     @   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 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)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   R/home/ubuntu/.local/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_configr-   entry_point	image_urir   r   r   build_image_from_project:   sF   






r@   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(   rA   image_source_stringunique_id_string	image_tagr   r   r   $image_tag_from_dockerfile_and_sourcew   s   rK   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(   rL   rN   usernameuseridr   r   r   get_docker_user   s   rV   
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_packagesrW   zpython3-devgcc)py_base_image)	intsplitr]   _loggerinfor   formatr'   r   )r(   rW   rX   minorpython_base_imager^   
base_setupr   r   r   get_base_setup   s0   
ri   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 r7   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   rr      s    projectdependenciesc                 S   rn   ro   r   rz   r   r   r   rr      rs   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   rn   ro   r   rp   r   r   r   rr   
  rs   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_installedr7   termwarnpathlibPathexistsopen	readlinesanyr   r   re   r'   r   loadr/   writeparse_existing_requirementsr   )r(   rj   rk   buildx_installedprefixru   deps_install_line	base_pathfrequirementsrw   contentsproject_depsrequirements_liner   r   r   get_requirements_section   s   






r   rT   rU   c                 C   s,   |dkrdS t j|| d}|d|  7 }|S )NrM   z	USER root)uiduserz
USER )r   re   )rT   rU   rL   user_creater   r   r   get_user_setup  s
   r   r>   c                 C   s   t jt| jdS )N)
entrypoint)r   re   jsondumpscommand)r>   r   r   r   get_entrypoint_setup%  s   r   )r   N)5rD   r   loggingrR   r   r!   r   typingr   r7   	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   	getLoggerr3   rc   _WANDB_DOCKERFILE_NAMEr   listry   r'   r1   r@   rK   tuplera   rV   ri   r   r   r   r   r   r   r   <module>   s~    

	

=


%
i	