Browse Source

renames: added optional parameter to restrict overriding

Fabian Peter Hammerle 8 years ago
parent
commit
ef08df0f7c
2 changed files with 26 additions and 11 deletions
  1. 8 5
      osex/__init__.py
  2. 18 6
      tests/test_renames.py

+ 8 - 5
osex/__init__.py

@@ -10,12 +10,15 @@ def append_to_name(source_path, suffix):
         dest_path = dest_path_prefix + str(dest_index)
     os.rename(source_path, dest_path)
 
-def renames(source, destination, backup = False, backup_suffix = "~"):
+def renames(source, destination, override = True, backup = False, backup_suffix = "~"):
     if os.path.exists(destination):
-        if backup:
-            append_to_name(destination, backup_suffix)
-        elif os.path.isdir(destination) and not os.path.islink(destination):
-            raise OSError("may not override directory '%s'" % destination)
+        if override:
+            if backup:
+                append_to_name(destination, backup_suffix)
+            elif os.path.isdir(destination) and not os.path.islink(destination):
+                raise OSError("may not override directory '%s'" % destination)
+        else:
+            raise OSError("path '%s' already exists" % destination)
     os.renames(source, destination)
 
 def symlink(source, link_name, relative = False, override = False, backup = True, backup_suffix = "~"):

+ 18 - 6
tests/test_renames.py

@@ -32,13 +32,25 @@ def test_create_destination_dirs(tmpdir):
     assert os.path.isdir('1/2')
     assert os.path.isfile('1/2/b')
 
+def test_file_exists(tmpdir):
+    os.chdir(tmpdir.strpath)
+    open('src', 'w').close()
+    open('dest', 'w').close()
+    assert os.path.isfile('src')
+    assert os.path.isfile('dest')
+    with pytest.raises(OSError):
+        osex.renames('src', 'dest', override = False, backup = True)
+    assert os.path.isfile('src')
+    assert os.path.isfile('dest')
+    assert len(os.listdir(tmpdir.strpath)) == 2
+
 def test_backup_file(tmpdir):
     os.chdir(tmpdir.strpath)
     open('src', 'w').close()
     open('dest', 'w').close()
     assert os.path.isfile('src')
     assert os.path.isfile('dest')
-    osex.renames('src', 'dest', backup = True, backup_suffix = "~")
+    osex.renames('src', 'dest', override = True, backup = True, backup_suffix = "~")
     assert not os.path.isfile('src')
     assert os.path.isfile('dest')
     assert os.path.isfile('dest~')
@@ -50,7 +62,7 @@ def test_backup_directory(tmpdir):
     os.mkdir('dest')
     assert os.path.isfile('src')
     assert os.path.isdir('dest')
-    osex.renames('src', 'dest', backup = True, backup_suffix = "~")
+    osex.renames('src', 'dest', override = True, backup = True, backup_suffix = "~")
     assert not os.path.isfile('src')
     assert os.path.isfile('dest')
     assert os.path.isdir('dest~')
@@ -65,7 +77,7 @@ def test_backup_directory_symlink(tmpdir):
     assert os.path.isdir('dir')
     assert os.path.islink('dest')
     assert os.path.isdir('dest')
-    osex.renames('src', 'dest', backup = True, backup_suffix = "~")
+    osex.renames('src', 'dest', override = True, backup = True, backup_suffix = "~")
     assert not os.path.isfile('src')
     assert os.path.isdir('dir')
     assert os.path.islink('dest~')
@@ -79,7 +91,7 @@ def test_override_file(tmpdir):
     open('dest', 'w').close()
     assert os.path.isfile('src')
     assert os.path.isfile('dest')
-    osex.renames('src', 'dest', backup = False)
+    osex.renames('src', 'dest', override = True, backup = False)
     assert not os.path.isfile('src')
     assert os.path.isfile('dest')
     assert len(os.listdir(tmpdir.strpath)) == 1
@@ -91,7 +103,7 @@ def test_attempt_override_directory(tmpdir):
     assert os.path.isfile('src')
     assert os.path.isdir('dest')
     with pytest.raises(OSError):
-        osex.renames('src', 'dest', backup = False)
+        osex.renames('src', 'dest', override = True, backup = False)
 
 def test_override_directory_symlink(tmpdir):
     os.chdir(tmpdir.strpath)
@@ -102,7 +114,7 @@ def test_override_directory_symlink(tmpdir):
     assert os.path.isdir('dir')
     assert os.path.islink('link')
     assert os.path.isdir('link')
-    osex.renames('src', 'link', backup = False)
+    osex.renames('src', 'link', override = True, backup = False)
     assert not os.path.isfile('src')
     assert os.path.isdir('dir')
     assert not os.path.islink('link')