Browse Source

added script 'symlink', a wrapper for ioex.symlink

Fabian Peter Hammerle 8 years ago
parent
commit
83dbe036e4
3 changed files with 160 additions and 2 deletions
  1. 40 0
      scripts/symlink
  2. 5 2
      setup.py
  3. 115 0
      tests/test_symlink_script.py

+ 40 - 0
scripts/symlink

@@ -0,0 +1,40 @@
+#!/usr/bin/python
+# PYTHON_ARGCOMPLETE_OK
+
+import osex
+
+import sys
+import argparse
+import argcomplete
+
+def _init_argparser():
+
+    argparser = argparse.ArgumentParser(description = None)
+    argparser.add_argument("source")
+    argparser.add_argument("link_name")
+    argparser.add_argument("--relative", action = "store_true")
+    argparser.add_argument("--override", action = "store_true")
+    argparser.add_argument("--no-backup", action = "store_true")
+    argparser.add_argument("--backup-suffix", default = "~")
+    return argparser
+
+def main(argv):
+
+    argparser = _init_argparser()
+    argcomplete.autocomplete(argparser)
+    args = argparser.parse_args(argv)
+
+    params = vars(args)
+    params["backup"] = not params["no_backup"]
+    del params["no_backup"]
+
+    try:
+        osex.symlink(**params)
+    except Exception, ex:
+        print(ex)
+        return 1
+
+    return 0
+
+if __name__ == "__main__":
+    sys.exit(main(sys.argv[1:]))

+ 5 - 2
setup.py

@@ -1,15 +1,18 @@
 from setuptools import setup
 
+import glob
+
 setup(
     name = 'osex',
     packages = ['osex'],
-    version = '0.1',
+    version = '0.2',
     description = 'extension for python\'s build-in operating system interface',
     author = 'Fabian Peter Hammerle',
     author_email = 'fabian.hammerle@gmail.com',
     url = 'https://github.com/fphammerle/osex',
-    download_url = 'https://github.com/fphammerle/osex/tarball/0.1',
+    download_url = 'https://github.com/fphammerle/osex/tarball/0.2',
     keywords = [],
     classifiers = [],
+    scripts = glob.glob('scripts/*'),
     tests_require = ['pytest']
     )

+ 115 - 0
tests/test_symlink_script.py

@@ -0,0 +1,115 @@
+import pytest
+
+import os
+import subprocess
+
+def test_new(tmpdir):
+    os.chdir(tmpdir.strpath)
+    subprocess.check_call(["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")
+    subprocess.check_call(["symlink", source, link])
+    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")
+    subprocess.check_call(["symlink", source, link, "--relative"])
+    assert os.path.lexists(link)
+    assert os.readlink(link) == "../source"
+
+def test_suitable_link_exists(tmpdir):
+    os.chdir(tmpdir.strpath)
+    subprocess.check_call(["symlink", "source", "link"])
+    assert os.path.lexists("link")
+    subprocess.check_call(["symlink", "source", "link"])
+
+def test_divergent_link_exists(tmpdir):
+    os.chdir(tmpdir.strpath)
+    subprocess.check_call(["symlink", "source", "link"])
+    assert os.path.lexists("link")
+    with pytest.raises(subprocess.CalledProcessError):
+        subprocess.check_call(["symlink", "source2", "link"])
+
+def test_dir_exists(tmpdir):
+    os.chdir(tmpdir.strpath)
+    os.mkdir("dir")
+    assert os.path.exists("dir")
+    with pytest.raises(subprocess.CalledProcessError):
+        subprocess.check_call(["symlink", "source", "dir"])
+
+def test_override_link(tmpdir):
+    os.chdir(tmpdir.strpath)
+    subprocess.check_call(["symlink", "source1", "link"])
+    assert os.path.lexists("link")
+    subprocess.check_call(["symlink", "source2", "link", "--override", "--no-backup"])
+    assert os.path.lexists("link")
+    assert os.readlink("link") == "source2"
+    assert not os.path.lexists("link~")
+
+def test_override_dir(tmpdir):
+    os.chdir(tmpdir.strpath)
+    os.mkdir("link")
+    assert os.path.exists("link")
+    subprocess.check_call(["symlink", "source", "link", "--override", "--no-backup"])
+    assert os.path.lexists("link")
+    assert os.readlink("link") == "source"
+    assert not os.path.isdir("link-")
+
+def test_backup_link(tmpdir):
+    os.chdir(tmpdir.strpath)
+    subprocess.check_call(["symlink", "source1", "link"])
+    assert os.path.lexists("link")
+    subprocess.check_call(["symlink", "source2", "link", "--override", "--backup-suffix", "~"])
+    assert os.path.lexists("link")
+    assert os.readlink("link") == "source2"
+    assert os.path.lexists("link~")
+    assert os.readlink("link~") == "source1"
+
+def test_backup_dir(tmpdir):
+    os.chdir(tmpdir.strpath)
+    os.mkdir("link")
+    assert os.path.exists("link")
+    subprocess.check_call(["symlink", "source", "link", "--override", "--backup-suffix", "-"])
+    assert os.path.lexists("link")
+    assert os.readlink("link") == "source"
+    assert os.path.isdir("link-")
+
+def test_backup_multiple_links(tmpdir):
+    os.chdir(tmpdir.strpath)
+    # source 1
+    subprocess.check_call(["symlink", "source1", "link"])
+    assert os.path.lexists("link")
+    # source 2
+    subprocess.check_call(["symlink", "source2", "link", "--override", "--backup-suffix", "~"])
+    assert os.path.lexists("link")
+    assert os.readlink("link") == "source2"
+    assert os.path.lexists("link~")
+    assert os.readlink("link~") == "source1"
+    # source 3
+    subprocess.check_call(["symlink", "source3", "link", "--override", "--backup-suffix", "~"])
+    assert os.path.lexists("link")
+    assert os.readlink("link") == "source3"
+    assert os.path.lexists("link~")
+    assert os.readlink("link~") == "source1"
+    assert os.path.lexists("link~1")
+    assert os.readlink("link~1") == "source2"
+    # source 4
+    subprocess.check_call(["symlink", "source4", "link", "--override", "--backup-suffix", "~"])
+    assert os.path.lexists("link")
+    assert os.readlink("link") == "source4"
+    assert os.path.lexists("link~")
+    assert os.readlink("link~") == "source1"
+    assert os.path.lexists("link~1")
+    assert os.readlink("link~1") == "source2"
+    assert os.path.lexists("link~2")
+    assert os.readlink("link~2") == "source3"