3

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rddlmZmZmZmZmZ ejeZee ZyddlZee7 ZW n ek
r   ej d	 Y nX yddl!Z!ee7 ZW n  ek
r   ej 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 jd} t j|  | S )zBGet the current umask which involves having to set it temporarily.r   )osumask)mask r   A/tmp/pip-unpacked-wheel-v59g9nu1/pip/_internal/utils/unpacking.pycurrent_umask2   s    

r   c             C   sh   | j dj d} d| krHd| kr4| jd| jdk s<d| krH| jddS d| kr\| jddS | dgS d S )N/\    )lstripfindsplit)pathr   r   r   split_leading_dir9   s    r   c             C   sD   d}x:| D ]2}t |\}}|s"dS |dkr0|}q
||kr
dS q
W 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j| }t jj|}t j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   sL  t | t| d}z(tj|dd}t|j o2|}x|j D ]}|j}|}|r^t|d }t	j
j||}t	j
j|}	t||sd}
t|
j| |||jds|jdrt | q@t |	 |j|}z&t|d}tj|| W d	Q R X W d	|j  |jd
? }|r4tj|r4|d@ r4t	j|dt  dB  X q@W W d	|j  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   I   i  )r   openzipfileZipFiler"   namelistinfolistfilenamer   r   r   joindirnamer)   r   formatendswithshutilcopyfileobjcloseexternal_attrstatS_ISREGchmodr   )r4   locationflattenzipfpzipleadinginfonamefndirmessagefpdestfpmoder   r   r   
unzip_filee   s6    





 rM   c              C   s$  t | | j jds$| j jdr*d}nL| j jtr>d}n8| j jtrRd}n$| j jdrfd}ntjd|  d	}tj| |}zt	d
d |j
 D }xr|j
 D ]d}|j}|rt|d }tjj||}t||sd}t|j| |||j rt | q|j r\y|j|| W n8 tk
rX }	 ztjd| |j|	 wW Y dd}	~	X nX qy|j|}
W n< ttfk
r }	 ztjd| |j|	 wW Y dd}	~	X nX t tjj| t|d}tj|
| W dQ R X |
j  |j|| |jd@ rtj |dt!  dB  qW W d|j  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   )rF   ).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.   i  )"r   lowerr8   r   r   loggerwarningtarfiler/   r"   
getmembersrF   r   r   r   r5   r)   r   r7   isdirissym_extract_member	ExceptionextractfileKeyErrorAttributeErrorr6   r9   r:   r;   utimerL   r?   r   )r4   r@   rL   tarrD   rP   rG   r   rI   excrJ   rK   r   r   r   
untar_file   sd    





ra   c             C   s   t jj| } |dks,| j jts,tj| rDt| || jd d nR|dkslt	j
| sl| j jtt t rxt| | ntjd| || tdj|d S )Nzapplication/zipz.whl)rA   zapplication/x-gzipzZCannot unpack file %s (downloaded from %s, content-type: %s); cannot detect archive formatz%Cannot determine archive format of {})r   r   realpathrR   r8   r   r0   
is_zipfilerM   rU   
is_tarfiler   r   r   ra   rS   criticalr   r7   )r4   r@   content_typer   r   r   unpack_file   s$    


rg   )T)N))__doc__
__future__r   loggingr   r9   r=   rU   r0   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__rS   SUPPORTED_EXTENSIONSbz2ImportErrordebuglzmar   r   r"   r)   rM   ra   rg   r   r   r   r   <module>   s@   

4W