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 _RunLint(lint_path, config_path, processed_config_path, manifest_path, |
23 result_path, product_dir, src_dirs, jar_path): | 23 result_path, product_dir, sources, jar_path): |
24 | 24 |
25 def _RelativizePath(path): | 25 def _RelativizePath(path): |
26 """Returns relative path to top-level src dir. | 26 """Returns relative path to top-level src dir. |
27 | 27 |
28 Args: | 28 Args: |
29 path: A path relative to cwd. | 29 path: A path relative to cwd. |
30 """ | 30 """ |
31 return os.path.relpath(os.path.abspath(path), _SRC_ROOT) | 31 return os.path.relpath(os.path.abspath(path), _SRC_ROOT) |
32 | 32 |
33 def _ProcessConfigFile(): | 33 def _ProcessConfigFile(): |
(...skipping 30 matching lines...) Expand all Loading... |
64 else: | 64 else: |
65 # Issues in class files don't have a line number. | 65 # Issues in class files don't have a line number. |
66 error = '%s %s: %s [warning]' % (path, message, issue_id) | 66 error = '%s %s: %s [warning]' % (path, message, issue_id) |
67 print >> sys.stderr, error | 67 print >> sys.stderr, error |
68 for attr in ['errorLine1', 'errorLine2']: | 68 for attr in ['errorLine1', 'errorLine2']: |
69 error_line = issue.getAttribute(attr) | 69 error_line = issue.getAttribute(attr) |
70 if error_line: | 70 if error_line: |
71 print >> sys.stderr, error_line | 71 print >> sys.stderr, error_line |
72 return len(issues) | 72 return len(issues) |
73 | 73 |
74 _ProcessConfigFile() | 74 with build_utils.TempDir() as temp_dir: |
| 75 _ProcessConfigFile() |
75 | 76 |
76 cmd = [ | 77 cmd = [ |
77 lint_path, '-Werror', '--exitcode', '--showall', | 78 _RelativizePath(lint_path), '-Werror', '--exitcode', '--showall', |
78 '--config', _RelativizePath(processed_config_path), | 79 '--config', _RelativizePath(processed_config_path), |
79 '--classpath', _RelativizePath(jar_path), | 80 '--classpath', _RelativizePath(jar_path), |
80 '--xml', _RelativizePath(result_path), | 81 '--xml', _RelativizePath(result_path), |
81 ] | 82 ] |
82 for src in src_dirs: | 83 for src in sources: |
83 cmd.extend(['--sources', _RelativizePath(src)]) | 84 os.symlink(src, os.path.join(temp_dir, os.path.basename(src))) |
84 cmd.append(_RelativizePath(os.path.join(manifest_path, os.pardir))) | |
85 | 85 |
86 if os.path.exists(result_path): | 86 cmd.extend(['--sources', _RelativizePath(temp_dir)]) |
87 os.remove(result_path) | 87 cmd.append(_RelativizePath(os.path.join(manifest_path, os.pardir))) |
88 | 88 |
89 try: | 89 if os.path.exists(result_path): |
90 build_utils.CheckOutput(cmd, cwd=_SRC_ROOT) | 90 os.remove(result_path) |
91 except build_utils.CalledProcessError as e: | |
92 # There is a problem with lint usage | |
93 if not os.path.exists(result_path): | |
94 print 'Something is wrong:' | |
95 print e | |
96 return 0 | |
97 | 91 |
98 # There are actual lint issues | 92 try: |
99 else: | 93 build_utils.CheckOutput(cmd, cwd=_SRC_ROOT) |
100 try: | 94 except build_utils.CalledProcessError as e: |
101 num_issues = _ParseAndShowResultFile() | 95 # There is a problem with lint usage |
102 except Exception: | 96 if not os.path.exists(result_path): |
103 print 'Lint created unparseable xml file...' | 97 print 'Something is wrong:' |
104 print 'File contents:' | 98 print e |
105 with open(result_path) as f: | |
106 print f.read() | |
107 return 0 | 99 return 0 |
108 | 100 |
109 _ProcessResultFile() | 101 # There are actual lint issues |
110 msg = ('\nLint found %d new issues.\n' | 102 else: |
111 ' - For full explanation refer to %s\n' | 103 try: |
112 ' - Wanna suppress these issues?\n' | 104 num_issues = _ParseAndShowResultFile() |
113 ' 1. Read comment in %s\n' | 105 except Exception: |
114 ' 2. Run "python %s %s"\n' % | 106 print 'Lint created unparseable xml file...' |
115 (num_issues, | 107 print 'File contents:' |
116 _RelativizePath(result_path), | 108 with open(result_path) as f: |
117 _RelativizePath(config_path), | 109 print f.read() |
118 _RelativizePath(os.path.join(_SRC_ROOT, 'build', 'android', | 110 return 0 |
119 'lint', 'suppress.py')), | 111 |
120 _RelativizePath(result_path))) | 112 _ProcessResultFile() |
121 print >> sys.stderr, msg | 113 msg = ('\nLint found %d new issues.\n' |
122 # Lint errors do not fail the build. | 114 ' - For full explanation refer to %s\n' |
123 return 0 | 115 ' - Wanna suppress these issues?\n' |
| 116 ' 1. Read comment in %s\n' |
| 117 ' 2. Run "python %s %s"\n' % |
| 118 (num_issues, |
| 119 _RelativizePath(result_path), |
| 120 _RelativizePath(config_path), |
| 121 _RelativizePath(os.path.join(_SRC_ROOT, 'build', 'android', |
| 122 'lint', 'suppress.py')), |
| 123 _RelativizePath(result_path))) |
| 124 print >> sys.stderr, msg |
| 125 # Lint errors do not fail the build. |
| 126 return 0 |
124 | 127 |
125 return 0 | 128 return 0 |
126 | 129 |
127 | 130 |
128 def main(): | 131 def main(): |
129 parser = optparse.OptionParser() | 132 parser = optparse.OptionParser() |
| 133 build_utils.AddDepfileOption(parser) |
130 parser.add_option('--lint-path', help='Path to lint executable.') | 134 parser.add_option('--lint-path', help='Path to lint executable.') |
131 parser.add_option('--config-path', help='Path to lint suppressions file.') | 135 parser.add_option('--config-path', help='Path to lint suppressions file.') |
132 parser.add_option('--processed-config-path', | 136 parser.add_option('--processed-config-path', |
133 help='Path to processed lint suppressions file.') | 137 help='Path to processed lint suppressions file.') |
134 parser.add_option('--manifest-path', help='Path to AndroidManifest.xml') | 138 parser.add_option('--manifest-path', help='Path to AndroidManifest.xml') |
135 parser.add_option('--result-path', help='Path to XML lint result file.') | 139 parser.add_option('--result-path', help='Path to XML lint result file.') |
136 parser.add_option('--product-dir', help='Path to product dir.') | 140 parser.add_option('--product-dir', help='Path to product dir.') |
137 parser.add_option('--src-dirs', help='Directories containing java files.') | 141 parser.add_option('--src-dirs', help='Directories containing java files.') |
| 142 parser.add_option('--java-files', help='Paths to java files.') |
138 parser.add_option('--jar-path', help='Jar file containing class files.') | 143 parser.add_option('--jar-path', help='Jar file containing class files.') |
139 parser.add_option('--stamp', help='Path to touch on success.') | 144 parser.add_option('--stamp', help='Path to touch on success.') |
140 parser.add_option('--enable', action='store_true', | 145 parser.add_option('--enable', action='store_true', |
141 help='Run lint instead of just touching stamp.') | 146 help='Run lint instead of just touching stamp.') |
142 | 147 |
143 options, _ = parser.parse_args() | 148 options, _ = parser.parse_args() |
144 | 149 |
145 build_utils.CheckOptions( | 150 build_utils.CheckOptions( |
146 options, parser, required=['lint_path', 'config_path', | 151 options, parser, required=['lint_path', 'config_path', |
147 'processed_config_path', 'manifest_path', | 152 'processed_config_path', 'manifest_path', |
148 'result_path', 'product_dir', 'src_dirs', | 153 'result_path', 'product_dir', |
149 'jar_path']) | 154 'jar_path']) |
150 | 155 |
151 src_dirs = build_utils.ParseGypList(options.src_dirs) | |
152 | |
153 rc = 0 | 156 rc = 0 |
154 | 157 |
155 if options.enable: | 158 if options.enable: |
| 159 sources = [] |
| 160 if options.src_dirs: |
| 161 src_dirs = build_utils.ParseGypList(options.src_dirs) |
| 162 sources = build_utils.FindInDirectories(src_dirs, '*.java') |
| 163 elif options.java_files: |
| 164 sources = build_utils.ParseGypList(options.java_files) |
| 165 else: |
| 166 print 'One of --src-dirs or --java-files must be specified.' |
| 167 return 1 |
156 rc = _RunLint(options.lint_path, options.config_path, | 168 rc = _RunLint(options.lint_path, options.config_path, |
157 options.processed_config_path, | 169 options.processed_config_path, |
158 options.manifest_path, options.result_path, | 170 options.manifest_path, options.result_path, |
159 options.product_dir, src_dirs, options.jar_path) | 171 options.product_dir, sources, options.jar_path) |
| 172 |
| 173 if options.depfile: |
| 174 build_utils.WriteDepfile( |
| 175 options.depfile, |
| 176 build_utils.GetPythonDependencies()) |
160 | 177 |
161 if options.stamp and not rc: | 178 if options.stamp and not rc: |
162 build_utils.Touch(options.stamp) | 179 build_utils.Touch(options.stamp) |
163 | 180 |
164 return rc | 181 return rc |
165 | 182 |
166 | 183 |
167 if __name__ == '__main__': | 184 if __name__ == '__main__': |
168 sys.exit(main()) | 185 sys.exit(main()) |
OLD | NEW |