Chromium Code Reviews| 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 collections | |
| 11 import optparse | |
| 12 import os | |
| 13 import sys | |
| 14 from xml.dom import minidom | |
| 15 | |
| 16 _BUILD_ANDROID_DIR = os.path.join(os.path.dirname(__file__), '..') | |
| 17 sys.path.append(_BUILD_ANDROID_DIR) | |
| 18 | |
| 19 from pylib import constants | |
| 20 | |
| 21 | |
| 22 _THIS_FILE = os.path.abspath(__file__) | |
| 23 _CONFIG_PATH = os.path.join(os.path.dirname(_THIS_FILE), 'suppressions.xml') | |
| 24 _RESULT_FILE_NAME = 'lint_result.xml' | |
| 25 _DOC = ( | |
| 26 '\nSTOP! It looks like you want to suppress some lint errors:\n' | |
| 27 '- Have you tried identifing the offending patch?\n' | |
| 28 ' Ask the author for a fix and/or revert the patch.\n' | |
| 29 '- It is preferred to add suppressions in the code instead of\n' | |
| 30 ' sweeping it under the rug here. See:\n\n' | |
| 31 ' http://developer.android.com/tools/debugging/improving-w-lint.html\n' | |
| 32 '\n' | |
| 33 'Still reading?\n' | |
| 34 '- You can edit this file manually to suppress an issue\n' | |
| 35 ' globally if it is not applicable to the project.\n' | |
| 36 '- You can also automatically add issues found so for in the\n' | |
| 37 ' build process by running:\n\n' | |
| 38 ' ' + os.path.relpath(_THIS_FILE, constants.DIR_SOURCE_ROOT) + '\n\n' | |
| 39 ' which will generate this file (Comments are not preserved).\n' | |
| 40 ) | |
| 41 | |
| 42 | |
| 43 _Issue = collections.namedtuple('Issue', ['severity', 'paths']) | |
| 44 | |
| 45 | |
| 46 def _ParseConfigFile(config_path): | |
| 47 print 'Parsing %s' % config_path | |
| 48 issues_dict = {} | |
| 49 dom = minidom.parse(config_path) | |
| 50 for issue in dom.getElementsByTagName('issue'): | |
| 51 issue_id = issue.attributes['id'].value | |
| 52 severity = issue.getAttribute('severity') | |
| 53 paths = set( | |
| 54 [p.attributes['path'].value for p in | |
| 55 issue.getElementsByTagName('ignore')]) | |
| 56 issues_dict[issue_id] = _Issue(severity, paths) | |
| 57 return issues_dict | |
| 58 | |
| 59 | |
| 60 def _ParseAndMergeResultFile(result_path, issues_dict): | |
| 61 print 'Parsing and merging %s' % result_path | |
| 62 dom = minidom.parse(result_path) | |
| 63 for issue in dom.getElementsByTagName('issue'): | |
| 64 issue_id = issue.attributes['id'].value | |
| 65 severity = issue.attributes['severity'].value | |
| 66 path = issue.getElementsByTagName('location')[0].attributes['file'].value | |
| 67 if issue_id not in issues_dict: | |
| 68 issues_dict[issue_id] = _Issue(severity, set()) | |
| 69 issues_dict[issue_id].paths.add(path) | |
| 70 | |
| 71 | |
| 72 def _WriteConfigFile(config_path, issues_dict): | |
| 73 new_dom = minidom.getDOMImplementation().createDocument(None, 'lint', None) | |
| 74 top_element = new_dom.documentElement | |
| 75 top_element.appendChild(new_dom.createComment(_DOC)) | |
| 76 for issue_id in sorted(issues_dict.keys()): | |
| 77 severity = issues_dict[issue_id].severity | |
| 78 paths = issues_dict[issue_id].paths | |
| 79 issue = new_dom.createElement('issue') | |
| 80 issue.attributes['id'] = issue_id | |
| 81 if severity: | |
| 82 issue.attributes['severity'] = severity | |
| 83 if severity == 'ignore': | |
| 84 print 'Warning: [%s] is suppressed globally.' % issue_id | |
| 85 else: | |
| 86 for path in sorted(paths): | |
| 87 ignore = new_dom.createElement('ignore') | |
| 88 ignore.attributes['path'] = path | |
| 89 issue.appendChild(ignore) | |
| 90 top_element.appendChild(issue) | |
| 91 | |
| 92 with open(config_path, 'w') as f: | |
| 93 f.write(new_dom.toprettyxml(indent=' ', encoding='utf-8')) | |
| 94 print 'Updated %s' % config_path | |
| 95 | |
| 96 | |
| 97 def _Suppress(config_path): | |
| 98 issues_dict = _ParseConfigFile(config_path) | |
| 99 | |
| 100 for dirpath, _, files in os.walk(constants.GetOutDirectory()): | |
|
newt (away)
2013/12/03 00:54:16
Can we just pass in the path to lint_result.xml in
frankf
2013/12/03 01:13:03
Yes, there is a trade-off here. With your suggesti
newt (away)
2013/12/03 02:01:00
I'd suggest that if lint.py fails, it print out th
| |
| 101 for f in files: | |
| 102 if f == _RESULT_FILE_NAME: | |
| 103 _ParseAndMergeResultFile(os.path.join(dirpath, f), issues_dict) | |
| 104 | |
| 105 _WriteConfigFile(config_path, issues_dict) | |
| 106 | |
| 107 | |
| 108 def main(argv): | |
| 109 parser = optparse.OptionParser() | |
|
newt (away)
2013/12/03 00:54:16
duplicate line
frankf
2013/12/04 23:29:31
Done.
| |
| 110 parser = optparse.OptionParser() | |
| 111 parser.add_option('--release', action='store_true', | |
| 112 help='Whether this is a Release build.') | |
| 113 options, _ = parser.parse_args() | |
| 114 | |
| 115 if options.release: | |
| 116 constants.SetBuildType('Release') | |
| 117 else: | |
| 118 constants.SetBuildType('Debug') | |
| 119 | |
| 120 _Suppress(_CONFIG_PATH) | |
| 121 | |
| 122 | |
| 123 if __name__ == '__main__': | |
| 124 sys.exit(main(sys.argv)) | |
| OLD | NEW |