OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import codecs |
| 6 import multiprocessing |
| 7 from os import path |
| 8 import re |
| 9 import shutil |
| 10 |
| 11 try: |
| 12 import simplejson as json |
| 13 except ImportError: |
| 14 import json |
| 15 |
| 16 special_case_namespaces_path = path.join(path.dirname(path.abspath(__file__)), '
special_case_namespaces.json') |
| 17 |
| 18 |
| 19 class DependencyPreprocessor(object): |
| 20 |
| 21 def __init__(self, descriptors, temp_frontend_path, devtools_frontend_path): |
| 22 self.descriptors = descriptors |
| 23 self.temp_frontend_path = temp_frontend_path |
| 24 self.module_descriptors = descriptors.modules |
| 25 self.modules = set(self.descriptors.sorted_modules()) |
| 26 shutil.copytree(devtools_frontend_path, self.temp_frontend_path) |
| 27 with open(special_case_namespaces_path) as json_file: |
| 28 self._special_case_namespaces = json.load(json_file) |
| 29 |
| 30 def enforce_dependencies(self): |
| 31 arg_list = [] |
| 32 for module in self.modules: |
| 33 dependencies = set(self.descriptors.sorted_dependencies_closure(modu
le)) |
| 34 excluded_modules = self.modules - {module} - dependencies - self._tr
ansitive_implicit_dependencies(module) |
| 35 excluded_namespaces = [self._map_module_to_namespace(m) for m in exc
luded_modules] |
| 36 file_paths = [path.join(self.temp_frontend_path, module, file_name) |
| 37 for file_name in self.descriptors.module_compiled_file
s(module)] |
| 38 arg = { |
| 39 'excluded_namespaces': excluded_namespaces, |
| 40 'file_paths': file_paths, |
| 41 } |
| 42 arg_list.append(arg) |
| 43 parallelize(poison_module, arg_list) |
| 44 |
| 45 def _transitive_implicit_dependencies(self, module): |
| 46 """Finds implicit dependencies for workers (which cherry-pick files from
other modules)""" |
| 47 explicit_dependencies = self.descriptors.sorted_dependencies_closure(mod
ule) |
| 48 implicit_dependencies = set() |
| 49 for explicit_dependency in explicit_dependencies: |
| 50 implicit_dependencies |= self._implicit_dependencies_for_module(expl
icit_dependency) |
| 51 return implicit_dependencies |
| 52 |
| 53 def _implicit_dependencies_for_module(self, module): |
| 54 implicit_dependencies = set() |
| 55 for module_file in self.descriptors.module_compiled_files(module): |
| 56 if "../" in module_file: |
| 57 components = module_file.split('/') |
| 58 implicit_dependencies.add(components[1]) |
| 59 return implicit_dependencies |
| 60 |
| 61 def _map_module_to_namespace(self, module): |
| 62 return self._special_case_namespaces.get(module, self._to_camel_case(mod
ule)) |
| 63 |
| 64 def _to_camel_case(self, snake_string): |
| 65 components = snake_string.split('_') |
| 66 return ''.join(x.title() for x in components) |
| 67 |
| 68 |
| 69 def poison_module(target): |
| 70 excluded_namespaces = target['excluded_namespaces'] |
| 71 file_paths = target['file_paths'] |
| 72 for file_path in file_paths: |
| 73 with codecs.open(file_path, 'r', 'utf-8') as file: |
| 74 file_contents = file.read() |
| 75 file_contents = poison_contents_for_namespaces(file_contents, excluded_n
amespaces) |
| 76 with codecs.open(file_path, 'w', 'utf-8') as file: |
| 77 file.write(file_contents) |
| 78 |
| 79 |
| 80 def poison_contents_for_namespaces(file_contents, namespaces): |
| 81 # Technically, should be [^.]\s*\b + NAMESPACES + \b\s*[^:] |
| 82 # but we rely on clang-format to format like this: |
| 83 # SomeModule |
| 84 # .Console |
| 85 regex = r'([^.]\b)(' + '|'.join(namespaces) + r')(\b[^:])' |
| 86 replace = r'\1$$UndeclaredDependency_\2$$\3' |
| 87 return re.sub(regex, replace, file_contents) |
| 88 |
| 89 |
| 90 def parallelize(fn, arg_list): |
| 91 number_of_processes = min(multiprocessing.cpu_count(), 8) |
| 92 pool = multiprocessing.Pool(number_of_processes) |
| 93 pool.map(fn, arg_list) |
| 94 pool.close() |
| 95 pool.join() |
OLD | NEW |