link_fixer for single file now works!

This commit is contained in:
2024-07-24 21:25:38 -04:00
parent 82b4963bb0
commit aea33d4fac
3 changed files with 9 additions and 3 deletions

View File

@@ -1,3 +1,3 @@
# link-fixer # link-fixer
Fix broken symbolic links. Search for file of same name in target directory and link to that target if unique and if exists. Fix broken symbolic links. Search for file of same name as original target in provided directory and link to that target if unique and if exists.

View File

@@ -3,6 +3,9 @@ import os
from pathlib import Path from pathlib import Path
import sys import sys
from search_file import search_file
from swap_link import swap_link
def link_fixer(ln_path, tgt_dir_path): def link_fixer(ln_path, tgt_dir_path):
ln_is_dir = False ln_is_dir = False
@@ -33,6 +36,9 @@ def link_fixer(ln_path, tgt_dir_path):
if ln_is_dir: if ln_is_dir:
sys.exit("But ln dir version not yet implemented. Sorry!") sys.exit("But ln dir version not yet implemented. Sorry!")
tgt = search_file(link.resolve().name, tgt_dir_path)
swap_link(ln_path, tgt)
def fix_link(lnk, tgt_dir_path): def fix_link(lnk, tgt_dir_path):
return True return True

View File

@@ -6,8 +6,8 @@ from pathlib import Path
from os.path import relpath # won't be necessary with Python 3.12 thanks to walk_up arg in relative_to() from os.path import relpath # won't be necessary with Python 3.12 thanks to walk_up arg in relative_to()
def swap_link(lnk, tgt): def swap_link(lnk, tgt):
#lnk: symbolic link to swap #lnk: symbolic link to swap, str
#tgt: target for new link #tgt: target for new link, str
lnpath = Path(lnk) lnpath = Path(lnk)
if not lnpath.is_symlink(): if not lnpath.is_symlink():
raise Exception("First argument is not a symbolic link") raise Exception("First argument is not a symbolic link")