From 68d30360e0289ab69b2fe25fb93bb7ef4d7d3c82 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Wed, 8 Apr 2015 17:15:45 +0200 Subject: [PATCH] Add tool to compare directories recursively. --- recursive_cmp.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100755 recursive_cmp.py diff --git a/recursive_cmp.py b/recursive_cmp.py new file mode 100755 index 000000000000..407abc5f290d --- /dev/null +++ b/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)