Fix a type mismatch on Windows caused by r201738.
[chromium-blink-merge.git] / tools / cc-frame-viewer / ccfv.py
blob0be2db2efc5096fbb77f69461080e7c7bd259e7d
1 # Copyright (c) 2012 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 import optparse
5 import os
6 import sys
8 def Init():
9 chromeapp_path = os.path.abspath(
10 os.path.join(os.path.dirname(__file__),
11 'third_party', 'py-chrome-app'))
12 assert os.path.isdir(chromeapp_path)
13 sys.path.append(chromeapp_path)
14 Init()
16 import chromeapp
17 from build import parse_deps
19 srcdir = os.path.abspath(os.path.join(os.path.dirname(__file__), "src"))
21 js_warning_message = """/**
22 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
23 // Use of this source code is governed by a BSD-style license that can be
24 // found in the LICENSE file.
26 /* WARNING: This file is generated by ccfv.py
28 * Do not edit directly.
30 """
32 css_warning_message = """/**
33 /* Copyright (c) 2012 The Chromium Authors. All rights reserved.
34 * Use of this source code is governed by a BSD-style license that can be
35 * found in the LICENSE file. */
37 /* WARNING: This file is generated by ccfv.py
39 * Do not edit directly.
41 """
43 py_warning_message = """#!/usr/bin/env python
45 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
46 # Use of this source code is governed by a BSD-style license that can be
47 # found in the LICENSE file. */
49 # WARNING: This file is generated by ccfv.py
51 # Do not edit directly.
53 """
55 def _sopen(filename, mode):
56 if filename != '-':
57 return open(filename, mode)
58 return os.fdopen(os.dup(sys.stdout.fileno()), 'w')
60 def generate_css(input_filenames):
61 load_sequence = parse_deps.calc_load_sequence(input_filenames, srcdir)
63 style_sheet_chunks = [css_warning_message, '\n']
64 for module in load_sequence:
65 for style_sheet in module.style_sheets:
66 style_sheet_chunks.append("""%s\n""" % style_sheet.contents)
68 return ''.join(style_sheet_chunks)
70 def generate_js(input_filenames):
71 load_sequence = parse_deps.calc_load_sequence(input_filenames, srcdir)
73 js_chunks = [js_warning_message, '\n']
74 js_chunks.append("window.FLATTENED = {};\n")
75 js_chunks.append("window.FLATTENED_RAW_SCRIPTS = {};\n")
77 for module in load_sequence:
78 js_chunks.append("window.FLATTENED['%s'] = true;\n" % module.name)
79 for raw_script in module.dependent_raw_scripts:
80 js_chunks.append("window.FLATTENED_RAW_SCRIPTS['%s'] = true;\n" %
81 raw_script.name)
83 raw_scripts_that_have_been_written = set()
84 for module in load_sequence:
85 for raw_script in module.dependent_raw_scripts:
86 if raw_script.name in raw_scripts_that_have_been_written:
87 continue
88 js_chunks.append(raw_script.contents)
89 js_chunks.append("\n")
90 raw_scripts_that_have_been_written.add(raw_script.name)
91 js_chunks.append(module.contents)
92 js_chunks.append("\n")
94 return ''.join(js_chunks)
96 def Main(args):
97 parser = optparse.OptionParser('%prog <filename>')
98 parser.add_option('--debug', dest='debug_mode', action='store_true',
99 default=False, help='Enables debugging features')
100 options, args = parser.parse_args(args)
101 if len(args) != 1:
102 parser.error("argument required")
103 if not os.path.exists(args[0]):
104 parser.error("%s does not exist" % args[0])
106 manifest_file = os.path.join(os.path.dirname(__file__),
107 'app', 'manifest.json')
108 app = chromeapp.App('cc-frame-viewer',
109 manifest_file,
110 debug_mode=options.debug_mode)
112 def OnLoad(req):
113 with open(args[0], 'r') as f:
114 return f.read()
116 input_filenames = [os.path.join(srcdir, f)
117 for f in ['base.js', 'model_view.js']]
118 view_js_file = os.path.join(os.path.dirname(__file__),
119 'app', 'model_view.js')
120 view_css_file = os.path.join(os.path.dirname(__file__),
121 'app', 'model_view.css')
122 with open(view_js_file, 'w') as f:
123 f.write(generate_js(input_filenames))
124 with open(view_css_file, 'w') as f:
125 f.write(generate_css(input_filenames))
127 with chromeapp.AppInstance(app, []) as app_instance:
128 app_instance.AddListener('load', OnLoad)
129 try:
130 return app_instance.Run()
131 finally:
132 if os.path.exists(view_js_file):
133 os.unlink(view_js_file)
134 if os.path.exists(view_css_file):
135 os.unlink(view_css_file)
138 if __name__ == "__main__":
139 sys.exit(Main(sys.argv[1:]))