o
    1 i                     @   s   d dl mZ d dlmZmZ d dlmZ zd dlmZ W n e	y+   d dl
mZ Y nw deeef ddfdd	Zd
eeef ddfddZ	dddZdd Zdd Zdd Zdd Zdd ZdS )    )Path)AnyDict)
cli_logger)quotefile_mountsreturnNc                 C   s:   |   D ]\}}t| rtd| d| d qdS )a  Checks if files are passed as file_mounts. This is a problem for Docker
    based clusters because when a file is bind-mounted in Docker, updates to
    the file on the host do not always propagate to the container. Using
    directories is recommended.
    zFile Mount: (:zS) refers to a file.
 To ensure this mount updates properly, please use a directory.N)itemsr   is_filer   warning)r   remotelocal r   j/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/ray/autoscaler/_private/docker.py_check_docker_file_mounts   s   r   configc                 C   s   d| vrdS t | di  | d d}| d d}| d d|}| d d|}|p1|o1|}|s8|s8dS |r<|s@J ddS )	z1Checks whether the Docker configuration is valid.dockerNr   imagecontainer_nameZ
head_imageZworker_imagez%Must provide a container & image name)r   get)r   Zdocker_imagecnameZhead_docker_imageZworker_docker_imageZimage_presentr   r   r   validate_docker_config   s   r   Fc                    s@   sJ dd|rd dd |D  fdd| D S )NzMust provide docker command  c                 S   s   g | ]}d j |dqS )z-e {env}=${env})envformat).0r   r   r   r   
<listcomp>7   s    z$with_docker_exec.<locals>.<listcomp>c              	      s,   g | ]}d j rdnd t|dqS )zE{docker_cmd} exec {interactive} {env} {container} /bin/bash -c {cmd} -itr   )
docker_cmdinteractiver   	containercmd)r   r   )r   r$   r   r!   Zenv_strwith_interactiver   r   r   8   s    
join)Zcmdsr   r!   env_varsr&   r   r%   r   with_docker_exec1   s   r*   c              	   C   s    d |ddd| d | ddgS )Nr   inspectz-fz'{{z}}'z||truer'   )r   templater!   r   r   r   _check_helperD   s   r.   c                 C      t | d|S )Nz.State.Runningr.   r   r!   r   r   r   check_docker_running_cmdJ      r2   c                 C   r/   )Nzjson .Mountsr0   r1   r   r   r   check_bind_mounts_cmdN   r3   r4   c                 C   r/   )Nz.Config.Imager0   r1   r   r   r   check_docker_imageR   r3   r5   c                    s   ddl m} ||  fdd|D }	dfdd|	 D }
ddd	}dd
d | D }d|}|ddd|dd|
||d|dg}d|S )Nr   )get_docker_host_mount_locationc                    s   i | ]
}  d | |qS )/r   )r   dst)docker_mount_prefixr   r   
<dictcomp>d   s    z%docker_start_cmds.<locals>.<dictcomp>r   c              	      s*   g | ]\}}d j ||d d dqS )z-v {src}:{dest}z~/r7   )srcdest)r   replacer   kv)home_directoryr   r   r   g   s    z%docker_start_cmds.<locals>.<listcomp>zC.UTF-8)LC_ALLLANGc                 S   s   g | ]\}}d j ||dqS )z-e {name}={val})namevalr   r>   r   r   r   r   p   s    runz--rmz	--name {}z-dr    z
--net=hostbash)Zray.autoscaler.sdkr6   r(   r
   r   )userr   Z
mount_dictr   Zuser_optionsZcluster_namerA   r!   r6   mountZmount_flagsr)   Z	env_flagsZuser_options_strZ
docker_runr   )r9   rA   r   docker_start_cmdsV   s6   



rJ   )NF)pathlibr   typingr   r   Z"ray.autoscaler._private.cli_loggerr   shlexr   ImportErrorZpipesstrr   r   r*   r.   r2   r4   r5   rJ   r   r   r   r   <module>   s"    
