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 import shutil |
| 14 |
| 15 try: |
| 16 import simplejson as json |
| 17 except ImportError: |
| 18 import json |
| 19 |
| 20 |
| 21 def read_file(filename): |
| 22 with open(path.normpath(filename), 'rt') as input: |
| 23 return input.read() |
| 24 |
| 25 |
| 26 def write_file(filename, content): |
| 27 if path.exists(filename): |
| 28 os.remove(filename) |
| 29 with open(filename, 'wt') as output: |
| 30 output.write(content) |
| 31 |
| 32 |
| 33 def bail_error(message): |
| 34 raise Exception(message) |
| 35 |
| 36 |
| 37 class Descriptors: |
| 38 def __init__(self, application_dir, application_descriptor, module_descripto
rs, application_json): |
| 39 self.application_dir = application_dir |
| 40 self.application = application_descriptor |
| 41 self.modules = module_descriptors |
| 42 self.application_json = application_json |
| 43 |
| 44 |
| 45 def all_compiled_files(self): |
| 46 files = {} |
| 47 for name in self.modules: |
| 48 module = self.modules[name] |
| 49 skipped_files = set(module.get('skip_compilation', [])) |
| 50 for script in module.get('scripts', []): |
| 51 if not script in skipped_files: |
| 52 files[path.normpath(path.join(self.application_dir, name, sc
ript))] = True |
| 53 return files.keys() |
| 54 |
| 55 |
| 56 def sorted_modules(self): |
| 57 result = [] |
| 58 unvisited_modules = set(self.modules) |
| 59 temp_modules = set() |
| 60 |
| 61 def visit(parent, name): |
| 62 if not name in unvisited_modules: |
| 63 return None |
| 64 if not name in self.modules: |
| 65 return (parent, name) |
| 66 if name in temp_modules: |
| 67 bail_error('Dependency cycle found at module "%s"' % name) |
| 68 temp_modules.add(name) |
| 69 deps = self.modules[name].get('dependencies') |
| 70 if deps: |
| 71 for dep_name in deps: |
| 72 bad_dep = visit(name, dep_name) |
| 73 if bad_dep: |
| 74 return bad_dep |
| 75 unvisited_modules.remove(name) |
| 76 temp_modules.remove(name) |
| 77 result.append(name) |
| 78 return None |
| 79 |
| 80 while len(unvisited_modules): |
| 81 for next in unvisited_modules: |
| 82 break |
| 83 failure = visit(None, next) |
| 84 if failure: |
| 85 # failure[0] can never be None |
| 86 bail_error('Unknown module "%s" encountered in dependencies of "
%s"' % (failure[1], failure[0])) |
| 87 |
| 88 return result |
| 89 |
| 90 |
| 91 class DescriptorLoader: |
| 92 def __init__(self, application_dir): |
| 93 self.application_dir = application_dir |
| 94 |
| 95 |
| 96 def load_application(self, application_descriptor_name): |
| 97 application_descriptor_filename = path.join(self.application_dir, applic
ation_descriptor_name) |
| 98 application_descriptor_json = read_file(application_descriptor_filename) |
| 99 application_descriptor = {desc['name']: desc for desc in json.loads(appl
ication_descriptor_json)} |
| 100 |
| 101 module_descriptors = {} |
| 102 for (module_name, module) in application_descriptor.items(): |
| 103 if module_descriptors.get(module_name): |
| 104 bail_error('Duplicate definition of module "%s" in %s' % (module
_name, application_descriptor_filename)) |
| 105 module_json_filename = path.join(self.application_dir, module_name,
'module.json') |
| 106 module_descriptors[module_name] = self._read_module_descriptor(modul
e_name) |
| 107 |
| 108 return Descriptors(self.application_dir, application_descriptor, module_
descriptors, application_descriptor_json) |
| 109 |
| 110 def _read_module_descriptor(self, module_name): |
| 111 json_filename = path.join(self.application_dir, module_name, 'module.jso
n') |
| 112 if not path.exists(json_filename): |
| 113 bail_error('Module descriptor %s referenced in %s is missing' % (jso
n_filename, application_descriptor_filename)) |
| 114 module_json = json.loads(read_file(json_filename)) |
| 115 module_json['name'] = module_name |
| 116 return module_json |
| 117 |
| 118 |
| 119 def concatenate_scripts(file_names, module_dir, output_dir, output): |
| 120 for file_name in file_names: |
| 121 output.write('/* %s */\n' % file_name) |
| 122 file_path = path.join(module_dir, file_name) |
| 123 if not path.isfile(file_path): |
| 124 file_path = path.join(output_dir, path.basename(module_dir), file_na
me) |
| 125 output.write(read_file(file_path)) |
| 126 output.write(';') |
OLD | NEW |