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