Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # Copyright 2016 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 argparse | |
| 7 import itertools | |
| 8 import os | |
| 9 import re | |
| 10 import subprocess | |
| 11 import sys | |
| 12 import tempfile | |
| 13 | |
| 14 _HERE_PATH = os.path.join(os.path.dirname(__file__)) | |
| 15 _SRC_PATH = os.path.normpath(os.path.join(_HERE_PATH, '..', '..', '..')) | |
| 16 | |
| 17 sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'node')) | |
| 18 import node | |
| 19 import node_modules | |
| 20 | |
| 21 _NODE_BINARY = node.GetBinaryPath() | |
| 22 | |
| 23 | |
| 24 _RESOURCES_PATH = os.path.join(_SRC_PATH, 'ui', 'webui', 'resources') | |
| 25 | |
| 26 | |
| 27 _CR_ELEMENTS_PATH = os.path.join(_RESOURCES_PATH, 'cr_elements') | |
| 28 | |
| 29 | |
| 30 _CSS_RESOURCES_PATH = os.path.join(_RESOURCES_PATH, 'css') | |
| 31 | |
| 32 | |
| 33 _HTML_RESOURCES_PATH = os.path.join(_RESOURCES_PATH, 'html') | |
| 34 | |
| 35 | |
| 36 _JS_RESOURCES_PATH = os.path.join(_RESOURCES_PATH, 'js') | |
| 37 | |
| 38 | |
| 39 _POLYMER_PATH = os.path.join( | |
| 40 _SRC_PATH, 'third_party', 'polymer', 'v1_0', 'components-chromium') | |
| 41 | |
| 42 | |
| 43 _VULCANIZE_BASE_ARGS = [ | |
| 44 '--exclude', 'crisper.js', | |
| 45 | |
| 46 # These files are already combined and minified. | |
| 47 '--exclude', 'chrome://resources/html/polymer.html', | |
| 48 '--exclude', 'web-animations-next-lite.min.js', | |
| 49 | |
| 50 # These files are dynamically created by C++. | |
| 51 '--exclude', 'load_time_data.js', | |
| 52 '--exclude', 'strings.js', | |
| 53 '--exclude', 'text_defaults.css', | |
| 54 '--exclude', 'text_defaults_md.css', | |
| 55 | |
| 56 '--inline-css', | |
| 57 '--inline-scripts', | |
| 58 '--strip-comments', | |
| 59 ] | |
| 60 | |
| 61 | |
| 62 _URL_MAPPINGS = [ | |
| 63 ('chrome://resources/cr_elements/', _CR_ELEMENTS_PATH), | |
| 64 ('chrome://resources/css/', _CSS_RESOURCES_PATH), | |
| 65 ('chrome://resources/html/', _HTML_RESOURCES_PATH), | |
| 66 ('chrome://resources/js/', _JS_RESOURCES_PATH), | |
| 67 ('chrome://resources/polymer/v1_0/', _POLYMER_PATH) | |
| 68 ] | |
| 69 | |
| 70 | |
| 71 _VULCANIZE_REDIRECT_ARGS = list(itertools.chain.from_iterable(map( | |
| 72 lambda m: ['--redirect', '%s|%s' % (m[0], m[1])], _URL_MAPPINGS))) | |
|
Dan Beam
2017/01/24 02:59:43
everything with a | char needs to be quoted with d
dpapad
2017/01/24 18:36:12
Done.
| |
| 73 | |
| 74 | |
| 75 _REQUEST_LIST_FILE = 'request_list.txt' | |
| 76 | |
| 77 | |
| 78 _PAK_UNPACK_FOLDER = 'flattened' | |
| 79 | |
| 80 | |
| 81 def _run_cmd(cmd_parts, stdout=None): | |
| 82 cmd = "'" + "' '".join(cmd_parts) + "'" | |
|
Dan Beam
2017/01/24 02:59:43
this needs to be
cmd = ' '.join(cmd_parts)
as in
dpapad
2017/01/24 18:36:12
Done.
| |
| 83 process = subprocess.Popen( | |
| 84 cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) | |
| 85 stdout, stderr = process.communicate() | |
| 86 | |
| 87 if stderr: | |
| 88 print >> sys.stderr, '%s failed: %s' % (cmd, stderr) | |
| 89 raise | |
| 90 | |
| 91 return stdout | |
| 92 | |
| 93 | |
| 94 def _undo_mapping(mappings, url): | |
| 95 for (redirect_url, file_path) in mappings: | |
| 96 if url.startswith(redirect_url): | |
| 97 return url.replace(redirect_url, file_path + '/') | |
|
Dan Beam
2017/01/24 02:02:31
maybe use os.sep instead?
dpapad
2017/01/24 18:36:12
Done.
| |
| 98 return url | |
| 99 | |
| 100 | |
| 101 # Get a list of all files that were bundled with Vulcanize and update the | |
| 102 # depfile accordingly such that Ninja knows when to trigger re-vulcanization. | |
| 103 def _update_dep_file(in_folder, args): | |
| 104 in_path = os.path.join(os.getcwd(), in_folder) | |
| 105 out_path = os.path.join(os.getcwd(), args.out_folder) | |
| 106 | |
| 107 # Prior call to vulcanize already generated the deps list, grab it from there. | |
| 108 request_list = open(os.path.join( | |
| 109 out_path, _REQUEST_LIST_FILE), 'r').read().splitlines() | |
| 110 | |
| 111 # Undo the URL mappings applied by vulcanize to get file paths relative to | |
| 112 # current working directory. | |
| 113 url_mappings = _URL_MAPPINGS + [ | |
| 114 ('/', os.path.relpath(in_path, os.getcwd())), | |
| 115 ('chrome://%s/' % args.host, os.path.relpath(in_path, os.getcwd())), | |
| 116 ] | |
| 117 | |
| 118 dependencies = map( | |
| 119 lambda url: _undo_mapping(url_mappings, url), request_list) | |
| 120 | |
| 121 # If the input was a .pak file, the generated depfile should not list files | |
| 122 # already in the .pak file. | |
| 123 filtered_dependencies = dependencies | |
| 124 if (args.input_type == 'PAK_FILE'): | |
| 125 filter_url = os.path.join(args.out_folder, _PAK_UNPACK_FOLDER) | |
| 126 filtered_dependencies = filter( | |
| 127 lambda url: not url.startswith(filter_url), dependencies) | |
| 128 | |
| 129 with open(os.path.join(os.getcwd(), args.depfile), 'w') as f: | |
| 130 f.write(os.path.join( | |
| 131 args.out_folder, args.html_out_file) + ': ' + ' '.join( | |
| 132 filtered_dependencies)) | |
| 133 | |
| 134 | |
| 135 def _vulcanize(in_folder, out_folder, host, html_in_file, | |
| 136 html_out_file, js_out_file): | |
| 137 in_path = os.path.join(os.getcwd(), in_folder) | |
| 138 out_path = os.path.join(os.getcwd(), out_folder) | |
| 139 | |
| 140 html_out_path = os.path.join(out_path, html_out_file) | |
| 141 js_out_path = os.path.join(out_path, js_out_file) | |
| 142 | |
| 143 output = _run_cmd( | |
| 144 [_NODE_BINARY, node_modules.PathToVulcanize()] + | |
|
Dan Beam
2017/01/24 02:59:43
just change run_cmd to run_node and move _NODE_BIN
dpapad
2017/01/24 18:36:12
Done.
| |
| 145 _VULCANIZE_BASE_ARGS + _VULCANIZE_REDIRECT_ARGS + | |
| 146 ['--out-request-list', os.path.join(out_path, _REQUEST_LIST_FILE), | |
| 147 '--redirect', '/|%s' % in_path, | |
|
Dan Beam
2017/01/24 02:59:43
'--redirect', '"/|%s"' % in_path,
^
dpapad
2017/01/24 18:36:12
Done.
| |
| 148 '--redirect', 'chrome://%s/|%s' % (host, in_path), | |
|
Dan Beam
2017/01/24 02:59:43
'--redirect', '"chrome://%s/|%s"' % (host, in_path
dpapad
2017/01/24 18:36:12
Done.
| |
| 149 os.path.join('/', html_in_file)]) | |
|
Dan Beam
2017/01/24 02:02:31
might want to check / use as well here
dpapad
2017/01/24 18:36:12
Done.
| |
| 150 | |
| 151 with tempfile.NamedTemporaryFile(mode='wt+', delete=False) as tmp: | |
| 152 # Grit includes are not supported, use HTML imports instead. | |
| 153 tmp.write(output.replace( | |
| 154 '<include src="', '<include src-disabled="')) | |
| 155 | |
| 156 try: | |
| 157 _run_cmd([_NODE_BINARY, node_modules.PathToCrisper(), | |
| 158 '--source', tmp.name, | |
| 159 '--script-in-head', 'false', | |
| 160 '--html', html_out_path, | |
| 161 '--js', js_out_path]) | |
| 162 | |
| 163 # TODO(tsergeant): Remove when JS resources are minified by default: | |
| 164 # crbug.com/619091. | |
| 165 _run_cmd([_NODE_BINARY, node_modules.PathToUglifyJs(), js_out_path, | |
| 166 '--comments', '/Copyright|license|LICENSE|\<\/?if/', | |
|
Dan Beam
2017/01/24 02:59:43
'--comments', '"/Copyright|license|LICENSE|\<\/?if
dpapad
2017/01/24 18:36:12
Done.
| |
| 167 '--output', js_out_path]) | |
| 168 finally: | |
| 169 os.remove(tmp.name) | |
| 170 | |
| 171 | |
| 172 def _css_build(out_folder, files): | |
| 173 out_path = os.path.join(os.getcwd(), out_folder) | |
| 174 paths = map(lambda f: os.path.join(out_path, f), files) | |
| 175 | |
| 176 _run_cmd([_NODE_BINARY, node_modules.PathToPolymerCssBuild()] + paths) | |
| 177 | |
| 178 | |
| 179 def main(): | |
| 180 parser = argparse.ArgumentParser() | |
| 181 parser.add_argument('--depfile') | |
| 182 parser.add_argument('--host') | |
| 183 parser.add_argument('--html_in_file') | |
| 184 parser.add_argument('--html_out_file') | |
| 185 parser.add_argument('--input') | |
| 186 parser.add_argument('--input_type') | |
| 187 parser.add_argument('--js_out_file') | |
| 188 parser.add_argument('--out_folder') | |
| 189 args = parser.parse_args() | |
| 190 | |
| 191 vulcanize_input_folder = args.input | |
| 192 | |
| 193 # If a .pak file was specified, unpack that file first and pass the output to | |
| 194 # vulcanize. | |
| 195 if (args.input_type == 'PAK_FILE'): | |
| 196 import unpack_pak | |
| 197 input_folder = os.path.join(os.getcwd(), args.input) | |
| 198 output_folder = os.path.join(args.out_folder, _PAK_UNPACK_FOLDER) | |
| 199 unpack_pak.unpack(args.input, output_folder) | |
| 200 vulcanize_input_folder = output_folder | |
| 201 | |
| 202 _vulcanize(vulcanize_input_folder, args.out_folder, args.host, | |
| 203 args.html_in_file, args.html_out_file, args.js_out_file) | |
| 204 _css_build(args.out_folder, files=[args.html_out_file]) | |
| 205 | |
| 206 _update_dep_file(vulcanize_input_folder, args) | |
| 207 | |
| 208 | |
| 209 if __name__ == '__main__': | |
| 210 main() | |
| OLD | NEW |