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 cmd.extend(['--sources', _RelativizePath(src)]) | |
84 cmd.append(_RelativizePath(os.path.join(manifest_path, os.pardir))) | |
85 | 83 |
86 if os.path.exists(result_path): | 84 # There may be multiple source files with the same basename (but in |
87 os.remove(result_path) | 85 # different directories). It is difficult to determine what part of the path |
| 86 # corresponds to the java package, and so instead just link the source files |
| 87 # into temporary directories (creating a new one whenever there is a name |
| 88 # conflict). |
| 89 src_dirs = [] |
| 90 def NewSourceDir(): |
| 91 new_dir = os.path.join(temp_dir, str(len(src_dirs))) |
| 92 os.mkdir(new_dir) |
| 93 src_dirs.append(new_dir) |
| 94 cmd.extend(['--sources', _RelativizePath(new_dir)]) |
| 95 return new_dir |
88 | 96 |
89 try: | 97 def PathInDir(d, src): |
90 build_utils.CheckOutput(cmd, cwd=_SRC_ROOT) | 98 return os.path.join(d, os.path.basename(src)) |
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 | 99 |
98 # There are actual lint issues | 100 for src in sources: |
99 else: | 101 src_dir = None |
100 try: | 102 for d in src_dirs: |
101 num_issues = _ParseAndShowResultFile() | 103 if not os.path.exists(PathInDir(d, src)): |
102 except Exception: | 104 src_dir = d |
103 print 'Lint created unparseable xml file...' | 105 break |
104 print 'File contents:' | 106 if not src_dir: |
105 with open(result_path) as f: | 107 src_dir = NewSourceDir() |
106 print f.read() | 108 os.symlink(os.path.abspath(src), PathInDir(src_dir, src)) |
| 109 |
| 110 cmd.append(_RelativizePath(os.path.join(manifest_path, os.pardir))) |
| 111 |
| 112 if os.path.exists(result_path): |
| 113 os.remove(result_path) |
| 114 |
| 115 try: |
| 116 build_utils.CheckOutput(cmd, cwd=_SRC_ROOT) |
| 117 except build_utils.CalledProcessError as e: |
| 118 # There is a problem with lint usage |
| 119 if not os.path.exists(result_path): |
| 120 print 'Something is wrong:' |
| 121 print e |
107 return 0 | 122 return 0 |
108 | 123 |
109 _ProcessResultFile() | 124 # There are actual lint issues |
110 msg = ('\nLint found %d new issues.\n' | 125 else: |
111 ' - For full explanation refer to %s\n' | 126 try: |
112 ' - Wanna suppress these issues?\n' | 127 num_issues = _ParseAndShowResultFile() |
113 ' 1. Read comment in %s\n' | 128 except Exception: |
114 ' 2. Run "python %s %s"\n' % | 129 print 'Lint created unparseable xml file...' |
115 (num_issues, | 130 print 'File contents:' |
116 _RelativizePath(result_path), | 131 with open(result_path) as f: |
117 _RelativizePath(config_path), | 132 print f.read() |
118 _RelativizePath(os.path.join(_SRC_ROOT, 'build', 'android', | 133 return 0 |
119 'lint', 'suppress.py')), | 134 |
120 _RelativizePath(result_path))) | 135 _ProcessResultFile() |
121 print >> sys.stderr, msg | 136 msg = ('\nLint found %d new issues.\n' |
122 # Lint errors do not fail the build. | 137 ' - For full explanation refer to %s\n' |
123 return 0 | 138 ' - Wanna suppress these issues?\n' |
| 139 ' 1. Read comment in %s\n' |
| 140 ' 2. Run "python %s %s"\n' % |
| 141 (num_issues, |
| 142 _RelativizePath(result_path), |
| 143 _RelativizePath(config_path), |
| 144 _RelativizePath(os.path.join(_SRC_ROOT, 'build', 'android', |
| 145 'lint', 'suppress.py')), |
| 146 _RelativizePath(result_path))) |
| 147 print >> sys.stderr, msg |
| 148 # Lint errors do not fail the build. |
| 149 return 0 |
124 | 150 |
125 return 0 | 151 return 0 |
126 | 152 |
127 | 153 |
128 def main(): | 154 def main(): |
129 parser = optparse.OptionParser() | 155 parser = optparse.OptionParser() |
| 156 build_utils.AddDepfileOption(parser) |
130 parser.add_option('--lint-path', help='Path to lint executable.') | 157 parser.add_option('--lint-path', help='Path to lint executable.') |
131 parser.add_option('--config-path', help='Path to lint suppressions file.') | 158 parser.add_option('--config-path', help='Path to lint suppressions file.') |
132 parser.add_option('--processed-config-path', | 159 parser.add_option('--processed-config-path', |
133 help='Path to processed lint suppressions file.') | 160 help='Path to processed lint suppressions file.') |
134 parser.add_option('--manifest-path', help='Path to AndroidManifest.xml') | 161 parser.add_option('--manifest-path', help='Path to AndroidManifest.xml') |
135 parser.add_option('--result-path', help='Path to XML lint result file.') | 162 parser.add_option('--result-path', help='Path to XML lint result file.') |
136 parser.add_option('--product-dir', help='Path to product dir.') | 163 parser.add_option('--product-dir', help='Path to product dir.') |
137 parser.add_option('--src-dirs', help='Directories containing java files.') | 164 parser.add_option('--src-dirs', help='Directories containing java files.') |
| 165 parser.add_option('--java-files', help='Paths to java files.') |
138 parser.add_option('--jar-path', help='Jar file containing class files.') | 166 parser.add_option('--jar-path', help='Jar file containing class files.') |
139 parser.add_option('--stamp', help='Path to touch on success.') | 167 parser.add_option('--stamp', help='Path to touch on success.') |
140 parser.add_option('--enable', action='store_true', | 168 parser.add_option('--enable', action='store_true', |
141 help='Run lint instead of just touching stamp.') | 169 help='Run lint instead of just touching stamp.') |
142 | 170 |
143 options, _ = parser.parse_args() | 171 options, _ = parser.parse_args() |
144 | 172 |
145 build_utils.CheckOptions( | 173 build_utils.CheckOptions( |
146 options, parser, required=['lint_path', 'config_path', | 174 options, parser, required=['lint_path', 'config_path', |
147 'processed_config_path', 'manifest_path', | 175 'processed_config_path', 'manifest_path', |
148 'result_path', 'product_dir', 'src_dirs', | 176 'result_path', 'product_dir', |
149 'jar_path']) | 177 'jar_path']) |
150 | 178 |
151 src_dirs = build_utils.ParseGypList(options.src_dirs) | |
152 | |
153 rc = 0 | 179 rc = 0 |
154 | 180 |
155 if options.enable: | 181 if options.enable: |
| 182 sources = [] |
| 183 if options.src_dirs: |
| 184 src_dirs = build_utils.ParseGypList(options.src_dirs) |
| 185 sources = build_utils.FindInDirectories(src_dirs, '*.java') |
| 186 elif options.java_files: |
| 187 sources = build_utils.ParseGypList(options.java_files) |
| 188 else: |
| 189 print 'One of --src-dirs or --java-files must be specified.' |
| 190 return 1 |
156 rc = _RunLint(options.lint_path, options.config_path, | 191 rc = _RunLint(options.lint_path, options.config_path, |
157 options.processed_config_path, | 192 options.processed_config_path, |
158 options.manifest_path, options.result_path, | 193 options.manifest_path, options.result_path, |
159 options.product_dir, src_dirs, options.jar_path) | 194 options.product_dir, sources, options.jar_path) |
| 195 |
| 196 if options.depfile: |
| 197 build_utils.WriteDepfile( |
| 198 options.depfile, |
| 199 build_utils.GetPythonDependencies()) |
160 | 200 |
161 if options.stamp and not rc: | 201 if options.stamp and not rc: |
162 build_utils.Touch(options.stamp) | 202 build_utils.Touch(options.stamp) |
163 | 203 |
164 return rc | 204 return rc |
165 | 205 |
166 | 206 |
167 if __name__ == '__main__': | 207 if __name__ == '__main__': |
168 sys.exit(main()) | 208 sys.exit(main()) |
OLD | NEW |