~ubuntu-branches/ubuntu/trusty/mozjs17/trusty

« back to all changes in this revision

Viewing changes to js/src/config/check-sync-dirs.py

  • Committer: Package Import Robot
  • Author(s): Rico Tzschichholz
  • Date: 2013-05-25 12:24:23 UTC
  • Revision ID: package-import@ubuntu.com-20130525122423-zmxucrhtensw90xy
Tags: upstream-17.0.0
ImportĀ upstreamĀ versionĀ 17.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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/.
 
4
 
 
5
# check-sync-dirs.py --- check that one directory is an exact subset of another
 
6
#
 
7
# Usage: python check-sync-dirs.py COPY ORIGINAL
 
8
#
 
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.
 
13
 
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
 
19
# are ignored.
 
20
 
 
21
import sys
 
22
import os
 
23
from os.path import join
 
24
import filecmp
 
25
import textwrap
 
26
import fnmatch
 
27
 
 
28
if len(sys.argv) != 3:
 
29
    print >> sys.stderr, 'TEST-UNEXPECTED-FAIL | check-sync-dirs.py | Usage: %s COPY ORIGINAL' % sys.argv[0]
 
30
    sys.exit(1)
 
31
 
 
32
copy = os.path.abspath(sys.argv[1])
 
33
original = os.path.abspath(sys.argv[2])
 
34
 
 
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)):
 
41
        f = file(filename)
 
42
        exceptions = {}
 
43
        for line in f:
 
44
            line = line.strip()
 
45
            if line != '' and line[0] != '#':
 
46
                exceptions[line] = None
 
47
        exceptions[os.path.basename (filename)] = None
 
48
        f.close()
 
49
        return exceptions
 
50
    else:
 
51
        return {}
 
52
 
 
53
# Return true if FILENAME matches any pattern in the list of filename
 
54
# patterns PATTERNS.
 
55
def fnmatch_any(filename, patterns):
 
56
    for pattern in patterns:
 
57
        if fnmatch.fnmatch(filename, pattern):
 
58
            return True
 
59
    return False
 
60
 
 
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):
 
66
    os.chdir(copy)
 
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)
 
72
                break
 
73
        for filename in filenames:
 
74
            if fnmatch_any(filename, exceptions):
 
75
                continue
 
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)):
 
80
                continue
 
81
            report(copy, original, relative_name)
 
82
 
 
83
differences_found = False
 
84
 
 
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' \
 
93
                             % (copy, original)
 
94
    print >> sys.stderr, 'TEST-INFO | check-sync-dirs.py | differing file:                 %s' % differing
 
95
    differences_found = True
 
96
 
 
97
check(copy, original)
 
98
 
 
99
if differences_found:
 
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.''' \
 
103
         % (copy, original)
 
104
    print >> sys.stderr, textwrap.fill(msg, 75)
 
105
    sys.exit(1)
 
106
 
 
107
print >> sys.stderr, 'TEST-PASS | check-sync-dirs.py | %s <= %s' % (copy, original)
 
108
sys.exit(0)