Created symlink_to_hdlink function succeeded

master
Jason Zhu 2021-07-17 23:46:38 +10:00
parent c4dde2cc3f
commit 7c6710469b
5 changed files with 75 additions and 6 deletions

View File

@ -28,3 +28,10 @@ class Media:
def symlink_to(self, target: pathlib.Path): def symlink_to(self, target: pathlib.Path):
self.path.symlink_to(target=os.path.join(self.relative_to_parents(target=target), target.name), self.path.symlink_to(target=os.path.join(self.relative_to_parents(target=target), target.name),
target_is_directory=False) 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)

View File

@ -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()

View File

@ -27,7 +27,3 @@ def create_test_files(cleanup) -> pd.DataFrame:
df.append({'path': p, 'target': t} , ignore_index=True) df.append({'path': p, 'target': t} , ignore_index=True)
return df return df
def test1(create_test_files):
p = Path('./tests/file/')
assert p.exists()

View File

@ -48,3 +48,20 @@ def test_media_symlink(create_one_pair_links):
with open(p2.path, 'r') as reader: with open(p2.path, 'r') as reader:
text_out = reader.read() text_out = reader.read()
assert text_in == text_out 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

View File

@ -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