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