Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tool to compare directories recursively. #437

Merged
merged 1 commit into from Apr 9, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions recursive_cmp.py
@@ -0,0 +1,42 @@
#!/usr/bin/env python3

CHECK_CONTENT = False

import os
import sys
import subprocess

join = os.path.join

def compare_dirs(dir1, dir2):
l1 = set(os.listdir(dir1))
l2 = set(os.listdir(dir2))
if l1 != l2:
print('“%s” and “%s” differ:' % (dir1, dir2))
print('\tfirst has: %s' % (l1-l2))
print('\tsecond has: %s' % (l2-l1))
for filename in (l1 & l2):
compare(join(dir1, filename), join(dir2, filename))

def compare_files(f1, f2):
if CHECK_CONTENT:
subprocess.call(['cmp', f1, f2])

def compare(dir1, dir2):
if os.path.isdir(dir1) and os.path.isdir(dir2):
return compare_dirs(dir1, dir2)
elif os.path.isfile(dir1) and os.path.isfile(dir2):
return compare_files(dir1, dir2)
elif not os.path.isdir(dir1) and not os.path.isfile(dir1) and \
not os.path.isdir(dir2) and not os.path.isfile(dir2):
print('Skipping “%s” and “%s”: neither file or dir.' % (dir1, dir2))
else:
print('“%s” and “%s” are not the same type (dir/file).' % (dir1, dir2))

if __name__ == '__main__':
if len(sys.argv) != 3:
print('Syntax: %s dir1 dir2' % sys.argv[0])
exit(1)

(prog, dir1, dir2) = sys.argv
compare(dir1, dir2)