1#!/usr/bin/env python
2# Copyright (c) 2012 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"""Output the list of files to be generated by GRIT from an input.
7"""
8
9import getopt
10import os
11
12from grit import grd_reader
13from grit.node import structure
14from grit.tool import interface
15
16class DetermineBuildInfo(interface.Tool):
17  """Determine what files will be read and output by GRIT."""
18
19  def __init__(self):
20    pass
21
22  def ShortDescription(self):
23    """Describes this tool for the usage message."""
24    return ('Determine what files will be needed and\n'
25            'output by GRIT with a given input.')
26
27  def Run(self, opts, args):
28    """Main method for the buildinfo tool.  Outputs the list
29       of generated files and inputs used to stdout."""
30    self.output_directory = '.'
31    (own_opts, args) = getopt.getopt(args, 'o:')
32    for (key, val) in own_opts:
33      if key == '-o':
34        self.output_directory = val
35    if len(args) > 0:
36      print 'This tool takes exactly one argument: the output directory via -o'
37      return 2
38    self.SetOptions(opts)
39
40    res_tree = grd_reader.Parse(opts.input, debug=opts.extra_verbose)
41
42    langs = {}
43    for output in res_tree.GetOutputFiles():
44      if output.attrs['lang']:
45        langs[output.attrs['lang']] = os.path.dirname(output.GetFilename())
46
47    for lang, dirname in langs.iteritems():
48      old_output_language = res_tree.output_language
49      res_tree.SetOutputLanguage(lang)
50      for node in res_tree.ActiveDescendants():
51        with node:
52          if (isinstance(node, structure.StructureNode) and
53              node.HasFileForLanguage()):
54            path = node.FileForLanguage(lang, dirname, create_file=False,
55                                        return_if_not_generated=False)
56            if path:
57              path = os.path.join(self.output_directory, path)
58              path = os.path.normpath(path)
59              print '%s|%s' % ('rc_all', path)
60      res_tree.SetOutputLanguage(old_output_language)
61
62    for output in res_tree.GetOutputFiles():
63      path = os.path.join(self.output_directory, output.GetFilename())
64      path = os.path.normpath(path)
65      print '%s|%s' % (output.GetType(), path)
66
67    for infile in res_tree.GetInputFiles():
68      print 'input|%s' % os.path.normpath(infile)
69