ó
Ð²Ebc           @@  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 n  e j e ƒ Z e e Z y d d l Z e e 7Z Wn e  k
 rDe j! d
 ƒ n Xy d d l" Z" e e 7Z Wn e  k
 re j! d ƒ n Xd „  Z# d „  Z$ d „  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   Union(   t   ZipInfos   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-UPPWic/pip/pip/_internal/utils/unpacking.pyt   current_umask/   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-UPPWic/pip/pip/_internal/utils/unpacking.pyt   split_leading_dir7   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-UPPWic/pip/pip/_internal/utils/unpacking.pyt   has_leading_dirG   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-UPPWic/pip/pip/_internal/utils/unpacking.pyt   is_within_directoryW   s    c         C@  s    t  j |  d t ƒ  @d Bƒ d S(   sx   
    Make file present at path have execute for user/group/world
    (chmod +x) is no-op on windows per python docs
    iÿ  iI   N(   R   t   chmodR   (   R   (    (    s:   /tmp/pip-build-UPPWic/pip/pip/_internal/utils/unpacking.pyt2   set_extracted_file_to_default_mode_plus_executablec   s    c         C@  s0   |  j  d ?} t | o, t j | ƒ o, | d @ƒ S(   Ni   iI   (   t   external_attrt   boolt   statt   S_ISREG(   t   infot   mode(    (    s:   /tmp/pip-build-UPPWic/pip/pip/_internal/utils/unpacking.pyt   zip_item_is_executablel   s    c         C@  sŒ  t  | ƒ t |  d ƒ } zat j | d t ƒ} t | j ƒ  ƒ oF | } x-| j ƒ  D]} | 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 ƒ  t | ƒ rtt | ƒ 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   wbN(   R   t   opent   zipfilet   ZipFileR   R    t   namelistt   infolistt   filenameR   R   R   t   joint   dirnameR'   R   t   formatt   endswitht   shutilt   copyfileobjt   closeR0   R)   (   R9   t   locationt   flattent   zipfpt   zipt   leadingR.   t   namet   fnt   dirt   messaget   fpt   destfp(    (    s:   /tmp/pip-build-UPPWic/pip/pip/_internal/utils/unpacking.pyt
   unzip_filet   s4    

	

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 | ƒ ƒ |
 d k	 sHt ‚ t | d ƒ  } t j |
 | ƒ Wd QX|
 j ƒ  | j  | | ƒ | j! d @r÷ t" | ƒ 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: %sR3   NiI   (#   R   t   lowerR=   R   R   t   loggert   warningt   tarfileR4   R    t
   getmembersRF   R   R   R   R:   R'   R   R<   t   isdirt   issymt   _extract_membert	   Exceptiont   extractfilet   KeyErrort   AttributeErrorR;   R   t   AssertionErrorR>   R?   R@   t   utimeR/   R)   (   R9   RA   R/   t   tart   memberRE   RG   R   RI   t   excRJ   RK   (    (    s:   /tmp/pip-build-UPPWic/pip/pip/_internal/utils/unpacking.pyt
   untar_file£   sf    

*				
%	

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/zipRB   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   realpathRN   R=   R   R5   t
   is_zipfileRL   RQ   t
   is_tarfileR   R   R   R_   RO   t   criticalR   R<   (   R9   RA   t   content_type(    (    s:   /tmp/pip-build-UPPWic/pip/pip/_internal/utils/unpacking.pyt   unpack_fileö   s$    (.   t   __doc__t
   __future__R    t   loggingR   R>   R,   RQ   R5   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   R   t	   getLoggert   __name__RO   t   SUPPORTED_EXTENSIONSt   bz2t   ImportErrort   debugt   lzmaR   R   R    R'   R)   R0   R   RL   R_   R   Re   (    (    (    s:   /tmp/pip-build-UPPWic/pip/pip/_internal/utils/unpacking.pyt   <module>   sF   "(
							/	V