PageRenderTime 53ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/build/android/lint/suppress.py

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