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-7.6.1-all.jar') | 16 'checkstyle-6.5-all.jar') |
17 | |
18 | |
19 def FormatCheckstyleOutput(checkstyle_output): | |
20 lines = checkstyle_output.splitlines(True) | |
21 if 'Checkstyle ends with' in lines[-1]: | |
22 return ''.join(lines[:-1]) | |
23 else: | |
24 return checkstyle_output | |
25 | 17 |
26 | 18 |
27 def RunCheckstyle(input_api, output_api, style_file, black_list=None): | 19 def RunCheckstyle(input_api, output_api, style_file, black_list=None): |
28 if not os.path.exists(style_file): | 20 if not os.path.exists(style_file): |
29 file_error = (' Java checkstyle configuration file is missing: ' | 21 file_error = (' Java checkstyle configuration file is missing: ' |
30 + style_file) | 22 + style_file) |
31 return [output_api.PresubmitError(file_error)] | 23 return [output_api.PresubmitError(file_error)] |
32 | 24 |
33 # Filter out non-Java files and files that were deleted. | 25 # Filter out non-Java files and files that were deleted. |
34 java_files = [x.AbsoluteLocalPath() for x in input_api.AffectedSourceFiles( | 26 java_files = [x.AbsoluteLocalPath() for x in input_api.AffectedSourceFiles( |
(...skipping 15 matching lines...) Expand all Loading... |
50 except OSError as e: | 42 except OSError as e: |
51 import errno | 43 import errno |
52 if e.errno == errno.ENOENT: | 44 if e.errno == errno.ENOENT: |
53 install_error = (' checkstyle is not installed. Please run ' | 45 install_error = (' checkstyle is not installed. Please run ' |
54 'build/install-build-deps-android.sh') | 46 'build/install-build-deps-android.sh') |
55 return [output_api.PresubmitPromptWarning(install_error)] | 47 return [output_api.PresubmitPromptWarning(install_error)] |
56 | 48 |
57 result_errors = [] | 49 result_errors = [] |
58 result_warnings = [] | 50 result_warnings = [] |
59 | 51 |
60 formatted_checkstyle_output = FormatCheckstyleOutput(stdout) | |
61 | |
62 local_path = input_api.PresubmitLocalPath() | 52 local_path = input_api.PresubmitLocalPath() |
63 root = xml.dom.minidom.parseString(formatted_checkstyle_output) | 53 root = xml.dom.minidom.parseString(stdout) |
64 for fileElement in root.getElementsByTagName('file'): | 54 for fileElement in root.getElementsByTagName('file'): |
65 fileName = fileElement.attributes['name'].value | 55 fileName = fileElement.attributes['name'].value |
66 fileName = os.path.relpath(fileName, local_path) | 56 fileName = os.path.relpath(fileName, local_path) |
67 errors = fileElement.getElementsByTagName('error') | 57 errors = fileElement.getElementsByTagName('error') |
68 for error in errors: | 58 for error in errors: |
69 line = error.attributes['line'].value | 59 line = error.attributes['line'].value |
70 column = '' | 60 column = '' |
71 if error.hasAttribute('column'): | 61 if error.hasAttribute('column'): |
72 column = '%s:' % (error.attributes['column'].value) | 62 column = '%s:' % (error.attributes['column'].value) |
73 message = error.attributes['message'].value | 63 message = error.attributes['message'].value |
74 result = ' %s:%s:%s %s' % (fileName, line, column, message) | 64 result = ' %s:%s:%s %s' % (fileName, line, column, message) |
75 | 65 |
76 severity = error.attributes['severity'].value | 66 severity = error.attributes['severity'].value |
77 if severity == 'error': | 67 if severity == 'error': |
78 result_errors.append(result) | 68 result_errors.append(result) |
79 elif severity == 'warning': | 69 elif severity == 'warning': |
80 result_warnings.append(result) | 70 result_warnings.append(result) |
81 | 71 |
82 result = [] | 72 result = [] |
83 if result_warnings: | 73 if result_warnings: |
84 result.append(output_api.PresubmitPromptWarning('\n'.join(result_warnings))) | 74 result.append(output_api.PresubmitPromptWarning('\n'.join(result_warnings))) |
85 if result_errors: | 75 if result_errors: |
86 result.append(output_api.PresubmitError('\n'.join(result_errors))) | 76 result.append(output_api.PresubmitError('\n'.join(result_errors))) |
87 return result | 77 return result |
OLD | NEW |