1# Copyright (c) 2012 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Output the list of files to be generated by GRIT from an input.
6"""
7
8from __future__ import print_function
9
10import getopt
11import os
12import sys
13
14from grit import grd_reader
15from grit.node import structure
16from grit.tool import interface
17
18class DetermineBuildInfo(interface.Tool):
19  """Determine what files will be read and output by GRIT.
20Outputs the list of generated files and inputs used to stdout.
21
22Usage: grit buildinfo [-o DIR]
23
24The output directory is used for display only.
25"""
26
27  def __init__(self):
28    pass
29
30  def ShortDescription(self):
31    """Describes this tool for the usage message."""
32    return ('Determine what files will be needed and\n'
33            'output by GRIT with a given input.')
34
35  def Run(self, opts, args):
36    """Main method for the buildinfo tool."""
37    self.output_directory = '.'
38    (own_opts, args) = getopt.getopt(args, 'o:', ('help',))
39    for (key, val) in own_opts:
40      if key == '-o':
41        self.output_directory = val
42      elif key == '--help':
43        self.ShowUsage()
44        sys.exit(0)
45    if len(args) > 0:
46      print('This tool takes exactly one argument: the output directory via -o')
47      return 2
48    self.SetOptions(opts)
49
50    res_tree = grd_reader.Parse(opts.input, debug=opts.extra_verbose)
51
52    langs = {}
53    for output in res_tree.GetOutputFiles():
54      if output.attrs['lang']:
55        langs[output.attrs['lang']] = os.path.dirname(output.GetFilename())
56
57    for lang, dirname in langs.items():
58      old_output_language = res_tree.output_language
59      res_tree.SetOutputLanguage(lang)
60      for node in res_tree.ActiveDescendants():
61        with node:
62          if (isinstance(node, structure.StructureNode) and
63              node.HasFileForLanguage()):
64            path = node.FileForLanguage(lang, dirname, create_file=False,
65                                        return_if_not_generated=False)
66            if path:
67              path = os.path.join(self.output_directory, path)
68              path = os.path.normpath(path)
69              print('%s|%s' % ('rc_all', path))
70      res_tree.SetOutputLanguage(old_output_language)
71
72    for output in res_tree.GetOutputFiles():
73      path = os.path.join(self.output_directory, output.GetFilename())
74      path = os.path.normpath(path)
75      print('%s|%s' % (output.GetType(), path))
76
77    for infile in res_tree.GetInputFiles():
78      print('input|%s' % os.path.normpath(infile))
79