o
    ni%                     @   s   d dl Z d dlZd dl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mZmZmZ dd	lmZ e eZG d
d dZdS )    N)Tuple)LaunchProject)$image_tag_from_dockerfile_and_source)LaunchError)get_current_python_version   )_WANDB_DOCKERFILE_NAMEget_base_setupget_docker_userget_entrypoint_setupget_requirements_sectionget_user_setup)DOCKERFILE_TEMPLATEc                   @   sJ   e Zd ZdZdefddZdedefddZdedeeef fd	d
Z	dS )BuildContextManagerah  Creates a build context for a container image from job source code.

    The dockerfile and build context may be specified by the job itself. If not,
    the behavior for creating the build context is as follows:

    - If a Dockerfile.wandb is found adjacent to the entrypoint, the directory
        containing the entrypoint is used as the build context and Dockerfile.wandb
        is used as the Dockerfile.

    - If `override_dockerfile` is set on the LaunchProject, the directory
        containing the Dockerfile is used as the build context and the Dockerfile
        is used as the Dockerfile. `override_dockerfile` can be set in a launch
        spec via the `-D` flag to `wandb launch` or in the `overrides` section
        of the launch drawer.

    - If no dockerfile is set, a Dockerfile is generated from the job's
        requirements and entrypoint.
    launch_projectc                 C   s$   || _ | j jdusJ t | _dS )znInitialize a BuildContextManager.

        Arguments:
            launch_project: The launch project.
        N)_launch_projectproject_dirtempfilemkdtemp
_directory)selfr    r   f/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/wandb/sdk/launch/builder/context_manager.py__init__-   s   zBuildContextManager.__init__builder_typereturnc              	   C   s   | j }|jp	| }|jr"|jddd }d||d }}nt \}}d| }t|| j|}t	|||}	t
||j\}
}t|
||j}d|
 }|dusSJ t|}tj|||	||||d}|S )zGenerate a Dockerfile for the container image.

        Arguments:
            builder_type: The type of builder to use. One of "docker" or "kaniko".

        Returns:
            The contents of the Dockerfile.
        .N   r   zpython:z/home/)py_build_imagerequirements_section
base_setupuid
user_setupworkdirentrypoint_section)r   override_entrypointget_job_entry_pointpython_versionsplitjoinr   r   r   r	   r
   resourcer   r   r   format)r   r   r   entry_pointspl
py_versionpy_majorpython_build_imager   python_base_setupusernameuseridr"   r#   r$   dockerfile_contentsr   r   r   _generate_dockerfile7   s8   	

	z(BuildContextManager._generate_dockerfilec                 C   s~  | j  p| j j}|dusJ |jdusJ | j jdusJ | j j}| j j}|r>tj||}tj	|s<t
d| |}| j j}|rtj||}tj	|sXt
d| tj|| jddtdd t|tj| jt | jt| j t| fS | j j}|rtj||}tj	|st
d| tj|| jddtdd t|tj| jt | jt| j t| fS tj|j}	|	rtj||	t}
ntj|t}
tj	|
r-tjtj|
| jddtdd |	rtj|j}| j  }|dur|| t|
}| }W d   n	1 sw   Y  | jt| j |fS tj| jd}| j jdus>J tj| j j|dtdd ttjtjtd	d
tj| j | j jrtj|d}t|d}|d| j j  W d   n	1 sw   Y  ttj| jtd}| j|d}|| W d   n	1 sw   Y  t| j |}| j|fS )zCreate the build context for the container image.

        Returns:
            A pair of str: the path to the build context locally and the image
            tag computed from the Dockerfile.
        Nz Build context does not exist at zDockerfile does not exist at Tzfsmonitor--daemon.ipc)symlinksdirs_exist_okignoresrc)r9   dstr6   r8   	templatesz_wandb_bootstrap.pyzruntime.txtwzpython-)r   )r   r&   r%   namer   job_build_contextospathr)   existsr   override_dockerfileshutilcopytreer   ignore_patternscopyr   r   openreadjob_dockerfiledirnamebasenameupdate_entrypoint_path__file__r'   writer5   )r   r   
entrypointbuild_context_root_dirr>   	full_pathrB   rI   dockerfile_pathentrypoint_dirr@   new_pathfdocker_file_contentsdst_pathruntime_pathfphandle	image_tagr   r   r   create_build_contexth   s   








z(BuildContextManager.create_build_contextN)
__name__
__module____qualname____doc__r   r   strr5   r   r\   r   r   r   r   r      s
    
1r   )loggingr?   rC   r   typingr   wandb.sdk.launch._project_specr   wandb.sdk.launch.builder.buildr   wandb.sdk.launch.errorsr   wandb.sdk.launch.utilsr   buildr   r	   r
   r   r   r   templates.dockerfiler   	getLoggerr]   _loggerr   r   r   r   r   <module>   s     
