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