OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # | |
3 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
4 # Use of this source code is governed by a BSD-style license that can be | |
5 # found in the LICENSE file. | |
6 | |
7 """Add all generated lint_result.xml files to suppressions.xml""" | |
8 | |
9 | |
10 import optparse | |
11 import os | |
12 import sys | |
13 from xml.dom import minidom | |
14 | |
15 _BUILD_ANDROID_DIR = os.path.join(os.path.dirname(__file__), '..') | |
16 sys.path.append(_BUILD_ANDROID_DIR) | |
17 | |
18 from pylib import constants | |
19 | |
20 | |
21 _THIS_FILE = os.path.abspath(__file__) | |
22 _CONFIG_PATH = os.path.join(os.path.dirname(_THIS_FILE), 'suppressions.xml') | |
23 _RESULT_FILE_NAME = 'lint_result.xml' | |
24 _DOC = ( | |
25 '\nSTOP! It looks like you want to suppress some lint errors:\n' | |
26 '- Have you tried identifing the offending patch?\n' | |
27 ' Ask the author for a fix and/or revert the patch.\n' | |
28 '- It is preferred to add suppressions in the code instead of\n' | |
29 ' sweeping it under the rug here. See:\n' | |
30 ' http://developer.android.com/tools/debugging/improving-w-lint.html\n' | |
31 '\n' | |
32 'Still reading?\n' | |
33 '- You can edit this file manually to suppress an issue\n' | |
34 ' globally if it is not applicable to the project.\n' | |
35 '- You can also automatically add issues found so for in the\n' | |
36 ' build process by running:\n' | |
cjhopman
2013/11/26 23:44:15
Maybe mention that this string should be edited in
frankf
2013/11/27 03:28:45
Done.
| |
37 ' ' + os.path.relpath(_THIS_FILE, constants.DIR_SOURCE_ROOT) + '\n' | |
38 ) | |
39 _GLOBAL_IGNORE_FLAG = 'ignore' | |
40 | |
41 | |
cjhopman
2013/11/26 23:44:15
I think these three functions would be simpler if
frankf
2013/11/27 03:28:45
Actually, I've changed this to use a namedtuple to
| |
42 def _ParseConfigFile(config_path): | |
43 print 'Parsing %s' % config_path | |
44 issues_dict = {} | |
45 dom = minidom.parse(config_path) | |
46 for issue in dom.getElementsByTagName('issue'): | |
47 issue_id = issue.attributes['id'].value | |
48 if issue.getAttribute('severity') == 'ignore': | |
49 print 'Warning: %s is suppresed globally.' % issue_id | |
cjhopman
2013/11/26 23:44:15
s/suppresed/suppressed/
frankf
2013/11/27 03:28:45
Done.
| |
50 issues_dict[issue_id] = _GLOBAL_IGNORE_FLAG | |
51 else: | |
52 issues_dict[issue_id] = set() | |
53 issues_dict[issue_id].update( | |
54 [p.attributes['path'].value for p in | |
55 issue.getElementsByTagName('ignore')]) | |
56 return issues_dict | |
57 | |
58 | |
59 def _ParseAndMergeResultFile(result_path, issues_dict): | |
60 print 'Parsing and merging %s' % result_path | |
61 dom = minidom.parse(result_path) | |
62 for issue in dom.getElementsByTagName('issue'): | |
63 issue_id = issue.attributes['id'].value | |
64 if issue_id not in issues_dict: | |
65 issues_dict[issue_id] = set() | |
66 elif issues_dict.get(issue_id) == _GLOBAL_IGNORE_FLAG: | |
67 continue | |
68 issues_dict[issue_id].add( | |
69 issue.getElementsByTagName('location')[0].attributes['file'].value) | |
70 | |
71 | |
72 def _WriteConfigFile(config_path, issues_dict): | |
73 print 'Updating %s' % config_path | |
74 new_dom = minidom.getDOMImplementation().createDocument(None, 'lint', None) | |
75 top_element = new_dom.documentElement | |
76 top_element.appendChild(new_dom.createComment(_DOC)) | |
77 for issue_id in sorted(issues_dict.keys()): | |
78 locations = issues_dict[issue_id] | |
79 issue = new_dom.createElement('issue') | |
80 issue.attributes['id'] = issue_id | |
81 if locations == _GLOBAL_IGNORE_FLAG: | |
82 issue.attributes['severity'] = 'ignore' | |
83 else: | |
84 for loc in sorted(locations): | |
85 ignore = new_dom.createElement('ignore') | |
86 ignore.attributes['path'] = loc | |
87 issue.appendChild(ignore) | |
88 top_element.appendChild(issue) | |
89 | |
90 with open(config_path, 'w') as f: | |
91 f.write(new_dom.toprettyxml(indent=' ', encoding='utf-8')) | |
92 | |
93 | |
94 def _Suppress(config_path): | |
95 issues_dict = _ParseConfigFile(config_path) | |
96 | |
97 for dirpath, _, files in os.walk(constants.GetOutDirectory()): | |
98 for f in files: | |
99 if f == _RESULT_FILE_NAME: | |
100 _ParseAndMergeResultFile(os.path.join(dirpath, f), issues_dict) | |
101 | |
102 _WriteConfigFile(config_path, issues_dict) | |
103 | |
104 | |
105 def main(argv): | |
106 parser = optparse.OptionParser() | |
107 parser = optparse.OptionParser() | |
108 parser.add_option('--release', action='store_true', | |
109 help='Whether this is a Release build.') | |
110 options, _ = parser.parse_args() | |
111 | |
112 if options.release: | |
113 constants.SetBuildType('Release') | |
114 else: | |
115 constants.SetBuildType('Debug') | |
116 | |
117 _Suppress(_CONFIG_PATH) | |
118 | |
119 | |
120 if __name__ == '__main__': | |
121 sys.exit(main(sys.argv)) | |
OLD | NEW |