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("source", nargs
=1)
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 source
= opts
.source
[0]
83 depends
, externs
= resolve_recursive_dependencies(
89 for file in {source} |
set(depends
) | externs
:
91 files
.update(processor
.Processor(file).included_files
)
96 if __name__
== "__main__":
97 print "\n".join(GetInputs(sys
.argv
[1:]))