Remove workaround for monospace font resources.
[chromium-blink-merge.git] / tools / grit / grit_info.py
blobc0e0ed8e61247318b49ee855c454d69572d3ac4c
1 #!/usr/bin/python
2 # Copyright (c) 2011 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.
6 '''Tool to determine inputs and outputs of a grit file.
7 '''
9 import optparse
10 import os
11 import posixpath
12 import types
13 import sys
14 from grit import grd_reader
15 from grit import util
18 def Outputs(filename, defines):
19 grd = grd_reader.Parse(
20 filename, defines=defines, tags_to_ignore=set(['messages']))
22 target = []
23 lang_folders = {}
24 # Add all explicitly-specified output files
25 for output in grd.GetOutputFiles():
26 path = output.GetFilename()
27 target.append(path)
29 if path.endswith('.h'):
30 path, filename = os.path.split(path)
31 if output.attrs['lang']:
32 lang_folders[output.attrs['lang']] = os.path.dirname(path)
34 # Add all generated files, once for each output language.
35 for node in grd:
36 if node.name == 'structure':
37 # TODO(joi) Should remove the "if sconsdep is true" thing as it is a
38 # hack - see grit/node/structure.py
39 if node.HasFileForLanguage() and node.attrs['sconsdep'] == 'true':
40 for lang in lang_folders:
41 path = node.FileForLanguage(lang, lang_folders[lang],
42 create_file=False,
43 return_if_not_generated=False)
44 if path:
45 target.append(path)
47 return [t.replace('\\', '/') for t in target]
50 def Inputs(filename, defines):
51 grd = grd_reader.Parse(
52 filename, debug=False, defines=defines, tags_to_ignore=set(['messages']))
53 files = []
54 for node in grd:
55 if (node.name == 'structure' or node.name == 'skeleton' or
56 (node.name == 'file' and node.parent and
57 node.parent.name == 'translations')):
58 files.append(node.GetFilePath())
59 elif node.name == 'include':
60 # Only include files that we actually plan on using.
61 if node.SatisfiesOutputCondition():
62 files.append(node.FilenameToOpen())
63 # If it's a flattened node, grab inlined resources too.
64 if node.attrs['flattenhtml'] == 'true':
65 files.extend(node.GetHtmlResourceFilenames())
67 # Add in the grit source files. If one of these change, we want to re-run
68 # grit.
69 grit_root_dir = os.path.dirname(__file__)
70 for root, dirs, filenames in os.walk(grit_root_dir):
71 grit_src = [os.path.join(root, f) for f in filenames
72 if f.endswith('.py') or f == 'resource_ids']
73 files.extend(grit_src)
75 return [f.replace('\\', '/') for f in files]
78 def PrintUsage():
79 print 'USAGE: ./grit_info.py --inputs [-D foo] <grd-files>..'
80 print ' ./grit_info.py --outputs [-D foo] <out-prefix> <grd-files>..'
83 def DoMain(argv):
84 parser = optparse.OptionParser()
85 parser.add_option("--inputs", action="store_true", dest="inputs")
86 parser.add_option("--outputs", action="store_true", dest="outputs")
87 parser.add_option("-D", action="append", dest="defines", default=[])
88 # grit build also supports '-E KEY=VALUE', support that to share command
89 # line flags.
90 parser.add_option("-E", action="append", dest="build_env", default=[])
91 parser.add_option("-w", action="append", dest="whitelist_files", default=[])
93 options, args = parser.parse_args(argv)
95 if not len(args):
96 return None
98 defines = {}
99 for define in options.defines:
100 defines[define] = 1
102 if options.inputs:
103 for filename in args:
104 inputs = Inputs(filename, defines)
105 # Include grd file as second input (works around gyp expecting it).
106 inputs = [inputs[0], filename] + inputs[1:]
107 if options.whitelist_files:
108 inputs.extend(options.whitelist_files)
109 return '\n'.join(inputs)
110 elif options.outputs:
111 if len(args) < 2:
112 return None
114 for f in args[1:]:
115 outputs = [posixpath.join(args[0], f) for f in Outputs(f, defines)]
116 return '\n'.join(outputs)
117 else:
118 return None
121 def main(argv):
122 result = DoMain(argv[1:])
123 if result == None:
124 PrintUsage()
125 return 1
126 print result
127 return 0
130 if __name__ == '__main__':
131 sys.exit(main(sys.argv))