a
    sh.                     @  s  d Z ddlm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 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y   ed	 Y n0 zddlZee7 ZW n ey   ed
 Y n0 ddddZdddddZdddddZ ddddddZ!ddddZ"ddddd Z#d!dd"d#d$Z$d4ddddd&d'd(Z%dddd)d*d+Z&ddd,ddd-d.d/Z'd5ddd0dd1d2d3Z(dS )6zUtilities related archives.    )annotationsN)Iterable)ZipInfo)InstallationError)BZ2_EXTENSIONSTAR_EXTENSIONSXZ_EXTENSIONSZIP_EXTENSIONS)
ensure_dirzbz2 module is not availablezlzma module is not availableint)returnc                  C  s   t d} t |  | S )zBGet the current umask which involves having to set it temporarily.r   )osumask)mask r   Y/var/www/html/assistant/venv/lib/python3.9/site-packages/pip/_internal/utils/unpacking.pycurrent_umask-   s    

r   strz	list[str])pathr   c                 C  sh   |  d d} d| v rHd| v r4| d| dk s<d| vrH| ddS d| v r\| ddS | dgS d S )N/\    )lstripfindsplitr   r   r   r   split_leading_dir4   s    r   zIterable[str]bool)pathsr   c                 C  sD   d}| D ]6}t |\}}|s" dS |du 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   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%   abs_directory
abs_targetr!   r   r   r   is_within_directoryO   s    r*   c                   C  s   dt   @ dB S )Ni  I   )r   r   r   r   r   !_get_default_mode_plus_executableZ   s    r,   Nonec                 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/   r   )infor   c                 C  s$   | j d? }t|o t|o |d@ S )N   r+   )external_attrr   statS_ISREG)r0   moder   r   r   zip_item_is_executablef   s    
r6   T)filenamelocationflattenr   c              	   C  sH  t | t| d}z$tj|dd}t| o2|}| D ]}|j}|}|rZt|d }t	j
||}t	j
|}	t||sd}
t|
| |||drt | q<t |	 ||}zTt|d}t|| W d   n1 s0    Y  W |  t|r,t| q<|  t|r*t| 0 q<W |  n
|  0 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zipfileZipFiler#   namelistinfolistr7   r   r   r   joindirnamer*   r   formatendswithshutilcopyfileobjcloser6   r/   )r7   r8   r9   zipfpzipleadingr0   namefndirmessagefpdestfpr   r   r   
unzip_filem   s<    	




,


rR   )r7   r8   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y   t|| Y n~0 t |r|
 D ]<}t|j\}}||_| rt|j\}}	||kr|	|_qdddd fdd}
|j|
d W |  n
|  0 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   )rL   ).0memberr   r   r   
<listcomp>       zuntar_file.<locals>.<listcomp>ztarfile.TarInfor   )rV   r   r   c              
     s   | j }zJz | } W n6 tjyL   tjd d dv rFt| } n Y n0 W n: tjy } z d}t||W Y d }~n
d }~0 0 | 	 r|d@ r| _ nd | _ | S )N   >   )rY   	      )rY         )rY   
      z%Invalid member in the tar file {}: {}r+   )
r5   tarfileZLinkOutsideDestinationErrorsysversion_infoZ
tar_filterTarErrorr   rD   isfile)rV   r   Z	orig_modeexcrO   data_filterZdefault_mode_plus_executabler7   r8   r   r   
pip_filter   s(    
zuntar_file.<locals>.pip_filter)filterN)r
   lowerrE   r   r   loggerwarningr`   r=   r#   
getmembersrg   AttributeError_untar_without_filterr,   r   rL   islnklinkname
extractallrH   )r7   r8   r5   tarrK   rV   Z	name_leadZ	name_restZlnk_leadZlnk_restrh   r   rf   r   
untar_file   sB    	
'rt   ztarfile.TarFile)r7   r8   rs   rK   r   c                 C  s  |  D ]}|j}|r$t|d }tj||}t||sRd}t|| |||	 rdt
| q| rz||| W nB ty } z*td| |j| W Y d}~qW Y d}~n
d}~0 0 qz||}	W nH ttfy } z*td| |j| W Y d}~qW Y d}~n
d}~0 0 t
tj| |	dus8J t|d}
t|	|
 W d   n1 sf0    Y  |	  ||| |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: %sNr<   r+   )rm   rL   r   r   r   rB   r*   r   rD   isdirr
   issym_extract_member	Exceptionrk   rl   extractfileKeyErrorrn   rC   r=   rF   rG   rH   utimer5   r/   )r7   r8   rs   rK   rV   rM   r   rO   re   rP   rQ   r   r   r   ro      sP    

$",
ro   z
str | None)r7   r8   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)r9   zapplication/x-gzipzZCannot unpack file %s (downloaded from %s, content-type: %s); cannot detect archive formatz#Cannot determine archive format of )r   r   realpathrj   rE   r	   r>   
is_zipfilerR   r`   
is_tarfiler   r   r   rt   rk   criticalr   )r7   r8   r|   r   r   r   unpack_file5  s,    r   )T)N))__doc__
__future__r   loggingr   rF   r3   ra   r`   r>   collections.abcr   r   pip._internal.exceptionsr   pip._internal.utils.filetypesr   r   r   r	   pip._internal.utils.miscr
   	getLogger__name__rk   SUPPORTED_EXTENSIONSbz2ImportErrordebuglzmar   r   r#   r*   r,   r/   r6   rR   rt   ro   r   r   r   r   r   <module>   sJ   
.`= 