1# Copyright 2015 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"""Top-level presubmit script for src/components/cronet. 6 7See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts 8for more details about the presubmit API built into depot_tools. 9""" 10 11import os 12 13def _PyLintChecks(input_api, output_api): 14 pylint_checks = input_api.canned_checks.GetPylint(input_api, output_api, 15 extra_paths_list=_GetPathsToPrepend(input_api), pylintrc='pylintrc') 16 return input_api.RunTests(pylint_checks) 17 18 19def _GetPathsToPrepend(input_api): 20 current_dir = input_api.PresubmitLocalPath() 21 chromium_src_dir = input_api.os_path.join(current_dir, '..', '..') 22 return [ 23 input_api.os_path.join(chromium_src_dir, 'components'), 24 input_api.os_path.join(chromium_src_dir, 'tools', 'perf'), 25 input_api.os_path.join(chromium_src_dir, 'build', 'android'), 26 input_api.os_path.join(chromium_src_dir, 'build', 'android', 'gyp'), 27 input_api.os_path.join(chromium_src_dir, 28 'mojo', 'public', 'tools', 'bindings', 'pylib'), 29 input_api.os_path.join(chromium_src_dir, 'net', 'tools', 'net_docs'), 30 input_api.os_path.join(chromium_src_dir, 'tools'), 31 input_api.os_path.join(chromium_src_dir, 'third_party'), 32 input_api.os_path.join(chromium_src_dir, 33 'third_party', 'catapult', 'telemetry'), 34 input_api.os_path.join(chromium_src_dir, 35 'third_party', 'catapult', 'devil'), 36 input_api.os_path.join(chromium_src_dir, 37 'third_party', 'catapult', 'common', 'py_utils'), 38 ] 39 40 41def _PackageChecks(input_api, output_api): 42 """Verify API classes are in org.chromium.net package, and implementation 43 classes are not in org.chromium.net package.""" 44 api_file_pattern = input_api.re.compile( 45 r'^components/cronet/android/api/.*\.(java|template)$') 46 impl_file_pattern = input_api.re.compile( 47 r'^components/cronet/android/java/.*\.(java|template)$') 48 api_package_pattern = input_api.re.compile(r'^package (?!org.chromium.net;)') 49 impl_package_pattern = input_api.re.compile(r'^package org.chromium.net;') 50 51 source_filter = lambda path: input_api.FilterSourceFile(path, 52 files_to_check=[r'^components/cronet/android/.*\.(java|template)$']) 53 54 problems = [] 55 for f in input_api.AffectedSourceFiles(source_filter): 56 local_path = f.LocalPath() 57 for line_number, line in f.ChangedContents(): 58 if (api_file_pattern.search(local_path)): 59 if (api_package_pattern.search(line)): 60 problems.append( 61 '%s:%d\n %s' % (local_path, line_number, line.strip())) 62 elif (impl_file_pattern.search(local_path)): 63 if (impl_package_pattern.search(line)): 64 problems.append( 65 '%s:%d\n %s' % (local_path, line_number, line.strip())) 66 67 if problems: 68 return [output_api.PresubmitError( 69 'API classes must be in org.chromium.net package, and implementation\n' 70 'classes must not be in org.chromium.net package.', 71 problems)] 72 else: 73 return [] 74 75 76def _RunToolsUnittests(input_api, output_api): 77 return input_api.canned_checks.RunUnitTestsInDirectory( 78 input_api, output_api, '.', [ r'^tools_unittest\.py$']) 79 80 81def _ChangeAffectsCronetTools(change): 82 """ Returns |true| if the change may affect Cronet tools. """ 83 84 for path in change.LocalPaths(): 85 if path.startswith(os.path.join('components', 'cronet', 'tools')): 86 return True 87 return False 88 89 90def CheckChangeOnUpload(input_api, output_api): 91 results = [] 92 results.extend(_PyLintChecks(input_api, output_api)) 93 results.extend(_PackageChecks(input_api, output_api)) 94 if _ChangeAffectsCronetTools(input_api.change): 95 results.extend(_RunToolsUnittests(input_api, output_api)) 96 return results 97 98 99def CheckChangeOnCommit(input_api, output_api): 100 return _RunToolsUnittests(input_api, output_api) 101