U
    @v¨gÞ$  ã                   @   sX  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 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 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ÚUnionz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   úA/tmp/pip-unpacked-wheel-gw11q0wt/pip/_internal/utils/unpacking.pyÚcurrent_umask2   s    

r   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)Úpathr   r   r   Úsplit_leading_dir9   s    ÿþþýr   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   )ÚpathsÚcommon_prefixr   ÚprefixÚrestr   r   r   Úhas_leading_dirI   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_directoryY   s    r)   Tc                 C   sF  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 |	ƒ | |¡}z&t|dƒ}t ||¡ W 5 Q R X W 5 | ¡  |jd? }|r0t |¡r0|d	@ r0t
 |d
tƒ  d	B ¡ 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   é   éI   éÿ  ÚwbN)r   ÚopenÚcloseÚzipfileÚZipFiler"   ÚnamelistÚinfolistÚfilenamer   r   r   ÚjoinÚdirnamer)   r   ÚformatÚendswithÚexternal_attrÚstatÚS_ISREGÚchmodr   ÚshutilÚcopyfileobj)r6   ÚlocationÚflattenÚzipfpÚzipÚleadingÚinfoÚnameÚfnÚdirÚmessageÚfpÚmodeÚdestfpr   r   r   Ú
unzip_filee   s8    


ÿ


rN   c                 C   s*  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 ]p}|j}|rÀt|ƒd }tj ||¡}t||ƒsîd}t| | ||¡ƒ‚| ¡ rt |ƒ q¤| ¡ r^z| ||¡ W n> tk
rZ }	 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 |¡ƒ t|dƒ}t |
|¡ W 5 Q R X |
 	¡  | ||¡ |jd@ r¤t  |dt!ƒ  dB ¡ q¤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 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   )rG   )Ú.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: %sNr/   r-   r.   )"r   Úlowerr:   r   r   ÚloggerÚwarningÚtarfiler0   r1   r"   Ú
getmembersrG   r   r   r   r7   r)   r   r9   ÚisdirÚissymÚ_extract_memberÚ	ExceptionÚextractfileÚKeyErrorÚAttributeErrorr8   r?   r@   ÚutimerL   r>   r   )r6   rA   rL   ÚtarrE   rQ   rH   r   rJ   ÚexcrK   rM   r   r   r   Ú
untar_file™   sz    
 ÿÿ
ÿÿ


  þ  þ
rb   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)rB   zapplication/x-gzipzZCannot unpack file %s (downloaded from %s, content-type: %s); cannot detect archive formatz%Cannot determine archive format of {})r   r   ÚrealpathrS   r:   r   r2   Ú
is_zipfilerN   rV   Ú
is_tarfiler   r   r   rb   rT   Úcriticalr   r9   )r6   rA   Úcontent_typer   r   r   Úunpack_fileí   s<    ÿþý
ýÿþ
ÿý  ýÿrh   )T)N))Ú__doc__Ú
__future__r   Úloggingr   r?   r<   rV   r2   Úpip._internal.exceptionsr   Úpip._internal.utils.filetypesr   r   r   r   Úpip._internal.utils.miscr   Úpip._internal.utils.typingr	   Útypingr
   r   r   r   r   Ú	getLoggerÚ__name__rT   ÚSUPPORTED_EXTENSIONSÚbz2ÚImportErrorÚdebugÚlzmar   r   r"   r)   rN   rb   rh   r   r   r   r   Ú<module>   sB   

4W ý