Index: build/android/lint.py |
diff --git a/build/android/lint.py b/build/android/lint.py |
new file mode 100755 |
index 0000000000000000000000000000000000000000..90f74237b400974a0c5b1dfbbe94920a9f47428e |
--- /dev/null |
+++ b/build/android/lint.py |
@@ -0,0 +1,224 @@ |
+#!/usr/bin/env python |
+# |
+# Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+"""Runs Android's lint tool.""" |
+ |
+ |
+import optparse |
+import os |
+import sys |
+import tempfile |
+from xml.dom import minidom |
+ |
+from pylib import cmd_helper |
+from pylib import constants |
+ |
+ |
+# Dirs relative to /src to exclude from source linting. |
+_EXCLUDE_SRC_DIRS = [ |
+ 'third_party', |
+] |
+# Dirs relative to out/<type> to exclude from class linting. |
+_EXCLUDE_CLASSES_DIRS = [ |
+ 'gen/eyesfree_java', |
+] |
+_LINT_EXE = os.path.join(constants.ANDROID_SDK_ROOT, 'tools', 'lint') |
+_DOC = ('\nSTOP! It looks like you want to suppress some lint errors:\n' |
+ '- Have you tried identifing the offending patch?\n' |
+ ' Ask the author for a fix and/or revert the patch.\n' |
+ '- It is preferred to add suppressions in the code instead of\n' |
+ ' sweeping it under the rug here. See:\n' |
+ ' http://developer.android.com/tools/debugging/improving-w-lint.html\n' |
+ '\n' |
+ 'Still reading?\n' |
+ '- You can edit this file manually to suppress an issue\n' |
+ ' globally if it is not applicable to the project.\n' |
+ '- You can also rebaseline this file by:\n' |
+ ' %s -r\n' |
+ ' If the rebaseline is temporary, please make sure there is a bug\n' |
+ ' filed and assigned to the author of the offending patch.\n') |
+ |
+ |
+def _GetClassesDirs(): |
+ """Get a list of absolute paths to compiled 'classes' directories.""" |
+ classes_dir = [] |
+ for dirpath, _, _ in os.walk(constants.GetOutDirectory()): |
+ if any([dirpath.startswith(os.path.join(constants.GetOutDirectory(), x)) for |
+ x in _EXCLUDE_CLASSES_DIRS]): |
+ continue |
+ if os.path.basename(dirpath) == 'classes': |
+ classes_dir.append(dirpath) |
+ |
+ assert classes_dir, 'Did not find class files. Did you build?' |
+ return classes_dir |
+ |
+ |
+def _GetSrcDirs(): |
+ """Get a list of absolute paths to Java 'src' directories.""" |
+ source_dirs = [] |
+ for dirpath, _, _ in os.walk(constants.DIR_SOURCE_ROOT): |
+ if any([dirpath.startswith(os.path.join(constants.DIR_SOURCE_ROOT, x)) for |
+ x in _EXCLUDE_SRC_DIRS]): |
+ continue |
+ if os.path.basename(dirpath) == 'src' and 'java' in dirpath: |
+ source_dirs.append(dirpath) |
+ |
+ assert source_dirs, 'Did not find any src directories.' |
+ return source_dirs |
+ |
+ |
+def _Lint(project_path, result_xml_path=None, report_html=False, |
+ config_xml_path=None, src_dirs=None, classes_dirs=None): |
+ """Execute the lint tool. |
+ |
+ Args: |
+ project_path: Absoluate path to the project dir containing the manifest. |
+ result_xml_path: Result file generated by lint using --xml. |
+ report_html: Whether to generate a pretty html report instead of outputing |
+ to stdout. |
+ config_xml_path: Config file passed to lint using --config. |
+ src_dirs: List of absolute paths to Java 'src' dirs. |
+ classes_dirs: List of absolute paths to 'classes' dirs. |
+ |
+ Returns: |
+ Non-zero on failure. |
+ """ |
+ |
+ cmd = [_LINT_EXE, '-Werror', '--exitcode', '--showall'] |
+ |
+ if result_xml_path: |
+ cmd.extend(['--xml', result_xml_path]) |
+ |
+ if report_html: |
+ _, html_path = tempfile.mkstemp(prefix='lint_', suffix='.html') |
+ cmd.extend(['--html', html_path]) |
+ |
+ if config_xml_path: |
+ cmd.extend(['--config', config_xml_path]) |
+ |
+ if not src_dirs: |
+ src_dirs = _GetSrcDirs() |
+ for src in src_dirs: |
+ cmd.extend(['--sources', os.path.relpath(src, constants.DIR_SOURCE_ROOT)]) |
+ |
+ if not classes_dirs: |
+ classes_dirs = _GetClassesDirs() |
+ for cls in classes_dirs: |
+ if not os.path.isabs(cls): |
+ cls = os.path.join(constants.GetOutDirectory(), cls) |
+ cmd.extend(['--classpath', os.path.relpath(cls, constants.DIR_SOURCE_ROOT)]) |
+ |
+ cmd.append(project_path) |
+ return cmd_helper.RunCmd(cmd, cwd=constants.DIR_SOURCE_ROOT) |
+ |
+ |
+def _Rebaseline(result_xml_path, config_xml_path, script_path): |
+ """Create a config file which ignores all lint issues. |
+ |
+ Args: |
+ result_xml_path: Result file generated by lint using --xml. |
+ config_xml_path: Config file passed to lint using --config. |
+ script_path: Absolute path to the script being executed. |
+ """ |
+ dom = minidom.parse(result_xml_path) |
+ issues = {} |
+ for issue in dom.getElementsByTagName('issue'): |
+ issue_id = issue.attributes['id'].value |
+ if issue_id not in issues: |
+ issues[issue_id] = set() |
+ issues[issue_id].add( |
+ issue.getElementsByTagName('location')[0].attributes['file'].value) |
+ |
+ # Get a list of globally ignored issues from existing config file. |
+ globally_ignored_issues = [] |
+ if os.path.exists(config_xml_path): |
+ dom = minidom.parse(config_xml_path) |
+ for issue in dom.getElementsByTagName('issue'): |
+ if issue.getAttribute('severity') == 'ignore': |
+ globally_ignored_issues.append(issue.attributes['id'].value) |
+ |
+ new_dom = minidom.getDOMImplementation().createDocument(None, 'lint', None) |
+ top_element = new_dom.documentElement |
+ comment = _DOC % os.path.relpath(script_path, constants.DIR_SOURCE_ROOT) |
+ top_element.appendChild(new_dom.createComment(comment)) |
+ for issue_id, locations in issues.iteritems(): |
+ issue = new_dom.createElement('issue') |
+ issue.attributes['id'] = issue_id |
+ if issue_id in globally_ignored_issues: |
+ print 'Warning: %s is suppresed globally.' % issue_id |
+ issue.attributes['severity'] = 'ignore' |
+ else: |
+ for loc in locations: |
+ ignore = new_dom.createElement('ignore') |
+ ignore.attributes['path'] = loc |
+ issue.appendChild(ignore) |
+ top_element.appendChild(issue) |
+ |
+ with open(config_xml_path, 'w') as f: |
+ f.write(new_dom.toprettyxml(indent=' ', encoding='utf-8')) |
+ |
+ |
+def Run(project_path, config_xml_path, script_path, src_dirs, classes_dirs): |
+ """ |
+ Args: |
+ project_path: Absolute path to the project dir containing the manifest. |
+ config_xml_path: Config file passed to lint using --config. |
+ script_path: Absolute path to the script being executed. |
+ src_dirs: List of absolute paths to Java 'src' dirs. |
+ classes_dirs: List of absolute paths to 'classes' dirs. |
+ |
+ Returns: |
+ Non-zero on failure. |
+ """ |
+ assert os.path.exists(project_path), 'No such project path: %s' % project_path |
+ |
+ parser = optparse.OptionParser() |
+ parser.add_option('-r', '--rebaseline', |
+ action='store_true', |
+ help='Rebaseline existing lint issues.') |
+ parser.add_option('--release', |
+ action='store_true', |
+ help='Whether this is a Release build.') |
+ parser.add_option('--html', |
+ action='store_true', |
+ help='Generate a html report instead of writing to stdout.') |
+ options, _ = parser.parse_args() |
+ |
+ if options.release: |
+ constants.SetBuildType('Release') |
+ else: |
+ constants.SetBuildType('Debug') |
+ |
+ rc = 0 |
+ if options.rebaseline: |
+ result_xml_path = tempfile.NamedTemporaryFile() |
+ _Lint(project_path, result_xml_path=result_xml_path.name, src_dirs=src_dirs, |
+ classes_dirs=classes_dirs) |
+ _Rebaseline(result_xml_path.name, config_xml_path, script_path) |
+ print 'Updated %s' % config_xml_path |
+ else: |
+ rc = _Lint(project_path, report_html=options.html, |
+ config_xml_path=config_xml_path, src_dirs=src_dirs, |
+ classes_dirs=classes_dirs) |
+ if rc: |
+ print ('\nWanna suppress errors? Refer to %s' % |
+ os.path.relpath(config_xml_path, constants.DIR_SOURCE_ROOT)) |
+ |
+ return rc |
+ |
+ |
+def main(argv): |
+ project_path = os.path.join(constants.DIR_SOURCE_ROOT, 'chrome', 'android', |
+ 'testshell', 'java') |
+ script_path = os.path.abspath(__file__) |
+ config_xml_path = os.path.join(os.path.dirname(script_path), |
+ 'lint_suppressions.xml') |
+ |
+ return Run(project_path, config_xml_path, script_path, None, None) |
+ |
+ |
+if __name__ == '__main__': |
+ sys.exit(main(sys.argv)) |