1#!/usr/bin/env python
2# Copyright (c) 2012 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
6import errno
7import optparse
8import parse_deps
9import sys
10import os
11
12import generate_template_contents as template_generator
13from generate_template_contents import generate_templates
14
15srcdir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../src"))
16
17html_warning_message = """
18
19
20<!------------------------------------------------------------------------------
21WARNING: This file is generated by generate_about_tracing_contents.py
22
23         Do not edit directly.
24
25
26------------------------------------------------------------------------------->
27
28
29"""
30
31js_warning_message = """/**
32// Copyright (c) 2013 The Chromium Authors. All rights reserved.
33// Use of this source code is governed by a BSD-style license that can be
34// found in the LICENSE file.
35
36* WARNING: This file is generated by generate_about_tracing_contents.py
37*
38*        Do not edit directly.
39*/
40"""
41
42def generate_html(outdir):
43  f = open(os.path.join(srcdir, "about_tracing.html.template"), 'r')
44  template = f.read()
45  f.close()
46
47  assert template.find("<WARNING_MESSAGE></WARNING_MESSAGE>") != -1
48  assert template.find("<STYLE_SHEET_CONTENTS></STYLE_SHEET_CONTENTS>") != -1
49  assert template.find("<TEMPLATE_CONTENTS></TEMPLATE_CONTENTS>") != -1
50
51  filenames = [os.path.join(srcdir, x) for x in ["base.js", "about_tracing/profiling_view.js"]]
52
53  load_sequence = parse_deps.calc_load_sequence(filenames, srcdir)
54
55  style_sheet_contents = ""
56  for module in load_sequence:
57    for style_sheet in module.style_sheets:
58      rel_filename = os.path.relpath(style_sheet.filename, outdir)
59      link_tag = """<link rel="stylesheet" href="%s">\n""" % rel_filename
60      style_sheet_contents += link_tag
61
62  template_contents = generate_templates()
63
64  result = template
65  result = result.replace("<WARNING_MESSAGE></WARNING_MESSAGE>",
66      html_warning_message)
67  result = result.replace("<STYLE_SHEET_CONTENTS></STYLE_SHEET_CONTENTS>",
68      style_sheet_contents)
69  result = result.replace("<TEMPLATE_CONTENTS></TEMPLATE_CONTENTS>",
70      template_contents)
71
72  return result
73
74def generate_js(outdir):
75  f = open(os.path.join(srcdir, "about_tracing.js.template"), 'r')
76  template = f.read()
77  f.close()
78
79  assert template.find("<WARNING_MESSAGE></WARNING_MESSAGE>") != -1
80  assert template.find("<SCRIPT_CONTENTS></SCRIPT_CONTENTS>") != -1
81
82  filenames = [os.path.join(srcdir, x) for x in ["base.js", "about_tracing/profiling_view.js"]]
83
84  import parse_deps
85  load_sequence = parse_deps.calc_load_sequence(filenames, srcdir)
86  script_contents = ""
87  script_contents += "window.FLATTENED = {};\n"
88  script_contents += "window.FLATTENED_RAW_SCRIPTS = {};\n"
89  for module in load_sequence:
90    for dependent_raw_script_name in module.dependent_raw_script_names:
91      script_contents += (
92        "window.FLATTENED_RAW_SCRIPTS['%s'] = true;\n" %
93        dependent_raw_script_name)
94    script_contents += "window.FLATTENED['%s'] = true;\n" % module.name
95
96  for module in load_sequence:
97    for dependent_raw_script in module.dependent_raw_scripts:
98      rel_filename = os.path.relpath(dependent_raw_script.filename, outdir)
99      script_contents += """<include src="%s">\n""" % rel_filename
100
101    rel_filename = os.path.relpath(module.filename, outdir)
102    script_contents += """<include src="%s">\n""" % rel_filename
103
104
105  result = template
106  result = result.replace("<WARNING_MESSAGE></WARNING_MESSAGE>",
107                          js_warning_message)
108  result = result.replace("<SCRIPT_CONTENTS></SCRIPT_CONTENTS>", script_contents)
109
110  return result
111
112def main(args):
113  parser = optparse.OptionParser(usage="%prog --outdir=<directory>")
114  parser.add_option("--outdir", dest="out_dir",
115                    help="Where to place generated content")
116  options, args = parser.parse_args(args)
117
118  if not options.out_dir:
119    sys.stderr.write("ERROR: Must specify --outdir=<directory>")
120    parser.print_help()
121    return 1
122
123  olddir = os.getcwd()
124  try:
125    try:
126      result_html = generate_html(options.out_dir)
127    except parse_deps.DepsException, ex:
128      sys.stderr.write("Error: %s\n\n" % str(ex))
129      return 255
130
131    o = open(os.path.join(options.out_dir, "about_tracing.html"), 'w')
132    o.write(result_html)
133    o.close()
134
135    result_js = generate_js(options.out_dir)
136    o = open(os.path.join(options.out_dir, "about_tracing.js"), 'w')
137    o.write(result_js)
138    o.close()
139
140  finally:
141    os.chdir(olddir)
142
143  return 0
144
145if __name__ == "__main__":
146  sys.exit(main(sys.argv))
147