63 lines
1.6 KiB
Python
63 lines
1.6 KiB
Python
import argparse
|
|
import os
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
from search_file import search_file
|
|
from swap_link import swap_link
|
|
|
|
def link_fixer(ln_path, tgt_dir_path):
|
|
ln_is_dir = False
|
|
|
|
link = Path(ln_path)
|
|
tgt_dir = Path(tgt_dir_path)
|
|
|
|
if link.is_dir():
|
|
ln_is_dir = True
|
|
elif not link.is_symlink():
|
|
if not link.exists():
|
|
sys.exit("Link argument matches no file or directory")
|
|
else:
|
|
sys.exit("Link argument is not a symbolic link")
|
|
elif link.exists():
|
|
sys.exit("Link is not broken!")
|
|
|
|
if not tgt_dir.exists():
|
|
sys.exit("Target directory not found.")
|
|
if not tgt_dir.is_dir():
|
|
sys.exit("Pointed target is not a directory")
|
|
|
|
print("Starting link fixer")
|
|
if ln_is_dir:
|
|
print("Links dir: \t", link)
|
|
else:
|
|
print("Link: \t\t", link)
|
|
print("Targets dir: \t", tgt_dir)
|
|
if ln_is_dir:
|
|
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):
|
|
return True
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser(
|
|
description='''Fix broken symbolic links.
|
|
Search for file with same name as link in target dir.
|
|
Replace link to point to found file if any.
|
|
'''
|
|
|
|
)
|
|
parser.add_argument("link", type=str, help="Broken link, or directory with broken links")
|
|
parser.add_argument("tgt_path", type=str, help="Directory in which to find target(s)")
|
|
args = parser.parse_args()
|
|
|
|
link = Path(args.link)
|
|
tgt_dir = Path(args.tgt_path)
|
|
|
|
link_fixer(link, tgt_dir)
|