import argparse from pathlib import Path import sys 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) ln_is_dir = False if not link.is_symlink(): sys.exit("Link not found or not a symbolic link") if link.is_dir(): ln_is_dir = True 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!")