Index: tools/gn_check_autofix.py |
diff --git a/tools/gn_check_autofix.py b/tools/gn_check_autofix.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9f7da51a2763f242f65675a95cfd472f5e143fbd |
--- /dev/null |
+++ b/tools/gn_check_autofix.py |
@@ -0,0 +1,106 @@ |
+#!/usr/bin/env python |
+ |
+# Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
+# |
+# Use of this source code is governed by a BSD-style license |
+# that can be found in the LICENSE file in the root of the source |
+# tree. An additional intellectual property rights grant can be found |
+# in the file PATENTS. All contributing project authors may |
+# be found in the AUTHORS file in the root of the source tree. |
+ |
+import os |
+import re |
+import subprocess |
+import sys |
+ |
+from collections import defaultdict |
+ |
+def Run(cmd): |
+ sub = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
+ return sub.communicate() |
+ |
+def fix_missing(filename, missing_deps, deleted_sources): |
+ with open(filename) as f: |
+ contents = f.readlines() |
+ target_re = re.compile(r'(?P<indentation_level>\s*)\w*\("(?P<target_name>\w*)"\) {$') |
+ blocks = "" |
+ last_indentation_level = None |
+ for line_number, line in enumerate(contents): |
+ match = target_re.match(line) |
+ if match: |
+ target = match.group('target_name') |
+ if target in missing_deps: |
+ last_indentation_level = match.group('indentation_level') |
+ elif last_indentation_level is not None: |
+ match = re.match(last_indentation_level + '}$', line) |
+ if match: |
+ line = ('deps = [\n' + |
+ ''.join(' "' + dep + '",\n' for dep in missing_deps[target]) + |
+ ']\n') + line |
+ last_indentation_level = None |
+ elif line.strip().startswith('deps'): |
+ is_inline_deps = line.strip() == 'deps = []' |
+ line = (('deps = [\n' if is_inline_deps else line) + |
+ ''.join(' "' + dep + '",\n' for dep in missing_deps[target]) + |
+ (']\n' if is_inline_deps else '')) |
+ last_indentation_level = None |
+ if line.strip() in deleted_sources: |
+ line = "" |
+ |
+ blocks += line |
+ with open(filename, 'w') as f: |
+ f.write(blocks) |
+ Run(['gn', 'format', filename]) |
+ |
+def rebase(base_path, dependency_path, dependency): |
+ base_path = base_path.split(os.path.sep) |
+ dependency_path = dependency_path.split(os.path.sep) |
+ for common_prefix in range(min(len(dependency_path), len(base_path))): |
+ if dependency_path[common_prefix] != base_path[common_prefix]: |
+ common_prefix -= 1 |
+ break |
+ common_prefix += 1 |
+ base_path = base_path[common_prefix:] |
+ dependency_path = dependency_path[common_prefix:] |
+ return (os.path.sep.join((['..'] * len(base_path)) + dependency_path) + |
+ ':' + dependency) |
+ |
+def main(): |
+ deleted_sources = set() |
+ errors_by_file = defaultdict(lambda: defaultdict(set)) |
+ |
+ gn_output = Run(['gn', 'gen', 'out/gn', '--check'])[0] |
kjellander_webrtc
2016/12/08 11:57:23
Ideally we'd generate a temporary directory here a
|
+ errors = gn_output.split('ERROR')[1:] |
+ for error in errors: |
+ error = error.splitlines() |
+ target_msg = 'The target:' |
+ if target_msg not in error: |
+ target_msg = 'It is not in any dependency of' |
+ if target_msg not in error: |
+ print '\n'.join(error) |
+ continue |
+ index = error.index(target_msg) + 1 |
+ path, target = error[index].strip().split(':') |
+ if error[index+1] in ('is including a file from the target:', |
+ 'The include file is in the target(s):'): |
+ dep = error[index+2].strip() |
+ dep_path, dep = dep.split(':') |
+ dep = rebase(path, dep_path, dep) |
+ path = os.path.join(path[2:], 'BUILD.gn') |
+ errors_by_file[path][target].add(dep) |
+ elif error[index+1] == 'has a source file:': |
+ deleted_file = '"' + os.path.basename(error[index+2].strip()) + '",' |
+ deleted_sources.add(deleted_file) |
+ else: |
+ print '\n'.join(error) |
+ continue |
+ |
+ print errors_by_file |
+ |
+ for path, missing_deps in errors_by_file.items(): |
+ fix_missing(path, missing_deps, deleted_sources) |
+ |
+ return 0 |
+ |
+if __name__ == '__main__': |
+ sys.exit(main()) |