1 import filecmp
2 import os.path
3
5 """
6 Compare two directories recursively. Files in each directory are
7 assumed to be equal if their names and contents are equal.
8
9 @param dir1: First directory path
10 @param dir2: Second directory path
11
12 @return: True if the directory trees are the same and
13 there were no errors while accessing the directories or files,
14 False otherwise.
15 """
16 dirs_cmp = filecmp.dircmp(dir1, dir2)
17 if len(dirs_cmp.left_only)>0 or len(dirs_cmp.right_only)>0 or \
18 len(dirs_cmp.funny_files)>0:
19 return False
20 (_, mismatch, errors) = filecmp.cmpfiles(
21 dir1, dir2, dirs_cmp.common_files, shallow=False)
22 if len(mismatch)>0 or len(errors)>0:
23 return False
24 for common_dir in dirs_cmp.common_dirs:
25 new_dir1 = os.path.join(dir1, common_dir)
26 new_dir2 = os.path.join(dir2, common_dir)
27 if not are_dir_trees_equal(new_dir1, new_dir2):
28 return False
29 return True
30