pauljensen | c3a0423 | 2015-08-14 17:21:01 | [diff] [blame] | 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 | |
| 7 | See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
| 8 | for more details about the presubmit API built into depot_tools. |
| 9 | """ |
| 10 | |
pauljensen | 6c178be | 2016-11-18 23:06:56 | [diff] [blame] | 11 | import re |
| 12 | |
| 13 | |
pauljensen | 6a6e783 | 2016-05-05 15:43:03 | [diff] [blame] | 14 | def _PyLintChecks(input_api, output_api): |
| 15 | pylint_checks = input_api.canned_checks.GetPylint(input_api, output_api, |
| 16 | extra_paths_list=_GetPathsToPrepend(input_api), pylintrc='pylintrc') |
| 17 | return input_api.RunTests(pylint_checks) |
| 18 | |
pauljensen | 6c178be | 2016-11-18 23:06:56 | [diff] [blame] | 19 | |
pauljensen | 6a6e783 | 2016-05-05 15:43:03 | [diff] [blame] | 20 | def _GetPathsToPrepend(input_api): |
| 21 | current_dir = input_api.PresubmitLocalPath() |
| 22 | chromium_src_dir = input_api.os_path.join(current_dir, '..', '..') |
| 23 | return [ |
| 24 | input_api.os_path.join(current_dir, 'tools'), |
| 25 | input_api.os_path.join(current_dir, 'android', 'test', 'javaperftests'), |
| 26 | input_api.os_path.join(chromium_src_dir, 'tools', 'perf'), |
| 27 | input_api.os_path.join(chromium_src_dir, 'build', 'android'), |
| 28 | input_api.os_path.join(chromium_src_dir, 'build', 'android', 'gyp', 'util'), |
| 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 | ] |
| 37 | |
pauljensen | 6c178be | 2016-11-18 23:06:56 | [diff] [blame] | 38 | |
pauljensen | 17ebec65 | 2016-09-14 18:53:22 | [diff] [blame] | 39 | def _PackageChecks(input_api, output_api): |
| 40 | """Verify API classes are in org.chromium.net package, and implementation |
| 41 | classes are not in org.chromium.net package.""" |
| 42 | api_file_pattern = input_api.re.compile( |
| 43 | r'^components/cronet/android/api/.*\.(java|template)$') |
| 44 | impl_file_pattern = input_api.re.compile( |
| 45 | r'^components/cronet/android/java/.*\.(java|template)$') |
| 46 | api_package_pattern = input_api.re.compile(r'^package (?!org.chromium.net;)') |
| 47 | impl_package_pattern = input_api.re.compile(r'^package org.chromium.net;') |
| 48 | |
| 49 | source_filter = lambda path: input_api.FilterSourceFile(path, |
| 50 | white_list=[r'^components/cronet/android/.*\.(java|template)$']) |
| 51 | |
| 52 | problems = [] |
| 53 | for f in input_api.AffectedSourceFiles(source_filter): |
| 54 | local_path = f.LocalPath() |
| 55 | for line_number, line in f.ChangedContents(): |
| 56 | if (api_file_pattern.search(local_path)): |
| 57 | if (api_package_pattern.search(line)): |
| 58 | problems.append( |
| 59 | '%s:%d\n %s' % (local_path, line_number, line.strip())) |
| 60 | elif (impl_file_pattern.search(local_path)): |
| 61 | if (impl_package_pattern.search(line)): |
| 62 | problems.append( |
| 63 | '%s:%d\n %s' % (local_path, line_number, line.strip())) |
| 64 | |
| 65 | if problems: |
| 66 | return [output_api.PresubmitError( |
| 67 | 'API classes must be in org.chromium.net package, and implementation\n' |
| 68 | 'classes must not be in org.chromium.net package.', |
| 69 | problems)] |
| 70 | else: |
| 71 | return [] |
| 72 | |
pauljensen | 6c178be | 2016-11-18 23:06:56 | [diff] [blame] | 73 | |
pauljensen | c3a0423 | 2015-08-14 17:21:01 | [diff] [blame] | 74 | def CheckChangeOnUpload(input_api, output_api): |
pauljensen | 6a6e783 | 2016-05-05 15:43:03 | [diff] [blame] | 75 | results = [] |
| 76 | results.extend(_PyLintChecks(input_api, output_api)) |
| 77 | results.extend( |
| 78 | input_api.canned_checks.CheckPatchFormatted(input_api, output_api)) |
pauljensen | 17ebec65 | 2016-09-14 18:53:22 | [diff] [blame] | 79 | results.extend(_PackageChecks(input_api, output_api)) |
pauljensen | 6a6e783 | 2016-05-05 15:43:03 | [diff] [blame] | 80 | return results |
pauljensen | 6c178be | 2016-11-18 23:06:56 | [diff] [blame] | 81 | |
| 82 | |
| 83 | def _GetTryMasters(project, change): |
| 84 | return { |
| 85 | 'master.tryserver.chromium.android': { |
| 86 | 'android_cronet_tester': [], |
| 87 | }, |
| 88 | } |
| 89 | |
| 90 | |
| 91 | def GetPreferredTryMasters(project, change): |
| 92 | # TODO(nick, dcheng): Using the value of _GetTryMasters() instead of an empty |
| 93 | # value here would cause 'git cl try' to include the Cronet trybot, |
| 94 | # which would be nice. But it has the side effect of replacing, rather than |
| 95 | # augmenting, the default set of try servers. Re-enable this when we figure |
| 96 | # out a way to augment the default set. |
| 97 | return {} |
| 98 | |
| 99 | |
| 100 | def PostUploadHook(cl, change, output_api): |
| 101 | """git cl upload will call this hook after the issue is created/modified. |
| 102 | |
| 103 | This hook adds an extra try bot to the CL description in order to run Cronet |
| 104 | tests in addition to CQ try bots. |
| 105 | """ |
| 106 | rietveld_obj = cl.RpcServer() |
| 107 | issue = cl.issue |
| 108 | description = rietveld_obj.get_description(issue) |
| 109 | if re.search(r'^CQ_INCLUDE_TRYBOTS=.*', description, re.M | re.I): |
| 110 | return [] |
| 111 | |
| 112 | masters = _GetTryMasters(None, change) |
| 113 | results = [] |
| 114 | new_description = description |
| 115 | new_description += '\nCQ_INCLUDE_TRYBOTS=%s' % ';'.join( |
| 116 | '%s:%s' % (master, ','.join(bots)) |
| 117 | for master, bots in masters.iteritems()) |
| 118 | results.append(output_api.PresubmitNotifyResult( |
| 119 | 'Automatically added Cronet trybot to run tests on CQ.')) |
| 120 | |
| 121 | rietveld_obj.update_description(issue, new_description) |
| 122 | |
| 123 | return results |