| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # | 2 # |
| 3 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 """Runs Android's lint tool.""" | 7 """Runs Android's lint tool.""" |
| 8 | 8 |
| 9 | 9 |
| 10 import optparse | 10 import optparse |
| 11 import os | 11 import os |
| 12 import sys | 12 import sys |
| 13 from xml.dom import minidom | 13 from xml.dom import minidom |
| 14 | 14 |
| 15 from util import build_utils | 15 from util import build_utils |
| 16 | 16 |
| 17 | 17 |
| 18 _SRC_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), | 18 _SRC_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), |
| 19 '..', '..', '..')) | 19 '..', '..', '..')) |
| 20 | 20 |
| 21 | 21 |
| 22 def _RunLint(lint_path, config_path, processed_config_path, manifest_path, | 22 def _OnStaleMd5(changes, lint_path, config_path, processed_config_path, |
| 23 result_path, product_dir, sources, jar_path, resource_dir=None): | 23 manifest_path, result_path, product_dir, sources, jar_path, |
| 24 resource_dir=None, can_fail_build=False): |
| 24 | 25 |
| 25 def _RelativizePath(path): | 26 def _RelativizePath(path): |
| 26 """Returns relative path to top-level src dir. | 27 """Returns relative path to top-level src dir. |
| 27 | 28 |
| 28 Args: | 29 Args: |
| 29 path: A path relative to cwd. | 30 path: A path relative to cwd. |
| 30 """ | 31 """ |
| 31 return os.path.relpath(os.path.abspath(path), _SRC_ROOT) | 32 return os.path.relpath(os.path.abspath(path), _SRC_ROOT) |
| 32 | 33 |
| 33 def _ProcessConfigFile(): | 34 def _ProcessConfigFile(): |
| (...skipping 30 matching lines...) Expand all Loading... |
| 64 else: | 65 else: |
| 65 # Issues in class files don't have a line number. | 66 # Issues in class files don't have a line number. |
| 66 error = '%s %s: %s [warning]' % (path, message, issue_id) | 67 error = '%s %s: %s [warning]' % (path, message, issue_id) |
| 67 print >> sys.stderr, error.encode('utf-8') | 68 print >> sys.stderr, error.encode('utf-8') |
| 68 for attr in ['errorLine1', 'errorLine2']: | 69 for attr in ['errorLine1', 'errorLine2']: |
| 69 error_line = issue.getAttribute(attr) | 70 error_line = issue.getAttribute(attr) |
| 70 if error_line: | 71 if error_line: |
| 71 print >> sys.stderr, error_line.encode('utf-8') | 72 print >> sys.stderr, error_line.encode('utf-8') |
| 72 return len(issues) | 73 return len(issues) |
| 73 | 74 |
| 75 # Need to include all sources when a resource_dir is set so that resources are |
| 76 # not marked as unused. |
| 77 if not resource_dir and changes.AddedOrModifiedOnly(): |
| 78 changed_paths = set(changes.IterChangedPaths()) |
| 79 sources = [s for s in sources if s in changed_paths] |
| 80 |
| 74 with build_utils.TempDir() as temp_dir: | 81 with build_utils.TempDir() as temp_dir: |
| 75 _ProcessConfigFile() | 82 _ProcessConfigFile() |
| 76 | 83 |
| 77 cmd = [ | 84 cmd = [ |
| 78 _RelativizePath(lint_path), '-Werror', '--exitcode', '--showall', | 85 _RelativizePath(lint_path), '-Werror', '--exitcode', '--showall', |
| 79 '--config', _RelativizePath(processed_config_path), | 86 '--config', _RelativizePath(processed_config_path), |
| 80 '--classpath', _RelativizePath(jar_path), | 87 '--classpath', _RelativizePath(jar_path), |
| 81 '--xml', _RelativizePath(result_path), | 88 '--xml', _RelativizePath(result_path), |
| 82 ] | 89 ] |
| 83 if resource_dir: | 90 if resource_dir: |
| (...skipping 30 matching lines...) Expand all Loading... |
| 114 if os.path.exists(result_path): | 121 if os.path.exists(result_path): |
| 115 os.remove(result_path) | 122 os.remove(result_path) |
| 116 | 123 |
| 117 try: | 124 try: |
| 118 build_utils.CheckOutput(cmd, cwd=_SRC_ROOT) | 125 build_utils.CheckOutput(cmd, cwd=_SRC_ROOT) |
| 119 except build_utils.CalledProcessError as e: | 126 except build_utils.CalledProcessError as e: |
| 120 # There is a problem with lint usage | 127 # There is a problem with lint usage |
| 121 if not os.path.exists(result_path): | 128 if not os.path.exists(result_path): |
| 122 print 'Something is wrong:' | 129 print 'Something is wrong:' |
| 123 print e | 130 print e |
| 124 return 1 | 131 raise |
| 125 | 132 |
| 126 # There are actual lint issues | 133 # There are actual lint issues |
| 127 else: | 134 else: |
| 128 try: | 135 try: |
| 129 num_issues = _ParseAndShowResultFile() | 136 num_issues = _ParseAndShowResultFile() |
| 130 except Exception: # pylint: disable=broad-except | 137 except Exception: # pylint: disable=broad-except |
| 131 print 'Lint created unparseable xml file...' | 138 print 'Lint created unparseable xml file...' |
| 132 print 'File contents:' | 139 print 'File contents:' |
| 133 with open(result_path) as f: | 140 with open(result_path) as f: |
| 134 print f.read() | 141 print f.read() |
| 135 return 1 | 142 raise |
| 136 | 143 |
| 137 _ProcessResultFile() | 144 _ProcessResultFile() |
| 138 msg = ('\nLint found %d new issues.\n' | 145 msg = ('\nLint found %d new issues.\n' |
| 139 ' - For full explanation refer to %s\n' | 146 ' - For full explanation refer to %s\n' |
| 140 ' - Wanna suppress these issues?\n' | 147 ' - Wanna suppress these issues?\n' |
| 141 ' 1. Read comment in %s\n' | 148 ' 1. Read comment in %s\n' |
| 142 ' 2. Run "python %s %s"\n' % | 149 ' 2. Run "python %s %s"\n' % |
| 143 (num_issues, | 150 (num_issues, |
| 144 _RelativizePath(result_path), | 151 _RelativizePath(result_path), |
| 145 _RelativizePath(config_path), | 152 _RelativizePath(config_path), |
| 146 _RelativizePath(os.path.join(_SRC_ROOT, 'build', 'android', | 153 _RelativizePath(os.path.join(_SRC_ROOT, 'build', 'android', |
| 147 'lint', 'suppress.py')), | 154 'lint', 'suppress.py')), |
| 148 _RelativizePath(result_path))) | 155 _RelativizePath(result_path))) |
| 149 print >> sys.stderr, msg | 156 print >> sys.stderr, msg |
| 150 return 1 | 157 if can_fail_build: |
| 151 | 158 raise Exception('Lint failed.') |
| 152 return 0 | |
| 153 | 159 |
| 154 | 160 |
| 155 def main(): | 161 def main(): |
| 156 parser = optparse.OptionParser() | 162 parser = optparse.OptionParser() |
| 157 build_utils.AddDepfileOption(parser) | 163 build_utils.AddDepfileOption(parser) |
| 158 parser.add_option('--lint-path', help='Path to lint executable.') | 164 parser.add_option('--lint-path', help='Path to lint executable.') |
| 159 parser.add_option('--config-path', help='Path to lint suppressions file.') | 165 parser.add_option('--config-path', help='Path to lint suppressions file.') |
| 160 parser.add_option('--processed-config-path', | 166 parser.add_option('--processed-config-path', |
| 161 help='Path to processed lint suppressions file.') | 167 help='Path to processed lint suppressions file.') |
| 162 parser.add_option('--manifest-path', help='Path to AndroidManifest.xml') | 168 parser.add_option('--manifest-path', help='Path to AndroidManifest.xml') |
| (...skipping 11 matching lines...) Expand all Loading... |
| 174 help='Run lint instead of just touching stamp.') | 180 help='Run lint instead of just touching stamp.') |
| 175 | 181 |
| 176 options, _ = parser.parse_args() | 182 options, _ = parser.parse_args() |
| 177 | 183 |
| 178 build_utils.CheckOptions( | 184 build_utils.CheckOptions( |
| 179 options, parser, required=['lint_path', 'config_path', | 185 options, parser, required=['lint_path', 'config_path', |
| 180 'processed_config_path', 'manifest_path', | 186 'processed_config_path', 'manifest_path', |
| 181 'result_path', 'product_dir', | 187 'result_path', 'product_dir', |
| 182 'jar_path']) | 188 'jar_path']) |
| 183 | 189 |
| 184 rc = 0 | |
| 185 | |
| 186 if options.enable: | 190 if options.enable: |
| 187 sources = [] | 191 sources = [] |
| 188 if options.src_dirs: | 192 if options.src_dirs: |
| 189 src_dirs = build_utils.ParseGypList(options.src_dirs) | 193 src_dirs = build_utils.ParseGypList(options.src_dirs) |
| 190 sources = build_utils.FindInDirectories(src_dirs, '*.java') | 194 sources = build_utils.FindInDirectories(src_dirs, '*.java') |
| 191 elif options.java_files: | 195 elif options.java_files: |
| 192 sources = build_utils.ParseGypList(options.java_files) | 196 sources = build_utils.ParseGypList(options.java_files) |
| 193 else: | 197 else: |
| 194 print 'One of --src-dirs or --java-files must be specified.' | 198 print 'One of --src-dirs or --java-files must be specified.' |
| 195 return 1 | 199 return 1 |
| 196 rc = _RunLint(options.lint_path, options.config_path, | |
| 197 options.processed_config_path, | |
| 198 options.manifest_path, options.result_path, | |
| 199 options.product_dir, sources, options.jar_path, | |
| 200 options.resource_dir) | |
| 201 | 200 |
| 202 if options.depfile: | 201 input_paths = [ |
| 203 build_utils.WriteDepfile( | 202 options.lint_path, |
| 204 options.depfile, | 203 options.config_path, |
| 205 build_utils.GetPythonDependencies()) | 204 options.manifest_path, |
| 205 options.jar_path, |
| 206 ] |
| 207 input_paths.extend(sources) |
| 208 if options.resource_dir: |
| 209 input_paths.extend(build_utils.FindInDirectory(options.resource_dir, '*')) |
| 206 | 210 |
| 207 if options.stamp and not rc: | 211 input_strings = [ options.processed_config_path ] |
| 208 build_utils.Touch(options.stamp) | 212 output_paths = [ options.result_path ] |
| 209 | 213 |
| 210 return rc if options.can_fail_build else 0 | 214 build_utils.CallAndWriteDepfileIfStale( |
| 215 lambda changes: _OnStaleMd5(changes, options.lint_path, |
| 216 options.config_path, |
| 217 options.processed_config_path, |
| 218 options.manifest_path, options.result_path, |
| 219 options.product_dir, sources, |
| 220 options.jar_path, |
| 221 resource_dir=options.resource_dir, |
| 222 can_fail_build=options.can_fail_build), |
| 223 options, |
| 224 input_paths=input_paths, |
| 225 input_strings=input_strings, |
| 226 output_paths=output_paths, |
| 227 pass_changes=True) |
| 211 | 228 |
| 212 | 229 |
| 213 if __name__ == '__main__': | 230 if __name__ == '__main__': |
| 214 sys.exit(main()) | 231 sys.exit(main()) |
| OLD | NEW |