OLD | NEW |
---|---|
(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))] | |
OLD | NEW |