a
    hbòfÊ"  ã                   @   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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yÆ   e d¡ Y n0 zddlZee7 ZW n eyø   e d	¡ Y n0 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d„Z$ee!dœdd„Z%d%eee!ddœdd„Z&e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   úZ/home/httpd/docs/test/DocsMgr/lib/python3.9/site-packages/pip/_internal/utils/unpacking.pyÚcurrent_umask+   s    

r   )Ú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_dir2   s    ÿÿÿr   )Ú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_directoryM   s    r(   c                 C   s   t  | dtƒ  @ dB ¡ dS )zx
    Make file present at path have execute for user/group/world
    (chmod +x) is no-op on windows per python docs
    iÿ  éI   N)r   Úchmodr   r   r   r   r   Ú2set_extracted_file_to_default_mode_plus_executableX   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_executable`   s    
r3   T)ÚfilenameÚlocationÚflattenr   c              	   C   sR  t |ƒ t| dƒ}�z.tj|dd�}t| ¡ ƒo2|}| ¡ D ]ú}|j}|}|rZt|ƒd }t	j
 ||¡}t	j
 |¡}	t||ƒs”d}
t|
 | ||¡ƒ‚| d¡s¨| d¡r²t |ƒ q<t |	ƒ | |¡}zTt|dƒ�}t ||¡ W d	  ƒ n1 sò0    Y  W | ¡  t|ƒ�r6t|ƒ q<| ¡  t|ƒ�r4t|ƒ 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Úinfolistr4   r   r   r   ÚjoinÚdirnamer(   r   ÚformatÚendswithÚshutilÚcopyfileobjÚcloser3   r+   )r4   r5   r6   ÚzipfpÚzipÚleadingr,   ÚnameÚfnÚdirÚmessageÚfpÚdestfpr   r   r   Ú
unzip_fileg   s<    	

ÿ

,

þ
rO   )r4   r5   r   c                 C   sX  t |ƒ |  ¡  d¡s$|  ¡  d¡r*d}nL|  ¡  t¡r>d}n8|  ¡  t¡rRd}n$|  ¡  d¡rfd}nt d| ¡ d	}tj| |d
d�}�zÀt	dd„ | 
¡ D ƒƒ}| 
¡ D �]’}|j}|rÄt|ƒd }tj ||¡}t||ƒsòd}t| | ||¡ƒ‚| ¡ �rt |ƒ q¨| ¡ �rhz| ||¡ W nD t�yd }	 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�sÞJ ‚t|dƒ�}t |
|¡ W d  ƒ n1 �s0    Y  |
 ¡  | ||¡ |jd@ r¨t |ƒ q¨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 after being
    written.  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   )rI   )Ú.0Úmemberr   r   r   Ú
<listcomp>¯   ó    zuntar_file.<locals>.<listcomp>r   zQThe tar file ({}) has a file ({}) trying to install outside target directory ({})z/In the tar file %s the member %s is invalid: %sNr9   r)   )!r   ÚlowerrB   r   r	   ÚloggerÚwarningÚtarfiler:   r!   Ú
getmembersrI   r   r   r   r?   r(   r   rA   ÚisdirÚissymÚ_extract_memberÚ	ExceptionÚextractfileÚKeyErrorÚAttributeErrorr@   rC   rD   rE   Úutimer2   r+   )r4   r5   r2   ÚtarrH   rS   rJ   r   rL   ÚexcrM   rN   r   r   r   Ú
untar_file•   st    	þ
ÿ


ü$ü",
re   )r4   r5   Ú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)r6   zapplication/x-gzipzZCannot unpack file %s (downloaded from %s, content-type: %s); cannot detect archive formatz#Cannot determine archive format of )r   r   ÚrealpathrV   rB   r
   r;   Ú
is_zipfilerO   rY   Ú
is_tarfiler   r   r	   re   rW   Úcriticalr   )r4   r5   rf   r   r   r   Úunpack_fileæ   s,    ÿþýÿþýûrk   )T)N))Ú__doc__Úloggingr   rC   r0   rY   r;   Útypingr   r   r   r   Úpip._internal.exceptionsr   Úpip._internal.utils.filetypesr   r   r	   r
   Úpip._internal.utils.miscr   Ú	getLoggerÚ__name__rW   ÚSUPPORTED_EXTENSIONSÚbz2ÚImportErrorÚdebugÚlzmaÚintr   Ústrr   r/   r!   r(   r+   r3   rO   re   rk   r   r   r   r   Ú<module>   sL   
.T ýü