James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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 |
James Robinson | 20c2417 | 2014-11-07 15:41:25 -0800 | [diff] [blame] | 12 | from mopy.paths import Paths |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 13 | |
James Robinson | 20c2417 | 2014-11-07 15:41:25 -0800 | [diff] [blame] | 14 | paths = Paths() |
| 15 | |
| 16 | sys.path.insert(0, os.path.join(paths.mojo_dir, "public", "tools", "bindings", |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 17 | "pylib")) |
| 18 | from mojom_tests.support.find_files import FindFiles |
| 19 | from mojom_tests.support.run_bindings_generator import RunBindingsGenerator |
| 20 | |
| 21 | |
| 22 | def _ProcessDircmpResults(results, verbose=False): |
| 23 | """Prints results of directory comparison and returns true if they are |
| 24 | identical (note: the "left" directory should be the golden directory).""" |
| 25 | rv = not (bool(results.left_only) or bool(results.right_only) or \ |
| 26 | bool(results.common_funny) or bool(results.funny_files) or \ |
| 27 | bool(results.diff_files)) |
| 28 | if verbose: |
| 29 | for f in results.left_only: |
| 30 | print "%s exists in golden directory but not in current output" % f |
| 31 | for f in results.right_only: |
| 32 | print "%s exists in current output but not in golden directory" % f |
| 33 | for f in results.common_funny + results.funny_files: |
| 34 | print "Unable to compare %s between golden directory and current output" \ |
| 35 | % f |
| 36 | for f in results.diff_files: |
| 37 | print "%s differs between golden directory and current output" % f |
| 38 | for r in results.subdirs.values(): |
| 39 | # If we're being verbose, check subdirectories even if we know that there |
| 40 | # are differences. Note that it's "... and rv" to avoid the short-circuit. |
| 41 | if rv or verbose: |
| 42 | rv = _ProcessDircmpResults(r, verbose=verbose) and rv |
| 43 | return rv |
| 44 | |
| 45 | |
| 46 | def main(): |
| 47 | parser = argparse.ArgumentParser() |
| 48 | parser.add_argument("--generate_golden_files", action="store_true", |
| 49 | help=("generate golden files (does not obliterate " |
| 50 | "directory")) |
| 51 | parser.add_argument("--keep_temp_dir", action="store_true", |
| 52 | help="don't delete the temporary directory") |
| 53 | parser.add_argument("--verbose", action="store_true", |
| 54 | help="spew excess verbiage") |
| 55 | parser.add_argument("golden_dir", metavar="GOLDEN_DIR", |
| 56 | help="directory with the golden files") |
| 57 | args = parser.parse_args() |
| 58 | |
| 59 | if args.generate_golden_files: |
| 60 | if os.path.exists(args.golden_dir): |
| 61 | print "WARNING: golden directory %s already exists" % args.golden_dir |
| 62 | out_dir = args.golden_dir |
| 63 | else: |
| 64 | if not os.path.exists(args.golden_dir): |
| 65 | print "ERROR: golden directory %s does not exist" % args.golden_dir |
| 66 | return 1 |
| 67 | out_dir = mkdtemp() |
| 68 | if args.verbose: |
| 69 | print "Generating files to %s ..." % out_dir |
| 70 | |
James Robinson | 20c2417 | 2014-11-07 15:41:25 -0800 | [diff] [blame] | 71 | mojom_files = FindFiles(paths.mojo_dir, "*.mojom") |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 72 | for mojom_file in mojom_files: |
| 73 | if args.verbose: |
James Robinson | 20c2417 | 2014-11-07 15:41:25 -0800 | [diff] [blame] | 74 | print " Processing %s ..." % os.path.relpath(mojom_file, paths.mojo_dir) |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 75 | # TODO(vtl): This may wrong, since the path can be overridden in the .gyp |
| 76 | # file. |
James Robinson | 20c2417 | 2014-11-07 15:41:25 -0800 | [diff] [blame] | 77 | RunBindingsGenerator(out_dir, paths.mojo_dir, mojom_file, |
| 78 | ["-I", paths.src_root]) |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 79 | |
| 80 | if args.generate_golden_files: |
| 81 | return 0 |
| 82 | |
| 83 | identical = _ProcessDircmpResults(dircmp(args.golden_dir, out_dir, ignore=[]), |
| 84 | verbose=args.verbose) |
| 85 | |
| 86 | if args.keep_temp_dir: |
| 87 | if args.verbose: |
| 88 | print "Not removing %s ..." % out_dir |
| 89 | else: |
| 90 | if args.verbose: |
| 91 | print "Removing %s ..." % out_dir |
| 92 | rmtree(out_dir) |
| 93 | |
| 94 | if not identical: |
| 95 | print "FAILURE: current output differs from golden files" |
| 96 | return 1 |
| 97 | |
| 98 | print "SUCCESS: current output identical to golden files" |
| 99 | return 0 |
| 100 | |
| 101 | |
| 102 | if __name__ == '__main__': |
| 103 | sys.exit(main()) |