ó
äq™^c           @   sk  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 Z d  d l m Z d  d l	 m
 Z
 d  d l m Z d  d l m Z d  d l m Z d  d l m Z m Z e rï d  d l m Z m Z d	 e f d
 „  ƒ  YZ n  d „  Z d „  Z d „  Z e d „  ƒ Z e d d d d ƒ Z e rFe d „  ƒ Z n e e j ƒ Z d „  Z d „  Z d S(   iÿÿÿÿN(   t   contextmanager(   t   NamedTemporaryFile(   t   retry(   t   PY2(   t   get_path_uid(   t   MYPY_CHECK_RUNNINGt   cast(   t   BinaryIOt   Iteratort   NamedTemporaryFileResultc           B   s   e  Z e d  „  ƒ Z RS(   c         C   s   d  S(   N(    (   t   self(    (    s;   /tmp/pip-build-aSgKnK/pip/pip/_internal/utils/filesystem.pyt   file   s    (   t   __name__t
   __module__t   propertyR   (    (    (    s;   /tmp/pip-build-aSgKnK/pip/pip/_internal/utils/filesystem.pyR	      s   c         C   sÚ   t  j d k s t t d ƒ r# t St j j |  ƒ s; t ‚ d  } x’ |  | k rÕ t j j	 |  ƒ r¹ t j
 ƒ  d k r£ y t |  ƒ } Wn t k
 r˜ t SX| d k St j |  t j ƒ SqD |  t j j |  ƒ } }  qD Wt S(   Nt   win32t   geteuidi    (   t   syst   platformt   hasattrt   ost   Truet   patht   isabst   AssertionErrort   Nonet   lexistsR   R   t   OSErrort   Falset   accesst   W_OKt   dirname(   R   t   previoust   path_uid(    (    s;   /tmp/pip-build-aSgKnK/pip/pip/_internal/utils/filesystem.pyt   check_path_owner   s    
c         C   sŒ   y t  j |  | ƒ Wnq t t f k
 r‡ xT |  | g D]F } y t | ƒ } Wn t k
 r` q7 X| r7 t  j d | ƒ ‚ q7 q7 W‚  n Xd S(   s“   Wrap shutil.copy2() but map errors copying socket files to
    SpecialFileError as expected.

    See also https://bugs.python.org/issue37700.
    s   `%s` is a socketN(   t   shutilt   copy2R   t   IOErrort	   is_sockett   SpecialFileError(   t   srct   destt   ft   is_socket_file(    (    s;   /tmp/pip-build-aSgKnK/pip/pip/_internal/utils/filesystem.pyt   copy2_fixed9   s    c         C   s   t  j t j |  ƒ j ƒ S(   N(   t   statt   S_ISSOCKR   t   lstatt   st_mode(   R   (    (    s;   /tmp/pip-build-aSgKnK/pip/pip/_internal/utils/filesystem.pyR&   Q   s    c      	   c   s…   t  d t d t j j |  ƒ d t j j |  ƒ d d ƒ F } t d | ƒ } z	 | VWd | j j ƒ  t j	 | j j
 ƒ  ƒ XWd QXd S(   s‰   Given a path to a file, open a temp file next to it securely and ensure
    it is written to disk after the context reaches its end.
    t   deletet   dirt   prefixt   suffixs   .tmpR	   N(   R   R   R   R   R   t   basenameR   R   t   flusht   fsynct   fileno(   R   R*   t   result(    (    s;   /tmp/pip-build-aSgKnK/pip/pip/_internal/utils/filesystem.pyt   adjacent_tmp_fileV   s    		t   stop_max_delayiè  t
   wait_fixediú   c         C   sI   y t  j |  | ƒ Wn. t k
 rD t  j | ƒ t  j |  | ƒ n Xd  S(   N(   R   t   renameR   t   remove(   R(   R)   (    (    s;   /tmp/pip-build-aSgKnK/pip/pip/_internal/utils/filesystem.pyt   replacem   s
    c         C   sm   x> t  j j |  ƒ s@ t  j j |  ƒ } | |  k r7 Pn  | }  q Wt  j d k rc t  j |  t  j ƒ St |  ƒ S(   sg   Check if a directory is writable.

    Uses os.access() on POSIX, tries creating files on Windows.
    t   posix(   R   R   t   isdirR   t   nameR   R   t   _test_writable_dir_win(   R   t   parent(    (    s;   /tmp/pip-build-aSgKnK/pip/pip/_internal/utils/filesystem.pyt   test_writable_dir|   s    
c            sý   d } d ‰  xÞ t  d ƒ D]Ð } | d j ‡  f d †  t  d ƒ Dƒ ƒ } t j j |  | ƒ } y' t j | t j t j Bt j Bƒ } WnD t k
 rÊ } | j	 t	 j
 k r® q n  | j	 t	 j k rÄ t S‚  q Xt j | ƒ t j | ƒ t Sq Wt d ƒ ‚ d  S(   Nt(   accesstest_deleteme_fishfingers_custard_t$   abcdefghijklmnopqrstuvwxyz0123456789i
   t    c         3   s   |  ] } t  j ˆ  ƒ Vq d  S(   N(   t   randomt   choice(   t   .0t   _(   t   alphabet(    s;   /tmp/pip-build-aSgKnK/pip/pip/_internal/utils/filesystem.pys	   <genexpr>–   s    i   s3   Unexpected condition testing for writable directory(   t   ranget   joinR   R   t   opent   O_RDWRt   O_CREATt   O_EXCLR   t   errnot   EEXISTt   EPERMR   t   closet   unlinkR   t   EnvironmentError(   R   R5   t   iRB   R   t   fdt   e(    (   RM   s;   /tmp/pip-build-aSgKnK/pip/pip/_internal/utils/filesystem.pyRC      s$    )'(    RT   R   t   os.pathRI   R#   R-   R   t
   contextlibR    t   tempfileR   t   pip._vendor.retryingR   t   pip._vendor.sixR   t   pip._internal.utils.compatR   t   pip._internal.utils.typingR   R   t   typingR   R   R	   R"   R,   R&   R:   t   _replace_retryR?   RE   RC   (    (    (    s;   /tmp/pip-build-aSgKnK/pip/pip/_internal/utils/filesystem.pyt   <module>   s2   			
	