| 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() # NOTE(dbeam): this is typically out/<gn_name>/. | 17 _CWD = os.getcwd() # NOTE(dbeam): this is typically out/<gn_name>/. |
| 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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 ] | 66 ] |
| 68 | 67 |
| 69 | 68 |
| 70 _VULCANIZE_REDIRECT_ARGS = list(itertools.chain.from_iterable(map( | 69 _VULCANIZE_REDIRECT_ARGS = list(itertools.chain.from_iterable(map( |
| 71 lambda m: ['--redirect', '"%s|%s"' % (m[0], m[1])], _URL_MAPPINGS))) | 70 lambda m: ['--redirect', '"%s|%s"' % (m[0], m[1])], _URL_MAPPINGS))) |
| 72 | 71 |
| 73 | 72 |
| 74 _PAK_UNPACK_FOLDER = 'flattened' | 73 _PAK_UNPACK_FOLDER = 'flattened' |
| 75 | 74 |
| 76 | 75 |
| 77 def _run_node(cmd_parts, stdout=None): | |
| 78 cmd = " ".join([node.GetBinaryPath()] + cmd_parts) | |
| 79 process = subprocess.Popen( | |
| 80 cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) | |
| 81 stdout, stderr = process.communicate() | |
| 82 | |
| 83 if stderr: | |
| 84 print >> sys.stderr, '%s failed: %s' % (cmd, stderr) | |
| 85 raise | |
| 86 | |
| 87 return stdout | |
| 88 | |
| 89 | |
| 90 def _undo_mapping(mappings, url): | 76 def _undo_mapping(mappings, url): |
| 91 for (redirect_url, file_path) in mappings: | 77 for (redirect_url, file_path) in mappings: |
| 92 if url.startswith(redirect_url): | 78 if url.startswith(redirect_url): |
| 93 return url.replace(redirect_url, file_path + os.sep) | 79 return url.replace(redirect_url, file_path + os.sep) |
| 94 # TODO(dbeam): can we make this stricter? | 80 # TODO(dbeam): can we make this stricter? |
| 95 return url | 81 return url |
| 96 | 82 |
| 97 def _request_list_path(out_path, html_out_file): | 83 def _request_list_path(out_path, html_out_file): |
| 98 return os.path.join(out_path, html_out_file + '_requestlist.txt') | 84 return os.path.join(out_path, html_out_file + '_requestlist.txt') |
| 99 | 85 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 out_path = os.path.join(_CWD, args.out_folder) | 133 out_path = os.path.join(_CWD, args.out_folder) |
| 148 | 134 |
| 149 html_out_path = os.path.join(out_path, args.html_out_file) | 135 html_out_path = os.path.join(out_path, args.html_out_file) |
| 150 js_out_path = os.path.join(out_path, args.js_out_file) | 136 js_out_path = os.path.join(out_path, args.js_out_file) |
| 151 | 137 |
| 152 exclude_args = [] | 138 exclude_args = [] |
| 153 for f in args.exclude or []: | 139 for f in args.exclude or []: |
| 154 exclude_args.append('--exclude') | 140 exclude_args.append('--exclude') |
| 155 exclude_args.append(f) | 141 exclude_args.append(f) |
| 156 | 142 |
| 157 output = _run_node( | 143 output = node.RunNode( |
| 158 [node_modules.PathToVulcanize()] + | 144 [node_modules.PathToVulcanize()] + |
| 159 _VULCANIZE_BASE_ARGS + _VULCANIZE_REDIRECT_ARGS + exclude_args + | 145 _VULCANIZE_BASE_ARGS + _VULCANIZE_REDIRECT_ARGS + exclude_args + |
| 160 ['--out-request-list', _request_list_path(out_path, args.html_out_file), | 146 ['--out-request-list', _request_list_path(out_path, args.html_out_file), |
| 161 '--redirect', '"/|%s"' % in_path, | 147 '--redirect', '"/|%s"' % in_path, |
| 162 '--redirect', '"chrome://%s/|%s"' % (args.host, in_path), | 148 '--redirect', '"chrome://%s/|%s"' % (args.host, in_path), |
| 163 # TODO(dpapad): Figure out why vulcanize treats the input path | 149 # TODO(dpapad): Figure out why vulcanize treats the input path |
| 164 # differently on Windows VS Linux/Mac. | 150 # differently on Windows VS Linux/Mac. |
| 165 os.path.join( | 151 os.path.join( |
| 166 in_path if platform.system() == 'Windows' else os.sep, | 152 in_path if platform.system() == 'Windows' else os.sep, |
| 167 args.html_in_file)]) | 153 args.html_in_file)]) |
| 168 | 154 |
| 169 # Grit includes are not supported, use HTML imports instead. | 155 # Grit includes are not supported, use HTML imports instead. |
| 170 output = output.replace('<include src="', '<include src-disabled="') | 156 output = output.replace('<include src="', '<include src-disabled="') |
| 171 | 157 |
| 172 if args.insert_in_head: | 158 if args.insert_in_head: |
| 173 assert '<head>' in output | 159 assert '<head>' in output |
| 174 # NOTE(dbeam): Vulcanize eats <base> tags after processing. This undoes | 160 # NOTE(dbeam): Vulcanize eats <base> tags after processing. This undoes |
| 175 # that by adding a <base> tag to the (post-processed) generated output. | 161 # that by adding a <base> tag to the (post-processed) generated output. |
| 176 output = output.replace('<head>', '<head>' + args.insert_in_head) | 162 output = output.replace('<head>', '<head>' + args.insert_in_head) |
| 177 | 163 |
| 178 with tempfile.NamedTemporaryFile(mode='wt+', delete=False) as tmp: | 164 with tempfile.NamedTemporaryFile(mode='wt+', delete=False) as tmp: |
| 179 tmp.write(output) | 165 tmp.write(output) |
| 180 | 166 |
| 181 try: | 167 try: |
| 182 _run_node([node_modules.PathToCrisper(), | 168 node.RunNode([node_modules.PathToCrisper(), |
| 183 '--source', tmp.name, | 169 '--source', tmp.name, |
| 184 '--script-in-head', 'false', | 170 '--script-in-head', 'false', |
| 185 '--html', html_out_path, | 171 '--html', html_out_path, |
| 186 '--js', js_out_path]) | 172 '--js', js_out_path]) |
| 187 | 173 |
| 188 _run_node([node_modules.PathToUglifyJs(), js_out_path, | 174 node.RunNode([node_modules.PathToUglifyJs(), js_out_path, |
| 189 '--comments', '"/Copyright|license|LICENSE|\<\/?if/"', | 175 '--comments', '"/Copyright|license|LICENSE|\<\/?if/"', |
| 190 '--output', js_out_path]) | 176 '--output', js_out_path]) |
| 191 finally: | 177 finally: |
| 192 os.remove(tmp.name) | 178 os.remove(tmp.name) |
| 193 | 179 |
| 194 | 180 |
| 195 def _css_build(out_folder, files): | |
| 196 out_path = os.path.join(_CWD, out_folder) | |
| 197 paths = [os.path.join(out_path, f) for f in files] | |
| 198 | |
| 199 _run_node([node_modules.PathToPolymerCssBuild()] + paths) | |
| 200 | |
| 201 | |
| 202 def main(argv): | 181 def main(argv): |
| 203 parser = argparse.ArgumentParser() | 182 parser = argparse.ArgumentParser() |
| 204 parser.add_argument('--depfile', required=True) | 183 parser.add_argument('--depfile', required=True) |
| 205 parser.add_argument('--exclude', nargs='*') | 184 parser.add_argument('--exclude', nargs='*') |
| 206 parser.add_argument('--host', required=True) | 185 parser.add_argument('--host', required=True) |
| 207 parser.add_argument('--html_in_file', required=True) | 186 parser.add_argument('--html_in_file', required=True) |
| 208 parser.add_argument('--html_out_file', required=True) | 187 parser.add_argument('--html_out_file', required=True) |
| 209 parser.add_argument('--input', required=True) | 188 parser.add_argument('--input', required=True) |
| 210 parser.add_argument('--insert_in_head') | 189 parser.add_argument('--insert_in_head') |
| 211 parser.add_argument('--js_out_file', required=True) | 190 parser.add_argument('--js_out_file', required=True) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 223 | 202 |
| 224 # If a .pak file was specified, unpack that file first and pass the output to | 203 # If a .pak file was specified, unpack that file first and pass the output to |
| 225 # vulcanize. | 204 # vulcanize. |
| 226 if args.input.endswith('.pak'): | 205 if args.input.endswith('.pak'): |
| 227 import unpack_pak | 206 import unpack_pak |
| 228 output_folder = os.path.join(args.out_folder, _PAK_UNPACK_FOLDER) | 207 output_folder = os.path.join(args.out_folder, _PAK_UNPACK_FOLDER) |
| 229 unpack_pak.unpack(args.input, output_folder) | 208 unpack_pak.unpack(args.input, output_folder) |
| 230 vulcanize_input_folder = output_folder | 209 vulcanize_input_folder = output_folder |
| 231 | 210 |
| 232 _vulcanize(vulcanize_input_folder, args) | 211 _vulcanize(vulcanize_input_folder, args) |
| 233 _css_build(args.out_folder, files=[args.html_out_file]) | |
| 234 | 212 |
| 235 _update_dep_file(vulcanize_input_folder, args) | 213 _update_dep_file(vulcanize_input_folder, args) |
| 236 | 214 |
| 237 | 215 |
| 238 if __name__ == '__main__': | 216 if __name__ == '__main__': |
| 239 main(sys.argv[1:]) | 217 main(sys.argv[1:]) |
| OLD | NEW |