U
    cUh.                     @   s  d Z ddlZddlZ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	m
Z
mZ ddlmZ ddlmZ ddlmZmZmZmZ ddlmZ eeZee ZzddlZee7 ZW n ek
r   ed Y nX zddlZee7 ZW n  ek
r   ed	 Y nX ed
ddZe e
e  dddZ!e	e  e"dddZ#e e e"dddZ$ed
ddZ%e ddddZ&ee"dddZ'd*e e e"dddd Z(e e dd!d"d#Z)e e ej*e"dd$d%d&Z+d+e e ee  dd'd(d)Z,dS ),zUtilities related archives.
    N)IterableListOptional)ZipInfo)InstallationError)BZ2_EXTENSIONSTAR_EXTENSIONSXZ_EXTENSIONSZIP_EXTENSIONS)
ensure_dirzbz2 module is not availablezlzma module is not available)returnc                  C   s   t d} t |  | S )zBGet the current umask which involves having to set it temporarily.r   )osumask)mask r   ]/opt/bot/vods_filmes/script/venv/lib/python3.8/site-packages/pip/_internal/utils/unpacking.pycurrent_umask,   s    

r   )pathr   c                 C   sh   |  d d} d| krHd| kr4| d| dk s<d| krH| ddS d| kr\| ddS | dgS d S )N/\    )lstripfindsplitr   r   r   r   split_leading_dir3   s    r   )pathsr   c                 C   sD   d}| D ]6}t |\}}|s" dS |dkr0|}q||kr dS qdS )zyReturns true if all the paths have the same leading path name
    (i.e., everything is in one subdirectory in an archive)NFT)r   )r   Zcommon_prefixr   prefixrestr   r   r   has_leading_dir?   s    r    )	directorytargetr   c                 C   s0   t j| }t j|}t j||g}||kS )zL
    Return true if the absolute path of target is within the directory
    )r   r   abspathcommonprefix)r!   r"   Zabs_directoryZ
abs_targetr   r   r   r   is_within_directoryN   s    r%   c                   C   s   dt   @ dB S )Ni  I   )r   r   r   r   r   !_get_default_mode_plus_executableY   s    r'   c                 C   s   t | t  dS )zx
    Make file present at path have execute for user/group/world
    (chmod +x) is no-op on windows per python docs
    N)r   chmodr'   r   r   r   r   2set_extracted_file_to_default_mode_plus_executable]   s    r)   )infor   c                 C   s$   | j d? }t|o t|o |d@ S )N   r&   )external_attrboolstatS_ISREG)r*   moder   r   r   zip_item_is_executablee   s    
r1   T)filenamelocationflattenr   c              
   C   s  t | t| d}ztj|dd}t| o0|}| D ]}|j}|}|rXt	|d }t
j||}t
j|}	t||sd}
t|
| |||ds|drt | q:t |	 ||}z&t|d}t|| W 5 Q R X W 5 |  t|rt| X q:W 5 |  X d	S )
a  
    Unzip the file (with path `filename`) to the destination `location`.  All
    files are written based on system defaults and umask (i.e. permissions are
    not preserved), except that regular file members with any execute
    permissions (user, group, or world) have "chmod +x" applied after being
    written. Note that for windows, any execute changes using os.chmod are
    no-ops per the python docs.
    rbT)
allowZip64r   zQThe zip file ({}) has a file ({}) trying to install outside target directory ({})r   r   wbN)r   openclosezipfileZipFiler    namelistinfolistr2   r   r   r   joindirnamer%   r   formatendswithr1   r)   shutilcopyfileobj)r2   r3   r4   Zzipfpzipleadingr*   namefndirmessagefpdestfpr   r   r   
unzip_filel   s6    	




rL   )r2   r3   r   c                    s^  t   ds$ dr*d}nL tr>d}n8 trRd}n$ drfd}ntd d	}tj|d
d}zt
dd | D }z
tj W n" tk
r   t|| Y nX t |r| D ]<}t|j\}}||_| rt|j\}}	||kr|	|_qtjttjd fdd}
|j|
d W 5 |	  X dS )a  
    Untar the file (with path `filename`) to the destination `location`.
    All files are written based on system defaults and umask (i.e. permissions
    are not preserved), except that regular file members with any execute
    permissions (user, group, or world) have "chmod +x" applied on top of the
    default.  Note that for windows, any execute changes using os.chmod are
    no-ops per the python docs.
    z.gzz.tgzzr:gzzr:bz2zr:xzz.tarrz-Cannot determine compression type for file %szr:*zutf-8)encodingc                 S   s   g | ]
}|j qS r   )rF   ).0memberr   r   r   
<listcomp>   s     zuntar_file.<locals>.<listcomp>)rP   r   r   c              
      s   | j }zLz | } W n8 tjk
rN   tjd d dkrHt| } n Y nX W n8 tjk
r } zd}t||W 5 d }~X Y nX | 	 r|d@ r| _ nd | _ | S )N   >   rR         rR   	   rU   rR         rR   
      z%Invalid member in the tar file {}: {}r&   )
r0   tarfileZLinkOutsideDestinationErrorsysversion_info
tar_filterTarErrorr   r@   isfile)rP   r   Z	orig_modeexcrI   data_filterZdefault_mode_plus_executabler2   r3   r   r   
pip_filter   s(    zuntar_file.<locals>.pip_filter)filterN)r   lowerrA   r   r	   loggerwarningr^   r8   r9   r    
getmembersrf   AttributeError_untar_without_filterr'   r   rF   islnklinknameTarInfostr
extractall)r2   r3   r0   tarrE   rP   Z	name_leadZ	name_restZlnk_leadZlnk_restrg   r   re   r   
untar_file   sB    	
 (ru   )r2   r3   rt   rE   r   c                 C   sz  |  D ]j}|j}|r$t|d }tj||}t||sRd}t|| |||	 rdt
| q| rz||| W n< tk
r } ztd| |j| W Y qW 5 d}~X Y nX qz||}	W nB ttfk
r } ztd| |j| W Y qW 5 d}~X Y nX t
tj| |	dk	s,tt|d}
t|	|
 W 5 Q R X |	  ||| |jd@ rt| qdS )z/Fallback for Python without tarfile.data_filterr   zQThe tar file ({}) has a file ({}) trying to install outside target directory ({})z/In the tar file %s the member %s is invalid: %sNr7   r&   )rl   rF   r   r   r   r>   r%   r   r@   isdirr   issym_extract_member	Exceptionrj   rk   extractfileKeyErrorrm   r?   AssertionErrorr8   rB   rC   r9   utimer0   r)   )r2   r3   rt   rE   rP   rG   r   rI   rd   rJ   rK   r   r   r   rn      sP    


rn   )r2   r3   content_typer   c                 C   s   t j| } |dks,|  ts,t| rDt| || d d nR|dkslt	
| sl|  tt t rxt| | ntd| || td| d S )Nzapplication/zipz.whl)r4   zapplication/x-gzipzZCannot unpack file %s (downloaded from %s, content-type: %s); cannot detect archive formatz#Cannot determine archive format of )r   r   realpathri   rA   r
   r:   
is_zipfilerL   r^   
is_tarfiler   r   r	   ru   rj   criticalr   )r2   r3   r~   r   r   r   unpack_file5  s,    r   )T)N)-__doc__loggingr   rB   r.   r_   r^   r:   typingr   r   r   r   pip._internal.exceptionsr   pip._internal.utils.filetypesr   r   r	   r
   pip._internal.utils.miscr   	getLogger__name__rj   SUPPORTED_EXTENSIONSbz2ImportErrordebuglzmaintr   rr   r   r-   r    r%   r'   r)   r1   rL   ru   TarFilern   r   r   r   r   r   <module>   s\   
.b= 