Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / scripts / modular_build.py
blobf82a6ebcd3c24513dad3cb6c2c8395617912dd20
1 #!/usr/bin/env python
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.
7 """
8 Utilities for the modular DevTools build.
9 """
11 from os import path
12 import os
14 try:
15 import simplejson as json
16 except ImportError:
17 import json
20 def read_file(filename):
21 with open(path.normpath(filename), 'rt') as input:
22 return input.read()
25 def write_file(filename, content):
26 if path.exists(filename):
27 os.remove(filename)
28 with open(filename, 'wt') as output:
29 output.write(content)
32 def bail_error(message):
33 raise Exception(message)
36 def load_and_parse_json(filename):
37 try:
38 return json.loads(read_file(filename))
39 except:
40 print 'ERROR: Failed to parse %s' % filename
41 raise
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))
51 output.write(';')
54 class Descriptors:
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):
65 files = {}
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
72 return files.keys()
74 def module_compiled_files(self, name):
75 files = []
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:
80 files.append(script)
81 return 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
90 result = []
91 unvisited_modules = set(self.modules)
92 temp_modules = set()
94 def visit(parent, name):
95 if name not in unvisited_modules:
96 return None
97 if name not in self.modules:
98 return (parent, name)
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')
103 if deps:
104 for dep_name in deps:
105 bad_dep = visit(name, dep_name)
106 if bad_dep:
107 return bad_dep
108 unvisited_modules.remove(name)
109 temp_modules.remove(name)
110 result.append(name)
111 return None
113 while len(unvisited_modules):
114 for next in unvisited_modules:
115 break
116 failure = visit(None, next)
117 if failure:
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
122 return result
124 def sorted_dependencies_closure(self, module_name):
125 visited = set()
127 def sorted_deps_for_module(name):
128 result = []
129 desc = self.modules[name]
130 deps = desc.get('dependencies', [])
131 for dep in deps:
132 result += sorted_deps_for_module(dep)
133 if name not in visited:
134 result.append(name)
135 visited.add(name)
136 return result
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', [])
168 for dep in deps:
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
180 return module_json