Chromecast ATV: distinguish actual shutdown vs. lifecycle from sleep.
[chromium-blink-merge.git] / third_party / closure_compiler / build / inputs.py
blob9d561462d4772c4fa48ac9594658204568511291
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("source", nargs=1)
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 source = opts.source[0]
83 depends, externs = resolve_recursive_dependencies(
84 source,
85 opts.depends,
86 opts.externs)
88 files = set()
89 for file in {source} | set(depends) | externs:
90 files.add(file)
91 files.update(processor.Processor(file).included_files)
93 return files
96 if __name__ == "__main__":
97 print "\n".join(GetInputs(sys.argv[1:]))