cmd/move_all.py
Ethan Dalool 4a9051e617
Big migrations and linting.
With pathclass.glob_many, we can clean up and feel more confident
about many programs that use pipeable to take glob patterns.

Added return 0 to all programs that didn't have it, so we have
consistent and explicit command line return values.

Other linting and whitespace.
2021-09-23 23:42:45 -07:00

53 lines
1.4 KiB
Python

'''
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 argparse
import shutil
import sys
from voussoirkit import pathclass
from voussoirkit import pipeable
def moveall_argparse(args):
patterns = pipeable.input(args.source, skip_blank=True, strip=True)
files = pathclass.glob_many(patterns)
destination = pathclass.Path(args.destination)
try:
destination.assert_is_directory()
except pathclass.NotDirectory:
pipeable.stderr('destination must be a directory.')
return 1
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))
if fail:
return 1
for (file, new_path) in pairs:
pipeable.stdout(new_path.absolute_path)
shutil.move(file.absolute_path, new_path.absolute_path)
return 0
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:]))