| 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 |
| 11 import subprocess | |
| 12 import sys | 11 import sys |
| 13 import tempfile | 12 import tempfile |
| 14 | 13 |
| 15 | 14 |
| 16 _HERE_PATH = os.path.dirname(__file__) | 15 _HERE_PATH = os.path.dirname(__file__) |
| 17 _SRC_PATH = os.path.normpath(os.path.join(_HERE_PATH, '..', '..', '..')) | 16 _SRC_PATH = os.path.normpath(os.path.join(_HERE_PATH, '..', '..', '..')) |
| 18 _CWD = os.getcwd() | 17 _CWD = os.getcwd() |
| 19 | 18 |
| 20 sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'node')) | 19 sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'node')) |
| 21 import node | 20 import node |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 ('chrome://resources/html/', _HTML_RESOURCES_PATH), | 65 ('chrome://resources/html/', _HTML_RESOURCES_PATH), |
| 67 ('chrome://resources/js/', _JS_RESOURCES_PATH), | 66 ('chrome://resources/js/', _JS_RESOURCES_PATH), |
| 68 ('chrome://resources/polymer/v1_0/', _POLYMER_PATH) | 67 ('chrome://resources/polymer/v1_0/', _POLYMER_PATH) |
| 69 ] | 68 ] |
| 70 | 69 |
| 71 | 70 |
| 72 _VULCANIZE_REDIRECT_ARGS = list(itertools.chain.from_iterable(map( | 71 _VULCANIZE_REDIRECT_ARGS = list(itertools.chain.from_iterable(map( |
| 73 lambda m: ['--redirect', '"%s|%s"' % (m[0], m[1])], _URL_MAPPINGS))) | 72 lambda m: ['--redirect', '"%s|%s"' % (m[0], m[1])], _URL_MAPPINGS))) |
| 74 | 73 |
| 75 | 74 |
| 76 _REQUEST_LIST_FILE = 'request_list.txt' | |
| 77 | |
| 78 | |
| 79 _PAK_UNPACK_FOLDER = 'flattened' | 75 _PAK_UNPACK_FOLDER = 'flattened' |
| 80 | 76 |
| 81 | 77 def _request_list_path(out_path, html_out_file): |
| 82 def _run_node(cmd_parts, stdout=None): | 78 return os.path.join(out_path, html_out_file + '.requestlist') |
| 83 cmd = " ".join([node.GetBinaryPath()] + cmd_parts) | |
| 84 process = subprocess.Popen( | |
| 85 cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) | |
| 86 stdout, stderr = process.communicate() | |
| 87 | |
| 88 if stderr: | |
| 89 print >> sys.stderr, '%s failed: %s' % (cmd, stderr) | |
| 90 raise | |
| 91 | |
| 92 return stdout | |
| 93 | |
| 94 | 79 |
| 95 def _undo_mapping(mappings, url): | 80 def _undo_mapping(mappings, url): |
| 96 for (redirect_url, file_path) in mappings: | 81 for (redirect_url, file_path) in mappings: |
| 97 if url.startswith(redirect_url): | 82 if url.startswith(redirect_url): |
| 98 return url.replace(redirect_url, file_path + os.sep) | 83 return url.replace(redirect_url, file_path + os.sep) |
| 99 return url | 84 return url |
| 100 | 85 |
| 101 | 86 |
| 102 # Get a list of all files that were bundled with Vulcanize and update the | 87 # Get a list of all files that were bundled with Vulcanize and update the |
| 103 # depfile accordingly such that Ninja knows when to trigger re-vulcanization. | 88 # depfile accordingly such that Ninja knows when to trigger re-vulcanization. |
| 104 def _update_dep_file(in_folder, args): | 89 def _update_dep_file(in_folder, args): |
| 105 in_path = os.path.join(_CWD, in_folder) | 90 in_path = os.path.join(_CWD, in_folder) |
| 106 out_path = os.path.join(_CWD, args.out_folder) | 91 out_path = os.path.join(_CWD, args.out_folder) |
| 107 | 92 |
| 108 # Prior call to vulcanize already generated the deps list, grab it from there. | 93 # Prior call to vulcanize already generated the deps list, grab it from there. |
| 109 request_list = open(os.path.join( | 94 request_list = open(_request_list_path(out_path, args.html_out_file), |
| 110 out_path, _REQUEST_LIST_FILE), 'r').read().splitlines() | 95 'r').read().splitlines() |
| 111 | 96 |
| 112 # Undo the URL mappings applied by vulcanize to get file paths relative to | 97 # Undo the URL mappings applied by vulcanize to get file paths relative to |
| 113 # current working directory. | 98 # current working directory. |
| 114 url_mappings = _URL_MAPPINGS + [ | 99 url_mappings = _URL_MAPPINGS + [ |
| 115 ('/', os.path.relpath(in_path, _CWD)), | 100 ('/', os.path.relpath(in_path, _CWD)), |
| 116 ('chrome://%s/' % args.host, os.path.relpath(in_path, _CWD)), | 101 ('chrome://%s/' % args.host, os.path.relpath(in_path, _CWD)), |
| 117 ] | 102 ] |
| 118 | 103 |
| 119 dependencies = map( | 104 dependencies = map( |
| 120 lambda url: _undo_mapping(url_mappings, url), request_list) | 105 lambda url: _undo_mapping(url_mappings, url), request_list) |
| 121 | 106 |
| 122 # If the input was a .pak file, the generated depfile should not list files | 107 # If the input was a .pak file, the generated depfile should not list files |
| 123 # already in the .pak file. | 108 # already in the .pak file. |
| 124 filtered_dependencies = dependencies | 109 filtered_dependencies = dependencies |
| 125 if (args.input_type == 'PAK_FILE'): | 110 if (args.input_type == 'PAK_FILE'): |
| 126 filter_url = os.path.join(args.out_folder, _PAK_UNPACK_FOLDER) | 111 filter_url = os.path.join(args.out_folder, _PAK_UNPACK_FOLDER) |
| 127 filtered_dependencies = filter( | 112 filtered_dependencies = filter( |
| 128 lambda url: not url.startswith(filter_url), dependencies) | 113 lambda url: not url.startswith(filter_url), dependencies) |
| 129 | 114 |
| 130 with open(os.path.join(_CWD, args.depfile), 'w') as f: | 115 with open(os.path.join(_CWD, args.depfile), 'w') as f: |
| 131 f.write(os.path.join( | 116 f.write(os.path.join( |
| 132 args.out_folder, args.html_out_file) + ': ' + ' '.join( | 117 args.out_folder, args.html_out_file) + ': ' + ' '.join( |
| 133 filtered_dependencies)) | 118 filtered_dependencies)) |
| 134 | 119 |
| 135 | 120 |
| 136 def _vulcanize(in_folder, out_folder, host, html_in_file, | 121 def _vulcanize(in_folder, out_folder, host, html_in_file, |
| 137 html_out_file, js_out_file): | 122 html_out_file, js_out_file, exclude): |
| 138 in_path = os.path.normpath(os.path.join(_CWD, in_folder)) | 123 in_path = os.path.normpath(os.path.join(_CWD, in_folder)) |
| 139 out_path = os.path.join(_CWD, out_folder) | 124 out_path = os.path.join(_CWD, out_folder) |
| 140 | 125 |
| 141 html_out_path = os.path.join(out_path, html_out_file) | 126 html_out_path = os.path.join(out_path, html_out_file) |
| 142 js_out_path = os.path.join(out_path, js_out_file) | 127 js_out_path = os.path.join(out_path, js_out_file) |
| 143 | 128 |
| 144 output = _run_node( | 129 exclude = exclude or [] |
| 130 |
| 131 exclude_args = [] |
| 132 for f in exclude: |
| 133 exclude_args.append('--exclude') |
| 134 exclude_args.append(f) |
| 135 |
| 136 output = node.RunNode( |
| 145 [node_modules.PathToVulcanize()] + | 137 [node_modules.PathToVulcanize()] + |
| 146 _VULCANIZE_BASE_ARGS + _VULCANIZE_REDIRECT_ARGS + | 138 _VULCANIZE_BASE_ARGS + _VULCANIZE_REDIRECT_ARGS + exclude_args + |
| 147 ['--out-request-list', os.path.join(out_path, _REQUEST_LIST_FILE), | 139 ['--out-request-list', _request_list_path(out_path, html_out_file), |
| 148 '--redirect', '"/|%s"' % in_path, | 140 '--redirect', '"/|%s"' % in_path, |
| 149 '--redirect', '"chrome://%s/|%s"' % (host, in_path), | 141 '--redirect', '"chrome://%s/|%s"' % (host, in_path), |
| 150 # TODO(dpapad): Figure out why vulcanize treats the input path | 142 # TODO(dpapad): Figure out why vulcanize treats the input path |
| 151 # differently on Windows VS Linux/Mac. | 143 # differently on Windows VS Linux/Mac. |
| 152 os.path.join( | 144 os.path.join( |
| 153 in_path if platform.system() == 'Windows' else os.sep, | 145 in_path if platform.system() == 'Windows' else os.sep, |
| 154 html_in_file)]) | 146 html_in_file)]) |
| 155 | 147 |
| 156 with tempfile.NamedTemporaryFile(mode='wt+', delete=False) as tmp: | 148 with tempfile.NamedTemporaryFile(mode='wt+', delete=False) as tmp: |
| 157 # Grit includes are not supported, use HTML imports instead. | 149 # Grit includes are not supported, use HTML imports instead. |
| 158 tmp.write(output.replace( | 150 tmp.write(output.replace( |
| 159 '<include src="', '<include src-disabled="')) | 151 '<include src="', '<include src-disabled="')) |
| 160 | 152 |
| 161 try: | 153 try: |
| 162 _run_node([node_modules.PathToCrisper(), | 154 node.RunNode([node_modules.PathToCrisper(), |
| 163 '--source', tmp.name, | 155 '--source', tmp.name, |
| 164 '--script-in-head', 'false', | 156 '--script-in-head', 'false', |
| 165 '--html', html_out_path, | 157 '--html', html_out_path, |
| 166 '--js', js_out_path]) | 158 '--js', js_out_path]) |
| 167 | 159 |
| 168 # TODO(tsergeant): Remove when JS resources are minified by default: | 160 # TODO(tsergeant): Remove when JS resources are minified by default: |
| 169 # crbug.com/619091. | 161 # crbug.com/619091. |
| 170 _run_node([node_modules.PathToUglifyJs(), js_out_path, | 162 node.RunNode([node_modules.PathToUglifyJs(), js_out_path, |
| 171 '--comments', '"/Copyright|license|LICENSE|\<\/?if/"', | 163 '--comments', '"/Copyright|license|LICENSE|\<\/?if/"', |
| 172 '--output', js_out_path]) | 164 '--output', js_out_path]) |
| 173 finally: | 165 finally: |
| 174 os.remove(tmp.name) | 166 os.remove(tmp.name) |
| 175 | 167 |
| 176 | 168 |
| 177 def _css_build(out_folder, files): | |
| 178 out_path = os.path.join(_CWD, out_folder) | |
| 179 paths = map(lambda f: os.path.join(out_path, f), files) | |
| 180 | |
| 181 _run_node([node_modules.PathToPolymerCssBuild()] + paths) | |
| 182 | |
| 183 | |
| 184 def main(): | 169 def main(): |
| 185 parser = argparse.ArgumentParser() | 170 parser = argparse.ArgumentParser() |
| 186 parser.add_argument('--depfile') | 171 parser.add_argument('--depfile') |
| 172 parser.add_argument('--exclude', action='append') |
| 187 parser.add_argument('--host') | 173 parser.add_argument('--host') |
| 188 parser.add_argument('--html_in_file') | 174 parser.add_argument('--html_in_file') |
| 189 parser.add_argument('--html_out_file') | 175 parser.add_argument('--html_out_file') |
| 190 parser.add_argument('--input') | 176 parser.add_argument('--input') |
| 191 parser.add_argument('--input_type') | 177 parser.add_argument('--input_type') |
| 192 parser.add_argument('--js_out_file') | 178 parser.add_argument('--js_out_file') |
| 193 parser.add_argument('--out_folder') | 179 parser.add_argument('--out_folder') |
| 194 args = parser.parse_args() | 180 args = parser.parse_args() |
| 195 args.input = os.path.normpath(args.input) | 181 args.input = os.path.normpath(args.input) |
| 196 | 182 |
| 197 vulcanize_input_folder = args.input | 183 vulcanize_input_folder = args.input |
| 198 | 184 |
| 199 # If a .pak file was specified, unpack that file first and pass the output to | 185 # If a .pak file was specified, unpack that file first and pass the output to |
| 200 # vulcanize. | 186 # vulcanize. |
| 201 if (args.input_type == 'PAK_FILE'): | 187 if (args.input_type == 'PAK_FILE'): |
| 202 import unpack_pak | 188 import unpack_pak |
| 203 input_folder = os.path.join(_CWD, args.input) | |
| 204 output_folder = os.path.join(args.out_folder, _PAK_UNPACK_FOLDER) | 189 output_folder = os.path.join(args.out_folder, _PAK_UNPACK_FOLDER) |
| 205 unpack_pak.unpack(args.input, output_folder) | 190 unpack_pak.unpack(args.input, output_folder) |
| 206 vulcanize_input_folder = output_folder | 191 vulcanize_input_folder = output_folder |
| 207 | 192 |
| 208 _vulcanize(vulcanize_input_folder, args.out_folder, args.host, | 193 _vulcanize(vulcanize_input_folder, args.out_folder, args.host, |
| 209 args.html_in_file, args.html_out_file, args.js_out_file) | 194 args.html_in_file, args.html_out_file, args.js_out_file, |
| 210 _css_build(args.out_folder, files=[args.html_out_file]) | 195 args.exclude) |
| 211 | 196 |
| 212 _update_dep_file(vulcanize_input_folder, args) | 197 _update_dep_file(vulcanize_input_folder, args) |
| 213 | 198 |
| 214 | 199 |
| 215 if __name__ == '__main__': | 200 if __name__ == '__main__': |
| 216 main() | 201 main() |
| OLD | NEW |