James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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.""" |
| 6 | |
| 7 | import os |
| 8 | import subprocess |
James Robinson | e2ac7e8 | 2014-10-15 13:21:59 -0700 | [diff] [blame] | 9 | import xml.dom.minidom |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 10 | |
| 11 | |
| 12 | CHROMIUM_SRC = os.path.normpath( |
| 13 | os.path.join(os.path.dirname(__file__), |
| 14 | os.pardir, os.pardir, os.pardir)) |
| 15 | CHECKSTYLE_ROOT = os.path.join(CHROMIUM_SRC, 'third_party', 'checkstyle', |
James Robinson | c4c1c59 | 2014-11-21 18:27:04 -0800 | [diff] [blame^] | 16 | 'checkstyle-6.1-all.jar') |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 17 | |
| 18 | |
| 19 | def RunCheckstyle(input_api, output_api, style_file): |
| 20 | if not os.path.exists(style_file): |
| 21 | file_error = (' Java checkstyle configuration file is missing: ' |
| 22 | + style_file) |
| 23 | return [output_api.PresubmitError(file_error)] |
| 24 | |
| 25 | # Filter out non-Java files and files that were deleted. |
| 26 | java_files = [x.LocalPath() for x in input_api.AffectedFiles(False, False) |
| 27 | if os.path.splitext(x.LocalPath())[1] == '.java'] |
| 28 | if not java_files: |
| 29 | return [] |
| 30 | |
| 31 | # Run checkstyle |
| 32 | checkstyle_env = os.environ.copy() |
| 33 | checkstyle_env['JAVA_CMD'] = 'java' |
| 34 | try: |
| 35 | check = subprocess.Popen(['java', '-cp', |
| 36 | CHECKSTYLE_ROOT, |
| 37 | 'com.puppycrawl.tools.checkstyle.Main', '-c', |
James Robinson | e2ac7e8 | 2014-10-15 13:21:59 -0700 | [diff] [blame] | 38 | style_file, '-f', 'xml'] + java_files, |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 39 | stdout=subprocess.PIPE, env=checkstyle_env) |
| 40 | stdout, _ = check.communicate() |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 41 | except OSError as e: |
| 42 | import errno |
| 43 | if e.errno == errno.ENOENT: |
| 44 | install_error = (' checkstyle is not installed. Please run ' |
| 45 | 'build/install-build-deps-android.sh') |
| 46 | return [output_api.PresubmitPromptWarning(install_error)] |
| 47 | |
James Robinson | e2ac7e8 | 2014-10-15 13:21:59 -0700 | [diff] [blame] | 48 | result_errors = [] |
| 49 | result_warnings = [] |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 50 | |
| 51 | local_path = input_api.PresubmitLocalPath() |
James Robinson | e2ac7e8 | 2014-10-15 13:21:59 -0700 | [diff] [blame] | 52 | root = xml.dom.minidom.parseString(stdout) |
| 53 | for fileElement in root.getElementsByTagName('file'): |
| 54 | fileName = fileElement.attributes['name'].value |
| 55 | fileName = os.path.relpath(fileName, local_path) |
| 56 | errors = fileElement.getElementsByTagName('error') |
| 57 | for error in errors: |
| 58 | line = error.attributes['line'].value |
| 59 | column = '' |
| 60 | if error.hasAttribute('column'): |
| 61 | column = '%s:' % (error.attributes['column'].value) |
| 62 | message = error.attributes['message'].value |
| 63 | result = ' %s:%s:%s %s' % (fileName, line, column, message) |
| 64 | |
| 65 | severity = error.attributes['severity'].value |
| 66 | if severity == 'error': |
| 67 | result_errors.append(result) |
| 68 | elif severity == 'warning': |
| 69 | result_warnings.append(result) |
| 70 | |
| 71 | result = [] |
| 72 | if result_warnings: |
| 73 | result.append(output_api.PresubmitPromptWarning('\n'.join(result_warnings))) |
| 74 | if result_errors: |
| 75 | result.append(output_api.PresubmitError('\n'.join(result_errors))) |
| 76 | return result |