1# Copyright (c) 2015 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4import argparse
5import sys
6
7from tracing.mre import corpus_driver_cmdline
8from tracing.mre import map_runner
9from tracing.mre import function_handle
10from tracing.mre import job as job_module
11from tracing.mre import json_output_formatter
12
13
14def Main(argv):
15  parser = argparse.ArgumentParser(
16      description='Bulk trace processing')
17  parser.add_argument('--map_function_handle')
18  parser.add_argument('-j', '--jobs', type=int,
19                      default=map_runner.AUTO_JOB_COUNT)
20  parser.add_argument('-o', '--output-file')
21  parser.add_argument('-s', '--stop-on-error',
22                      action='store_true')
23
24  if len(sys.argv) == 1:
25    parser.print_help()
26    sys.exit(0)
27
28  args = parser.parse_args(argv[1:])
29
30  corpus_driver = corpus_driver_cmdline.GetCorpusDriver(parser, args)
31
32  if args.output_file:
33    ofile = open(args.output_file, 'w')
34  else:
35    ofile = sys.stdout
36
37  output_formatter = json_output_formatter.JSONOutputFormatter(ofile)
38
39  try:
40    map_handle = None
41    if args.map_function_handle:
42      map_handle = function_handle.FunctionHandle.FromUserFriendlyString(
43          args.map_function_handle)
44    job = job_module.Job(map_handle)
45  except function_handle.UserFriendlyStringInvalidError:
46    error_lines = [
47        'The map_traces command-line API has changed! You must now specify the',
48        'filenames to load and the map function name, separated by :. For '
49        'example, a mapper in',
50        'foo.html called Foo would be written as foo.html:Foo .'
51    ]
52    parser.error('\n'.join(error_lines))
53
54  try:
55    trace_handles = corpus_driver.GetTraceHandles()
56    runner = map_runner.MapRunner(trace_handles, job,
57                                  stop_on_error=args.stop_on_error,
58                                  jobs=args.jobs,
59                                  output_formatters=[output_formatter])
60    results = runner.Run()
61    if not any(result.failures for result in results):
62      return 0
63    else:
64      return 255
65  finally:
66    if ofile != sys.stdout:
67      ofile.close()
68