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