ó
TR¹Nc           @   s„   d  Z  d d l Z d d l m Z y d d l m Z Wn& e k
 r^ d d l Z d „  Z n Xd g Z d „  Z	 d e
 d
 „ Z d S(   sž   
Move a file in the safest way possible::

    >>> from django.core.files.move import file_move_safe
    >>> file_move_safe("/tmp/old_file", "/tmp/new_file")
iÿÿÿÿN(   t   locks(   t   copystatc         C   su   t  j |  ƒ } t j | j ƒ } t t  d ƒ rO t  j | | j | j f ƒ n  t t  d ƒ rq t  j | | ƒ n  d S(   s?   Copy all stat info (mode bits, atime and mtime) from src to dstt   utimet   chmodN(	   t   ost   statt   S_IMODEt   st_modet   hasattrR   t   st_atimet   st_mtimeR   (   t   srct   dstt   stt   mode(    (    sC   /home/panlixing/Python_Projects/gaeseries/django/core/files/move.pyR      s    t   file_move_safec         C   s{   t  t j d ƒ rA y t j j |  | ƒ SWqA t k
 r= t SXn  t j j t j j |  ƒ ƒ t j j t j j | ƒ ƒ k S(   Nt   samefile(   R   R   t   pathR   t   OSErrort   Falset   normcaset   abspath(   R   R   (    (    sC   /home/panlixing/Python_Projects/gaeseries/django/core/files/move.pyt	   _samefile   s    i   i@   c         C   s{  t  |  | ƒ r d Sy t j |  | ƒ d SWn t k
 r> n Xt |  d ƒ } z± t j | t j t j Bt t d d ƒ B| r‡ t j pŠ d Bƒ } zO t	 j
 | t	 j ƒ d } x/ | d k rÞ | j | ƒ } t j | | ƒ q° WWd t	 j | ƒ t j | ƒ XWd | j ƒ  Xt |  | ƒ y t j |  ƒ WnI t k
 rv} t | d d ƒ d k rwt | d d ƒ d	 k rw‚  qwn Xd S(
   sg  
    Moves a file from one location to another in the safest way possible.

    First, tries ``os.rename``, which is simple but will break across filesystems.
    If that fails, streams manually from one file to another in pure Python.

    If the destination file exists and ``allow_overwrite`` is ``False``, this
    function will throw an ``IOError``.
    Nt   rbt   O_BINARYi    t    t   winerrori    t   errnoi   (   R   R   t   renameR   t   opent   O_WRONLYt   O_CREATt   getattrt   O_EXCLR    t   lockt   LOCK_EXt   Nonet   readt   writet   unlockt   closeR   t   remove(   t   old_file_namet   new_file_namet
   chunk_sizet   allow_overwritet   old_filet   fdt   current_chunkt   e(    (    sC   /home/panlixing/Python_Projects/gaeseries/django/core/files/move.pyR   &   s4    &0i   (   t   __doc__R   t   django.core.filesR    t   shutilR   t   ImportErrorR   t   __all__R   R   R   (    (    (    sC   /home/panlixing/Python_Projects/gaeseries/django/core/files/move.pyt   <module>   s   			