cmd/move_all.py

54 lines
1.4 KiB
Python
Raw Normal View History

2020-09-09 05:02:28 +00:00
'''
Move all of the files into the destination directory, aborting the operation if
even a single file collides with a file in the destination.
'''
2020-12-09 15:17:48 +00:00
import argparse
2020-09-09 05:02:28 +00:00
import shutil
2020-12-09 15:17:48 +00:00
import sys
2020-09-09 05:02:28 +00:00
from voussoirkit import pathclass
2020-12-09 15:17:48 +00:00
from voussoirkit import pipeable
2020-09-09 05:02:28 +00:00
2020-12-09 15:17:48 +00:00
def moveall_argparse(args):
patterns = pipeable.input(args.source, skip_blank=True, strip=True)
files = pathclass.glob_many(patterns)
2020-12-09 15:17:48 +00:00
destination = pathclass.Path(args.destination)
try:
destination.assert_is_directory()
except pathclass.NotDirectory:
2020-12-09 15:17:48 +00:00
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)
2020-12-09 15:17:48 +00:00
shutil.move(file.absolute_path, new_path.absolute_path)
2020-09-09 05:02:28 +00:00
return 0
2020-12-09 15:17:48 +00:00
def main(argv):
parser = argparse.ArgumentParser(description=__doc__)
2020-09-09 05:02:28 +00:00
2020-12-09 15:17:48 +00:00
parser.add_argument('source')
parser.add_argument('destination')
parser.set_defaults(func=moveall_argparse)
2020-09-09 05:02:28 +00:00
2020-12-09 15:17:48 +00:00
args = parser.parse_args(argv)
return args.func(args)
2020-09-09 05:02:28 +00:00
2020-12-09 15:17:48 +00:00
if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:]))