Browse Source

added script 'renames'

Fabian Peter Hammerle 8 years ago
parent
commit
e3968ef485
2 changed files with 160 additions and 0 deletions
  1. 39 0
      scripts/renames
  2. 121 0
      tests/test_renames_script.py

+ 39 - 0
scripts/renames

@@ -0,0 +1,39 @@
+#!/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("destination")
+    argparser.add_argument("--no-override", action = "store_true")
+    argparser.add_argument("--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["override"] = not params["no_override"]
+    del params["no_override"]
+
+    try:
+        osex.renames(**params)
+    except Exception, ex:
+        print(ex)
+        return 1
+
+    return 0
+
+if __name__ == "__main__":
+    sys.exit(main(sys.argv[1:]))

+ 121 - 0
tests/test_renames_script.py

@@ -0,0 +1,121 @@
+import pytest
+
+import os
+import subprocess
+
+def test_rename(tmpdir):
+    os.chdir(tmpdir.strpath)
+    open('a', 'w').close()
+    assert os.path.exists('a')
+    subprocess.check_call(['renames', 'a', 'b'])
+    assert not os.path.exists('a')
+    assert os.path.exists('b')
+
+def test_prune_source_dirs(tmpdir):
+    os.chdir(tmpdir.strpath)
+    source_dir_path = os.path.join('1', '2')
+    os.makedirs(source_dir_path)
+    source_file_path = os.path.join(source_dir_path, 'a')
+    open(source_file_path, 'w').close()
+    assert os.path.isdir(source_dir_path)
+    assert os.path.exists(source_file_path)
+    subprocess.check_call(['renames', '1/2/a', 'b'])
+    assert os.listdir(tmpdir.strpath) == ['b']
+
+def test_create_destination_dirs(tmpdir):
+    os.chdir(tmpdir.strpath)
+    open('a', 'w').close()
+    assert os.listdir(tmpdir.strpath) == ['a']
+    subprocess.check_call(['renames', 'a', '1/2/b'])
+    assert os.listdir(tmpdir.strpath) == ['1']
+    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(subprocess.CalledProcessError):
+        subprocess.check_call(['renames', 'src', 'dest', '--no-override', '--backup'])
+    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')
+    subprocess.check_call(['renames', 'src', 'dest', '--backup', '--backup-suffix', '~'])
+    assert not os.path.isfile('src')
+    assert os.path.isfile('dest')
+    assert os.path.isfile('dest~')
+    assert len(os.listdir(tmpdir.strpath)) == 2
+
+def test_backup_directory(tmpdir):
+    os.chdir(tmpdir.strpath)
+    open('src', 'w').close()
+    os.mkdir('dest')
+    assert os.path.isfile('src')
+    assert os.path.isdir('dest')
+    subprocess.check_call(['renames', 'src', 'dest', '--backup', '--backup-suffix', '~'])
+    assert not os.path.isfile('src')
+    assert os.path.isfile('dest')
+    assert os.path.isdir('dest~')
+    assert len(os.listdir(tmpdir.strpath)) == 2
+
+def test_backup_directory_symlink(tmpdir):
+    os.chdir(tmpdir.strpath)
+    open('src', 'w').close()
+    os.mkdir('dir')
+    os.symlink('dir', 'dest')
+    assert os.path.isfile('src')
+    assert os.path.isdir('dir')
+    assert os.path.islink('dest')
+    assert os.path.isdir('dest')
+    subprocess.check_call(['renames', 'src', 'dest', '--backup', '--backup-suffix', '~'])
+    assert not os.path.isfile('src')
+    assert os.path.isdir('dir')
+    assert os.path.islink('dest~')
+    assert os.path.isdir('dest~')
+    assert os.path.isfile('dest')
+    assert len(os.listdir(tmpdir.strpath)) == 3
+
+def test_override_file(tmpdir):
+    os.chdir(tmpdir.strpath)
+    open('src', 'w').close()
+    open('dest', 'w').close()
+    assert os.path.isfile('src')
+    assert os.path.isfile('dest')
+    subprocess.check_call(['renames', 'src', 'dest'])
+    assert not os.path.isfile('src')
+    assert os.path.isfile('dest')
+    assert len(os.listdir(tmpdir.strpath)) == 1
+
+def test_attempt_override_directory(tmpdir):
+    os.chdir(tmpdir.strpath)
+    open('src', 'w').close()
+    os.mkdir('dest')
+    assert os.path.isfile('src')
+    assert os.path.isdir('dest')
+    with pytest.raises(subprocess.CalledProcessError):
+        subprocess.check_call(['renames', 'src', 'dest'])
+
+def test_override_directory_symlink(tmpdir):
+    os.chdir(tmpdir.strpath)
+    open('src', 'w').close()
+    os.mkdir('dir')
+    os.symlink('dir', 'link')
+    assert os.path.isfile('src')
+    assert os.path.isdir('dir')
+    assert os.path.islink('link')
+    assert os.path.isdir('link')
+    subprocess.check_call(['renames', 'src', 'link'])
+    assert not os.path.isfile('src')
+    assert os.path.isdir('dir')
+    assert not os.path.islink('link')
+    assert os.path.isfile('link')
+    assert len(os.listdir(tmpdir.strpath)) == 2