OLD | NEW |
---|---|
(Empty) | |
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 import codecs | |
6 import multiprocessing | |
7 from os import path | |
8 import re | |
9 import shutil | |
10 | |
11 scripts_path = path.dirname(path.abspath(__file__)) | |
12 devtools_path = path.dirname(scripts_path) | |
13 devtools_frontend_path = path.join(devtools_path, 'front_end') | |
14 | |
15 | |
16 class DependencyChecker(object): | |
dgozman
2016/12/27 18:07:36
AFAIU, this is not a checker per se, but rather a
chenwilliam
2016/12/27 23:49:15
good suggestion, I've renamed the file + class.
| |
17 | |
18 def __init__(self, descriptors, temp_frontend_path): | |
19 self.descriptors = descriptors | |
20 self.temp_frontend_path = temp_frontend_path | |
21 self.module_descriptors = descriptors.modules | |
22 self.modules = set(self.descriptors.sorted_modules()) | |
23 shutil.copytree(devtools_frontend_path, self.temp_frontend_path) | |
24 | |
25 def enforce_dependencies(self): | |
26 arg_list = [] | |
27 for module in self.modules: | |
28 dependencies = set(self.descriptors.sorted_dependencies_closure(modu le)) | |
29 excluded_modules = self.modules - {module} - dependencies - self._im plicit_dependencies(module) | |
30 excluded_namespaces = [self._map_module_to_namespace(m) for m in exc luded_modules] | |
31 file_paths = [path.join(self.temp_frontend_path, module, file_name) | |
32 for file_name in self.descriptors.module_compiled_file s(module)] | |
33 arg = { | |
34 'excluded_namespaces': excluded_namespaces, | |
35 'file_paths': file_paths, | |
36 } | |
37 arg_list.append(arg) | |
38 parallelize(poison_module, arg_list) | |
39 | |
40 def _implicit_dependencies(self, module): | |
41 """Finds implicit dependencies for workers (which include single files f rom other modules)""" | |
42 implicit_dependencies = set() | |
43 for module_file in self.descriptors.module_compiled_files(module): | |
44 if "../" in module_file: | |
45 components = module_file.split('/') | |
46 implicit_dependencies.add(components[1]) | |
47 return implicit_dependencies | |
48 | |
49 def _map_module_to_namespace(self, module): | |
50 if module == 'css_tracker': | |
51 return 'CSSTracker' | |
52 if module == 'ui_lazy': | |
53 return 'UILazy' | |
54 if module == 'sdk' or module == 'ui': | |
55 return module.upper() | |
56 return self._to_camel_case(module) | |
57 | |
58 def _to_camel_case(self, snake_string): | |
59 components = snake_string.split('_') | |
60 return ''.join(x.title() for x in components) | |
61 | |
62 | |
63 def poison_module(target): | |
64 excluded_namespaces = target['excluded_namespaces'] | |
65 file_paths = target['file_paths'] | |
66 for file_path in file_paths: | |
67 with codecs.open(file_path, 'r', 'utf-8') as file: | |
68 file_contents = file.read() | |
69 for namespace in excluded_namespaces: | |
70 file_contents = poison_contents_for_namespace(file_contents, namespa ce) | |
71 with codecs.open(file_path, 'w', 'utf-8') as file: | |
72 file.write(file_contents) | |
73 | |
74 | |
75 def poison_contents_for_namespace(file_contents, namespace): | |
76 regex = r'([^.]\b)' + namespace + r'(\b[^:])' | |
77 replace = r'\1$$UndeclaredDependency_%s$$\2' % namespace | |
78 return re.sub(regex, replace, file_contents) | |
79 | |
80 | |
81 def parallelize(fn, arg_list): | |
82 number_of_processes = min(multiprocessing.cpu_count(), 8) | |
83 pool = multiprocessing.Pool(number_of_processes) | |
84 pool.map(fn, arg_list) | |
85 pool.close() | |
86 pool.join() | |
87 | |
OLD | NEW |