From 7c6710469bd40fc601fe46b1a93ae74da6543fca Mon Sep 17 00:00:00 2001 From: "jason.zhu" Date: Sat, 17 Jul 2021 23:46:38 +1000 Subject: [PATCH] Created symlink_to_hdlink function succeeded --- media.py | 9 ++++++++- symlink_to_hdlink.py | 26 ++++++++++++++++++++++++++ tests/test_helper.py | 4 ---- tests/test_media.py | 19 ++++++++++++++++++- tests/test_symlink_to_hdlink.py | 23 +++++++++++++++++++++++ 5 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 symlink_to_hdlink.py create mode 100644 tests/test_symlink_to_hdlink.py diff --git a/media.py b/media.py index ac49fbf..eca3728 100644 --- a/media.py +++ b/media.py @@ -27,4 +27,11 @@ class Media: def symlink_to(self, target: pathlib.Path): self.path.symlink_to(target=os.path.join(self.relative_to_parents(target=target), target.name), - target_is_directory=False) \ No newline at end of file + target_is_directory=False) + + def convert_symlink_to_hdlink(self): + if self.is_symlink() is True: + src = self.path.resolve() + dst = self.path + os.remove(dst) + os.link(src=src, dst=dst) \ No newline at end of file diff --git a/symlink_to_hdlink.py b/symlink_to_hdlink.py new file mode 100644 index 0000000..1c698e1 --- /dev/null +++ b/symlink_to_hdlink.py @@ -0,0 +1,26 @@ +import pathlib +import argparse + +from media import Media + +def find_all_files(directory: pathlib.Path): + ps = [] + for p in directory.rglob('*'): + if "eaDir" not in str(p): + ps.append((p)) + return ps + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description='convert hardlink to symlink in download folder') + + parser.add_argument('Library', + metavar='--library', + type=str, + help='the path to library') + + args = parser.parse_args() + path_library = args.Library + paths = find_all_files(pathlib.Path(path_library)) + + for x in paths: + Media(x).convert_symlink_to_hdlink() \ No newline at end of file diff --git a/tests/test_helper.py b/tests/test_helper.py index daf11c3..120c468 100644 --- a/tests/test_helper.py +++ b/tests/test_helper.py @@ -27,7 +27,3 @@ def create_test_files(cleanup) -> pd.DataFrame: df.append({'path': p, 'target': t} , ignore_index=True) return df - -def test1(create_test_files): - p = Path('./tests/file/') - assert p.exists() \ No newline at end of file diff --git a/tests/test_media.py b/tests/test_media.py index 709b509..4cdb62a 100644 --- a/tests/test_media.py +++ b/tests/test_media.py @@ -47,4 +47,21 @@ def test_media_symlink(create_one_pair_links): p2.symlink_to(target=t.path) with open(p2.path, 'r') as reader: text_out = reader.read() - assert text_in == text_out \ No newline at end of file + assert text_in == text_out + +def test_media_symlink_to_hdlink(create_one_pair_links): + + p = Media('./tests/file/file1.txt') + t = Media('./tests/target/target1.txt') + + text_in = 'hello, world' + + p.convert_symlink_to_hdlink() + assert not p.is_symlink() + + with open(p.path, 'w') as writer: + writer.write(text_in) + + with open(t.path, 'r') as reader: + text_out = reader.read() + assert text_out == text_in \ No newline at end of file diff --git a/tests/test_symlink_to_hdlink.py b/tests/test_symlink_to_hdlink.py new file mode 100644 index 0000000..e181ade --- /dev/null +++ b/tests/test_symlink_to_hdlink.py @@ -0,0 +1,23 @@ +import pytest +import pathlib + +from test_helper import cleanup +from media import Media + +@pytest.fixture +def create_test_files(cleanup): + pathlib.Path('./tests/target/').mkdir(parents=True, exist_ok=False) + pathlib.Path('./tests/file').mkdir(parents=True, exist_ok=True) + + for x in range(1,6): + p = Media('./tests/file/file' + str(x) + '.txt') + t = Media('./tests/target/target' + str(x) + '.txt') + t.path.touch(exist_ok=False) + p.symlink_to(t.path) + +def test_symlink_to_hdlink(create_test_files): + for p in pathlib.Path('./tests/file/').iterdir(): + Media(p).convert_symlink_to_hdlink() + + assert True + # TODO: Modify test case to computationaly test inode of file \ No newline at end of file