OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Script that is used by PRESUBMIT.py to run style checks on Java files.""" | 5 """Script that is used by PRESUBMIT.py to run style checks on Java files.""" |
6 | 6 |
7 import os | 7 import os |
8 import subprocess | 8 import subprocess |
9 import xml.dom.minidom | 9 import xml.dom.minidom |
10 | 10 |
11 | 11 |
12 CHROMIUM_SRC = os.path.normpath( | 12 CHROMIUM_SRC = os.path.normpath( |
13 os.path.join(os.path.dirname(__file__), | 13 os.path.join(os.path.dirname(__file__), |
14 os.pardir, os.pardir, os.pardir)) | 14 os.pardir, os.pardir, os.pardir)) |
15 CHECKSTYLE_ROOT = os.path.join(CHROMIUM_SRC, 'third_party', 'checkstyle', | 15 CHECKSTYLE_ROOT = os.path.join(CHROMIUM_SRC, 'third_party', 'checkstyle', |
16 'checkstyle-6.1-all.jar') | 16 'checkstyle-6.1-all.jar') |
17 | 17 |
18 | 18 |
19 def RunCheckstyle(input_api, output_api, style_file): | 19 def RunCheckstyle(input_api, output_api, style_file, black_list=None): |
20 if not os.path.exists(style_file): | 20 if not os.path.exists(style_file): |
21 file_error = (' Java checkstyle configuration file is missing: ' | 21 file_error = (' Java checkstyle configuration file is missing: ' |
22 + style_file) | 22 + style_file) |
23 return [output_api.PresubmitError(file_error)] | 23 return [output_api.PresubmitError(file_error)] |
24 | 24 |
25 # Filter out non-Java files and files that were deleted. | 25 # Filter out non-Java files and files that were deleted. |
26 java_files = [x.LocalPath() for x in input_api.AffectedFiles(False, False) | 26 java_files = [x.AbsoluteLocalPath() for x in input_api.AffectedSourceFiles( |
| 27 lambda f: input_api.FilterSourceFile(f, black_list=black_list)) |
27 if os.path.splitext(x.LocalPath())[1] == '.java'] | 28 if os.path.splitext(x.LocalPath())[1] == '.java'] |
28 if not java_files: | 29 if not java_files: |
29 return [] | 30 return [] |
30 | 31 |
31 # Run checkstyle | 32 # Run checkstyle |
32 checkstyle_env = os.environ.copy() | 33 checkstyle_env = os.environ.copy() |
33 checkstyle_env['JAVA_CMD'] = 'java' | 34 checkstyle_env['JAVA_CMD'] = 'java' |
34 try: | 35 try: |
35 check = subprocess.Popen(['java', '-cp', | 36 check = subprocess.Popen(['java', '-cp', |
36 CHECKSTYLE_ROOT, | 37 CHECKSTYLE_ROOT, |
37 'com.puppycrawl.tools.checkstyle.Main', '-c', | 38 'com.puppycrawl.tools.checkstyle.Main', '-c', |
38 style_file, '-f', 'xml'] + java_files, | 39 style_file, '-f', 'xml'] + java_files, |
39 stdout=subprocess.PIPE, env=checkstyle_env) | 40 stdout=subprocess.PIPE, env=checkstyle_env) |
40 stdout, _ = check.communicate() | 41 stdout, _ = check.communicate() |
41 except OSError as e: | 42 except OSError as e: |
42 import errno | 43 import errno |
43 if e.errno == errno.ENOENT: | 44 if e.errno == errno.ENOENT: |
44 install_error = (' checkstyle is not installed. Please run ' | 45 install_error = (' checkstyle is not installed. Please run ' |
45 'build/install-build-deps-android.sh') | 46 'build/install-build-deps-android.sh') |
46 return [output_api.PresubmitPromptWarning(install_error)] | 47 return [output_api.PresubmitPromptWarning(install_error)] |
47 | 48 |
48 result_errors = [] | 49 result_errors = [] |
49 result_warnings = [] | 50 result_warnings = [] |
(...skipping 17 matching lines...) Expand all Loading... |
67 result_errors.append(result) | 68 result_errors.append(result) |
68 elif severity == 'warning': | 69 elif severity == 'warning': |
69 result_warnings.append(result) | 70 result_warnings.append(result) |
70 | 71 |
71 result = [] | 72 result = [] |
72 if result_warnings: | 73 if result_warnings: |
73 result.append(output_api.PresubmitPromptWarning('\n'.join(result_warnings))) | 74 result.append(output_api.PresubmitPromptWarning('\n'.join(result_warnings))) |
74 if result_errors: | 75 if result_errors: |
75 result.append(output_api.PresubmitError('\n'.join(result_errors))) | 76 result.append(output_api.PresubmitError('\n'.join(result_errors))) |
76 return result | 77 return result |
OLD | NEW |