ó
'—^c           @@  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 n  e j e ƒ Z e e Z y d d l Z e e 7Z Wn e k
 r4e j  d	 ƒ n Xy d d l! Z! e e 7Z Wn e k
 roe j  d
 ƒ n Xd „  Z" d „  Z# d „  Z$ d „  Z% e& d „ Z' d „  Z( d d „ Z* d S(   s   Utilities related archives.
i    (   t   absolute_importN(   t   InstallationError(   t   BZ2_EXTENSIONSt   TAR_EXTENSIONSt   XZ_EXTENSIONSt   ZIP_EXTENSIONS(   t
   ensure_dir(   t   MYPY_CHECK_RUNNING(   t   Iterablet   Listt   Optionalt   Textt   Unions   bz2 module is not availables   lzma module is not availablec          C@  s    t  j d ƒ }  t  j |  ƒ |  S(   sB   Get the current umask which involves having to set it temporarily.i    (   t   ost   umask(   t   mask(    (    s:   /tmp/pip-build-eiI0Wo/pip/pip/_internal/utils/unpacking.pyt   current_umask2   s    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(   Nt   /s   \i   t    (   t   lstript   findt   split(   t   path(    (    s:   /tmp/pip-build-eiI0Wo/pip/pip/_internal/utils/unpacking.pyt   split_leading_dir9   s    *c         C@  s\   d } xO |  D]G } t | ƒ \ } } | s/ t S| d k rD | } q | | k r t Sq Wt S(   sy   Returns true if all the paths have the same leading path name
    (i.e., everything is in one subdirectory in an archive)N(   t   NoneR   t   Falset   True(   t   pathst   common_prefixR   t   prefixt   rest(    (    s:   /tmp/pip-build-eiI0Wo/pip/pip/_internal/utils/unpacking.pyt   has_leading_dirI   s    	c         C@  sF   t  j j |  ƒ } t  j j | ƒ } t  j j | | g ƒ } | | k S(   sL   
    Return true if the absolute path of target is within the directory
    (   R   R   t   abspatht   commonprefix(   t	   directoryt   targett   abs_directoryt
   abs_targetR   (    (    s:   /tmp/pip-build-eiI0Wo/pip/pip/_internal/utils/unpacking.pyt   is_within_directoryY   s    c         C@  s½  t  | ƒ t |  d ƒ } z’t j | d t ƒ} t | j ƒ  ƒ oF | } x^| j ƒ  D]P} | j } | } | r„ t	 | ƒ d } n  t
 j j | | ƒ } t
 j j | ƒ }	 t | | ƒ sÞ d }
 t |
 j |  | | ƒ ƒ ‚ n  | j d ƒ sü | j d ƒ r	t  | ƒ qV t  |	 ƒ | j | ƒ } z, t | d ƒ  } t j | | ƒ Wd QXWd | j ƒ  | j d	 ?} | r¥t j | ƒ r¥| d
 @r¥t
 j | d t ƒ  d
 Bƒ n  XqV WWd | j ƒ  Xd S(   sŸ  
    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.
    t   rbt
   allowZip64i   sQ   The zip file ({}) has a file ({}) trying to install outside target directory ({})R   s   \t   wbNi   iI   iÿ  (   R   t   opent   zipfilet   ZipFileR   R   t   namelistt   infolistt   filenameR   R   R   t   joint   dirnameR&   R   t   formatt   endswitht   shutilt   copyfileobjt   closet   external_attrt   statt   S_ISREGt   chmodR   (   R/   t   locationt   flattent   zipfpt   zipt   leadingt   infot   namet   fnt   dirt   messaget   fpt   destfpt   mode(    (    s:   /tmp/pip-build-eiI0Wo/pip/pip/_internal/utils/unpacking.pyt
   unzip_filee   s6    

	

'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	 g  | j
 ƒ  D] } | j ^ qÒ ƒ } xº| j
 ƒ  D]¬} | j } | rt | ƒ d
 } n  t j j | | ƒ } t | | ƒ sgd } t | j |  | | ƒ ƒ ‚ n  | j ƒ  r€t  | ƒ q÷ | j ƒ  rÕy | j | | ƒ Wq£t k
 rÑ}	 t j d |  | j |	 ƒ q÷ q£Xq÷ y | j | ƒ }
 Wn5 t t f k
 r}	 t j d |  | j |	 ƒ q÷ n Xt  t j j | ƒ ƒ t | d ƒ  } t j |
 | ƒ Wd QX|
 j ƒ  | j | | ƒ | j d @r÷ t j  | d t! ƒ  d Bƒ q÷ q÷ WWd | j ƒ  Xd S(   sŸ  
    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.
    s   .gzs   .tgzs   r:gzs   r:bz2s   r:xzs   .tart   rs-   Cannot determine compression type for file %ss   r:*i   sQ   The tar file ({}) has a file ({}) trying to install outside target directory ({})s/   In the tar file %s the member %s is invalid: %sR)   NiI   iÿ  ("   R   t   lowerR3   R   R   t   loggert   warningt   tarfileR*   R   t
   getmembersRA   R   R   R   R0   R&   R   R2   t   isdirt   issymt   _extract_membert	   Exceptiont   extractfilet   KeyErrort   AttributeErrorR1   R4   R5   R6   t   utimeRG   R:   R   (   R/   R;   RG   t   tart   memberR?   RB   R   RD   t   excRE   RF   (    (    s:   /tmp/pip-build-eiI0Wo/pip/pip/_internal/utils/unpacking.pyt
   untar_file™   sd    

*				
%	

&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(   Ns   application/zipR<   s   .whls   application/x-gzipsZ   Cannot unpack file %s (downloaded from %s, content-type: %s); cannot detect archive formats%   Cannot determine archive format of {}(   R   R   t   realpathRJ   R3   R   R+   t
   is_zipfileRH   RM   t
   is_tarfileR   R   R   RZ   RK   t   criticalR   R2   (   R/   R;   t   content_type(    (    s:   /tmp/pip-build-eiI0Wo/pip/pip/_internal/utils/unpacking.pyt   unpack_fileí   s$    (+   t   __doc__t
   __future__R    t   loggingR   R4   R8   RM   R+   t   pip._internal.exceptionsR   t   pip._internal.utils.filetypesR   R   R   R   t   pip._internal.utils.miscR   t   pip._internal.utils.typingR   t   typingR   R	   R
   R   R   t	   getLoggert   __name__RK   t   SUPPORTED_EXTENSIONSt   bz2t   ImportErrort   debugt   lzmaR   R   R   R&   R   RH   RZ   R   R`   (    (    (    s:   /tmp/pip-build-eiI0Wo/pip/pip/_internal/utils/unpacking.pyt   <module>   s@   "+
				4	W