
    nd	                    r    d 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
 ddlmZmZ d	d
gZddZddZdS )zh
Extract package to directory, with checks against tar members extracting outside
the target directory.
    )annotationsN)ELOOP)Path)	Generator   )
exceptionspackage_streamingextract_streamextractstream>Generator[tuple[tarfile.TarFile, tarfile.TarInfo], None, None]dest_dir
Path | strc                H   t           j                                      fd| D ]w\  }fd}	                      |                       n7# t          $ r*}|j        t          k    rt          j                    | d}~ww xY w| 	                                 xdS )z
    Pipe ``stream_conda_component`` output here to extract every member into
    dest_dir.

    For ``.conda`` will need to be called twice (for info and pkg components);
    for ``.tar.bz2`` every member is extracted.
    c                    t           j                            t           j                            |                     }t           j                            |f          }|k    S )N)ospathrealpathjoin
commonpath)name
abs_targetprefixr   s      ?lib/python3.11/site-packages/conda_package_streaming/extract.pyis_within_dest_dirz*extract_stream.<locals>.is_within_dest_dir    sM    W%%bgll8T&B&BCC
##Xz$:;;!!    c               3  v   K   D ]2}  | j                   st          j        d| j                    | V  3d S )Nzcontains unsafe path: )r   r   SafetyError)memberr   tar_files    r   checked_membersz'extract_stream.<locals>.checked_members'   s]      "  ))&+66 Y$01W&+1W1WXXX r   )r   membersN)
r   r   r   
extractallOSErrorerrnor   r   CaseInsensitiveFileSystemErrorclose)r   r   _r!   er   r    s    `   @@r   r
   r
      s     w))H" " " " "
   !	 	 	 	 	 		X7H7HIIII 	 	 	w% ?AAqH	 	# s   A
B
 %BB
c                   |s
J d            t          |                               d          r#t          j        j        t          j        j        g}nt          j        j        g}d}|st          | d          }d}	 |D ])}t          j        | ||          }t          ||           *	 |r|	                                 dS dS # |r|	                                 w w xY w)z
    Extract all components of conda package to dest_dir.

    fileobj: must be seekable if provided, if a ``.conda`` package.
    zdest_dir is requiredz.condaFrbT)	componentN)
strendswithr	   CondaComponentpkginfoopenstream_conda_componentr
   r'   )filenamer   fileobj
componentsclosefdr,   r   s          r   r   r   9   s    ++++++
8}}h'' <,0,1



 (6:;
G x&&# 	- 	-I&='Y  F 68,,,,		-  	MMOOOOO	 	7 	MMOOOO	s   ;,C C)r   r   r   r   )NN)__doc__
__future__r   r   tarfiler%   r   pathlibr   typingr    r   r	   __all__r
   r    r   r   <module>r@      s    
 # " " " " " 				                    + + + + + + + +Y
'# # # #L     r   