PageRenderTime 28ms CodeModel.GetById 14ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 1ms

/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/js/config/check-sync-dirs.py

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
Python | 108 lines | 68 code | 12 blank | 28 comment | 20 complexity | 095e468af3d6aa4b7507876c2b91d254 MD5 | raw file
  1# check-sync-dirs.py --- check that one directory is an exact subset of another
  2#
  3# Usage: python check-sync-dirs.py COPY ORIGINAL
  4#
  5# Check that the files present in the directory tree COPY are exact
  6# copies of their counterparts in the directory tree ORIGINAL.  COPY
  7# need not have all the files in ORIGINAL, but COPY may not have files
  8# absent from ORIGINAL.
  9# 
 10# Each directory in COPY may have a file named
 11# 'check-sync-exceptions', which lists files in COPY that need not be
 12# the same as the corresponding file in ORIGINAL, or exist at all in
 13# ORIGINAL.  (The 'check-sync-exceptions' file itself is always
 14# treated as exceptional.)  Blank lines and '#' comments in the file
 15# are ignored.
 16
 17import sys
 18import os
 19from os.path import join
 20import filecmp
 21import textwrap
 22import fnmatch
 23
 24if len(sys.argv) != 3:
 25    print >> sys.stderr, "Usage: %s COPY ORIGINAL" % sys.argv[0]
 26    sys.exit(1)
 27
 28copy = sys.argv[1]
 29original = sys.argv[2]
 30
 31# Ignore detritus left lying around by editing tools.
 32ignored_patterns = ['*~', '.#*', '#*#', '*.orig', '*.rej']
 33
 34# Return the contents of FILENAME, a 'check-sync-exceptions' file, as
 35# a dictionary whose keys are exactly the list of filenames, along
 36# with the basename of FILENAME itself.  If FILENAME does not exist,
 37# return the empty dictionary.
 38def read_exceptions(filename):
 39    if (os.path.exists(filename)):
 40        f = file(filename)
 41        exceptions={}
 42        for line in f:
 43            line = line.strip()
 44            if line != '' and line[0] != '#':
 45                exceptions[line] = None
 46        exceptions[os.path.basename (filename)] = None
 47        f.close()
 48        return exceptions
 49    else:
 50        return {}
 51
 52# Return true if FILENAME matches any pattern in the list of filename
 53# patterns PATTERNS.
 54def fnmatch_any(filename, patterns):
 55    for pattern in patterns:
 56        if fnmatch.fnmatch(filename, pattern):
 57            return True
 58    return False
 59
 60# Check the contents of the directory tree COPY against ORIGINAL.  For each
 61# file that differs, apply REPORT to COPY, ORIGINAL, and the file's
 62# relative path.  COPY and ORIGINAL should be absolute.  Ignore files 
 63# that match patterns given in the list IGNORE.
 64def check(copy, original, ignore, report):
 65    os.chdir(copy)
 66    for (dirpath, dirnames, filenames) in os.walk('.'):
 67        exceptions = read_exceptions(join(dirpath, 'check-sync-exceptions'))
 68        for filename in filenames:
 69            if filename in exceptions:
 70                continue
 71            if fnmatch_any(filename, ignore):
 72                continue
 73            relative_name = join(dirpath, filename)
 74            original_name = join(original, relative_name)
 75            if (os.path.exists(original_name)
 76                and filecmp.cmp(relative_name, original_name)):
 77                continue
 78            report(copy, original, relative_name)
 79
 80
 81differences_found = False
 82
 83# Print an error message for DIFFERING, which was found to differ
 84# between COPY and ORIGINAL.  Set the global variable differences_found.
 85def report(copy, original, differing):
 86    global differences_found
 87    if not differences_found:
 88        print >> sys.stderr, "TEST-FAIL | build file copies are not in sync"
 89        print >> sys.stderr, "file(s) found in:               %s" % (copy)
 90        print >> sys.stderr, ("differ from their originals in: %s"
 91                              % (original))
 92    print >> sys.stderr, "file differs: %s" % (differing)
 93    differences_found = True
 94
 95check(os.path.abspath(copy),
 96      os.path.abspath(original),
 97      ignored_patterns,
 98      report)
 99
100if differences_found:
101    msg=('''In general, the files in '%s' should always be exact copies of
102originals in '%s'.  A change made to one should also be made to the
103other.  See 'check-sync-dirs.py' for more details.'''
104         % (copy, original))
105    print >> sys.stderr, textwrap.fill(msg, 75)
106    sys.exit(1)
107
108sys.exit(0)