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 sys | |
13 from xml.dom import minidom | |
14 | |
15 from util import build_utils | |
16 | |
17 | |
18 _SRC_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), | |
19 '..', '..', '..')) | |
20 | |
21 | |
22 def _RunLint(lint_path, config_path, processed_config_path, manifest_path, | |
23 result_path, product_dir, src_dirs, classes_dir): | |
24 | |
25 def _RelativizePath(path): | |
26 """Returns relative path to top-level src dir. | |
27 | |
28 Args: | |
29 path: A path relative to cwd. | |
30 """ | |
31 return os.path.relpath(os.path.abspath(path), _SRC_ROOT) | |
32 | |
33 def _ProcessConfigFile(): | |
34 if not build_utils.IsTimeStale(processed_config_path, [config_path]): | |
35 return | |
36 | |
37 with open(config_path, 'rb') as f: | |
38 content = f.read().replace( | |
39 'PRODUCT_DIR', _RelativizePath(product_dir)) | |
40 | |
41 with open(processed_config_path, 'wb') as f: | |
42 f.write(content) | |
43 | |
44 def _ProcessResultFile(): | |
45 with open(result_path, 'rb') as f: | |
46 content = f.read().replace( | |
47 _RelativizePath(product_dir), 'PRODUCT_DIR') | |
48 | |
49 with open(result_path, 'wb') as f: | |
50 f.write(content) | |
51 | |
52 def _ParseAndShowResultFile(): | |
53 dom = minidom.parse(result_path) | |
54 issues = dom.getElementsByTagName('issue') | |
55 print >> sys.stderr | |
56 for issue in issues: | |
57 issue_id = issue.attributes['id'].value | |
58 severity = issue.attributes['severity'].value | |
59 message = issue.attributes['message'].value | |
60 location_elem = issue.getElementsByTagName('location')[0] | |
61 path = location_elem.attributes['file'].value | |
62 line = location_elem.getAttribute('line') | |
63 if line: | |
64 error = '%s:%s %s: %s [%s]' % (path, line, severity, message, | |
65 issue_id) | |
66 else: | |
67 # Issues in class files don't have a line number. | |
68 error = '%s %s: %s [%s]' % (path, severity, message, issue_id) | |
69 print >> sys.stderr, error | |
70 return len(issues) | |
71 | |
72 _ProcessConfigFile() | |
73 | |
74 cmd = [ | |
75 lint_path, '-Werror', '--exitcode', '--showall', | |
76 '--config', _RelativizePath(processed_config_path), | |
77 '--classpath', _RelativizePath(classes_dir), | |
78 '--xml', _RelativizePath(result_path), | |
79 ] | |
80 for src in src_dirs: | |
81 cmd.extend(['--sources', _RelativizePath(src)]) | |
82 cmd.append(_RelativizePath(os.path.join(manifest_path, os.pardir))) | |
83 | |
84 if os.path.exists(result_path): | |
85 os.remove(result_path) | |
86 | |
87 try: | |
88 build_utils.CheckOutput(cmd, cwd=_SRC_ROOT, | |
89 print_stdout=False, print_stderr=False) | |
newt (away)
2013/12/06 23:59:24
don't need "print_stdout=False" since that's the d
frankf
2013/12/07 01:51:01
Done.
| |
90 except build_utils.CalledProcessError: | |
91 # There is a problem with lint usage | |
92 if not os.path.exists(result_path): | |
93 raise | |
94 # There are actual lint issues | |
95 else: | |
96 num_issues = _ParseAndShowResultFile() | |
97 _ProcessResultFile() | |
98 msg = ('\nLint found %d new issues.\n' | |
99 ' - For full explanation refer to %s\n' | |
100 ' - Wanna suppress these issues?\n' | |
101 ' 1. Read comment in %s\n' | |
102 ' 2. Run "python %s %s"\n' % | |
103 (num_issues, | |
104 _RelativizePath(result_path), | |
105 _RelativizePath(config_path), | |
106 _RelativizePath(os.path.join(_SRC_ROOT, 'build', 'android', | |
107 'lint', 'suppress.py')), | |
108 _RelativizePath(result_path))) | |
109 print >> sys.stderr, msg | |
110 return 1 | |
111 | |
112 return 0 | |
113 | |
114 | |
115 def main(argv): | |
116 parser = optparse.OptionParser() | |
117 parser.add_option('--lint-path', help='Path to lint executable.') | |
118 parser.add_option('--config-path', help='Path to lint suppressions file.') | |
119 parser.add_option('--processed-config-path', | |
120 help='Path to processed lint suppressions file.') | |
121 parser.add_option('--manifest-path', help='Path to AndroidManifest.xml') | |
122 parser.add_option('--result-path', help='Path to XML lint result file.') | |
123 parser.add_option('--product-dir', help='Path to product dir.') | |
124 parser.add_option('--src-dirs', help='Directories containing java files.') | |
125 parser.add_option('--classes-dir', help='Directory containing class files.') | |
126 parser.add_option('--stamp', help='Path to touch on success.') | |
127 parser.add_option('--enable', action='store_true', | |
128 help='Run lint instead of just touching stamp.') | |
129 | |
130 options, _ = parser.parse_args() | |
131 | |
132 build_utils.CheckOptions( | |
133 options, parser, required=['lint_path', 'config_path', | |
134 'processed_config_path', 'manifest_path', | |
135 'result_path', 'product_dir', 'src_dirs', | |
136 'classes_dir']) | |
137 | |
138 src_dirs = build_utils.ParseGypList(options.src_dirs) | |
139 | |
140 rc = 0 | |
141 | |
142 if options.enable: | |
143 rc = _RunLint(options.lint_path, options.config_path, | |
144 options.processed_config_path, | |
145 options.manifest_path, options.result_path, | |
146 options.product_dir, src_dirs, options.classes_dir) | |
147 | |
148 if options.stamp and not rc: | |
149 build_utils.Touch(options.stamp) | |
150 | |
151 return rc | |
152 | |
153 | |
154 if __name__ == '__main__': | |
155 sys.exit(main(sys.argv)) | |
OLD | NEW |