OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright 2016 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 import codecs | |
7 import multiprocessing | |
8 from os import path | |
9 import re | |
10 import shutil | |
11 | |
12 skip_modules = ['extensions'] | |
dgozman
2016/12/20 07:02:18
why?
chenwilliam
2016/12/20 20:23:35
Good catch. I thought it was an issue because it w
| |
13 | |
14 scripts_path = path.dirname(path.abspath(__file__)) | |
15 devtools_path = path.dirname(scripts_path) | |
16 devtools_frontend_path = path.join(devtools_path, 'front_end') | |
17 | |
18 | |
19 class DependencyChecker(object): | |
20 | |
21 def __init__(self, descriptors, temp_frontend_path): | |
22 self.descriptors = descriptors | |
23 self.temp_frontend_path = temp_frontend_path | |
24 self.module_descriptors = descriptors.modules | |
25 self.modules = set(self.descriptors.sorted_modules()) | |
26 self._copy_frontend() | |
27 | |
28 def enforce_dependencies(self): | |
29 arg_list = [] | |
30 for module in self.modules: | |
31 if module in skip_modules: | |
32 continue | |
33 dependencies = set(self.descriptors.sorted_dependencies_closure(modu le)) | |
34 excluded_modules = self.modules - {module} - dependencies - self._im plicit_dependencies(module) | |
35 excluded_namespaces = [self._map_module_to_namespace(m) for m in exc luded_modules] | |
36 file_paths = [path.join(self.temp_frontend_path, module, file_name) | |
37 for file_name in self.descriptors.module_compiled_file s(module)] | |
38 arg = { | |
39 'excluded_namespaces': excluded_namespaces, | |
40 'file_paths': file_paths, | |
41 } | |
42 arg_list.append(arg) | |
43 parallelize(poison_module, arg_list) | |
44 | |
45 def _implicit_dependencies(self, module): | |
46 """Finds implicit dependencies for workers (which include single files f rom other modules)""" | |
47 implicit_dependencies = set() | |
48 for module_file in self.descriptors.module_compiled_files(module): | |
49 if "../" in module_file: | |
50 components = module_file.split('/') | |
51 implicit_dependencies.add(components[1]) | |
52 return implicit_dependencies | |
53 | |
54 def _copy_frontend(self): | |
55 if path.exists(self.temp_frontend_path): | |
56 shutil.rmtree(self.temp_frontend_path) | |
57 shutil.copytree(devtools_frontend_path, self.temp_frontend_path) | |
58 | |
59 def _map_module_to_namespace(self, module): | |
60 if module == 'css_tracker': | |
61 return 'CSSTracker' | |
62 if module == 'ui_lazy': | |
dgozman
2016/12/20 07:02:18
components_lazy?
chenwilliam
2016/12/20 20:23:35
components_lazy is handled properly by the default
| |
63 return 'UILazy' | |
64 if module == 'sdk' or module == 'ui': | |
65 return module.upper() | |
66 return self.to_camel_case(module) | |
67 | |
68 def to_camel_case(self, snake_string): | |
69 components = snake_string.split('_') | |
70 return ''.join(x.title() for x in components) | |
71 | |
72 | |
73 def poison_module(target): | |
74 excluded_namespaces = target['excluded_namespaces'] | |
75 file_paths = target['file_paths'] | |
76 for file_path in file_paths: | |
77 with codecs.open(file_path, 'r', 'utf-8') as file: | |
78 file_contents = file.read() | |
79 for namespace in excluded_namespaces: | |
80 file_contents = poison_contents_for_namespace(namespace, file_conten ts) | |
81 with codecs.open(file_path, 'w', 'utf-8') as file: | |
82 file.write(file_contents) | |
83 | |
84 | |
85 def poison_contents_for_namespace(namespace, file_contents): | |
86 regex = r'([^.]\b)' + namespace + r'(\b[^:])' | |
87 replace = r'\1$$UndeclaredDependency_%s$$\2' % namespace | |
88 return re.sub(regex, replace, file_contents) | |
89 | |
90 | |
91 def parallelize(fn, arg_list): | |
92 number_of_processes = min(multiprocessing.cpu_count(), 8) | |
93 pool = multiprocessing.Pool(number_of_processes) | |
94 pool.map(fn, arg_list) | |
95 pool.close() | |
96 pool.join() | |
97 | |
OLD | NEW |