1
# This Source Code Form is subject to the terms of the Mozilla Public
2
# License, v. 2.0. If a copy of the MPL was not distributed with this
3
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
5
# check-sync-dirs.py --- check that one directory is an exact subset of another
7
# Usage: python check-sync-dirs.py COPY ORIGINAL
9
# Check that the files present in the directory tree COPY are exact
10
# copies of their counterparts in the directory tree ORIGINAL. COPY
11
# need not have all the files in ORIGINAL, but COPY may not have files
12
# absent from ORIGINAL.
14
# Each directory in COPY may have a file named
15
# 'check-sync-exceptions', which lists files in COPY that need not be
16
# the same as the corresponding file in ORIGINAL, or exist at all in
17
# ORIGINAL. (The 'check-sync-exceptions' file itself is always
18
# treated as exceptional.) Blank lines and '#' comments in the file
23
from os.path import join
28
if len(sys.argv) != 3:
29
print >> sys.stderr, 'TEST-UNEXPECTED-FAIL | check-sync-dirs.py | Usage: %s COPY ORIGINAL' % sys.argv[0]
32
copy = os.path.abspath(sys.argv[1])
33
original = os.path.abspath(sys.argv[2])
35
# Return the contents of FILENAME, a 'check-sync-exceptions' file, as
36
# a dictionary whose keys are exactly the list of filenames, along
37
# with the basename of FILENAME itself. If FILENAME does not exist,
38
# return the empty dictionary.
39
def read_exceptions(filename):
40
if (os.path.exists(filename)):
45
if line != '' and line[0] != '#':
46
exceptions[line] = None
47
exceptions[os.path.basename (filename)] = None
53
# Return true if FILENAME matches any pattern in the list of filename
55
def fnmatch_any(filename, patterns):
56
for pattern in patterns:
57
if fnmatch.fnmatch(filename, pattern):
61
# Check the contents of the directory tree COPY against ORIGINAL. For each
62
# file that differs, apply REPORT to COPY, ORIGINAL, and the file's
63
# relative path. COPY and ORIGINAL should be absolute. Ignore files
64
# that match patterns given in the list IGNORE.
65
def check(copy, original):
67
for (dirpath, dirnames, filenames) in os.walk('.'):
68
exceptions = read_exceptions(join(dirpath, 'check-sync-exceptions'))
69
for dirname in dirnames:
70
if fnmatch_any(dirname, exceptions):
71
dirnames.remove(dirname)
73
for filename in filenames:
74
if fnmatch_any(filename, exceptions):
76
relative_name = join(dirpath, filename)
77
original_name = join(original, relative_name)
78
if (os.path.exists(original_name)
79
and filecmp.cmp(relative_name, original_name, False)):
81
report(copy, original, relative_name)
83
differences_found = False
85
# Print an error message for DIFFERING, which was found to differ
86
# between COPY and ORIGINAL. Set the global variable differences_found.
87
def report(copy, original, differing):
88
global differences_found
89
if not differences_found:
90
print >> sys.stderr, 'TEST-UNEXPECTED-FAIL | check-sync-dirs.py | build file copies are not in sync\n' \
91
'TEST-INFO | check-sync-dirs.py | file(s) found in: %s\n' \
92
'TEST-INFO | check-sync-dirs.py | differ from their originals in: %s' \
94
print >> sys.stderr, 'TEST-INFO | check-sync-dirs.py | differing file: %s' % differing
95
differences_found = True
100
msg = '''In general, the files in '%s' should always be exact copies of
101
originals in '%s'. A change made to one should also be made to the
102
other. See 'check-sync-dirs.py' for more details.''' \
104
print >> sys.stderr, textwrap.fill(msg, 75)
107
print >> sys.stderr, 'TEST-PASS | check-sync-dirs.py | %s <= %s' % (copy, original)