From d3392ef3a87f910248d0062066e9688d3c3cfa6e Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Wed, 9 Dec 2020 07:17:48 -0800 Subject: [PATCH] Update move_all.py. --- move_all.py | 57 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/move_all.py b/move_all.py index 44579cf..39b11b1 100644 --- a/move_all.py +++ b/move_all.py @@ -2,29 +2,52 @@ Move all of the files into the destination directory, aborting the operation if even a single file collides with a file in the destination. ''' -import sys +import argparse import shutil +import sys from voussoirkit import pathclass +from voussoirkit import pipeable from voussoirkit import winglob -argv = sys.argv[1:] +def moveall_argparse(args): + files = ( + pathclass.Path(file) + for pattern in pipeable.input(args.source) + for file in winglob.glob(pattern) + ) + destination = pathclass.Path(args.destination) -if len(argv) < 2: - raise TypeError() + if not destination.is_dir: + pipeable.stderr('destination must be a directory.') + return 1 -patterns = argv[:-1] -files = [file for pattern in patterns for file in winglob.glob(pattern)] -files = [pathclass.Path(file) for file in files] -destination = pathclass.Path(sys.argv[-1]) -if not destination.is_dir: - raise TypeError(destination) + pairs = [] + fail = False + for file in files: + new_path = destination.with_child(file.basename) + if new_path.exists: + pipeable.stderr(f'{file.basename} cannot be moved.') + fail = True + continue + pairs.append((file, new_path)) -for file in files: - if destination.with_child(file.basename).exists: - raise Exception(file.basename) + if fail: + return 1 -for file in files: - new_path = destination.with_child(file.basename) - print(new_path.absolute_path) - shutil.move(file.absolute_path, new_path.absolute_path) + for (file, new_path) in pairs: + pipeable.output(new_path.absolute_path) + shutil.move(file.absolute_path, new_path.absolute_path) + +def main(argv): + parser = argparse.ArgumentParser(description=__doc__) + + parser.add_argument('source') + parser.add_argument('destination') + parser.set_defaults(func=moveall_argparse) + + args = parser.parse_args(argv) + return args.func(args) + +if __name__ == '__main__': + raise SystemExit(main(sys.argv[1:]))