blob: e312430f7b3c69870a221fa0be1911961b62389f [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:37 +00001# 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 Reformat4c46d092018-04-07 15:32:37 +00004"""
5This ensures that each front-end module does not accidentally rely on a module
6that isn't listed as a transitive dependency in the module.json.
7
8How this works:
91. Renames any potential undeclared namespace usage across the entire front-end code
10(e.g. identifiers, strings) into e.g. "$$UndeclaredDependency_SDK$$.Foo".
112. Closure Compiler catches any illegal usage and safely ignores coincidental
12usages (e.g. "Console.Foo" in a string).
13"""
14
15import codecs
16import multiprocessing
17from os import path
18import re
19import shutil
20
Yang Guo75beda92019-10-28 08:29:25 +010021import special_case_namespaces
22
Blink Reformat4c46d092018-04-07 15:32:37 +000023try:
24 import simplejson as json
25except ImportError:
26 import json
27
Blink Reformat4c46d092018-04-07 15:32:37 +000028
29class 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 Guo75beda92019-10-28 08:29:25 +010037 self._special_case_namespaces = special_case_namespaces.special_case_namespaces
Blink Reformat4c46d092018-04-07 15:32:37 +000038
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
64def 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
75def 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
85def 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()