Index: PRESUBMIT.py |
diff --git a/PRESUBMIT.py b/PRESUBMIT.py |
index 2a96a9b3b986766ab658ea7453bc5b01ace60100..466533fb79fdaa90b46d3efbdf9bf49829202de2 100644 |
--- a/PRESUBMIT.py |
+++ b/PRESUBMIT.py |
@@ -976,6 +976,61 @@ def _CheckCygwinShell(input_api, output_api): |
return [] |
+def _CheckJavaStyle(input_api, output_api): |
+ """Runs checkstyle on changed java files and returns errors if any exist.""" |
+ style_file = './tools/android/checkstyle/chromium-style-5.0.xml' |
+ import os |
newt (away)
2013/12/10 00:04:28
move "import" to beginning of function
|
+ if not os.path.exists(style_file): |
+ file_error = (' Java checkstyle configuration file is missing') |
newt (away)
2013/12/10 00:04:28
I'd include the path to the missing file to help p
aurimas (slooooooooow)
2013/12/10 02:31:47
Done.
|
+ return [output_api.PresubmitError(file_error)] |
+ |
+ # Filter out non-Java files and files that were deleted. |
+ java_files = [x.LocalPath() for x in input_api.AffectedFiles(False, False) |
+ if os.path.splitext(x.LocalPath())[1].lower() == '.java'] |
+ if not java_files: |
+ return [] |
+ |
+ # Run checkstyle |
+ checkstyle_env = os.environ.copy() |
+ checkstyle_env['JAVA_CMD'] = 'java' |
+ try: |
+ check = subprocess.Popen(['checkstyle', '-c', style_file] + java_files, |
+ stdout=subprocess.PIPE, env=checkstyle_env) |
+ stdout, _ = check.communicate() |
+ if check.returncode == 0: |
+ return [] |
+ except OSError as e: |
+ import errno |
+ if e.errno == errno.ENOENT: |
+ install_error = (' checkstyle is not installed. Please run ' |
+ 'build/install-build-deps-android.sh') |
+ return [output_api.PresubmitPromptWarning(install_error)] |
+ |
+ # Remove non-error values from stdout |
+ errors = stdout.splitlines() |
+ output_header = 'Starting audit...' |
+ output_footer = 'Audit done.' |
+ |
+ if errors and errors[0] == output_header: |
+ del errors[0] |
+ if errors and errors[-1] == output_footer: |
+ del errors[-1] |
+ |
+ # Filter out warnings |
+ errors = [x for x in errors if 'warning: ' not in x] |
+ if not errors: |
+ return [] |
+ else: |
+ local_path = input_api.PresubmitLocalPath() |
+ output = [] |
+ for error in errors: |
+ # Change the full file path to relative path in the output lines |
+ full_path, end = error.split(':', 1) |
+ rel_path = os.path.relpath(full_path, local_path) |
+ output.append(' %s:%s' % (rel_path, end)) |
+ return [output_api.PresubmitPromptWarning('\n'.join(output))] |
+ |
+ |
def _CommonChecks(input_api, output_api): |
"""Checks common to both upload and commit.""" |
results = [] |
@@ -983,7 +1038,7 @@ def _CommonChecks(input_api, output_api): |
input_api, output_api, excluded_paths=_EXCLUDED_PATHS)) |
results.extend(_CheckAuthorizedAuthor(input_api, output_api)) |
results.extend( |
- _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api)) |
+ _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api)) |
results.extend(_CheckNoIOStreamInHeaders(input_api, output_api)) |
results.extend(_CheckNoUNIT_TESTInSourceFiles(input_api, output_api)) |
results.extend(_CheckNoNewWStrings(input_api, output_api)) |
@@ -1009,6 +1064,7 @@ def _CommonChecks(input_api, output_api): |
results.extend(_CheckSpamLogging(input_api, output_api)) |
results.extend(_CheckForAnonymousVariables(input_api, output_api)) |
results.extend(_CheckCygwinShell(input_api, output_api)) |
+ results.extend(_CheckJavaStyle(input_api, output_api)) |
if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()): |
results.extend(input_api.canned_checks.RunUnitTestsInDirectory( |