<s> """<STR_LIT>""" <EOL> import os <EOL> from django . core . files import locks <EOL> try : <EOL> from shutil import copystat <EOL> except ImportError : <EOL> import stat <EOL> def copystat ( src , dst ) : <EOL> """<STR_LIT>""" <EOL> st = os . stat ( src ) <EOL> mode = stat . S_IMODE ( st . st_mode ) <EOL> if hasattr ( os , '<STR_LIT>' ) : <EOL> os . utime ( dst , ( st . st_atime , st . st_mtime ) ) <EOL> if hasattr ( os , '<STR_LIT>' ) : <EOL> os . chmod ( dst , mode ) <EOL> __all__ = [ '<STR_LIT>' ] <EOL> def _samefile ( src , dst ) : <EOL> if hasattr ( os . path , '<STR_LIT>' ) : <EOL> try : <EOL> return os . path . samefile ( src , dst ) <EOL> except OSError : <EOL> return False <EOL> return ( os . path . normcase ( os . path . abspath ( src ) ) == <EOL> os . path . normcase ( os . path . abspath ( dst ) ) ) <EOL> def file_move_safe ( old_file_name , new_file_name , chunk_size = <NUM_LIT> * <NUM_LIT:64> , allow_overwrite = False ) : <EOL> """<STR_LIT>""" <EOL> if _samefile ( old_file_name , new_file_name ) : <EOL> return <EOL> try : <EOL> if not allow_overwrite and os . access ( new_file_name , os . F_OK ) : <EOL> raise IOError ( "<STR_LIT>" % new_file_name ) <EOL> os . rename ( old_file_name , new_file_name ) <EOL> return <EOL> except OSError : <EOL> pass <EOL> with open ( old_file_name , '<STR_LIT:rb>' ) as old_file : <EOL> fd = os . open ( new_file_name , os . O_WRONLY | os . O_CREAT | getattr ( os , '<STR_LIT>' , <NUM_LIT:0> ) | <EOL> ( os . O_EXCL if not allow_overwrite else <NUM_LIT:0> ) ) <EOL> try : <EOL> locks . lock ( fd , locks . LOCK_EX ) <EOL> current_chunk = None <EOL> while current_chunk != b'<STR_LIT>' : <EOL> current_chunk = old_file . read ( chunk_size ) <EOL> os . write ( fd , current_chunk ) <EOL> finally : <EOL> locks . unlock ( fd ) <EOL> os . close ( fd ) <EOL> copystat ( old_file_name , new_file_name ) <EOL> try : <EOL> os . remove ( old_file_name ) <EOL> except OSError as e : <EOL> if getattr ( e , '<STR_LIT>' , <NUM_LIT:0> ) != <NUM_LIT:32> and getattr ( e , '<STR_LIT>' , <NUM_LIT:0> ) != <NUM_LIT> : <EOL> raise </s>
