
0c@_%                 @   s  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 j e  Z e e Z y d d l Z e e 7Z Wn e  k
 rBe j! d
  Yn Xy d d l" Z" e e 7Z Wn e  k
 r~e j! d  Yn Xd 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  j d  }  t  j |   |  S)zBGet the current umask which involves having to set it temporarily.r   )osumask)mask r   </tmp/pip-build-jynh7p1z/pip/pip/_internal/utils/unpacking.pycurrent_umask/   s    r   c             C   s   |  j  d  j  d  }  d |  k rj d |  k rN |  j d  |  j d  k  sZ d |  k rj |  j d d  Sd |  k r |  j d d  S|  d g Sd  S)N/\    )lstripfindsplit)pathr   r   r   split_leading_dir7   s    *r   c             C   s\   d } xO |  D]G } t  |  \ } } | s/ d S| d k rD | } q | | k r d Sq Wd 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   sF   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targetZabs_directoryZ
abs_targetr!   r   r   r   is_within_directoryW   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_executablec   s    r+   c             C   s0   |  j  d ?} t | o, t j |  o, | d @ S)N   r)   )external_attrboolstatS_ISREG)infomoder   r   r   zip_item_is_executablel   s    r3   Tc             C   s  t  |  t |  d  } zYt j | d d } t | j    oF | } x%| j   D]} | j } | } | r t |  d } t	 j
 j | |  } t	 j
 j |  }	 t | |  s d }
 t |
 j |  | |    | j d  s | j d  rt  |  qV t  |	  | j |  } z- t | d   } t j | |  Wd	 QRXWd	 | j   t |  rlt |  XqV WWd	 | j   Xd	 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.
    rb
allowZip64Tr   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closer3   r+   )r<   locationflattenzipfpzipleadingr1   namefndirmessagefpdestfpr   r   r   
unzip_filet   s4    

	

rO   c             C   s  t  |  |  j   j d  s4 |  j   j d  r= d } np |  j   j t  r[ d } nR |  j   j t  ry d } n4 |  j   j d  r d } n t j d |   d	 } t j |  |  } zt	 d
 d   | j
   D  } x| j
   D]} | j } | rt |  d } t j j | |  } t | |  sXd } t | j |  | |    | j   rqt  |  q | j   ry | j | |  Wqt k
 r}	 z! t j d |  | j |	  w WYd d }	 ~	 XqXq y | j |  }
 WnG t t f k
 r4}	 z! t j d |  | j |	  w WYd d }	 ~	 Xn Xt  t j j |   |
 d k	 s]t  t | d   } t j |
 |  Wd QRX|
 j   | j | |  | j  d @r t! |  q WWd | j   Xd 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 Sr   )rI   ).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: %sNr6   r)   )"r   lowerr@   r   r   loggerwarningtarfiler7   r#   
getmembersrI   r   r   r   r=   r(   r   r?   isdirissym_extract_member	ExceptionextractfileKeyErrorAttributeErrorr>   AssertionErrorrA   rB   rC   utimer2   r+   )r<   rD   r2   tarrH   rR   rJ   r   rL   excrM   rN   r   r   r   
untar_file   sf    

*				
	
rd   c             C   s   t  j j |   }  | d k sB |  j   j t  sB t j |   rb t |  | d |  j d  ns | d k s t	 j
 |   s |  j   j t t t  r t |  |  n+ t j d |  | |  t d j |    d  S)Nzapplication/ziprE   z.whlzapplication/x-gzipzZCannot unpack file %s (downloaded from %s, content-type: %s); cannot detect archive formatz%Cannot determine archive format of {})r   r   realpathrT   r@   r   r8   
is_zipfilerO   rW   
is_tarfiler   r   r   rd   rU   criticalr   r?   )r<   rD   content_typer   r   r   unpack_file   s$    rj   ),__doc__
__future__r   loggingr   rA   r/   rW   r8   Zpip._internal.exceptionsr   Zpip._internal.utils.filetypesr   r   r   r   Zpip._internal.utils.miscr   Zpip._internal.utils.typingr	   Ztypingr
   r   r   r   r   r   	getLogger__name__rU   SUPPORTED_EXTENSIONSbz2ImportErrordebuglzmar   r   r#   r(   r+   r3   rO   rd   rj   r   r   r   r   <module>   sF   "(
	/V