Created
February 21, 2018 08:37
-
-
Save seven332/6b105aaf2096e84b492cec0c92cee9e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import os | |
import filecmp | |
import shutil | |
def dejoin(path, opath): | |
if path.startswith(opath): | |
path = path[len(opath):] | |
if len(path) >= 1 and path[0] == '\\' or path[0] == '/': | |
path = path[1:] | |
return path | |
def compare(src, std, dst, cc): | |
for root, _, files in os.walk(src): | |
for f in files: | |
src_path = os.path.join(root, f) | |
path_fragment = dejoin(src_path, src) | |
std_path = os.path.join(std, path_fragment) | |
dst_path = os.path.join(dst, path_fragment) | |
copy = not os.path.exists(std_path) | |
if cc and not copy: | |
copy = not filecmp.cmp(src_path, std_path) | |
if copy: | |
print(src_path) | |
os.makedirs(os.path.dirname(dst_path), exist_ok=True) | |
shutil.copy2(src_path, dst_path) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument('src', help='the source directory, whose child files might be saved') | |
parser.add_argument('std', help='the standard directory, which the source compared to') | |
parser.add_argument('dst', help='the directory to save files') | |
parser.add_argument('-c', '--compare-contents', help='compare file content', action='store_true', default=False) | |
args = parser.parse_args() | |
compare(args.src, args.std, args.dst, args.compare_contents) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment