blob: 6b0e55867be9751a7d995ef27118538dd3f67dac [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.
4
5"""
6This ensures that each front-end module does not accidentally rely on a module
7that isn't listed as a transitive dependency in the module.json.
8
9How this works:
101. Renames any potential undeclared namespace usage across the entire front-end code
11(e.g. identifiers, strings) into e.g. "$$UndeclaredDependency_SDK$$.Foo".
122. Closure Compiler catches any illegal usage and safely ignores coincidental
13usages (e.g. "Console.Foo" in a string).
14"""
15
16import codecs
17import multiprocessing
18from os import path
19import re
20import shutil
21
22try:
23 import simplejson as json
24except ImportError:
25 import json
26
27special_case_namespaces_path = path.join(path.dirname(path.abspath(__file__)), 'special_case_namespaces.json')
28
29
30class 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
66def 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
77def 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
87def 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()