a
    eÖ0h%  ã                   @   sp  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	m
Z
 ddlmZmZmZmZ ddlmZ ddlmZ er¨ddlmZ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„ Z#dd„ Z$dd„ Z%dd„ Z&dd„ Z'dd„ Z(ddd„Z)dd„ Z*d dd„Z+dS )!zUtilities related archives.
é    )Úabsolute_importN)ÚInstallationError)ÚBZ2_EXTENSIONSÚTAR_EXTENSIONSÚXZ_EXTENSIONSÚZIP_EXTENSIONS)Ú
ensure_dir)ÚMYPY_CHECK_RUNNING)ÚIterableÚListÚOptionalÚTextÚUnion)ÚZipInfozbz2 module is not availablezlzma module is not availablec                  C   s   t  d¡} t  | ¡ | S )zBGet the current umask which involves having to set it temporarily.r   )ÚosÚumask)Úmask© r   úK/var/www/auris/lib/python3.9/site-packages/pip/_internal/utils/unpacking.pyÚcurrent_umask/   s    

r   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©Úpathr   r   r   Úsplit_leading_dir7   s    ÿþþýr   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   )ÚpathsÚcommon_prefixr   ÚprefixÚrestr   r   r   Úhas_leading_dirG   s    r$   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)Ú	directoryÚtargetÚabs_directoryÚ
abs_targetr"   r   r   r   Úis_within_directoryW   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_executablec   s    r.   c                 C   s$   | j d? }t|o t |¡o |d@ ƒS )Né   r,   )Úexternal_attrÚboolÚstatÚS_ISREG)ÚinfoÚmoder   r   r   Úzip_item_is_executablel   s    
r6   Tc              	   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ÚinfolistÚfilenamer   r   r   ÚjoinÚdirnamer+   r   ÚformatÚendswithÚshutilÚcopyfileobjÚcloser6   r.   )r?   ÚlocationÚflattenÚzipfpÚzipÚleadingr4   ÚnameÚfnÚdirÚmessageÚfpÚdestfpr   r   r   Ú
unzip_filet   s<    


ÿ

,

þ
rR   c                 C   sT  t |ƒ |  ¡  d¡s$|  ¡  d¡r*d}nL|  ¡  t¡r>d}n8|  ¡  t¡rRd}n$|  ¡  d¡rfd}nt d| ¡ d	}t | |¡}zÀt	d
d„ | 
¡ D ƒƒ}| 
¡ D ]’}|j}|rÀt|ƒd }tj ||¡}t||ƒsîd}t| | ||¡ƒ‚| ¡ rt |ƒ q¤| ¡ rdz| ||¡ W nD 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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:*c                 S   s   g | ]
}|j ‘qS r   )rL   )Ú.0Úmemberr   r   r   Ú
<listcomp>½   s   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   ÚlowerrC   r   r   ÚloggerÚwarningÚtarfiler:   r$   Ú
getmembersrL   r   r   r   r@   r+   r   rB   ÚisdirÚissymÚ_extract_memberÚ	ExceptionÚextractfileÚKeyErrorÚAttributeErrorrA   rD   rE   rF   Úutimer5   r.   )r?   rG   r5   ÚtarrK   rU   rM   r   rO   ÚexcrP   rQ   r   r   r   Ú
untar_file£   sr    
ÿÿ
ÿÿ


þ$þ",
rf   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)rH   zapplication/x-gzipzZCannot unpack file %s (downloaded from %s, content-type: %s); cannot detect archive formatz%Cannot determine archive format of {})r   r   ÚrealpathrW   rC   r   r;   Ú
is_zipfilerR   rZ   Ú
is_tarfiler   r   r   rf   rX   Úcriticalr   rB   )r?   rG   Úcontent_typer   r   r   Úunpack_fileö   s8    ÿþý
ýÿþ
ÿýýÿrl   )T)N),Ú__doc__Ú
__future__r   Úloggingr   rD   r2   rZ   r;   Úpip._internal.exceptionsr   Úpip._internal.utils.filetypesr   r   r   r   Úpip._internal.utils.miscr   Úpip._internal.utils.typingr	   Útypingr
   r   r   r   r   r   Ú	getLoggerÚ__name__rX   ÚSUPPORTED_EXTENSIONSÚbz2ÚImportErrorÚdebugÚlzmar   r   r$   r+   r.   r6   rR   rf   rl   r   r   r   r   Ú<module>   sH   
	
/V ý