Browse Source

symlink: backup of link is no longer being created if existing target already matches to desired one

Fabian Peter Hammerle 8 years ago
parent
commit
1e0c7b409b
4 changed files with 33 additions and 10 deletions
  1. 13 9
      osex/__init__.py
  2. 1 1
      setup.py
  3. 10 0
      tests/test_symlink.py
  4. 9 0
      tests/test_symlink_script.py

+ 13 - 9
osex/__init__.py

@@ -15,13 +15,17 @@ def symlink(source, link_name, relative = False, override = False, backup = True
         source = os.path.relpath(source, os.path.dirname(link_name))
     # os.path.lexists() returns True if path refers to an existing path and 
     # True for broken symbolic links. 
-    if override and os.path.lexists(link_name):
-        if backup:
-            append_to_name(link_name, backup_suffix)
-        else:
-            if os.path.isdir(link_name):
-                os.rmdir(link_name)
-            else:
-                os.remove(link_name)
-    if not os.path.lexists(link_name) or os.readlink(link_name) != source:
+    if not os.path.lexists(link_name):
         os.symlink(source, link_name)
+    elif not os.path.islink(link_name) or os.readlink(link_name) != source:
+        if override:
+            if backup:
+                append_to_name(link_name, backup_suffix)
+            else:
+                if os.path.isdir(link_name):
+                    os.rmdir(link_name)
+                else:
+                    os.remove(link_name)
+            os.symlink(source, link_name)
+        else:
+            raise OSError("link's path '%s' already exists." % link_name)

+ 1 - 1
setup.py

@@ -5,7 +5,7 @@ import glob
 setup(
     name = 'osex',
     packages = ['osex'],
-    version = '0.2',
+    version = '0.2.1',
     description = 'extension for python\'s build-in operating system interface',
     author = 'Fabian Peter Hammerle',
     author_email = 'fabian.hammerle@gmail.com',

+ 10 - 0
tests/test_symlink.py

@@ -43,6 +43,16 @@ def test_suitable_link_exists(tmpdir):
     assert os.path.lexists("link")
     osex.symlink("source", "link")
     osex.symlink("source", "link", override = False)
+    assert os.listdir(tmpdir.strpath) == ["link"]
+
+def test_override_suitable_link(tmpdir):
+    os.chdir(tmpdir.strpath)
+    osex.symlink("source", "link")
+    assert os.path.lexists("link")
+    osex.symlink("source", "link", override = True, backup = True)
+    # no backup should have been created since the existing link
+    # already had the proper target
+    assert os.listdir(tmpdir.strpath) == ["link"]
 
 def test_divergent_link_exists(tmpdir):
     os.chdir(tmpdir.strpath)

+ 9 - 0
tests/test_symlink_script.py

@@ -33,6 +33,15 @@ def test_suitable_link_exists(tmpdir):
     assert os.path.lexists("link")
     subprocess.check_call(["symlink", "source", "link"])
 
+def test_override_suitable_link(tmpdir, **kwargs):
+    os.chdir(tmpdir.strpath)
+    subprocess.check_call(["symlink", "source", "link"])
+    assert os.path.lexists("link")
+    subprocess.check_call(["symlink", "--override", "source", "link"])
+    # no backup should have been created since the existing link
+    # already had the proper target
+    assert os.listdir(tmpdir.strpath) == ["link"]
+
 def test_divergent_link_exists(tmpdir):
     os.chdir(tmpdir.strpath)
     subprocess.check_call(["symlink", "source", "link"])