OLD | NEW |
---|---|
(Empty) | |
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 """Runs Android's lint tool.""" | |
8 | |
9 | |
10 import optparse | |
11 import os | |
12 import pipes | |
13 import subprocess | |
14 import sys | |
15 import time | |
16 from xml.dom import minidom | |
17 | |
18 from util import build_utils | |
19 | |
20 | |
21 def _RunLint(lint_path, config_path, processed_config_path, manifest_path, | |
22 result_path, product_dir, src_root, src_dirs, classes_dir): | |
23 | |
24 def _RelativizePath(path): | |
25 """Returns relative path to top-level src dir. | |
26 | |
27 Args: | |
28 path: A path relative to cwd. | |
29 """ | |
30 return os.path.relpath(os.path.abspath(path), os.path.abspath(src_root)) | |
31 | |
32 def _ProcessConfigFile(): | |
33 if not build_utils.IsTimeStale(processed_config_path, [config_path]): | |
34 return | |
35 | |
36 with open(config_path, 'rb') as f: | |
37 content = f.read().replace( | |
38 'PRODUCT_DIR', _RelativizePath(product_dir)) | |
39 | |
40 with open(processed_config_path, 'wb') as f: | |
41 f.write(content) | |
42 | |
43 def _ProcessResultFile(): | |
44 with open(result_path, 'rb') as f: | |
45 content = f.read().replace( | |
46 _RelativizePath(product_dir), 'PRODUCT_DIR') | |
47 | |
48 with open(result_path, 'wb') as f: | |
49 f.write(content) | |
50 | |
51 def _ParseAndShowResultFile(): | |
52 dom = minidom.parse(result_path) | |
53 num_issues = 0 | |
newt (away)
2013/12/05 23:39:33
just an idea: instead of counting num_issues manua
frankf
2013/12/06 22:17:36
Done.
| |
54 for issue in dom.getElementsByTagName('issue'): | |
55 num_issues += 1 | |
56 issue_id = issue.attributes['id'].value | |
57 severity = issue.attributes['severity'].value | |
58 message = issue.attributes['message'].value | |
59 location_elem = issue.getElementsByTagName('location')[0] | |
60 path = location_elem.attributes['file'].value | |
61 line = location_elem.getAttribute('line') | |
62 if line: | |
63 error = '%s:%s %s: %s [%s]' % (path, line, severity, message, | |
64 issue_id) | |
65 else: | |
66 # Issues in class files don't have a line number. | |
67 error = '%s %s: %s [%s]' % (path, severity, message, issue_id) | |
68 print >> sys.stderr, error | |
69 return num_issues | |
70 | |
71 _ProcessConfigFile() | |
72 | |
73 cmd = [ | |
74 lint_path, '-Werror', '--exitcode', '--showall', | |
75 '--config', _RelativizePath(processed_config_path), | |
76 '--classpath', _RelativizePath(classes_dir), | |
77 '--xml', _RelativizePath(result_path), | |
78 ] | |
79 for src in src_dirs: | |
80 cmd.extend(['--sources', _RelativizePath(src)]) | |
81 cmd.append(_RelativizePath(os.path.join(manifest_path, os.pardir))) | |
82 | |
83 if os.path.exists(result_path): | |
84 os.remove(result_path) | |
85 | |
86 cwd = os.path.abspath(src_root) | |
87 child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, | |
newt (away)
2013/12/05 23:39:33
I have a CL out for review to change build_utils.C
frankf
2013/12/06 22:17:36
Nice refactoring. Done. I'll rebase after your CL
| |
88 cwd=cwd) | |
89 stdout, stderr = child.communicate() | |
90 rc = child.returncode | |
91 | |
92 if rc: | |
93 # There is a problem with lint usage | |
94 if not os.path.exists(result_path): | |
95 # A user should be able to simply copy and paste the command that failed | |
96 # into their shell. | |
97 copyable_command = ' '.join(map(pipes.quote, cmd)) | |
98 copyable_command = ('( cd ' + os.path.abspath(cwd) + '; ' | |
99 + copyable_command + ' )') | |
100 print >> sys.stderr, 'Command failed:', copyable_command, '\n' | |
101 | |
102 if stdout: | |
103 print stdout, | |
104 if stderr: | |
105 print >> sys.stderr, stderr, | |
106 # There are actual lint issues | |
107 else: | |
108 num_issues = _ParseAndShowResultFile() | |
109 _ProcessResultFile() | |
110 msg = ('\nLint found %d new issues.\n' | |
111 ' - For full explanation refer to %s\n' | |
112 ' - Wanna suppress these issues?\n' | |
113 ' 1. Read comment in %s\n' | |
114 ' 2. Run "python %s %s"\n' % | |
115 (num_issues, | |
116 _RelativizePath(result_path), | |
117 _RelativizePath(config_path), | |
118 _RelativizePath(os.path.join(src_root, 'build', 'android', 'lint', | |
119 'suppress.py')), | |
120 _RelativizePath(result_path))) | |
121 print >> sys.stderr, msg | |
122 | |
123 return rc | |
124 | |
125 | |
126 def main(argv): | |
127 start = time.time() | |
128 | |
129 parser = optparse.OptionParser() | |
130 parser.add_option('--lint-path', help='Path to lint executable.') | |
131 parser.add_option('--config-path', help='Path to lint suppressions file.') | |
132 parser.add_option('--processed-config-path', | |
133 help='Path to processed lint suppressions file.') | |
134 parser.add_option('--manifest-path', help='Path to AndroidManifest.xml') | |
135 parser.add_option('--result-path', help='Path to XML lint result file.') | |
136 parser.add_option('--product-dir', help='Path to product dir.') | |
137 parser.add_option('--src-root', help='Path to top-level src dir.') | |
138 parser.add_option('--src-dirs', help='Directories containing java files.') | |
139 parser.add_option('--classes-dir', help='Directory containing class files.') | |
140 parser.add_option('--stamp', help='Path to touch on success.') | |
141 parser.add_option('--enable', action='store_true', | |
newt (away)
2013/12/05 23:39:33
Huh, where else is this used?
frankf
2013/12/06 22:17:36
See lint_action.gypi
On 2013/12/05 23:39:33, newt
| |
142 help='Run lint instead of just touching stamp.') | |
143 | |
144 options, _ = parser.parse_args() | |
145 | |
146 build_utils.CheckOptions( | |
147 options, parser, required=['lint_path', 'config_path', | |
148 'processed_config_path', 'manifest_path', | |
149 'result_path', 'product_dir', 'src_root', | |
150 'src_dirs', 'classes_dir']) | |
151 | |
152 src_dirs = build_utils.ParseGypList(options.src_dirs) | |
153 | |
154 rc = 0 | |
155 | |
156 if options.enable: | |
157 rc = _RunLint(options.lint_path, options.config_path, | |
158 options.processed_config_path, | |
159 options.manifest_path, options.result_path, | |
160 options.product_dir, options.src_root, src_dirs, | |
161 options.classes_dir) | |
162 | |
163 if options.stamp and not rc: | |
164 build_utils.Touch(options.stamp) | |
165 | |
166 # TODO(frankf): Remove this before committing. | |
newt (away)
2013/12/05 23:39:33
don't forget...
frankf
2013/12/06 22:17:36
Removed debugging logs.
| |
167 print 'Lint took %s for %s' % (int(time.time() - start), options.classes_dir) | |
168 | |
169 return rc | |
170 | |
171 | |
172 if __name__ == '__main__': | |
173 sys.exit(main(sys.argv)) | |
OLD | NEW |