PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/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 | 15 complexity | 095e468af3d6aa4b7507876c2b91d254 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  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. import sys
  17. import os
  18. from os.path import join
  19. import filecmp
  20. import textwrap
  21. import fnmatch
  22. if len(sys.argv) != 3:
  23. print >> sys.stderr, "Usage: %s COPY ORIGINAL" % sys.argv[0]
  24. sys.exit(1)
  25. copy = sys.argv[1]
  26. original = sys.argv[2]
  27. # Ignore detritus left lying around by editing tools.
  28. ignored_patterns = ['*~', '.#*', '#*#', '*.orig', '*.rej']
  29. # Return the contents of FILENAME, a 'check-sync-exceptions' file, as
  30. # a dictionary whose keys are exactly the list of filenames, along
  31. # with the basename of FILENAME itself. If FILENAME does not exist,
  32. # return the empty dictionary.
  33. def read_exceptions(filename):
  34. if (os.path.exists(filename)):
  35. f = file(filename)
  36. exceptions={}
  37. for line in f:
  38. line = line.strip()
  39. if line != '' and line[0] != '#':
  40. exceptions[line] = None
  41. exceptions[os.path.basename (filename)] = None
  42. f.close()
  43. return exceptions
  44. else:
  45. return {}
  46. # Return true if FILENAME matches any pattern in the list of filename
  47. # patterns PATTERNS.
  48. def fnmatch_any(filename, patterns):
  49. for pattern in patterns:
  50. if fnmatch.fnmatch(filename, pattern):
  51. return True
  52. return False
  53. # Check the contents of the directory tree COPY against ORIGINAL. For each
  54. # file that differs, apply REPORT to COPY, ORIGINAL, and the file's
  55. # relative path. COPY and ORIGINAL should be absolute. Ignore files
  56. # that match patterns given in the list IGNORE.
  57. def check(copy, original, ignore, report):
  58. os.chdir(copy)
  59. for (dirpath, dirnames, filenames) in os.walk('.'):
  60. exceptions = read_exceptions(join(dirpath, 'check-sync-exceptions'))
  61. for filename in filenames:
  62. if filename in exceptions:
  63. continue
  64. if fnmatch_any(filename, ignore):
  65. continue
  66. relative_name = join(dirpath, filename)
  67. original_name = join(original, relative_name)
  68. if (os.path.exists(original_name)
  69. and filecmp.cmp(relative_name, original_name)):
  70. continue
  71. report(copy, original, relative_name)
  72. differences_found = False
  73. # Print an error message for DIFFERING, which was found to differ
  74. # between COPY and ORIGINAL. Set the global variable differences_found.
  75. def report(copy, original, differing):
  76. global differences_found
  77. if not differences_found:
  78. print >> sys.stderr, "TEST-FAIL | build file copies are not in sync"
  79. print >> sys.stderr, "file(s) found in: %s" % (copy)
  80. print >> sys.stderr, ("differ from their originals in: %s"
  81. % (original))
  82. print >> sys.stderr, "file differs: %s" % (differing)
  83. differences_found = True
  84. check(os.path.abspath(copy),
  85. os.path.abspath(original),
  86. ignored_patterns,
  87. report)
  88. if differences_found:
  89. msg=('''In general, the files in '%s' should always be exact copies of
  90. originals in '%s'. A change made to one should also be made to the
  91. other. See 'check-sync-dirs.py' for more details.'''
  92. % (copy, original))
  93. print >> sys.stderr, textwrap.fill(msg, 75)
  94. sys.exit(1)
  95. sys.exit(0)