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

Side by Side Diff: tools/android/checkstyle/checkstyle.py

Issue 110773002: Adding Java style presubmit check. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Moved checkstyle to a separate directory so it could be used from other repos 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
« no previous file with comments | « PRESUBMIT.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 """ Script that is used by PRESUBMIT.py to run style checks on Java files."""
M-A Ruel 2013/12/10 13:26:51 ""Script
aurimas (slooooooooow) 2013/12/10 17:12:32 Done.
6
7
M-A Ruel 2013/12/10 13:26:51 remove one empty line
aurimas (slooooooooow) 2013/12/10 17:12:32 Done.
8 import os
9 import subprocess
10
11
12 def RunCheckstyle(input_api, output_api, style_file):
13 if not os.path.exists(style_file):
14 file_error = (' Java checkstyle configuration file is missing: '
15 + style_file)
16 return [output_api.PresubmitError(file_error)]
17
18 # Filter out non-Java files and files that were deleted.
19 java_files = [x.LocalPath() for x in input_api.AffectedFiles(False, False)
20 if os.path.splitext(x.LocalPath())[1].lower() == '.java']
M-A Ruel 2013/12/10 13:26:51 are .JAVA files valid?
aurimas (slooooooooow) 2013/12/10 17:12:32 Done.
21 if not java_files:
22 return []
23
24 # Run checkstyle
25 checkstyle_env = os.environ.copy()
26 checkstyle_env['JAVA_CMD'] = 'java'
27 try:
28 check = subprocess.Popen(['checkstyle', '-c', style_file] + java_files,
29 stdout=subprocess.PIPE, env=checkstyle_env)
30 stdout, _ = check.communicate()
31 if check.returncode == 0:
32 return []
33 except OSError as e:
34 import errno
35 if e.errno == errno.ENOENT:
36 install_error = (' checkstyle is not installed. Please run '
37 'build/install-build-deps-android.sh')
38 return [output_api.PresubmitPromptWarning(install_error)]
39
40 # Remove non-error values from stdout
41 errors = stdout.splitlines()
42 output_header = 'Starting audit...'
M-A Ruel 2013/12/10 13:26:51 These two are only used once so I wouldn't make th
aurimas (slooooooooow) 2013/12/10 17:12:32 Done.
43 output_footer = 'Audit done.'
44
45 if errors and errors[0] == output_header:
46 del errors[0]
47 if errors and errors[-1] == output_footer:
48 del errors[-1]
49
50 # Filter out warnings
51 errors = [x for x in errors if 'warning: ' not in x]
52 if not errors:
53 return []
54 else:
M-A Ruel 2013/12/10 13:26:51 optional: I'd remove the else, it's not necessary.
aurimas (slooooooooow) 2013/12/10 17:12:32 Done.
55 local_path = input_api.PresubmitLocalPath()
56 output = []
57 for error in errors:
58 # Change the full file path to relative path in the output lines
59 full_path, end = error.split(':', 1)
60 rel_path = os.path.relpath(full_path, local_path)
61 output.append(' %s:%s' % (rel_path, end))
62 return [output_api.PresubmitPromptWarning('\n'.join(output))]
OLDNEW
« no previous file with comments | « PRESUBMIT.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698