| 1 | Index: core/files/move.py
|
|---|
| 2 | ===================================================================
|
|---|
| 3 | --- core/files/move.py (revision 8295)
|
|---|
| 4 | +++ core/files/move.py (working copy)
|
|---|
| 5 | @@ -1,12 +1,13 @@
|
|---|
| 6 | """
|
|---|
| 7 | Move a file in the safest way possible::
|
|---|
| 8 |
|
|---|
| 9 | - >>> from django.core.files.move import file_move_save
|
|---|
| 10 | - >>> file_move_save("/tmp/old_file", "/tmp/new_file")
|
|---|
| 11 | + >>> from django.core.files.move import file_move_safe
|
|---|
| 12 | + >>> file_move_safe("/tmp/old_file", "/tmp/new_file")
|
|---|
| 13 | """
|
|---|
| 14 |
|
|---|
| 15 | import os
|
|---|
| 16 | from django.core.files import locks
|
|---|
| 17 | +from django.core.files.locks import system_type
|
|---|
| 18 |
|
|---|
| 19 | __all__ = ['file_move_safe']
|
|---|
| 20 |
|
|---|
| 21 | @@ -16,6 +17,16 @@
|
|---|
| 22 | except ImportError:
|
|---|
| 23 | file_move = os.rename
|
|---|
| 24 |
|
|---|
| 25 | +if system_type == 'nt' :
|
|---|
| 26 | + def _remove( file_name ) :
|
|---|
| 27 | + from exceptions import WindowsError
|
|---|
| 28 | + try:
|
|---|
| 29 | + return os.remove(file_name)
|
|---|
| 30 | + except:
|
|---|
| 31 | + pass # Since file_name is the name of a NamedTemporaryFile which is still open
|
|---|
| 32 | +else :
|
|---|
| 33 | + _remove = os.remove
|
|---|
| 34 | +
|
|---|
| 35 | def file_move_safe(old_file_name, new_file_name, chunk_size = 1024*64, allow_overwrite=False):
|
|---|
| 36 | """
|
|---|
| 37 | Moves a file from one location to another in the safest way possible.
|
|---|
| 38 | @@ -53,7 +64,8 @@
|
|---|
| 39 | current_chunk = old_file.read(chunk_size)
|
|---|
| 40 | new_file.write(current_chunk)
|
|---|
| 41 |
|
|---|
| 42 | + locks.unlock(new_file)
|
|---|
| 43 | new_file.close()
|
|---|
| 44 | old_file.close()
|
|---|
| 45 | -
|
|---|
| 46 | - os.remove(old_file_name)
|
|---|
| 47 | +
|
|---|
| 48 | + _remove( old_file_name )
|
|---|