#!/usr/bin/python3.8 # # USAGE: ./compare-links.py DIR1 DIR2 # # Recursively compare DIR1 and DIR2 for matching symlinks. Report # missing subdirs, missing links, and unequal link targets. # import sys import os.path import stat def compare(dir1, dir2): files1 = set(os.listdir(dir1)) files2 = set(os.listdir(dir2)) report_diffs(dir1, files1, dir2, files2) both = files1.intersection(files2) #print(f'BOTH: {list(both)[:10]}...') for f in both: p1 = os.path.join(dir1, f) p2 = os.path.join(dir2, f) s1 = os.stat(p1, follow_symlinks=False) s2 = os.stat(p2, follow_symlinks=False) t1 = stat.S_IFMT(s1.st_mode) t2 = stat.S_IFMT(s2.st_mode) if t1 != t2: print(f'TYPE DIFFERS:\n {p1}: {t1}\n {p2}: {t2}') elif stat.S_ISDIR(s1.st_mode): # Recurse on the subdirs compare(p1, p2) elif stat.S_ISLNK(s1.st_mode): pass else: print(f'UNKNOWN TYPE: {p1}') def report_diffs(dir1, files1, dir2, files2): only_dir1 = files1 - files2 if only_dir1: print(f'ONLY IN: {dir1}\n {", ".join(sorted(only_dir1))}') only_dir2 = files2 - files1 if only_dir2: print(f'ONLY IN: {dir2}\n {", ".join(sorted(only_dir2))}') if __name__ == '__main__': compare(sys.argv[1], sys.argv[2])