1# Copyright (c) 2014 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.
4
5import codecs
6import optparse
7import os
8import sys
9
10tracing_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
11                                            '..', '..'))
12if tracing_path not in sys.path:
13  sys.path.append(tracing_path)
14
15from tracing import tracing_project
16from tvcm import generate
17
18
19def Main(args):
20
21  parser = optparse.OptionParser(
22      usage="%prog <options>",
23      epilog="""Produces a standalone html import that contains the
24trace viewer.""")
25
26  project = tracing_project.TracingProject()
27  project.AddConfigNameOptionToParser(parser)
28
29  parser.add_option('--no-min', dest='no_min', default=False,
30                    action='store_true',
31                    help='skip minification')
32  parser.add_option('--report-sizes', dest='report_sizes', default=False,
33                    action='store_true',
34                    help='Explain what makes tracing big.')
35  parser.add_option('--report-deps', dest='report_deps', default=False,
36                    action='store_true',
37                    help='Print a dot-formatted deps graph.')
38  parser.add_option(
39      "--output", dest="output",
40      help='Where to put the generated result. If not ' +
41           'given, $TRACING/tracing/bin/trace_viewer.html is used.')
42
43  options, args = parser.parse_args(args)
44  if len(args) != 0:
45    parser.error('No arguments needed.')
46
47  tracing_dir = os.path.relpath(
48      os.path.join(os.path.dirname(__file__), '..', '..'))
49  if options.output:
50    output_filename = options.output
51  else:
52    output_filename = os.path.join(
53        tracing_dir, 'tracing/bin/trace_viewer_%s.html' % options.config_name)
54
55  with codecs.open(output_filename, 'w', encoding='utf-8') as f:
56    WriteTraceViewer(
57        f,
58        config_name=options.config_name,
59        minify=not options.no_min,
60        report_sizes=options.report_sizes,
61        report_deps=options.report_deps)
62
63  return 0
64
65
66def WriteTraceViewer(output_file,
67                     config_name=None,
68                     minify=False,
69                     report_sizes=False,
70                     report_deps=False,
71                     output_html_head_and_body=True,
72                     extra_search_paths=None,
73                     extra_module_names_to_load=None):
74  project = tracing_project.TracingProject()
75  if extra_search_paths:
76    for p in extra_search_paths:
77      project.source_paths.append(p)
78  if config_name is None:
79    config_name = project.GetDefaultConfigName()
80
81  module_names = [project.GetModuleNameForConfigName(config_name)]
82  if extra_module_names_to_load:
83    module_names += extra_module_names_to_load
84  load_sequence = project.CalcLoadSequenceForModuleNames(
85      module_names)
86
87  if report_deps:
88    sys.stdout.write(project.GetDepsGraphFromModuleNames(module_names))
89
90  generate.GenerateStandaloneHTMLToFile(
91      output_file, load_sequence,
92      minify=minify, report_sizes=report_sizes,
93      output_html_head_and_body=output_html_head_and_body)
94