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# pylint: disable=no-member 10 11 12import collections 13import optparse 14import os 15import sys 16from xml.dom import minidom 17 18_BUILD_ANDROID_DIR = os.path.join(os.path.dirname(__file__), '..') 19sys.path.append(_BUILD_ANDROID_DIR) 20 21from pylib.constants import host_paths 22 23 24_THIS_FILE = os.path.abspath(__file__) 25_CONFIG_PATH = os.path.join(os.path.dirname(_THIS_FILE), 'suppressions.xml') 26_DOC = ( 27 '\nSTOP! It looks like you want to suppress some lint errors:\n' 28 '- Have you tried identifing the offending patch?\n' 29 ' Ask the author for a fix and/or revert the patch.\n' 30 '- It is preferred to add suppressions in the code instead of\n' 31 ' sweeping it under the rug here. See:\n\n' 32 ' http://developer.android.com/tools/debugging/improving-w-lint.html\n' 33 '\n' 34 'Still reading?\n' 35 '- You can edit this file manually to suppress an issue\n' 36 ' globally if it is not applicable to the project.\n' 37 '- You can also automatically add issues found so for in the\n' 38 ' build process by running:\n\n' 39 ' ' + os.path.relpath(_THIS_FILE, host_paths.DIR_SOURCE_ROOT) + '\n\n' 40 ' which will generate this file (Comments are not preserved).\n' 41 ' Note: PRODUCT_DIR will be substituted at run-time with actual\n' 42 ' directory path (e.g. out/Debug)\n' 43) 44 45 46_Issue = collections.namedtuple('Issue', ['severity', 'paths', 'regexps']) 47 48 49def _ParseConfigFile(config_path): 50 print 'Parsing %s' % config_path 51 issues_dict = {} 52 dom = minidom.parse(config_path) 53 for issue in dom.getElementsByTagName('issue'): 54 issue_id = issue.attributes['id'].value 55 severity = issue.getAttribute('severity') 56 57 path_elements = ( 58 p.attributes.get('path') 59 for p in issue.getElementsByTagName('ignore')) 60 paths = set(p.value for p in path_elements if p) 61 62 regexp_elements = ( 63 p.attributes.get('regexp') 64 for p in issue.getElementsByTagName('ignore')) 65 regexps = set(r.value for r in regexp_elements if r) 66 67 issues_dict[issue_id] = _Issue(severity, paths, regexps) 68 return issues_dict 69 70 71def _ParseAndMergeResultFile(result_path, issues_dict): 72 print 'Parsing and merging %s' % result_path 73 dom = minidom.parse(result_path) 74 for issue in dom.getElementsByTagName('issue'): 75 issue_id = issue.attributes['id'].value 76 severity = issue.attributes['severity'].value 77 path = issue.getElementsByTagName('location')[0].attributes['file'].value 78 if issue_id not in issues_dict: 79 issues_dict[issue_id] = _Issue(severity, set(), set()) 80 issues_dict[issue_id].paths.add(path) 81 82 83def _WriteConfigFile(config_path, issues_dict): 84 new_dom = minidom.getDOMImplementation().createDocument(None, 'lint', None) 85 top_element = new_dom.documentElement 86 top_element.appendChild(new_dom.createComment(_DOC)) 87 for issue_id, issue in sorted(issues_dict.iteritems(), key=lambda i: i[0]): 88 issue_element = new_dom.createElement('issue') 89 issue_element.attributes['id'] = issue_id 90 if issue.severity: 91 issue_element.attributes['severity'] = issue.severity 92 if issue.severity == 'ignore': 93 print 'Warning: [%s] is suppressed globally.' % issue_id 94 else: 95 for path in sorted(issue.paths): 96 ignore_element = new_dom.createElement('ignore') 97 ignore_element.attributes['path'] = path 98 issue_element.appendChild(ignore_element) 99 for regexp in sorted(issue.regexps): 100 ignore_element = new_dom.createElement('ignore') 101 ignore_element.attributes['regexp'] = regexp 102 issue_element.appendChild(ignore_element) 103 top_element.appendChild(issue_element) 104 105 with open(config_path, 'w') as f: 106 f.write(new_dom.toprettyxml(indent=' ', encoding='utf-8')) 107 print 'Updated %s' % config_path 108 109 110def _Suppress(config_path, result_path): 111 issues_dict = _ParseConfigFile(config_path) 112 _ParseAndMergeResultFile(result_path, issues_dict) 113 _WriteConfigFile(config_path, issues_dict) 114 115 116def main(): 117 parser = optparse.OptionParser(usage='%prog RESULT-FILE') 118 _, args = parser.parse_args() 119 120 if len(args) != 1 or not os.path.exists(args[0]): 121 parser.error('Must provide RESULT-FILE') 122 123 _Suppress(_CONFIG_PATH, args[0]) 124 125 126if __name__ == '__main__': 127 main() 128