Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import argparse | 6 import argparse |
| 7 import itertools | 7 import itertools |
| 8 import os | 8 import os |
| 9 import platform | 9 import platform |
| 10 import re | 10 import re |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 35 | 35 |
| 36 | 36 |
| 37 _JS_RESOURCES_PATH = os.path.join(_RESOURCES_PATH, 'js') | 37 _JS_RESOURCES_PATH = os.path.join(_RESOURCES_PATH, 'js') |
| 38 | 38 |
| 39 | 39 |
| 40 _POLYMER_PATH = os.path.join( | 40 _POLYMER_PATH = os.path.join( |
| 41 _SRC_PATH, 'third_party', 'polymer', 'v1_0', 'components-chromium') | 41 _SRC_PATH, 'third_party', 'polymer', 'v1_0', 'components-chromium') |
| 42 | 42 |
| 43 | 43 |
| 44 _VULCANIZE_BASE_ARGS = [ | 44 _VULCANIZE_BASE_ARGS = [ |
| 45 '--exclude', 'crisper.js', | |
| 46 | |
| 47 # These files are already combined and minified. | 45 # These files are already combined and minified. |
| 48 '--exclude', 'chrome://resources/html/polymer.html', | 46 '--exclude', 'chrome://resources/html/polymer.html', |
| 49 '--exclude', 'web-animations-next-lite.min.js', | 47 '--exclude', 'web-animations-next-lite.min.js', |
| 50 | 48 |
| 51 # These files are dynamically created by C++. | 49 # These files are dynamically created by C++. |
| 52 '--exclude', 'load_time_data.js', | 50 '--exclude', 'load_time_data.js', |
| 53 '--exclude', 'strings.js', | 51 '--exclude', 'strings.js', |
| 54 '--exclude', 'text_defaults.css', | 52 '--exclude', 'text_defaults.css', |
| 55 '--exclude', 'text_defaults_md.css', | 53 '--exclude', 'text_defaults_md.css', |
| 56 | 54 |
| 57 '--inline-css', | 55 '--inline-css', |
| 58 '--inline-scripts', | 56 '--inline-scripts', |
| 59 '--strip-comments', | 57 '--strip-comments', |
| 60 ] | 58 ] |
| 61 | 59 |
| 62 | 60 |
| 63 _URL_MAPPINGS = [ | 61 _URL_MAPPINGS = [ |
| 64 ('chrome://resources/cr_elements/', _CR_ELEMENTS_PATH), | 62 ('chrome://resources/cr_elements/', _CR_ELEMENTS_PATH), |
| 65 ('chrome://resources/css/', _CSS_RESOURCES_PATH), | 63 ('chrome://resources/css/', _CSS_RESOURCES_PATH), |
| 66 ('chrome://resources/html/', _HTML_RESOURCES_PATH), | 64 ('chrome://resources/html/', _HTML_RESOURCES_PATH), |
| 67 ('chrome://resources/js/', _JS_RESOURCES_PATH), | 65 ('chrome://resources/js/', _JS_RESOURCES_PATH), |
| 68 ('chrome://resources/polymer/v1_0/', _POLYMER_PATH) | 66 ('chrome://resources/polymer/v1_0/', _POLYMER_PATH) |
| 69 ] | 67 ] |
| 70 | 68 |
| 71 | 69 |
| 72 _VULCANIZE_REDIRECT_ARGS = list(itertools.chain.from_iterable(map( | 70 _VULCANIZE_REDIRECT_ARGS = list(itertools.chain.from_iterable(map( |
| 73 lambda m: ['--redirect', '"%s|%s"' % (m[0], m[1])], _URL_MAPPINGS))) | 71 lambda m: ['--redirect', '"%s|%s"' % (m[0], m[1])], _URL_MAPPINGS))) |
| 74 | 72 |
| 75 | 73 |
| 76 _REQUEST_LIST_FILE = 'request_list.txt' | |
| 77 | |
| 78 | |
| 79 _PAK_UNPACK_FOLDER = 'flattened' | 74 _PAK_UNPACK_FOLDER = 'flattened' |
| 80 | 75 |
| 81 | 76 |
| 82 def _run_node(cmd_parts, stdout=None): | 77 def _run_node(cmd_parts, stdout=None): |
| 83 cmd = " ".join([node.GetBinaryPath()] + cmd_parts) | 78 cmd = " ".join([node.GetBinaryPath()] + cmd_parts) |
| 84 process = subprocess.Popen( | 79 process = subprocess.Popen( |
| 85 cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) | 80 cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) |
| 86 stdout, stderr = process.communicate() | 81 stdout, stderr = process.communicate() |
| 87 | 82 |
| 88 if stderr: | 83 if stderr: |
| 89 print >> sys.stderr, '%s failed: %s' % (cmd, stderr) | 84 print >> sys.stderr, '%s failed: %s' % (cmd, stderr) |
| 90 raise | 85 raise |
| 91 | 86 |
| 92 return stdout | 87 return stdout |
| 93 | 88 |
| 94 | 89 |
| 95 def _undo_mapping(mappings, url): | 90 def _undo_mapping(mappings, url): |
| 96 for (redirect_url, file_path) in mappings: | 91 for (redirect_url, file_path) in mappings: |
| 97 if url.startswith(redirect_url): | 92 if url.startswith(redirect_url): |
| 98 return url.replace(redirect_url, file_path + os.sep) | 93 return url.replace(redirect_url, file_path + os.sep) |
| 99 # TODO(dbeam): can we make this stricter? | 94 # TODO(dbeam): can we make this stricter? |
| 100 return url | 95 return url |
| 101 | 96 |
| 97 def _request_list_path(out_path, html_out_file): | |
| 98 return os.path.join(out_path, html_out_file + '_requestlist.txt') | |
| 102 | 99 |
| 103 # Get a list of all files that were bundled with Vulcanize and update the | 100 # Get a list of all files that were bundled with Vulcanize and update the |
| 104 # depfile accordingly such that Ninja knows when to trigger re-vulcanization. | 101 # depfile accordingly such that Ninja knows when to trigger re-vulcanization. |
| 105 def _update_dep_file(in_folder, args): | 102 def _update_dep_file(in_folder, args): |
| 106 in_path = os.path.join(_CWD, in_folder) | 103 in_path = os.path.join(_CWD, in_folder) |
| 107 out_path = os.path.join(_CWD, args.out_folder) | 104 out_path = os.path.join(_CWD, args.out_folder) |
| 108 | 105 |
| 109 # Prior call to vulcanize already generated the deps list, grab it from there. | 106 # Prior call to vulcanize already generated the deps list, grab it from there. |
| 110 request_list_path = os.path.join(out_path, _REQUEST_LIST_FILE) | 107 request_list_path = _request_list_path(out_path, args.html_out_file) |
| 111 request_list = open(request_list_path, 'r').read().splitlines() | 108 request_list = open(request_list_path, 'r').read().splitlines() |
| 112 | 109 |
| 113 if platform.system() == 'Windows': | 110 if platform.system() == 'Windows': |
| 114 # TODO(dbeam): UGH. For some reason Vulcanize is interpreting the target | 111 # TODO(dbeam): UGH. For some reason Vulcanize is interpreting the target |
| 115 # file path as a URL and using the drive letter (e.g. D:\) as a protocol. | 112 # file path as a URL and using the drive letter (e.g. D:\) as a protocol. |
| 116 # This is a little insane, but we're fixing here by normalizing case (which | 113 # This is a little insane, but we're fixing here by normalizing case (which |
| 117 # really shouldn't matter, these are all file paths and generally are all | 114 # really shouldn't matter, these are all file paths and generally are all |
| 118 # lower case) and writing from / to \ (file path) and then back again. This | 115 # lower case) and writing from / to \ (file path) and then back again. This |
| 119 # is compounded by NodeJS having a bug in url.resolve() that handles | 116 # is compounded by NodeJS having a bug in url.resolve() that handles |
| 120 # chrome:// protocol URLs poorly as well as us using startswith() to strip | 117 # chrome:// protocol URLs poorly as well as us using startswith() to strip |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 145 f.write(deps_file_header + ': ' + ' '.join(deps)) | 142 f.write(deps_file_header + ': ' + ' '.join(deps)) |
| 146 | 143 |
| 147 | 144 |
| 148 def _vulcanize(in_folder, args): | 145 def _vulcanize(in_folder, args): |
| 149 in_path = os.path.normpath(os.path.join(_CWD, in_folder)) | 146 in_path = os.path.normpath(os.path.join(_CWD, in_folder)) |
| 150 out_path = os.path.join(_CWD, args.out_folder) | 147 out_path = os.path.join(_CWD, args.out_folder) |
| 151 | 148 |
| 152 html_out_path = os.path.join(out_path, args.html_out_file) | 149 html_out_path = os.path.join(out_path, args.html_out_file) |
| 153 js_out_path = os.path.join(out_path, args.js_out_file) | 150 js_out_path = os.path.join(out_path, args.js_out_file) |
| 154 | 151 |
| 152 exclude_args = [] | |
| 153 for f in args.exclude or []: | |
| 154 exclude_args.append('--exclude') | |
| 155 exclude_args.append(f) | |
| 156 | |
| 155 output = _run_node( | 157 output = _run_node( |
| 156 [node_modules.PathToVulcanize()] + | 158 [node_modules.PathToVulcanize()] + |
| 157 _VULCANIZE_BASE_ARGS + _VULCANIZE_REDIRECT_ARGS + | 159 _VULCANIZE_BASE_ARGS + _VULCANIZE_REDIRECT_ARGS + exclude_args + |
| 158 ['--out-request-list', os.path.join(out_path, _REQUEST_LIST_FILE), | 160 ['--out-request-list', _request_list_path(out_path, args.html_out_file), |
| 159 '--redirect', '"/|%s"' % in_path, | 161 '--redirect', '"/|%s"' % in_path, |
| 160 '--redirect', '"chrome://%s/|%s"' % (args.host, in_path), | 162 '--redirect', '"chrome://%s/|%s"' % (args.host, in_path), |
| 161 # TODO(dpapad): Figure out why vulcanize treats the input path | 163 # TODO(dpapad): Figure out why vulcanize treats the input path |
| 162 # differently on Windows VS Linux/Mac. | 164 # differently on Windows VS Linux/Mac. |
| 163 os.path.join( | 165 os.path.join( |
| 164 in_path if platform.system() == 'Windows' else os.sep, | 166 in_path if platform.system() == 'Windows' else os.sep, |
| 165 args.html_in_file)]) | 167 args.html_in_file)]) |
| 166 | 168 |
| 167 # Grit includes are not supported, use HTML imports instead. | 169 # Grit includes are not supported, use HTML imports instead. |
| 168 output = output.replace('<include src="', '<include src-disabled="') | 170 output = output.replace('<include src="', '<include src-disabled="') |
| 169 | 171 |
| 170 if args.insert_in_head: | 172 if args.insert_in_head: |
| 171 assert '<head>' in output | 173 assert '<head>' in output |
| 172 # NOTE(dbeam): Vulcanize eats <base> tags after processing. This undoes | 174 # NOTE(dbeam): Vulcanize eats <base> tags after processing. This undoes |
| 173 # that by adding a <base> tag to the (post-processed) generated output. | 175 # that by adding a <base> tag to the (post-processed) generated output. |
| 174 output = output.replace('<head>', '<head>' + args.insert_in_head) | 176 output = output.replace('<head>', '<head>' + args.insert_in_head) |
| 175 | 177 |
| 176 with tempfile.NamedTemporaryFile(mode='wt+', delete=False) as tmp: | 178 with tempfile.NamedTemporaryFile(mode='wt+', delete=False) as tmp: |
| 177 tmp.write(output) | 179 tmp.write(output) |
| 178 | 180 |
| 179 try: | 181 try: |
| 180 _run_node([node_modules.PathToCrisper(), | 182 _run_node([node_modules.PathToCrisper(), |
| 181 '--source', tmp.name, | 183 '--source', tmp.name, |
| 182 '--script-in-head', 'false', | 184 '--script-in-head', 'false', |
| 183 '--html', html_out_path, | 185 '--html', html_out_path, |
| 184 '--js', js_out_path]) | 186 '--js', js_out_path]) |
| 185 | 187 |
| 186 # TODO(tsergeant): Remove when JS resources are minified by default: | 188 # TODO(tsergeant): Remove when JS resources are minified by default: |
| 187 # crbug.com/619091. | 189 # crbug.com/619091. |
|
dpapad
2017/02/09 19:14:04
The referenced bug is marked as Fixed. Is this TOD
| |
| 188 _run_node([node_modules.PathToUglifyJs(), js_out_path, | 190 _run_node([node_modules.PathToUglifyJs(), js_out_path, |
| 189 '--comments', '"/Copyright|license|LICENSE|\<\/?if/"', | 191 '--comments', '"/Copyright|license|LICENSE|\<\/?if/"', |
| 190 '--output', js_out_path]) | 192 '--output', js_out_path]) |
| 191 finally: | 193 finally: |
| 192 os.remove(tmp.name) | 194 os.remove(tmp.name) |
| 193 | 195 |
| 194 | 196 |
| 195 def _css_build(out_folder, files): | 197 def _css_build(out_folder, files): |
| 196 out_path = os.path.join(_CWD, out_folder) | 198 out_path = os.path.join(_CWD, out_folder) |
| 197 paths = [os.path.join(out_path, f) for f in files] | 199 paths = [os.path.join(out_path, f) for f in files] |
| 198 | 200 |
| 199 _run_node([node_modules.PathToPolymerCssBuild()] + paths) | 201 _run_node([node_modules.PathToPolymerCssBuild()] + paths) |
| 200 | 202 |
| 201 | 203 |
| 202 def main(argv): | 204 def main(argv): |
| 203 parser = argparse.ArgumentParser() | 205 parser = argparse.ArgumentParser() |
| 204 parser.add_argument('--depfile', required=True) | 206 parser.add_argument('--depfile', required=True) |
| 207 parser.add_argument('--exclude', nargs='*') | |
| 205 parser.add_argument('--host', required=True) | 208 parser.add_argument('--host', required=True) |
| 206 parser.add_argument('--html_in_file', required=True) | 209 parser.add_argument('--html_in_file', required=True) |
| 207 parser.add_argument('--html_out_file', required=True) | 210 parser.add_argument('--html_out_file', required=True) |
| 208 parser.add_argument('--input', required=True) | 211 parser.add_argument('--input', required=True) |
| 209 parser.add_argument('--insert_in_head') | 212 parser.add_argument('--insert_in_head') |
| 210 parser.add_argument('--js_out_file', required=True) | 213 parser.add_argument('--js_out_file', required=True) |
| 211 parser.add_argument('--out_folder', required=True) | 214 parser.add_argument('--out_folder', required=True) |
| 212 args = parser.parse_args(argv) | 215 args = parser.parse_args(argv) |
| 213 | 216 |
| 214 # NOTE(dbeam): on Windows, GN can send dirs/like/this. When joined, you might | 217 # NOTE(dbeam): on Windows, GN can send dirs/like/this. When joined, you might |
| 215 # get dirs/like/this\file.txt. This looks odd to windows. Normalize to right | 218 # get dirs/like/this\file.txt. This looks odd to windows. Normalize to right |
| 216 # the slashes. | 219 # the slashes. |
| 217 args.depfile = os.path.normpath(args.depfile) | 220 args.depfile = os.path.normpath(args.depfile) |
| 218 args.input = os.path.normpath(args.input) | 221 args.input = os.path.normpath(args.input) |
| 219 args.out_folder = os.path.normpath(args.out_folder) | 222 args.out_folder = os.path.normpath(args.out_folder) |
| 220 | 223 |
| 221 vulcanize_input_folder = args.input | 224 vulcanize_input_folder = args.input |
| 222 | 225 |
| 223 # If a .pak file was specified, unpack that file first and pass the output to | 226 # If a .pak file was specified, unpack that file first and pass the output to |
| 224 # vulcanize. | 227 # vulcanize. |
| 225 if args.input.endswith('.pak'): | 228 if args.input.endswith('.pak'): |
| 226 import unpack_pak | 229 import unpack_pak |
| 227 input_folder = os.path.join(_CWD, args.input) | |
| 228 output_folder = os.path.join(args.out_folder, _PAK_UNPACK_FOLDER) | 230 output_folder = os.path.join(args.out_folder, _PAK_UNPACK_FOLDER) |
| 229 unpack_pak.unpack(args.input, output_folder) | 231 unpack_pak.unpack(args.input, output_folder) |
| 230 vulcanize_input_folder = output_folder | 232 vulcanize_input_folder = output_folder |
| 231 | 233 |
| 232 _vulcanize(vulcanize_input_folder, args) | 234 _vulcanize(vulcanize_input_folder, args) |
| 233 _css_build(args.out_folder, files=[args.html_out_file]) | 235 _css_build(args.out_folder, files=[args.html_out_file]) |
| 234 | 236 |
| 235 _update_dep_file(vulcanize_input_folder, args) | 237 _update_dep_file(vulcanize_input_folder, args) |
| 236 | 238 |
| 237 | 239 |
| 238 if __name__ == '__main__': | 240 if __name__ == '__main__': |
| 239 main(sys.argv[1:]) | 241 main(sys.argv[1:]) |
| OLD | NEW |