Add ICU message format support
[chromium-blink-merge.git] / third_party / closure_compiler / build / inputs.py
bloba403c25bbb425c36e2be3c2859aaefadb33812e1
1 #!/usr/bin/python
2 # Copyright 2014 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 import argparse
7 import ast
8 import collections
9 import os
10 import sys
13 sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
14 import processor
17 def remove_duplicates_with_order(has_duplicates):
18 return list(collections.OrderedDict.fromkeys(has_duplicates))
21 def expand_depends(source, dep):
22 if ":" not in dep:
23 return [dep], {}
25 gyp_relative_path, target = dep.split(":")
26 gyp_path = os.path.join(os.path.dirname(source), gyp_relative_path)
27 gyp_content = ast.literal_eval(open(gyp_path).read())
29 for target_description in gyp_content["targets"]:
30 if target_description["target_name"] == target:
31 break
32 else:
33 raise ValueError("Target '%s' not found in file '%s'" %
34 (target, gyp_path))
36 depends = []
37 externs = []
38 if "variables" in target_description:
39 depends = target_description["variables"].get("depends", [])
40 externs = target_description["variables"].get("externs", [])
42 def attach_gyp_dir(relative_path):
43 return os.path.join(os.path.dirname(gyp_path), relative_path)
45 target_source = attach_gyp_dir(target + ".js")
46 expanded_depends, expanded_externs = resolve_recursive_dependencies(
47 target_source,
48 depends,
49 externs)
51 expanded_depends = map(attach_gyp_dir, expanded_depends)
52 expanded_externs = set(map(attach_gyp_dir, expanded_externs))
54 expanded_depends.append(target_source)
56 return expanded_depends, expanded_externs
59 def resolve_recursive_dependencies(source, input_depends, depends_externs):
60 output_depends = []
61 output_externs = set(depends_externs)
63 for depends in input_depends:
64 expanded_depends, expanded_externs = expand_depends(source, depends)
65 output_depends.extend(expanded_depends)
66 output_externs.update(expanded_externs)
68 output_depends = remove_duplicates_with_order(output_depends)
70 return output_depends, output_externs
73 def GetInputs(args):
74 parser = argparse.ArgumentParser()
75 parser.add_argument("sources", nargs=argparse.ONE_OR_MORE)
76 parser.add_argument("-d", "--depends", nargs=argparse.ZERO_OR_MORE,
77 default=[])
78 parser.add_argument("-e", "--externs", nargs=argparse.ZERO_OR_MORE,
79 default=[])
80 opts = parser.parse_args(args)
82 # TODO(twellington): resolve dependencies for multiple sources.
83 if len(opts.sources) == 1:
84 depends, externs = resolve_recursive_dependencies(
85 os.path.normpath(os.path.join(os.getcwd(), opts.sources[0])),
86 opts.depends,
87 opts.externs)
88 else:
89 depends = opts.depends
90 externs = set(opts.externs)
92 files = set()
93 for file in set(opts.sources) | set(depends) | externs:
94 files.add(file)
95 files.update(processor.Processor(file).included_files)
97 return files
100 if __name__ == "__main__":
101 print "\n".join(GetInputs(sys.argv[1:]))