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