Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame^] | 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 | """ |
| 6 | This ensures that each front-end module does not accidentally rely on a module |
| 7 | that isn't listed as a transitive dependency in the module.json. |
| 8 | |
| 9 | How this works: |
| 10 | 1. Renames any potential undeclared namespace usage across the entire front-end code |
| 11 | (e.g. identifiers, strings) into e.g. "$$UndeclaredDependency_SDK$$.Foo". |
| 12 | 2. Closure Compiler catches any illegal usage and safely ignores coincidental |
| 13 | usages (e.g. "Console.Foo" in a string). |
| 14 | """ |
| 15 | |
| 16 | import codecs |
| 17 | import multiprocessing |
| 18 | from os import path |
| 19 | import re |
| 20 | import shutil |
| 21 | |
| 22 | try: |
| 23 | import simplejson as json |
| 24 | except ImportError: |
| 25 | import json |
| 26 | |
| 27 | special_case_namespaces_path = path.join(path.dirname(path.abspath(__file__)), 'special_case_namespaces.json') |
| 28 | |
| 29 | |
| 30 | class DependencyPreprocessor(object): |
| 31 | |
| 32 | def __init__(self, descriptors, temp_frontend_path, devtools_frontend_path): |
| 33 | self.descriptors = descriptors |
| 34 | self.temp_frontend_path = temp_frontend_path |
| 35 | self.module_descriptors = descriptors.modules |
| 36 | self.modules = set(self.descriptors.sorted_modules()) |
| 37 | shutil.copytree(devtools_frontend_path, self.temp_frontend_path) |
| 38 | with open(special_case_namespaces_path) as json_file: |
| 39 | self._special_case_namespaces = json.load(json_file) |
| 40 | |
| 41 | def enforce_dependencies(self): |
| 42 | arg_list = [] |
| 43 | for module in self.modules: |
| 44 | dependencies = set(self.descriptors.sorted_dependencies_closure(module)) |
| 45 | excluded_modules = self.modules - {module} - dependencies |
| 46 | excluded_namespaces = [self._map_module_to_namespace(m) for m in excluded_modules] |
| 47 | file_paths = [ |
| 48 | path.join(self.temp_frontend_path, module, file_name) |
| 49 | for file_name in self.descriptors.module_compiled_files(module) |
| 50 | ] |
| 51 | arg = { |
| 52 | 'excluded_namespaces': excluded_namespaces, |
| 53 | 'file_paths': file_paths, |
| 54 | } |
| 55 | arg_list.append(arg) |
| 56 | parallelize(poison_module, arg_list) |
| 57 | |
| 58 | def _map_module_to_namespace(self, module): |
| 59 | return self._special_case_namespaces.get(module, self._to_camel_case(module)) |
| 60 | |
| 61 | def _to_camel_case(self, snake_string): |
| 62 | components = snake_string.split('_') |
| 63 | return ''.join(x.title() for x in components) |
| 64 | |
| 65 | |
| 66 | def poison_module(target): |
| 67 | excluded_namespaces = target['excluded_namespaces'] |
| 68 | file_paths = target['file_paths'] |
| 69 | for file_path in file_paths: |
| 70 | with codecs.open(file_path, 'r', 'utf-8') as file: |
| 71 | file_contents = file.read() |
| 72 | file_contents = poison_contents_for_namespaces(file_contents, excluded_namespaces) |
| 73 | with codecs.open(file_path, 'w', 'utf-8') as file: |
| 74 | file.write(file_contents) |
| 75 | |
| 76 | |
| 77 | def poison_contents_for_namespaces(file_contents, namespaces): |
| 78 | # Technically, should be [^.]\s*\b + NAMESPACES + \b\s*[^:] |
| 79 | # but we rely on clang-format to format like this: |
| 80 | # SomeModule |
| 81 | # .Console |
| 82 | regex = r'([^.]\b)(' + '|'.join(namespaces) + r')(\b[^:])' |
| 83 | replace = r'\1$$UndeclaredDependency_\2$$\3' |
| 84 | return re.sub(regex, replace, file_contents) |
| 85 | |
| 86 | |
| 87 | def parallelize(fn, arg_list): |
| 88 | number_of_processes = min(multiprocessing.cpu_count(), 8) |
| 89 | pool = multiprocessing.Pool(number_of_processes) |
| 90 | pool.map(fn, arg_list) |
| 91 | pool.close() |
| 92 | pool.join() |