Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(876)

Unified Diff: tools/grit/grit/flatten_resource_runner.py

Issue 2094193004: Strip comments and whitespace from Javascript resources (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Also strip Javascript browser resources Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..e5516b0e2f40005625534a751d565535e49bc62f
--- /dev/null
+++ b/tools/grit/grit/flatten_resource_runner.py
@@ -0,0 +1,92 @@
+#!/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, 'w')
Lei Zhang 2016/06/29 22:51:32 Probably want 'wb' ?
aberent 2016/07/14 15:41:36 Done.
+ except (IOError , OSError) as e:
Lei Zhang 2016/06/29 22:51:32 nit: no space before comma
aberent 2016/07/14 15:41:36 Done.
+ 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
Lei Zhang 2016/06/29 22:51:32 nit: I don't have Python readability, so what do I
aberent 2016/07/14 15:41:36 I think I hit an odd corner in Eclipse's python fo
+ 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, [])
+
+ with parsed.output as out:
Lei Zhang 2016/06/29 22:51:32 Why not just parsed.output.write(...) ?
aberent 2016/07/14 15:41:36 Done.
+ out.write(html_inline.InlineToString(parsed.input.name,
+ EvaluateCondition,
+ preprocess_only=True))
+
+ return 0
+
+if __name__ == '__main__':
+ sys.exit(Main(sys.argv[1:]))

Powered by Google App Engine
This is Rietveld 408576698