• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 os
7import sys
8import parse_deps
9import StringIO
10
11srcdir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../src"))
12
13def flatten_module_contents(filenames):
14  out = StringIO.StringIO()
15  load_sequence = parse_deps.calc_load_sequence(filenames, srcdir)
16
17  flattened_module_names = ["'%s'" % module.name for module in load_sequence]
18  out.write("    if (!window.FLATTENED) window.FLATTENED = {};\n")
19  for module in load_sequence:
20    out.write("    window.FLATTENED['%s'] = true;\n" % module.name);
21
22  for module in load_sequence:
23    out.write(module.contents)
24    if module.contents[-1] != '\n':
25      out.write('\n')
26  return out.getvalue()
27
28def flatten_style_sheet_contents(filenames):
29  out = StringIO.StringIO()
30  load_sequence = parse_deps.calc_load_sequence(filenames, srcdir)
31
32  # Stylesheets should be sourced from topmsot in, not inner-out.
33  load_sequence.reverse()
34
35  for module in load_sequence:
36    for style_sheet in module.style_sheets:
37      out.write(style_sheet.contents)
38      if style_sheet.contents[-1] != '\n':
39        out.write('\n')
40  return out.getvalue()
41
42def main(argv):
43  parser = optparse.OptionParser(usage="flatten filename1.js [filename2.js ...]",
44                                 epilog="""
45This is a low-level flattening tool. You probably are meaning to run
46generate_standalone_timeline_view.py
47""")
48
49  parser.add_option("--css", dest="flatten_css", action="store_true", help="Outputs a flattened stylesheet.")
50  options, args = parser.parse_args(argv[1:])
51
52  if len(args) == 0:
53    sys.stderr.write("Expected: filename or filenames to flatten\n")
54    return 255
55
56  if options.flatten_css:
57    sys.stdout.write(flatten_style_sheet_contents(args))
58  else:
59    sys.stdout.write(flatten_module_contents(args))
60  return 0
61
62if __name__ == "__main__":
63  sys.exit(main(sys.argv))
64