Home | History | Annotate | Download | only in build
      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 
      6 import base64
      7 import optparse
      8 import parse_deps
      9 import sys
     10 import os
     11 import re
     12 
     13 from generate_template_contents import generate_templates
     14 
     15 srcdir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../src"))
     16 
     17 js_warning_message = """/**
     18 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
     19 // Use of this source code is governed by a BSD-style license that can be
     20 // found in the LICENSE file.
     21 
     22 * WARNING: This file is generated by generate_standalone_timeline_view.py
     23 *
     24 *        Do not edit directly.
     25 */
     26 """
     27 
     28 css_warning_message = """/**
     29 /* Copyright (c) 2012 The Chromium Authors. All rights reserved.
     30  * Use of this source code is governed by a BSD-style license that can be
     31  * found in the LICENSE file. */
     32 
     33 * WARNING: This file is generated by generate_standalone_timeline_view.py
     34 *
     35 *        Do not edit directly.
     36 */
     37 """
     38 
     39 py_warning_message = """#!/usr/bin/env python
     40 #
     41 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
     42 # Use of this source code is governed by a BSD-style license that can be
     43 # found in the LICENSE file. */
     44 #
     45 # WARNING: This file is generated by generate_standalone_timeline_view.py
     46 #
     47 #        Do not edit directly.
     48 #
     49 """
     50 
     51 def _sopen(filename, mode):
     52   if filename != '-':
     53     return open(filename, mode)
     54   return os.fdopen(os.dup(sys.stdout.fileno()), 'w')
     55 
     56 def _get_input_filenames():
     57   return [os.path.join(srcdir, f)
     58           for f in ['base.js', 'tracing/standalone_timeline_view.js']]
     59 
     60 def generate_css():
     61   filenames = _get_input_filenames()
     62   load_sequence = parse_deps.calc_load_sequence(filenames, srcdir)
     63 
     64   style_sheet_chunks = [css_warning_message, '\n']
     65   for module in load_sequence:
     66     for style_sheet in module.style_sheets:
     67       style_sheet_chunks.append("""%s\n""" % style_sheet.contents)
     68 
     69   # Borrowed from grit html_format.py.
     70   def InlineUrl(m):
     71     filename = m.group('filename')
     72     idx = filename.index('/images')
     73     filename = "%s%s" % (srcdir, filename[idx:])
     74     ext = filename[filename.rindex('.') + 1:]
     75 
     76     with open(filename, 'rb') as f:
     77       data = f.read();
     78     data = base64.standard_b64encode(data)
     79 
     80     return "url(data:image/%s;base64,%s)" % (ext, data)
     81 
     82   full_style_sheet = ''.join(style_sheet_chunks)
     83   # I'm assuming we only have url()'s associated with images
     84   return re.sub('url\((?P<quote>"|\'|)(?P<filename>[^"\'()]*)(?P=quote)\)',
     85                 lambda m: InlineUrl(m),
     86                 full_style_sheet)
     87 
     88 
     89 def generate_js():
     90   filenames = _get_input_filenames()
     91   load_sequence = parse_deps.calc_load_sequence(filenames, srcdir)
     92 
     93 
     94   js_chunks = [js_warning_message, '\n']
     95   js_chunks.append("window.FLATTENED = {};\n")
     96   js_chunks.append("window.FLATTENED_RAW_SCRIPTS = {};\n")
     97 
     98   for module in load_sequence:
     99     for dependent_raw_script_name in module.dependent_raw_script_names:
    100       js_chunks.append("window.FLATTENED_RAW_SCRIPTS['%s'] = true;\n" %
    101         dependent_raw_script_name)
    102     js_chunks.append( "window.FLATTENED['%s'] = true;\n" % module.name)
    103 
    104   html_encoded = base64.b64encode(generate_templates())
    105   js_chunks.append("var templateData_ = window.atob('" +
    106                    html_encoded + "');\n");
    107   js_chunks.append("var templateElem_ = document.createElement('div');\n");
    108   js_chunks.append("templateElem_.innerHTML = templateData_;\n");
    109   js_chunks.append("while (templateElem_.hasChildNodes()) {\n");
    110   js_chunks.append("  document.head.appendChild(" +
    111                    "templateElem_.removeChild(templateElem_.firstChild));\n");
    112   js_chunks.append("}\n\n");
    113 
    114   for module in load_sequence:
    115     js_chunks.append(module.contents)
    116     js_chunks.append("\n")
    117 
    118   return ''.join(js_chunks)
    119 
    120 def main(args):
    121   parser = optparse.OptionParser(
    122     usage="%prog --js=<filename> --css=<filename>",
    123     epilog="""
    124 A script to takes all of the javascript and css files that comprise trace-viewer
    125 and merges them together into two giant js and css files, taking into account
    126 various ordering restrictions between them.
    127 """)
    128   parser.add_option("--js", dest="js_file",
    129                     help="Where to place generated javascript file")
    130   parser.add_option("--css", dest="css_file",
    131                     help="Where to place generated css file")
    132   options, args = parser.parse_args(args)
    133 
    134   if not options.js_file and not options.css_file:
    135     sys.stderr.write("ERROR: Must specify one of --js=<filename> or "
    136         "--css=<filename>\n\n")
    137     parser.print_help()
    138     return 1
    139 
    140   if options.js_file:
    141     with _sopen(options.js_file, 'w') as f:
    142       f.write(generate_js())
    143 
    144   if options.css_file:
    145     with _sopen(options.css_file, 'w') as f:
    146       f.write(generate_css())
    147 
    148   return 0
    149 
    150 if __name__ == "__main__":
    151   sys.exit(main(sys.argv))
    152