Created
June 24, 2016 06:11
-
-
Save shon/7d49531cf79ad6ea898d63dc3434d248 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 os | |
import hashlib | |
import sys | |
from pathlib import Path # python < 3.4 : pip install pathlib | |
def md5(path): | |
content = path.open('br').read() | |
return hashlib.md5(content).hexdigest() | |
def does_md5_match(path1, path2): | |
return md5(path1) == md5(path2) | |
def compare_dirs(dir1, dir2): | |
delta = {'missing': {'files': [], 'dirs': []}, 'differed': []} | |
for curdir, subdir_list, file_list in os.walk(dir1): | |
# compting relative path eg. src/foo -> dst/foo | |
dst_dir_path = Path(dir2) / Path(curdir).relative_to(dir1) | |
if not dst_dir_path.exists(): | |
delta['missing']['files'].extend([(Path(curdir) / p) for p in subdir_list]) | |
delta['missing']['dirs'].extend([(Path(curdir) / p) for p in file_list]) | |
else: | |
for path in file_list: | |
src_path = Path(curdir) / path | |
dst_path = dst_dir_path / path | |
if not dst_path.exists(): | |
delta['missing']['files'].append(src_path) | |
elif not does_md5_match(src_path, dst_path): | |
delta['differed'].append(src_path) | |
for path in subdir_list: | |
src_path = Path(curdir) / path | |
dst_path = dst_dir_path / path | |
if not dst_path.exists(): | |
delta['missing']['dirs'].append(src_path) | |
return delta | |
def test(): | |
src, dst = sys.argv[1:3] | |
print(compare_dirs(src, dst)) | |
test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment