Browse Source

symlink: added option 'relative'

Fabian Peter Hammerle 8 years ago
commit
e19146ed03
4 changed files with 55 additions and 0 deletions
  1. 8 0
      .gitignore
  2. 10 0
      osex/__init__.py
  3. 7 0
      setup.py
  4. 30 0
      tests/test_symlink.py

+ 8 - 0
.gitignore

@@ -0,0 +1,8 @@
+# Compiled python modules.
+*.pyc
+
+# Setuptools distribution folder.
+/dist/
+
+# Python egg metadata, regenerated from source files by setuptools.
+/*.egg-info

+ 10 - 0
osex/__init__.py

@@ -0,0 +1,10 @@
+import os
+
+def symlink(source, link_name, relative = False): #, override = False):
+    if relative:
+        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):
+    #     os.remove(link_name)
+    os.symlink(source, link_name)

+ 7 - 0
setup.py

@@ -0,0 +1,7 @@
+from setuptools import setup
+
+setup(
+    name = 'osex',
+    packages = ['osex'],
+    tests_require = ['pytest']
+    )

+ 30 - 0
tests/test_symlink.py

@@ -0,0 +1,30 @@
+import pytest
+
+import osex
+
+import os
+import sys
+
+def test_new(tmpdir):
+    os.chdir(tmpdir.strpath)
+    osex.symlink("source", "link")
+    assert os.path.lexists("link")
+    assert os.readlink("link") == "source"
+
+def test_absolute(tmpdir):
+    os.chdir(tmpdir.strpath)
+    os.makedirs(os.path.join("1", "2"))
+    link = os.path.join("1", "2", "link")
+    source = os.path.join(os.getcwd(), "1", "source")
+    osex.symlink(source, link, relative = False)
+    assert os.path.lexists(link)
+    assert os.readlink(link) == source
+
+def test_relative(tmpdir):
+    os.chdir(tmpdir.strpath)
+    os.makedirs(os.path.join("1", "2"))
+    link = os.path.join("1", "2", "link")
+    source = os.path.join(os.getcwd(), "1", "source")
+    osex.symlink(source, link, relative = True)
+    assert os.path.lexists(link)
+    assert os.readlink(link) == "../source"