o
    ;i                     @   sv   d dl Z d dlZd dlmZ d dlmZmZ ddlmZ dee	 de
e	 fdd	Zdd
edee dee fddZdS )    N)Path)OptionalSequence   )InvalidErrordockerfile_linesreturnc                 C   s  t  }d}tdtj}| D ]w}| }|r|drq|r*|d|d  7 }n|d }|ds||}|r|	d}t
|}|d drQd}qt|d	kr|d
d }|D ]#}	td}
|
|	rstd|	 d|	dkr}|d q_||	 q_d}qt|S )zv
    Extract all COPY command sources from a Dockerfile.
    Combines multiline COPY commands into a single line.
     z^\s*COPY\s+(.+)$# \   r   z--from=r   Nz^\s*--|\$\s*zCOPY command: z: using special flags/arguments/variables are not supported.z./**)setrecompile
IGNORECASEstrip
startswithrstripendswithmatchgroupshlexsplitlenr   addlist)r   copy_source_patternscurrent_commandcopy_patternliner   argspartssourcessourcespecial_pattern r(   M/home/ubuntu/.local/lib/python3.10/site-packages/modal/_utils/docker_utils.pyextract_copy_command_patterns
   s@   






r*   context_directorydockerfile_pathc                    sj    fddd}g }|r"|j  d}||j|  ||j|  | |  tfdd|D dS )a  
    Find dockerignore file relative to current context directory
    and if dockerfile path is provided, check if specific <dockerfile_name>.dockerignore
    file exists in the same directory as <dockerfile_name>
    Finds the most specific dockerignore file that exists.
    c                    s   |   sdS |  sdS dS )NFT)existsis_relative_to)fp)r+   r(   r)   valid_dockerignore_fileL   s
   
z7find_dockerignore_file.<locals>.valid_dockerignore_filez.dockerignorec                 3   s    | ]	} |r|V  qd S Nr(   ).0e)r0   r(   r)   	<genexpr>b   s    z)find_dockerignore_file.<locals>.<genexpr>N)nameappendparentnext)r+   r,   generic_namepossible_locationsspecific_namer(   )r+   r0   r)   find_dockerignore_fileD   s   
r<   r1   )r   r   pathlibr   typingr   r   	exceptionr   strr   r*   r<   r(   r(   r(   r)   <module>   s   $: