3 # Copyright 2014 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
8 Utilities for the modular DevTools build.
15 import simplejson
as json
20 def read_file(filename
):
21 with
open(path
.normpath(filename
), 'rt') as input:
25 def write_file(filename
, content
):
26 if path
.exists(filename
):
28 with
open(filename
, 'wt') as output
:
32 def bail_error(message
):
33 raise Exception(message
)
36 def load_and_parse_json(filename
):
38 return json
.loads(read_file(filename
))
40 print 'ERROR: Failed to parse %s' % filename
44 def concatenate_scripts(file_names
, module_dir
, output_dir
, output
):
45 for file_name
in file_names
:
46 output
.write('/* %s */\n' % file_name
)
47 file_path
= path
.join(module_dir
, file_name
)
48 if not path
.isfile(file_path
):
49 file_path
= path
.join(output_dir
, path
.basename(module_dir
), file_name
)
50 output
.write(read_file(file_path
))
55 def __init__(self
, application_dir
, application_descriptor
, module_descriptors
):
56 self
.application_dir
= application_dir
57 self
.application
= application_descriptor
58 self
.modules
= module_descriptors
59 self
._cached
_sorted
_modules
= None
61 def application_json(self
):
62 return json
.dumps(self
.application
.values())
64 def all_compiled_files(self
):
66 for name
in self
.modules
:
67 module
= self
.modules
[name
]
68 skipped_files
= set(module
.get('skip_compilation', []))
69 for script
in module
.get('scripts', []):
70 if script
not in skipped_files
:
71 files
[path
.normpath(path
.join(self
.application_dir
, name
, script
))] = True
74 def module_compiled_files(self
, name
):
76 module
= self
.modules
.get(name
)
77 skipped_files
= set(module
.get('skip_compilation', []))
78 for script
in module
.get('scripts', []):
79 if script
not in skipped_files
:
83 def module_resources(self
, name
):
84 return [name
+ '/' + resource
for resource
in self
.modules
[name
].get('resources', [])]
86 def sorted_modules(self
):
87 if self
._cached
_sorted
_modules
:
88 return self
._cached
_sorted
_modules
91 unvisited_modules
= set(self
.modules
)
94 def visit(parent
, name
):
95 if name
not in unvisited_modules
:
97 if name
not in self
.modules
:
99 if name
in temp_modules
:
100 bail_error('Dependency cycle found at module "%s"' % name
)
101 temp_modules
.add(name
)
102 deps
= self
.modules
[name
].get('dependencies')
104 for dep_name
in deps
:
105 bad_dep
= visit(name
, dep_name
)
108 unvisited_modules
.remove(name
)
109 temp_modules
.remove(name
)
113 while len(unvisited_modules
):
114 for next
in unvisited_modules
:
116 failure
= visit(None, next
)
118 # failure[0] can never be None
119 bail_error('Unknown module "%s" encountered in dependencies of "%s"' % (failure
[1], failure
[0]))
121 self
._cached
_sorted
_modules
= result
124 def sorted_dependencies_closure(self
, module_name
):
127 def sorted_deps_for_module(name
):
129 desc
= self
.modules
[name
]
130 deps
= desc
.get('dependencies', [])
132 result
+= sorted_deps_for_module(dep
)
133 if name
not in visited
:
138 return sorted_deps_for_module(module_name
)
141 class DescriptorLoader
:
142 def __init__(self
, application_dir
):
143 self
.application_dir
= application_dir
145 def load_application(self
, application_descriptor_name
):
146 return self
.load_applications([application_descriptor_name
])
148 def load_applications(self
, application_descriptor_names
):
149 merged_application_descriptor
= {}
150 all_module_descriptors
= {}
151 for application_descriptor_name
in application_descriptor_names
:
152 module_descriptors
= {}
153 application_descriptor_filename
= path
.join(self
.application_dir
, application_descriptor_name
)
154 application_descriptor
= {desc
['name']: desc
for desc
in load_and_parse_json(application_descriptor_filename
)}
156 for name
in application_descriptor
:
157 merged_application_descriptor
[name
] = application_descriptor
[name
]
159 for (module_name
, module
) in application_descriptor
.items():
160 if module_descriptors
.get(module_name
):
161 bail_error('Duplicate definition of module "%s" in %s' % (module_name
, application_descriptor_filename
))
162 if not all_module_descriptors
.get(module_name
):
163 module_descriptors
[module_name
] = self
._read
_module
_descriptor
(module_name
, application_descriptor_filename
)
164 all_module_descriptors
[module_name
] = module_descriptors
[module_name
]
166 for module
in module_descriptors
.values():
167 deps
= module
.get('dependencies', [])
169 if dep
not in application_descriptor
:
170 bail_error('Module "%s" (dependency of "%s") not listed in application descriptor %s' % (dep
, module
['name'], application_descriptor_filename
))
172 return Descriptors(self
.application_dir
, merged_application_descriptor
, all_module_descriptors
)
174 def _read_module_descriptor(self
, module_name
, application_descriptor_filename
):
175 json_filename
= path
.join(self
.application_dir
, module_name
, 'module.json')
176 if not path
.exists(json_filename
):
177 bail_error('Module descriptor %s referenced in %s is missing' % (json_filename
, application_descriptor_filename
))
178 module_json
= load_and_parse_json(json_filename
)
179 module_json
['name'] = module_name