
~9Vc           @   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 m Z e Z	 e j
 d k rd   Z d   Z y d d l Z d Z d Z e j j j Z d	   Z e j j j Z e j j j Z e j j j Z e j j j Z e Z	 d
   Z Wn e k
 rn Xd   Z n e j Z e Z	 d S(   s  
    werkzeug.posixemulation
    ~~~~~~~~~~~~~~~~~~~~~~~

    Provides a POSIX emulation for some features that are relevant to
    web applications.  The main purpose is to simplify support for
    systems such as Windows NT that are not 100% POSIX compatible.

    Currently this only implements a :func:`rename` function that
    follows POSIX semantics.  Eg: if the target file already exists it
    will be replaced without asking.

    This module was introduced in 0.6.1 and is not a public interface.
    It might become one in later versions of Werkzeug.

    :copyright: (c) 2014 by the Werkzeug Team, see AUTHORS for more details.
    :license: BSD, see LICENSE for more details.
iNi   (   t
   to_unicodet   ntc         C   s   t  S(   N(   t   False(   t   srct   dst(    (    s9   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/posixemulation.pyt   <lambda>   s    c         C   s   t  S(   N(   R   (   R   R   (    (    s9   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/posixemulation.pyR      s    i   c         C   s   t  |  t j    }  t  | t j    } t |  |  r= t Sd } t } xM | r | d k  r t |  | t t B } | sL t	 j
 d  | d 7} qL qL W| S(   Ni    id   gMbP?i   (   R    t   syst   getfilesystemencodingt   _rename_atomict   TrueR   t   _MoveFileExt   _MOVEFILE_REPLACE_EXISTINGt   _MOVEFILE_WRITE_THROUGHt   timet   sleep(   R   R   t   retryt   rv(    (    s9   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/posixemulation.pyt   _rename(   s    
c         C   s   t  d  d d d d d d  } | d k r. t Szz d } t } xc | r | d k  r t |  | d  d  t t B|  } | r t |  } Pq@ t j d  | d 7} q@ W| SWd  t	 |  Xd  S(   Ni    i  s   Werkzeug renameiid   gMbP?i   (
   t   _CreateTransactiont   NoneR   t   _MoveFileTransactedR   R   t   _CommitTransactionR   R   t   _CloseHandle(   R   R   t   taR   R   (    (    s9   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/posixemulation.pyR   >   s"    c         C   s   t  |  |  r d  Sy t j |  |  Wn t k
 r } | j t j k rQ   n  d | t j d t j	  f } t j | |  t j |  |  y t j
 |  Wq t k
 r q Xn Xd  S(   Ns   %s-%08xi    (   R   t   ost   renamet   OSErrort   errnot   EEXISTt   randomt   randintR   t   maxintt   unlinkt	   Exception(   R   R   t   et   old(    (    s9   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/posixemulation.pyR   U   s    (    t   __doc__R   R   R   R   R   t   _compatR    R   t   can_rename_open_filet   nameR   R   t   ctypesR   R   t   windllt   kernel32t   MoveFileExWR
   t   ktmw32t   CreateTransactionR   t   CommitTransactionR   t   MoveFileTransactedWR   t   CloseHandleR   R	   R!   R   (    (    (    s9   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/posixemulation.pyt   <module>   s6   				