changeset 4945:37b919adfab4

add filesys.rename_no_overwrite (win32 rename semantics, plus optional removal of src file in any case)
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 15 Aug 2009 22:08:33 +0200
parents 506ce7e1f8e0
children 020f7bdf3282
files MoinMoin/util/filesys.py
diffstat 1 files changed, 34 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/util/filesys.py	Sat Aug 15 20:11:53 2009 +0200
+++ b/MoinMoin/util/filesys.py	Sat Aug 15 22:08:33 2009 +0200
@@ -77,6 +77,40 @@
         # POSIX: just do it :)
         os.rename(oldname, newname)
 
+rename_overwrite = rename
+
+def rename_no_overwrite(oldname, newname, delete_old=False):
+    """ Multiplatform rename
+
+    This kind of rename is doing things differently: it fails if newname
+    already exists. This is the usual thing on win32, but not on posix.
+
+    If delete_old is True, oldname is removed in any case (even if the
+    rename did not succeed).
+    """
+    if os.name == 'nt':
+        try:
+            try:
+                os.rename(oldname, newname)
+                success = True
+            except:
+                success = False
+                raise
+        finally:
+            if not success and delete_old:
+                os.unlink(oldname)
+    else:
+        try:
+            try:
+                os.link(oldname, newname)
+                success = True
+            except:
+                success = False
+                raise
+        finally:
+            if success or delete_old:
+                os.unlink(oldname)
+
 
 def touch(name):
     if sys.platform == 'win32':