| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # | |
| 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. | |
| 6 | |
| 7 """ | |
| 8 Utilities for the modular DevTools build. | |
| 9 """ | |
| 10 | |
| 11 from os import path | |
| 12 import os | |
| 13 | |
| 14 try: | |
| 15 import simplejson as json | |
| 16 except ImportError: | |
| 17 import json | |
| 18 | |
| 19 | |
| 20 def read_file(filename): | |
| 21 with open(path.normpath(filename), 'rt') as input: | |
| 22 return input.read() | |
| 23 | |
| 24 | |
| 25 def write_file(filename, content): | |
| 26 if path.exists(filename): | |
| 27 os.remove(filename) | |
| 28 directory = path.dirname(filename) | |
| 29 if not path.exists(directory): | |
| 30 os.makedirs(directory) | |
| 31 with open(filename, 'wt') as output: | |
| 32 output.write(content) | |
| 33 | |
| 34 | |
| 35 def bail_error(message): | |
| 36 raise Exception(message) | |
| 37 | |
| 38 | |
| 39 def load_and_parse_json(filename): | |
| 40 try: | |
| 41 return json.loads(read_file(filename)) | |
| 42 except: | |
| 43 print 'ERROR: Failed to parse %s' % filename | |
| 44 raise | |
| 45 | |
| 46 | |
| 47 def concatenate_scripts(file_names, module_dir, output_dir, output): | |
| 48 for file_name in file_names: | |
| 49 output.write('/* %s */\n' % file_name) | |
| 50 file_path = path.join(module_dir, file_name) | |
| 51 if not path.isfile(file_path): | |
| 52 file_path = path.join(output_dir, path.basename(module_dir), file_na
me) | |
| 53 output.write(read_file(file_path)) | |
| 54 output.write(';') | |
| 55 | |
| 56 | |
| 57 class Descriptors: | |
| 58 def __init__(self, application_dir, application_descriptor, module_descripto
rs, has_html): | |
| 59 self.application_dir = application_dir | |
| 60 self.application = application_descriptor | |
| 61 self.modules = module_descriptors | |
| 62 self._cached_sorted_modules = None | |
| 63 self.has_html = has_html | |
| 64 | |
| 65 def application_json(self): | |
| 66 result = dict() | |
| 67 result['modules'] = self.application.values() | |
| 68 result['has_html'] = self.has_html | |
| 69 return json.dumps(result) | |
| 70 | |
| 71 def all_compiled_files(self): | |
| 72 files = {} | |
| 73 for name in self.modules: | |
| 74 module = self.modules[name] | |
| 75 skipped_files = set(module.get('skip_compilation', [])) | |
| 76 for script in module.get('scripts', []): | |
| 77 if script not in skipped_files: | |
| 78 files[path.normpath(path.join(self.application_dir, name, sc
ript))] = True | |
| 79 return files.keys() | |
| 80 | |
| 81 def module_compiled_files(self, name): | |
| 82 files = [] | |
| 83 module = self.modules.get(name) | |
| 84 skipped_files = set(module.get('skip_compilation', [])) | |
| 85 for script in module.get('scripts', []): | |
| 86 if script not in skipped_files: | |
| 87 files.append(script) | |
| 88 return files | |
| 89 | |
| 90 def module_resources(self, name): | |
| 91 return [name + '/' + resource for resource in self.modules[name].get('re
sources', [])] | |
| 92 | |
| 93 def sorted_modules(self): | |
| 94 if self._cached_sorted_modules: | |
| 95 return self._cached_sorted_modules | |
| 96 | |
| 97 result = [] | |
| 98 unvisited_modules = set(self.modules) | |
| 99 temp_modules = set() | |
| 100 | |
| 101 def visit(parent, name): | |
| 102 if name not in unvisited_modules: | |
| 103 return None | |
| 104 if name not in self.modules: | |
| 105 return (parent, name) | |
| 106 if name in temp_modules: | |
| 107 bail_error('Dependency cycle found at module "%s"' % name) | |
| 108 temp_modules.add(name) | |
| 109 deps = self.modules[name].get('dependencies') | |
| 110 if deps: | |
| 111 for dep_name in deps: | |
| 112 bad_dep = visit(name, dep_name) | |
| 113 if bad_dep: | |
| 114 return bad_dep | |
| 115 unvisited_modules.remove(name) | |
| 116 temp_modules.remove(name) | |
| 117 result.append(name) | |
| 118 return None | |
| 119 | |
| 120 while len(unvisited_modules): | |
| 121 for next in unvisited_modules: | |
| 122 break | |
| 123 failure = visit(None, next) | |
| 124 if failure: | |
| 125 # failure[0] can never be None | |
| 126 bail_error('Unknown module "%s" encountered in dependencies of "
%s"' % (failure[1], failure[0])) | |
| 127 | |
| 128 self._cached_sorted_modules = result | |
| 129 return result | |
| 130 | |
| 131 def sorted_dependencies_closure(self, module_name): | |
| 132 visited = set() | |
| 133 | |
| 134 def sorted_deps_for_module(name): | |
| 135 result = [] | |
| 136 desc = self.modules[name] | |
| 137 deps = desc.get('dependencies', []) | |
| 138 for dep in deps: | |
| 139 result += sorted_deps_for_module(dep) | |
| 140 if name not in visited: | |
| 141 result.append(name) | |
| 142 visited.add(name) | |
| 143 return result | |
| 144 | |
| 145 return sorted_deps_for_module(module_name) | |
| 146 | |
| 147 | |
| 148 class DescriptorLoader: | |
| 149 def __init__(self, application_dir): | |
| 150 self.application_dir = application_dir | |
| 151 | |
| 152 def load_application(self, application_descriptor_name): | |
| 153 return self.load_applications([application_descriptor_name]) | |
| 154 | |
| 155 def load_applications(self, application_descriptor_names): | |
| 156 merged_application_descriptor = {} | |
| 157 all_module_descriptors = {} | |
| 158 has_html = False | |
| 159 for application_descriptor_name in application_descriptor_names: | |
| 160 module_descriptors = {} | |
| 161 application_descriptor_filename = path.join(self.application_dir, ap
plication_descriptor_name) | |
| 162 descriptor_json = load_and_parse_json(application_descriptor_filenam
e) | |
| 163 application_descriptor = {desc['name']: desc for desc in descriptor_
json['modules']} | |
| 164 has_html = descriptor_json['has_html'] | |
| 165 | |
| 166 for name in application_descriptor: | |
| 167 merged_application_descriptor[name] = application_descriptor[nam
e] | |
| 168 | |
| 169 for (module_name, module) in application_descriptor.items(): | |
| 170 if module_descriptors.get(module_name): | |
| 171 bail_error('Duplicate definition of module "%s" in %s' % (mo
dule_name, application_descriptor_filename)) | |
| 172 if not all_module_descriptors.get(module_name): | |
| 173 module_descriptors[module_name] = self._read_module_descript
or(module_name, application_descriptor_filename) | |
| 174 all_module_descriptors[module_name] = module_descriptors[mod
ule_name] | |
| 175 | |
| 176 for module in module_descriptors.values(): | |
| 177 deps = module.get('dependencies', []) | |
| 178 for dep in deps: | |
| 179 if dep not in application_descriptor: | |
| 180 bail_error('Module "%s" (dependency of "%s") not listed
in application descriptor %s' % (dep, module['name'], application_descriptor_fil
ename)) | |
| 181 | |
| 182 return Descriptors(self.application_dir, merged_application_descriptor,
all_module_descriptors, has_html) | |
| 183 | |
| 184 def _read_module_descriptor(self, module_name, application_descriptor_filena
me): | |
| 185 json_filename = path.join(self.application_dir, module_name, 'module.jso
n') | |
| 186 if not path.exists(json_filename): | |
| 187 bail_error('Module descriptor %s referenced in %s is missing' % (jso
n_filename, application_descriptor_filename)) | |
| 188 module_json = load_and_parse_json(json_filename) | |
| 189 module_json['name'] = module_name | |
| 190 return module_json | |
| OLD | NEW |