Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(261)

Side by Side Diff: tools-webrtc/gn_check_autofix.py

Issue 2864213004: Rename tools-webrtc -> tools_webrtc (Closed)
Patch Set: REmove symlink Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools-webrtc/get_landmines.py ('k') | tools-webrtc/gtest-parallel-wrapper.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
4 #
5 # Use of this source code is governed by a BSD-style license
6 # that can be found in the LICENSE file in the root of the source
7 # tree. An additional intellectual property rights grant can be found
8 # in the file PATENTS. All contributing project authors may
9 # be found in the AUTHORS file in the root of the source tree.
10
11 """
12 This tool tries to fix (some) errors reported by `gn gen --check` or
13 `gn check`.
14 It will run `mb gen` in a temporary directory and it is really useful to
15 check for different configurations.
16
17 Usage:
18 $ python tools-webrtc/gn_check_autofix.py -m some_mater -b some_bot
19 or
20 $ python tools-webrtc/gn_check_autofix.py -c some_mb_config
21 """
22
23 import os
24 import re
25 import shutil
26 import subprocess
27 import sys
28 import tempfile
29
30 from collections import defaultdict
31
32 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
33
34 TARGET_RE = re.compile(
35 r'(?P<indentation_level>\s*)\w*\("(?P<target_name>\w*)"\) {$')
36
37 class TemporaryDirectory(object):
38 def __init__(self):
39 self._closed = False
40 self._name = None
41 self._name = tempfile.mkdtemp()
42
43 def __enter__(self):
44 return self._name
45
46 def __exit__(self, exc, value, _tb):
47 if self._name and not self._closed:
48 shutil.rmtree(self._name)
49 self._closed = True
50
51
52 def Run(cmd):
53 print 'Running:', ' '.join(cmd)
54 sub = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
55 return sub.communicate()
56
57 def FixErrors(filename, missing_deps, deleted_sources):
58 with open(filename) as f:
59 lines = f.readlines()
60
61 fixed_file = ''
62 indentation_level = None
63 for line in lines:
64 match = TARGET_RE.match(line)
65 if match:
66 target = match.group('target_name')
67 if target in missing_deps:
68 indentation_level = match.group('indentation_level')
69 elif indentation_level is not None:
70 match = re.match(indentation_level + '}$', line)
71 if match:
72 line = ('deps = [\n' +
73 ''.join(' "' + dep + '",\n' for dep in missing_deps[target]) +
74 ']\n') + line
75 indentation_level = None
76 elif line.strip().startswith('deps'):
77 is_empty_deps = line.strip() == 'deps = []'
78 line = 'deps = [\n' if is_empty_deps else line
79 line += ''.join(' "' + dep + '",\n' for dep in missing_deps[target])
80 line += ']\n' if is_empty_deps else ''
81 indentation_level = None
82
83 if line.strip() not in deleted_sources:
84 fixed_file += line
85
86 with open(filename, 'w') as f:
87 f.write(fixed_file)
88
89 Run(['gn', 'format', filename])
90
91 def Rebase(base_path, dependency_path, dependency):
92 base_path = base_path.split(os.path.sep)
93 dependency_path = dependency_path.split(os.path.sep)
94
95 first_difference = None
96 shortest_length = min(len(dependency_path), len(base_path))
97 for i in range(shortest_length):
98 if dependency_path[i] != base_path[i]:
99 first_difference = i
100 break
101
102 first_difference = first_difference or shortest_length
103 base_path = base_path[first_difference:]
104 dependency_path = dependency_path[first_difference:]
105 return (os.path.sep.join((['..'] * len(base_path)) + dependency_path) +
106 ':' + dependency)
107
108 def main():
109 deleted_sources = set()
110 errors_by_file = defaultdict(lambda: defaultdict(set))
111
112 with TemporaryDirectory() as tmp_dir:
113 mb_script_path = os.path.join(SCRIPT_DIR, 'mb', 'mb.py')
114 mb_config_file_path = os.path.join(SCRIPT_DIR, 'mb', 'mb_config.pyl')
115 mb_gen_command = ([
116 mb_script_path, 'gen',
117 tmp_dir,
118 '--config-file', mb_config_file_path,
119 ] + sys.argv[1:])
120
121 mb_output = Run(mb_gen_command)
122 errors = mb_output[0].split('ERROR')[1:]
123
124 if mb_output[1]:
125 print mb_output[1]
126 return 1
127
128 for error in errors:
129 error = error.splitlines()
130 target_msg = 'The target:'
131 if target_msg not in error:
132 target_msg = 'It is not in any dependency of'
133 if target_msg not in error:
134 print '\n'.join(error)
135 continue
136 index = error.index(target_msg) + 1
137 path, target = error[index].strip().split(':')
138 if error[index+1] in ('is including a file from the target:',
139 'The include file is in the target(s):'):
140 dep = error[index+2].strip()
141 dep_path, dep = dep.split(':')
142 dep = Rebase(path, dep_path, dep)
143 path = os.path.join(path[2:], 'BUILD.gn')
144 errors_by_file[path][target].add(dep)
145 elif error[index+1] == 'has a source file:':
146 deleted_file = '"' + os.path.basename(error[index+2].strip()) + '",'
147 deleted_sources.add(deleted_file)
148 else:
149 print '\n'.join(error)
150 continue
151
152 for path, missing_deps in errors_by_file.items():
153 FixErrors(path, missing_deps, deleted_sources)
154
155 return 0
156
157 if __name__ == '__main__':
158 sys.exit(main())
OLDNEW
« no previous file with comments | « tools-webrtc/get_landmines.py ('k') | tools-webrtc/gtest-parallel-wrapper.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698