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 from functools import partial | |
| 14 | |
| 15 _HERE_PATH = os.path.join(os.path.dirname(__file__)) | |
| 16 _SRC_PATH = os.path.normpath(os.path.join(_HERE_PATH, '..', '..', '..')) | |
| 17 | |
| 18 sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'node')) | |
| 19 import node | |
| 20 import node_modules | |
| 21 | |
| 22 _NODE_BINARY = node.get_binary() | |
| 23 | |
| 24 _RESOURCES_PATH = os.path.join(_SRC_PATH, 'ui', 'webui', 'resources') | |
| 25 | |
| 26 _CR_ELEMENTS_PATH = os.path.join(_RESOURCES_PATH, 'cr_elements') | |
| 27 _CSS_RESOURCES_PATH = os.path.join(_RESOURCES_PATH, 'css') | |
| 28 _HTML_RESOURCES_PATH = os.path.join(_RESOURCES_PATH, 'html') | |
| 29 _JS_RESOURCES_PATH = os.path.join(_RESOURCES_PATH, 'js') | |
| 30 _POLYMER_PATH = os.path.join( | |
| 31 _SRC_PATH, 'third_party', 'polymer', 'v1_0', 'components-chromium') | |
| 32 | |
| 33 _VULCANIZE_BASE_ARGS = [ | |
| 34 '--exclude', 'crisper.js', | |
| 35 | |
| 36 # These files are already combined and minified. | |
| 37 '--exclude', 'chrome://resources/html/polymer.html', | |
| 38 '--exclude', 'web-animations-next-lite.min.js', | |
| 39 | |
| 40 # These files are dynamically created by C++. | |
| 41 '--exclude', 'load_time_data.js', | |
| 42 '--exclude', 'strings.js', | |
| 43 '--exclude', 'text_defaults.css', | |
| 44 '--exclude', 'text_defaults_md.css', | |
| 45 | |
| 46 '--inline-css', | |
| 47 '--inline-scripts', | |
| 48 '--strip-comments', | |
| 49 ] | |
| 50 | |
| 51 _URL_MAPPINGS = [ | |
| 52 ('chrome://resources/cr_elements/', _CR_ELEMENTS_PATH), | |
| 53 ('chrome://resources/css/', _CSS_RESOURCES_PATH), | |
| 54 ('chrome://resources/html/', _HTML_RESOURCES_PATH), | |
| 55 ('chrome://resources/js/', _JS_RESOURCES_PATH), | |
| 56 ('chrome://resources/polymer/v1_0/', _POLYMER_PATH) | |
| 57 ] | |
| 58 | |
| 59 _VULCANIZE_REDIRECT_ARGS = list(itertools.chain.from_iterable(map( | |
|
Dan Beam
2017/01/20 00:56:50
what's goin on here? why can't this just be
['--
dpapad
2017/01/20 18:35:04
Need to generate an array that looks as follows
[
| |
| 60 lambda m: ['--redirect', m[0] + '|%s' % m[1]], _URL_MAPPINGS))) | |
| 61 | |
| 62 def _run_cmd(cmd_parts, stdout=None): | |
| 63 cmd = "'" + "' '".join(cmd_parts) + "'" | |
| 64 process = subprocess.Popen( | |
| 65 cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) | |
| 66 stdout, stderr = process.communicate() | |
| 67 | |
| 68 if stderr: | |
| 69 print >> sys.stderr, '%s failed: %s' % (cmd, stderr) | |
| 70 raise | |
| 71 | |
| 72 return stdout | |
| 73 | |
| 74 def _undo_mapping(mappings, url): | |
| 75 for tup in mappings: | |
| 76 res = re.match(tup[0], url) | |
| 77 if res: | |
| 78 return url.replace(tup[0], tup[1] + '/'); | |
| 79 return url | |
| 80 | |
| 81 # Get a list of all files that were bundled with Vulcanize and update the | |
| 82 # depfile accordingly such that Ninja knows when to trigger re-vulcanization. | |
| 83 def _update_dep_file(in_folder, out_folder, host, depfile, html_out_file): | |
| 84 in_path = os.path.join(os.getcwd(), in_folder) | |
| 85 out_path = os.path.join(os.getcwd(), out_folder) | |
| 86 | |
| 87 # Prior call to vulcanize already generated the deps list, grab it from there. | |
| 88 request_list = open(os.path.join( | |
| 89 out_path, 'request_list.txt'), 'r').read().splitlines() | |
| 90 | |
| 91 # Undo the URL mappings applied by vulcanize to get file paths relative to | |
| 92 # current working directory. | |
| 93 url_mappings = [ | |
| 94 ('/', os.path.relpath(in_path, os.getcwd())), | |
| 95 ('chrome://%s/' % host, os.path.relpath(in_path, os.getcwd())), | |
| 96 ] | |
| 97 dependencies = map(partial( | |
|
Dan Beam
2017/01/20 00:56:50
can you just use lambda instead?
dpapad
2017/01/20 18:35:04
Done.
| |
| 98 _undo_mapping, _URL_MAPPINGS + url_mappings), request_list) | |
| 99 | |
| 100 f = open(os.path.join(os.getcwd(), depfile), 'w') | |
| 101 f.write(os.path.join( | |
| 102 out_folder, html_out_file + ': ') + ' '.join(dependencies)) | |
| 103 f.close() | |
| 104 | |
| 105 def _vulcanize(in_folder, out_folder, host, html_in_file, | |
| 106 html_out_file, js_out_file): | |
| 107 in_path = os.path.join(os.getcwd(), in_folder) | |
| 108 out_path = os.path.join(os.getcwd(), out_folder) | |
| 109 | |
| 110 html_out_path = os.path.join(out_path, html_out_file) | |
| 111 js_out_path = os.path.join(out_path, js_out_file) | |
| 112 | |
| 113 output = _run_cmd( | |
| 114 [_NODE_BINARY, node_modules.get_vulcanize_binary()] + | |
| 115 _VULCANIZE_BASE_ARGS + _VULCANIZE_REDIRECT_ARGS + | |
| 116 ['--out-request-list', os.path.join(out_path, 'request_list.txt'), | |
| 117 '--redirect', '/|%s' % in_path, | |
| 118 '--redirect', 'chrome://%s/|%s' % (host, in_path), | |
| 119 os.path.join('/', html_in_file)]) | |
| 120 | |
| 121 with tempfile.NamedTemporaryFile(mode='wt+', delete=False) as tmp: | |
| 122 # Grit includes are not supported, use HTML imports instead. | |
| 123 tmp.write(output.replace( | |
| 124 '<include src="', '<include src-disabled="')) | |
| 125 | |
| 126 try: | |
| 127 _run_cmd([_NODE_BINARY, node_modules.get_crisper_binary(), | |
| 128 '--source', tmp.name, | |
| 129 '--script-in-head', 'false', | |
| 130 '--html', html_out_path, | |
| 131 '--js', js_out_path]) | |
| 132 | |
| 133 # TODO(tsergeant): Remove when JS resources are minified by default: | |
| 134 # crbug.com/619091. | |
| 135 _run_cmd([_NODE_BINARY, node_modules.get_uglifyjs_binary(), js_out_path, | |
| 136 '--comments', '/Copyright|license|LICENSE|\<\/?if/', | |
| 137 '--output', js_out_path]) | |
| 138 finally: | |
| 139 os.remove(tmp.name) | |
| 140 | |
| 141 def _css_build(out_folder, files): | |
| 142 out_path = os.path.join(os.getcwd(), out_folder) | |
| 143 paths = map(lambda f: os.path.join(out_path, f), files) | |
| 144 | |
| 145 _run_cmd([_NODE_BINARY, node_modules.get_polymer_css_build_binary()] + paths) | |
| 146 | |
| 147 def main(): | |
| 148 parser = argparse.ArgumentParser() | |
| 149 parser.add_argument('--host') | |
| 150 parser.add_argument('--htmlInFile') | |
| 151 parser.add_argument('--htmlOutFile') | |
| 152 parser.add_argument('--inputType') | |
| 153 parser.add_argument('--input') | |
| 154 parser.add_argument('--jsOutFile') | |
| 155 parser.add_argument('--outFolder') | |
| 156 parser.add_argument('--depfile') | |
| 157 args = parser.parse_args() | |
| 158 | |
| 159 vulcanize_input_folder = args.input; | |
| 160 | |
| 161 # If a .pak file was specified, unpack that file first and pass the output to | |
| 162 # vulcanize. | |
| 163 if (args.inputType == 'PAK_FILE'): | |
| 164 import unpack_pak | |
| 165 input_folder = os.path.join(os.getcwd(), args.input) | |
| 166 output_folder = os.path.join(args.outFolder, 'flattened'); | |
| 167 unpack_pak.unpack(args.input, output_folder) | |
| 168 vulcanize_input_folder = output_folder | |
| 169 # assert inputType == "FOLDER" | |
|
Dan Beam
2017/01/20 00:56:50
remove
dpapad
2017/01/20 18:35:05
Done.
| |
| 170 | |
| 171 _vulcanize(vulcanize_input_folder, args.outFolder, args.host, args.htmlInFile, | |
| 172 args.htmlOutFile, args.jsOutFile); | |
| 173 _css_build(args.outFolder, files=[args.htmlOutFile]) | |
| 174 | |
| 175 _update_dep_file(vulcanize_input_folder, args.outFolder, args.host, | |
| 176 args.depfile, args.htmlOutFile) | |
| 177 | |
| 178 if __name__ == '__main__': | |
| 179 main() | |
| OLD | NEW |