OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 import argparse |
| 7 import os.path |
| 8 import sys |
| 9 from filecmp import dircmp |
| 10 from shutil import rmtree |
| 11 from tempfile import mkdtemp |
| 12 |
| 13 _script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 14 _mojo_dir = os.path.join(_script_dir, os.pardir) |
| 15 sys.path.insert(0, os.path.join(_mojo_dir, "public", "tools", "bindings", |
| 16 "pylib")) |
| 17 from mojom_tests.support.find_files import FindFiles |
| 18 from mojom_tests.support.run_bindings_generator import RunBindingsGenerator |
| 19 |
| 20 |
| 21 def _ProcessDircmpResults(results, verbose=False): |
| 22 """Prints results of directory comparison and returns true if they are |
| 23 identical (note: the "left" directory should be the golden directory).""" |
| 24 rv = not (bool(results.left_only) or bool(results.right_only) or \ |
| 25 bool(results.common_funny) or bool(results.funny_files) or \ |
| 26 bool(results.diff_files)) |
| 27 if verbose: |
| 28 for f in results.left_only: |
| 29 print "%s exists in golden directory but not in current output" % f |
| 30 for f in results.right_only: |
| 31 print "%s exists in current output but not in golden directory" % f |
| 32 for f in results.common_funny + results.funny_files: |
| 33 print "Unable to compare %s between golden directory and current output" \ |
| 34 % f |
| 35 for f in results.diff_files: |
| 36 print "%s differs between golden directory and current output" % f |
| 37 for r in results.subdirs.values(): |
| 38 # If we're being verbose, check subdirectories even if we know that there |
| 39 # are differences. Note that it's "... and rv" to avoid the short-circuit. |
| 40 if rv or verbose: |
| 41 rv = _ProcessDircmpResults(r, verbose=verbose) and rv |
| 42 return rv |
| 43 |
| 44 |
| 45 def main(): |
| 46 parser = argparse.ArgumentParser() |
| 47 parser.add_argument("--generate_golden_files", action="store_true", |
| 48 help=("generate golden files (does not obliterate " |
| 49 "directory")) |
| 50 parser.add_argument("--keep_temp_dir", action="store_true", |
| 51 help="don't delete the temporary directory") |
| 52 parser.add_argument("--verbose", action="store_true", |
| 53 help="spew excess verbiage") |
| 54 parser.add_argument("golden_dir", metavar="GOLDEN_DIR", |
| 55 help="directory with the golden files") |
| 56 args = parser.parse_args() |
| 57 |
| 58 if args.generate_golden_files: |
| 59 if os.path.exists(args.golden_dir): |
| 60 print "WARNING: golden directory %s already exists" % args.golden_dir |
| 61 out_dir = args.golden_dir |
| 62 else: |
| 63 if not os.path.exists(args.golden_dir): |
| 64 print "ERROR: golden directory %s does not exist" % args.golden_dir |
| 65 return 1 |
| 66 out_dir = mkdtemp() |
| 67 if args.verbose: |
| 68 print "Generating files to %s ..." % out_dir |
| 69 |
| 70 mojom_files = FindFiles(_mojo_dir, "*.mojom") |
| 71 for mojom_file in mojom_files: |
| 72 if args.verbose: |
| 73 print " Processing %s ..." % os.path.relpath(mojom_file, _mojo_dir) |
| 74 RunBindingsGenerator(out_dir, _mojo_dir, mojom_file) |
| 75 |
| 76 if args.generate_golden_files: |
| 77 return 0 |
| 78 |
| 79 identical = _ProcessDircmpResults(dircmp(args.golden_dir, out_dir, ignore=[]), |
| 80 verbose=args.verbose) |
| 81 |
| 82 if args.keep_temp_dir: |
| 83 if args.verbose: |
| 84 print "Not removing %s ..." % out_dir |
| 85 else: |
| 86 if args.verbose: |
| 87 print "Removing %s ..." % out_dir |
| 88 rmtree(out_dir) |
| 89 |
| 90 if not identical: |
| 91 print "FAILURE: current output differs from golden files" |
| 92 return 1 |
| 93 |
| 94 print "SUCCESS: current output identical to golden files" |
| 95 return 0 |
| 96 |
| 97 |
| 98 if __name__ == '__main__': |
| 99 sys.exit(main()) |
OLD | NEW |