
    2.a                         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mZ er ej                  d      n ej                  d      Z	ddZ
dd	Zd
 Z	 	 ddZd Zd Zd ZddZ G d d      Z G d d      Zy)    N   )fnmatch   )IS_WINDOWS_PLATFORMz/|\\/c           
          t         j                  j                  |       }|xs g }|xs d}g }|d   #dj                  |xs dg|d   gz         }d|f|g}t	        t        t        |||d               ||||      S )N)NNr   
z.dockerignorer   )
dockerfile)filesrootfileobjgzipextra_files)ospathabspathjoincreate_archivesortedexclude_paths)r   excluder
   r   r   r   r   dockerignore_contentss           4/usr/lib/python3/dist-packages/docker/utils/build.pytarr      s    77??4 DmG+|JK!}  $		))jm_<!
 34
 ]4Z]KL7;     c                 |    |d}|j                  d|z          t        |      }t        |j                  |             S )a	  
    Given a root directory path and a list of .dockerignore patterns, return
    an iterator of all paths (both regular files and directories) in the root
    directory that do *not* match any of the patterns.

    All paths returned are relative to the root.
    
Dockerfile!)appendPatternMatchersetwalk)r   patternsr
   pms       r   r   r   !   s=     !
OOC*$%		!Brwwt}r   c           	          g }t        j                  |       D ]a  \  }}}||z   D ]S  }t         j                  j                  ||      }|j	                  |j                  | dd      j                  d             U c |S )N r   r   )r   r"   r   r   r   replacelstrip)r   r   dirnamedirnamesfnamesfilenamelongpaths          r   build_file_listr.   2   s}    E%'WWT] !6) 	Hww||GX6HLL  r1-44S9	 Lr   c                    |xs g }|st        j                         }t        j                  |rdnd|      }|t	        |       }|D ch c]  }|d   	 }}|D ]  }||v rt
        j                  j                  | |      }	|j                  |	|      }
|
>|
j                  dk  s|
j                  dkD  rt        |
j                        |
_	        t        r|
j                  dz  dz  |
_        |
j                         r)	 t        |	d	      5 }|j                  |
|       d d d        |j                  |
d         |D ]`  \  }}t        j                   |      }|j#                  d      }t%        |      |_        |j                  |t)        j*                  |             b |j-                          |j/                  d       |S c c}w # 1 sw Y   xY w# t        $ r t        d
|	       w xY w)Nzw:gzwmoder   r   )arcnamel    i  I   rbzCan not read file in context: zutf-8)tempfileNamedTemporaryFiletarfileopenr.   r   r   r   
gettarinfomtimeintr   r2   isfileaddfileOSErrorTarInfoencodelensizeioBytesIOcloseseek)r   r   r   r   r   teextra_namesr   	full_pathifnamecontentsinfocontents_encodeds                   r   r   r   >   s   #K--/D&c7CA}%!,-A1Q4-K- ;GGLLt,	LLDL19  77Q;!''I-!''lAG VVe^e+AF88:)T* $aIIaO$ IIa?B & 6ht$#??73()				$

#345	6 GGILLONU .0$ $ 4YK@ s*   	G=G	GGG	GG*c                    t        j                         }t        j                  d|      }t	        | t
        j                        r t        j                  d      }t        d      t	        | t
        j                        rEt        j                  d      }t        | j                               |_        | j                  d       n|j                  | d      }|j                  ||        |j!                          |j                  d       |S )Nr0   r1   r   zCPlease use io.BytesIO to create in-memory Dockerfiles with Python 3r   )r   r3   )r6   r7   r8   r9   
isinstancerD   StringIOr@   	TypeErrorrE   rB   getvaluerC   rG   r:   r>   rF   )r
   rM   rH   dfinfos       r   mkbuildcontextrX   s   s    ##%A#q)A*bkk*. 4 5 	5	J

	+.*--/0j,GIIfj!GGIFF1IHr   c                 n    t        j                  t        |       D cg c]  }|s|dk7  s| c}S c c}w )N.)resplit_SEP)ppts     r   
split_pathr`      s)    $*?2bR3YB???s   222c                 F    t         rdj                  t        |             S | S )Nr   )r   r   r`   r^   s    r   normalize_slashesrc      s    xx
1&&Hr   c                 :    t        |      }|j                  |       S N)r    r"   )r   r#   defaultr$   s       r   r"   r"      s    		!B774=r   c                       e Zd Zd Zd Zd Zy)r    c                     t        t        d |D cg c]  }t        |       c}            | _        | j                  j	                  t        d             y c c}w )Nc                     | j                   S re   )dirsrb   s    r   <lambda>z)PatternMatcher.__init__.<locals>.<lambda>   s
    aff r   z!.dockerignore)listfilterPatternr#   r   )selfr#   r^   s      r   __init__zPatternMatcher.__init__   sH    V8<awqz<
  	W%567 =s   Ac           
         d}t         j                  j                  |      }t        |      }| j                  D ]  }|j
                  }|j                  |      }|st|dk7  rot        |j                        t        |      k  rN|j                  t         j                  j                  j                  |d t        |j                                     }|s| } |S )NFr&   )r   r   r)   r`   r#   	exclusionmatchrB   rj   sepr   )ro   filepathmatchedparent_pathparent_path_dirspatternnegativers   s           r   matcheszPatternMatcher.matches   s    ggooh/%k2}} 
	'G((HMM(+E[B.w||$,<(==#MM(()9:L3w||;L)MNE &,
	' r   c                 &      fd       S )Nc              3     K   t        j                  |       D ]F  }t         j                  j                  t         j                  j	                  |       |      }|j                  dt         j                  j                  z         r|dd  }	j                  |      }|s| t         j                  j                  |      }t         j                  j                  |      rt         j                  j                  |      r|rLd}	j                  D ]7  }|j                  s|j                  j                  t        |            s5d} n |r7 |      E d {    I y 7 w)NrZ   r   TF)r   listdirr   r   relpath
startswithrt   r{   isdirislinkr#   rr   cleaned_patternrc   )
current_dirrM   fpathrs   curskippatrec_walkr   ro   s
          r   r   z%PatternMatcher.walk.<locals>.rec_walk   s    ZZ, )GGOOK6 ##C"''++$56!!"IEU+Kggll4/ww}}S)RWW^^C-@
  D#}} ""}}$..99 1% 8:#(D!"  #C=((=)< )s   EE&E&E$	E& )ro   r   r   s   ``@r   r"   zPatternMatcher.walk   s    	)B ~r   N)__name__
__module____qualname__rp   r{   r"   r   r   r   r    r       s    8&"r   r    c                   (    e Zd Zd Zed        Zd Zy)rn   c                     d| _         |j                  d      rd| _         |dd  }| j                  |      | _        dj	                  | j                        | _        y )NFr   Tr   r   )rr   r   	normalizerj   r   r   )ro   pattern_strs     r   rp   zPattern.__init__   sN    !!#&!DN%ab/KNN;/	"xx		2r   c                     d}t        |      }|t        |      k  r0||   dk(  r||= |dkD  r||dz
  = |dz  }n|dz  }|t        |      k  r0|S )Nr   z..r   )r`   rB   )clsr^   rL   r\   s       r   r   zPattern.normalize   sh     1#e*nQx4!Hq5a!eFAQ #e*n r   c                 @    t        t        |      | j                        S re   )r   rc   r   )ro   ru   s     r   rs   zPattern.match   s    (2D4H4HIIr   N)r   r   r   rp   classmethodr   rs   r   r   r   rn   rn      s!    3  .Jr   rn   )NNNFre   )NNFN)T)rD   r   r[   r8   r6   r   	constantsr   compiler]   r   r   r.   r   rX   r`   rc   r"   r    rn   r   r   r   <module>r      s    	 	 	    +  3zrzz(


3&"	 9>#2j&@< <~#J #Jr   