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.
13 sys
.path
.insert(0, os
.path
.join(os
.path
.dirname(__file__
), ".."))
17 def remove_duplicates_with_order(has_duplicates
):
18 return list(collections
.OrderedDict
.fromkeys(has_duplicates
))
21 def expand_depends(source
, 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
:
33 raise ValueError("Target '%s' not found in file '%s'" %
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(
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
):
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
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
,
78 parser
.add_argument("-e", "--externs", nargs
=argparse
.ZERO_OR_MORE
,
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])),
89 depends
= opts
.depends
90 externs
= set(opts
.externs
)
93 for file in set(opts
.sources
) |
set(depends
) | externs
:
95 files
.update(processor
.Processor(file).included_files
)
100 if __name__
== "__main__":
101 print "\n".join(GetInputs(sys
.argv
[1:]))