From c0a706104f8f1ae7b1928048978e40dff3a63fe1 Mon Sep 17 00:00:00 2001 From: Matthieu Lagouge Date: Wed, 12 Jun 2024 02:53:51 +0000 Subject: [PATCH] =?UTF-8?q?T=C3=A9l=C3=A9verser=20les=20fichiers=20vers=20?= =?UTF-8?q?"/"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Argument parser only First commit --- link_fixer.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 link_fixer.py diff --git a/link_fixer.py b/link_fixer.py new file mode 100644 index 0000000..92d1fc0 --- /dev/null +++ b/link_fixer.py @@ -0,0 +1,41 @@ +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!")