2017-02-20 12:59:28 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2017-02-20 13:05:01 +00:00
|
|
|
"""Interactively and recursively copy files, prompting user when file
|
|
|
|
differs, showing him diffs.
|
|
|
|
"""
|
|
|
|
|
2017-02-20 12:59:28 +00:00
|
|
|
from filecmp import cmpfiles
|
|
|
|
from difflib import unified_diff
|
2017-04-29 09:23:04 +00:00
|
|
|
from os.path import expanduser, join, isdir, exists, dirname
|
|
|
|
from os import listdir, makedirs
|
2017-02-20 13:05:01 +00:00
|
|
|
from shutil import copy2 as copy
|
2017-02-20 12:59:28 +00:00
|
|
|
from itertools import chain
|
|
|
|
|
|
|
|
|
|
|
|
def maybe_copy_file(src, dest):
|
|
|
|
if not exists(dest):
|
|
|
|
if input("Copy {} [y,n]? ".format(src)).lower()[0] == 'y':
|
2017-04-29 09:23:04 +00:00
|
|
|
makedirs(dirname(dest), exist_ok=True)
|
2017-02-20 12:59:28 +00:00
|
|
|
copy(src, dest)
|
|
|
|
return
|
|
|
|
with open(src) as a, open(dest) as b:
|
2017-03-14 16:03:37 +00:00
|
|
|
diff = unified_diff(list(b), list(a))
|
2017-02-20 12:59:28 +00:00
|
|
|
print(''.join(list(diff)))
|
2017-02-20 13:05:01 +00:00
|
|
|
if input("Copy {} [y,n]? ".format(src)).lower()[0] == 'y':
|
2017-02-20 12:59:28 +00:00
|
|
|
copy(src, dest)
|
|
|
|
|
|
|
|
|
|
|
|
def maybe_copy_dir(src, dest, common):
|
|
|
|
matches, missmatches, errors = cmpfiles(src, dest, common, shallow=False)
|
|
|
|
for match in sorted(matches):
|
|
|
|
print(join(dest, match), '√')
|
|
|
|
for missmatch in sorted(chain(missmatches, errors)):
|
2017-02-20 13:22:04 +00:00
|
|
|
if isdir(join(src, missmatch)):
|
2017-02-20 12:59:28 +00:00
|
|
|
maybe_copy_dir(join(src, missmatch), join(dest, missmatch),
|
|
|
|
listdir(join(src, missmatch)))
|
|
|
|
else:
|
2017-02-20 13:21:25 +00:00
|
|
|
maybe_copy_file(join(src, missmatch), join(dest, missmatch))
|
2017-02-20 12:59:28 +00:00
|
|
|
|
|
|
|
|
2017-02-20 13:09:05 +00:00
|
|
|
def parse_args():
|
|
|
|
import argparse
|
|
|
|
parser = argparse.ArgumentParser(description='Interactively copy files.')
|
|
|
|
parser.add_argument('src')
|
|
|
|
parser.add_argument('dest')
|
|
|
|
parser.add_argument('--exclude', help='Exclude those files', nargs='*',
|
|
|
|
default=[])
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
def main(src, dest, exclude):
|
|
|
|
maybe_copy_dir(expanduser(src), expanduser(dest),
|
2017-02-20 13:19:48 +00:00
|
|
|
set(listdir(src)) - set(exclude))
|
2017-02-20 13:09:05 +00:00
|
|
|
|
|
|
|
|
2017-02-20 12:59:28 +00:00
|
|
|
if __name__ == '__main__':
|
2017-02-20 13:09:05 +00:00
|
|
|
main(**vars(parse_args()))
|