3
Ug"                 @   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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 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 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-0ht26j5g/pip/_internal/utils/unpacking.pycurrent_umask+   s    

r   )pathr   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)r   r   r   r   split_leading_dir2   s    $r   )pathsr   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   )r   common_prefixr   prefixrestr   r   r   has_leading_dir>   s    
r    )	directorytargetr   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)r!   r"   abs_directory
abs_targetr   r   r   r   is_within_directoryM   s    r'   c             C   s   t j| 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j|o |d@ S )N   r(   )external_attrboolstatS_ISREG)r+   moder   r   r   zip_item_is_executable`   s    
r2   T)filenamelocationflattenr   c             C   s  t | t| d}ztj|dd}t|j o0|}x|j D ]}|j}|}|rZt|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  t|rt| 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)r   openzipfileZipFiler    namelistinfolistr3   r   r   r   joindirnamer'   r   formatendswithshutilcopyfileobjcloser2   r*   )r3   r4   r5   zipfpzipleadingr+   namefndirmessagefpdestfpr   r   r   
unzip_fileg   s4    	




rN   )r3   r4   r   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| |d
d}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
r\ }	 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| |
dk	stt|d}tj|
| W dQ R X |
j  |j|| |j d@ rt!| 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:*zutf-8)encodingc             S   s   g | ]
}|j qS r   )rH   ).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: %sNr8   r(   )"r   lowerrA   r   r	   loggerwarningtarfiler9   r    
getmembersrH   r   r   r   r>   r'   r   r@   isdirissym_extract_member	ExceptionextractfileKeyErrorAttributeErrorr?   AssertionErrorrB   rC   rD   utimer1   r*   )r3   r4   r1   tarrG   rR   rI   r   rK   excrL   rM   r   r   r   
untar_file   sl    	




rd   )r3   r4   content_typer   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| d S )Nzapplication/zipz.whl)r5   zapplication/x-gzipzZCannot unpack file %s (downloaded from %s, content-type: %s); cannot detect archive formatz#Cannot determine archive format of )r   r   realpathrT   rA   r
   r:   
is_zipfilerN   rW   
is_tarfiler   r   r	   rd   rU   criticalr   )r3   r4   re   r   r   r   unpack_file   s    

rj   )T)N))__doc__loggingr   rB   r/   rW   r:   typingr   r   r   r   pip._internal.exceptionsr   pip._internal.utils.filetypesr   r   r	   r
   pip._internal.utils.miscr   	getLogger__name__rU   SUPPORTED_EXTENSIONSbz2ImportErrordebuglzmaintr   strr   r.   r    r'   r*   r2   rN   rd   rj   r   r   r   r   <module>   sB   
.T