Created symlink_to_hdlink function succeeded
parent
c4dde2cc3f
commit
7c6710469b
7
media.py
7
media.py
|
@ -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)
|
|
@ -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()
|
|
@ -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()
|
|
|
@ -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
|
|
@ -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
|
Loading…
Reference in New Issue