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 | |
Yang Guo | 75beda9 | 2019-10-28 08:29:25 +0100 | [diff] [blame^] | 21 | import special_case_namespaces |
| 22 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 23 | try: |
| 24 | import simplejson as json |
| 25 | except ImportError: |
| 26 | import json |
| 27 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 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) |
Yang Guo | 75beda9 | 2019-10-28 08:29:25 +0100 | [diff] [blame^] | 37 | self._special_case_namespaces = special_case_namespaces.special_case_namespaces |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 38 | |
| 39 | def enforce_dependencies(self): |
| 40 | arg_list = [] |
| 41 | for module in self.modules: |
| 42 | dependencies = set(self.descriptors.sorted_dependencies_closure(module)) |
| 43 | excluded_modules = self.modules - {module} - dependencies |
| 44 | excluded_namespaces = [self._map_module_to_namespace(m) for m in excluded_modules] |
| 45 | file_paths = [ |
| 46 | path.join(self.temp_frontend_path, module, file_name) |
| 47 | for file_name in self.descriptors.module_compiled_files(module) |
| 48 | ] |
| 49 | arg = { |
| 50 | 'excluded_namespaces': excluded_namespaces, |
| 51 | 'file_paths': file_paths, |
| 52 | } |
| 53 | arg_list.append(arg) |
| 54 | parallelize(poison_module, arg_list) |
| 55 | |
| 56 | def _map_module_to_namespace(self, module): |
| 57 | return self._special_case_namespaces.get(module, self._to_camel_case(module)) |
| 58 | |
| 59 | def _to_camel_case(self, snake_string): |
| 60 | components = snake_string.split('_') |
| 61 | return ''.join(x.title() for x in components) |
| 62 | |
| 63 | |
| 64 | def poison_module(target): |
| 65 | excluded_namespaces = target['excluded_namespaces'] |
| 66 | file_paths = target['file_paths'] |
| 67 | for file_path in file_paths: |
| 68 | with codecs.open(file_path, 'r', 'utf-8') as file: |
| 69 | file_contents = file.read() |
| 70 | file_contents = poison_contents_for_namespaces(file_contents, excluded_namespaces) |
| 71 | with codecs.open(file_path, 'w', 'utf-8') as file: |
| 72 | file.write(file_contents) |
| 73 | |
| 74 | |
| 75 | def poison_contents_for_namespaces(file_contents, namespaces): |
| 76 | # Technically, should be [^.]\s*\b + NAMESPACES + \b\s*[^:] |
| 77 | # but we rely on clang-format to format like this: |
| 78 | # SomeModule |
| 79 | # .Console |
| 80 | regex = r'([^.]\b)(' + '|'.join(namespaces) + r')(\b[^:])' |
| 81 | replace = r'\1$$UndeclaredDependency_\2$$\3' |
| 82 | return re.sub(regex, replace, file_contents) |
| 83 | |
| 84 | |
| 85 | def parallelize(fn, arg_list): |
| 86 | number_of_processes = min(multiprocessing.cpu_count(), 8) |
| 87 | pool = multiprocessing.Pool(number_of_processes) |
| 88 | pool.map(fn, arg_list) |
| 89 | pool.close() |
| 90 | pool.join() |