OLD | NEW |
| (Empty) |
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. | |
5 | |
6 import argparse | |
7 import ast | |
8 import collections | |
9 import os | |
10 import sys | |
11 | |
12 | |
13 sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) | |
14 import processor | |
15 | |
16 | |
17 def remove_duplicates_with_order(has_duplicates): | |
18 return list(collections.OrderedDict.fromkeys(has_duplicates)) | |
19 | |
20 | |
21 def expand_depends(source, dep): | |
22 if ":" not in dep: | |
23 return [dep], {} | |
24 | |
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()) | |
28 | |
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)) | |
35 | |
36 depends = [] | |
37 externs = [] | |
38 if "variables" in target_description: | |
39 depends = target_description["variables"].get("depends", []) | |
40 externs = target_description["variables"].get("externs", []) | |
41 | |
42 def attach_gyp_dir(relative_path): | |
43 return os.path.join(os.path.dirname(gyp_path), relative_path) | |
44 | |
45 target_source = attach_gyp_dir(target + ".js") | |
46 expanded_depends, expanded_externs = resolve_recursive_dependencies( | |
47 target_source, | |
48 depends, | |
49 externs) | |
50 | |
51 expanded_depends = map(attach_gyp_dir, expanded_depends) | |
52 expanded_externs = set(map(attach_gyp_dir, expanded_externs)) | |
53 | |
54 expanded_depends.append(target_source) | |
55 | |
56 return expanded_depends, expanded_externs | |
57 | |
58 | |
59 def resolve_recursive_dependencies(source, input_depends, depends_externs): | |
60 output_depends = [] | |
61 output_externs = set(depends_externs) | |
62 | |
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) | |
67 | |
68 output_depends = remove_duplicates_with_order(output_depends) | |
69 | |
70 return output_depends, output_externs | |
71 | |
72 | |
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) | |
81 | |
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) | |
91 | |
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) | |
96 | |
97 return files | |
98 | |
99 | |
100 if __name__ == "__main__": | |
101 print "\n".join(GetInputs(sys.argv[1:])) | |
OLD | NEW |