Index: tools/grit/grit/flatten_resource_runner.py |
diff --git a/tools/grit/grit/flatten_resource_runner.py b/tools/grit/grit/flatten_resource_runner.py |
new file mode 100755 |
index 0000000000000000000000000000000000000000..6e78ca2e89e83bf9dd94d99c523e0d1f08b02b7e |
--- /dev/null |
+++ b/tools/grit/grit/flatten_resource_runner.py |
@@ -0,0 +1,91 @@ |
+#!/usr/bin/env python |
+# Copyright 2016 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+'''A resource flattener for removing <include ...> and <if ...> elements |
+ |
+This script inlines <include...> elements and processes and removes |
+<if...> blocks based on C-preprocessor style defines, hence removing |
+illegal language elements from HTML, Javascript, or CSS source files. |
+This allows them to be pre-processed by standard tools (e.g the Javascript |
+Closure compiler) before using them to build resource files. |
+ |
+Unlike the HTML flattener used by 'grit build' this does not flatten HTML |
+script, stylesheet, or icon elements; since those are legal HTML. |
+ |
+This script is not built as a grit tool, since it does not take a grd file |
+as input. It does, however, make use of various grit scripts. |
+''' |
+ |
+import sys |
+ |
+if sys.version_info < (2, 7, 0): |
+ sys.stderr.write('python 2.7 or later is required run this script\n') |
+ sys.exit(1) |
+ |
+import argparse |
+import os |
+ |
+from grit.format import html_inline |
+from grit.node import base |
+from grit import util |
+ |
+ |
+def Main(args): |
+ |
+ def OutputFileType(string): |
+ """A type of argument that automatically opens a file, creating""" |
+ """the directory if necessary""" |
+ try: |
+ if not os.path.exists(os.path.dirname(string)): |
+ os.makedirs(os.path.dirname(string)) |
+ return open(string, 'wb') |
+ except (IOError, OSError) as e: |
+ raise argparse.ArgumentTypeError("can't open '%s': %s" % (string, e)) |
+ |
+ parser = argparse.ArgumentParser( |
+ 'Flatten <include...> and <if...> elements in resource source files') |
+ parser.add_argument('-D', |
+ dest='defs', |
+ metavar='NAME[=VAL]', |
+ help=''' |
+ Specify a C-preprocessor-like define NAME with optional |
+ value VAL (defaults to 1) which will be used to control |
+ conditional inclusion of resources. |
+ ''', |
+ action='append', |
+ default=[]) |
+ parser.add_argument('-t', |
+ dest='platform', |
+ metavar='PLATFORM', |
+ help=''' |
+ Specifies the platform the build is targeting; defaults |
+ to the value of sys.platform. The value provided via this |
+ flag should match what sys.platform would report for your |
+ target platform; see grit.node.base.EvaluateCondition. |
+ ''', |
+ default=sys.platform) |
+ parser.add_argument('input', |
+ help='The input file', |
+ type=argparse.FileType('r')) |
+ parser.add_argument('output', help='The output file', type=OutputFileType) |
+ |
+ parsed = parser.parse_args(args) |
+ |
+ defines = {} |
+ for d in parsed.defs: |
+ name, value = util.ParseDefine(d) |
+ defines[name] = value |
+ |
+ def EvaluateCondition(expression, defines=defines, platform=parsed.platform): |
+ return base.Node.EvaluateExpression(expression, defines, platform, []) |
+ |
+ parsed.output.write(html_inline.InlineToString(parsed.input.name, |
+ EvaluateCondition, |
+ preprocess_only=True)) |
Dan Beam
2016/07/14 17:47:58
indent off
|
+ |
+ return 0 |
+ |
+ |
+if __name__ == '__main__': |
+ sys.exit(Main(sys.argv[1:])) |