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

Side by Side 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, 5 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 unified diff | Download patch
OLDNEW
(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 '''A resource flattener for removing <include ...> and <if ...> elements
7
8 This script inlines <include...> elements and processes and removes
9 <if...> blocks based on C-preprocessor style defines, hence removing
10 illegal language elements from HTML, Javascript, or CSS source files.
11 This allows them to be pre-processed by standard tools (e.g the Javascript
12 Closure compiler) before using them to build resource files.
13
14 Unlike the HTML flattener used by 'grit build' this does not flatten HTML
15 script, stylesheet, or icon elements; since those are legal HTML.
16
17 This script is not built as a grit tool, since it does not take a grd file
18 as input. It does, however, make use of various grit scripts.
19 '''
20
21 import sys
22
23 if sys.version_info < (2, 7, 0):
24 sys.stderr.write('python 2.7 or later is required run this script\n')
25 sys.exit(1)
26
27 import argparse
28 import os
29
30 from grit.format import html_inline
31 from grit.node import base
32 from grit import util
33
34 def Main(args):
35
36 def OutputFileType(string):
37 '''A type of argument that automatically opens a file, creating'''
38 '''the directory if necessary'''
39 try:
40 if not os.path.exists(os.path.dirname(string)):
41 os.makedirs(os.path.dirname(string))
42 return open(string, 'w')
Lei Zhang 2016/06/29 22:51:32 Probably want 'wb' ?
aberent 2016/07/14 15:41:36 Done.
43 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.
44 raise argparse.ArgumentTypeError("can't open '%s': %s" % (string, e))
45
46 parser = argparse.ArgumentParser(
47 'Flatten <include...> and <if...> elements in resource source files')
48 parser.add_argument('-D',
49 dest='defs',
50 metavar='NAME[=VAL]',
51 help='''
52 Specify a C-preprocessor-like define NAME with optional
53 value VAL (defaults to 1) which will be used to control
54 conditional inclusion of resources.
55 ''',
56 action='append',
57 default=[])
58 parser.add_argument(
59 '-t',
60 dest='platform',
61 metavar='PLATFORM',
62 help='''Specifies the platform the build is targeting; defaults
63 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
64 flag should match what sys.platform would report for your
65 target platform; see grit.node.base.EvaluateCondition.
66 ''',
67 default = sys.platform)
68 parser.add_argument('input',
69 help='The input file',
70 type=argparse.FileType('r'))
71 parser.add_argument('output',
72 help='The output file',
73 type=OutputFileType)
74 parsed = parser.parse_args(args)
75
76 defines = {}
77 for d in parsed.defs:
78 name, value = util.ParseDefine(d)
79 defines[name] = value
80
81 def EvaluateCondition(expression, defines=defines, platform=parsed.platform):
82 return base.Node.EvaluateExpression(expression, defines, platform, [])
83
84 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.
85 out.write(html_inline.InlineToString(parsed.input.name,
86 EvaluateCondition,
87 preprocess_only=True))
88
89 return 0
90
91 if __name__ == '__main__':
92 sys.exit(Main(sys.argv[1:]))
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698