Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(154)

Side by Side Diff: build/android/gyp/lint.py

Issue 86313004: [Android] Add lint as a gyp action. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 import time
14 from xml.dom import minidom
15
16 from util import build_utils
17
18
19 def _ParseAndShowResultFile(result_path):
20 dom = minidom.parse(result_path)
21 for issue in dom.getElementsByTagName('issue'):
22 issue_id = issue.attributes['id'].value
23 severity = issue.attributes['severity'].value
24 message = issue.attributes['message'].value
25 location_elem = issue.getElementsByTagName('location')[0]
26 path = location_elem.attributes['file'].value
27 line = location_elem.getAttribute('line')
28 if line:
29 error = '%s:%s %s: %s [%s]' % (path, line, severity, message,
30 issue_id)
31 else:
32 # Issues in class files don't have a line number.
33 error = '%s %s: %s [%s]' % (path, severity, message, issue_id)
34 print >> sys.stderr, error
35
36
37 def _RunLint(lint_path, config_path, result_path, src_root, src_dirs,
38 classes_dir):
39
40 def _RelativizePath(path):
41 """Returns relative path to top-level src dir.
42
43 Args:
44 path: A path relative to cwd.
45 """
46 return os.path.relpath(os.path.abspath(path), os.path.abspath(src_root))
47
48 cmd = [
49 lint_path, '-Werror', '--exitcode', '--showall',
50 '--config', _RelativizePath(config_path),
51 '--classpath', _RelativizePath(classes_dir),
52 '--xml', _RelativizePath(result_path),
53 ]
54
55 for src in src_dirs:
56 cmd.extend(['--sources', _RelativizePath(src)])
57
58 cmd.append(_RelativizePath(os.path.join(config_path, os.pardir)))
59
60 rc, _ = build_utils.CheckCall(cmd, suppress_output=True,
61 cwd=os.path.abspath(src_root))
62 if rc:
63 _ParseAndShowResultFile(result_path)
64 print >> sys.stderr, ('\nWanna suppress lint issues? Refer to %s' %
65 _RelativizePath(config_path))
66 sys.exit(rc)
newt (away) 2013/12/03 00:54:16 I'd probably return "rc" instead of calling sys.ex
frankf 2013/12/04 23:29:31 Done. You only return rc of zero if there are no l
67
68
69 def main(argv):
70 start = time.time()
71
72 parser = optparse.OptionParser()
73 parser.add_option('--lint-path', help='Path to lint executable.')
74 parser.add_option('--config-path', help='Path to lint suppressions file.')
75 parser.add_option('--result-path', help='Path to xml file')
newt (away) 2013/12/03 00:54:16 "Path to xml file"?
frankf 2013/12/04 23:29:31 Rephrased
76 parser.add_option('--src-root', help='Path to top-level src dir.')
77 parser.add_option('--src-dirs', help='Directories containing java files.')
78 parser.add_option('--classes-dir', help='Directory containing class files.')
79 parser.add_option('--stamp', help='Path to touch on success.')
80 parser.add_option('--enable', action='store_true',
newt (away) 2013/12/03 00:54:16 I would use gyp to conditionally enable or disable
frankf 2013/12/04 23:29:31 We want to trivially touch the stamp if lint is no
81 help='Run lint instead of just touching stamp.')
82
83 options, _ = parser.parse_args()
84
85 assert (options.lint_path and options.config_path and options.result_path and
newt (away) 2013/12/03 00:54:16 you can use build_utils.CheckOptions() to make thi
frankf 2013/12/04 23:29:31 Done. Although having required be an optional para
newt (away) 2013/12/05 23:39:33 The irony ;)
86 options.src_root and options.src_dirs and options.classes_dir)
87
88 src_dirs = build_utils.ParseGypList(options.src_dirs)
89
90 if options.enable:
91 _RunLint(options.lint_path, options.config_path, options.result_path,
92 options.src_root, src_dirs, options.classes_dir)
93
94 if options.stamp:
newt (away) 2013/12/03 00:54:16 should the stamp only be touched if lint detects n
frankf 2013/12/04 23:29:31 Nope, previously we exited in line 66, with new pa
95 build_utils.Touch(options.stamp)
96
97 # TODO(frankf): Remove this before committing.
98 print 'Lint took %s for %s' % (int(time.time() - start), options.classes_dir)
99
100
101 if __name__ == '__main__':
102 sys.exit(main(sys.argv))
newt (away) 2013/12/03 00:54:16 main() doesn't return anything, so this line isn't
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698