From 3681fdee72a450f7754922a99877c1f6a3ffd841 Mon Sep 17 00:00:00 2001 From: Jake Gustafson <7557867+poikilos@users.noreply.github.com> Date: Mon, 21 Jun 2021 14:20:20 -0400 Subject: [PATCH] Stop on syntax error. Make delete optional & finish implementing it. --- forwardfilesync.py | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/forwardfilesync.py b/forwardfilesync.py index 3134d3f..1a5aa04 100644 --- a/forwardfilesync.py +++ b/forwardfilesync.py @@ -47,13 +47,13 @@ def trim_branch(src, dst, dot_hidden=True): if not os.path.exists(src_sub_path): if os.path.isfile(dst_sub_path): print("{} \"{}\"...".format(CMD_RM, dst_sub_path)) - # os.remove(dst_sub_path) + os.remove(dst_sub_path) else: print("{} \"{}\"...".format(CMD_RMTREE, dst_sub_path)) - # shutil.rmtree(dst_sub_path) + shutil.rmtree(dst_sub_path) -def update_tree(src, dst, level=0, do_trim=True, dot_hidden=False): +def update_tree(src, dst, level=0, do_trim=False, dot_hidden=False): ''' Creates dst if not present, then copies everything from src to dst recursively. @@ -91,27 +91,49 @@ def update_tree(src, dst, level=0, do_trim=True, dot_hidden=False): "".format(CMD_CP, sub_path, dst_sub_path)) pass +USAGE = ''' +Syntax: +forwardfilesync.py [options] + +--hidden Process files & folders even if named starting with '.'. +--delete Delete files & folders on the destination if not in source. + +''' + +def usage(): + print(USAGE) def main(): flags = {} flags["hidden"] = False + flags["delete"] = False if len(sys.argv) == 3: pass elif (len(sys.argv) == 4) and (sys.argv[3][:2] == "--"): name = sys.argv[3][2:] if name not in flags: + usage() print("Error: The syntax is invalid. Expected:") print("forwardfilesync.py ") print("forwardfilesync.py --hidden") + exit(1) flags[name] = True else: + usage() print("Error: The syntax is invalid. Expected:") print("forwardfilesync.py ") - print("forwardfilesync.py --hidden") + print("forwardfilesync.py --hidden --delete") exit(1) src = sys.argv[1] dst = sys.argv[2] - update_tree(src, dst, dot_hidden=flags["hidden"]) + update_tree( + src, + dst, + do_trim=flags["delete"] is True, + dot_hidden=flags["hidden"] is True, + ) + print(CMD_COMMENT + "Done.") + if __name__ == "__main__":