andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 1 | # Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 2 | # |
| 3 | # Use of this source code is governed by a BSD-style license |
| 4 | # that can be found in the LICENSE file in the root of the source |
| 5 | # tree. An additional intellectual property rights grant can be found |
| 6 | # in the file PATENTS. All contributing project authors may |
| 7 | # be found in the AUTHORS file in the root of the source tree. |
niklase@google.com | da159d6 | 2011-05-30 11:51:34 +0000 | [diff] [blame] | 8 | |
kjellander | 986ee08 | 2015-06-16 04:32:13 -0700 | [diff] [blame] | 9 | import json |
kjellander@webrtc.org | aefe61a | 2014-12-08 13:00:30 +0000 | [diff] [blame] | 10 | import os |
kjellander | 986ee08 | 2015-06-16 04:32:13 -0700 | [diff] [blame] | 11 | import platform |
kjellander@webrtc.org | 8575980 | 2013-10-22 16:47:40 +0000 | [diff] [blame] | 12 | import re |
kjellander | 986ee08 | 2015-06-16 04:32:13 -0700 | [diff] [blame] | 13 | import subprocess |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 14 | import sys |
kjellander@webrtc.org | 8575980 | 2013-10-22 16:47:40 +0000 | [diff] [blame] | 15 | |
| 16 | |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 17 | # Directories that will be scanned by cpplint by the presubmit script. |
| 18 | CPPLINT_DIRS = [ |
Fredrik Solenberg | ea07373 | 2015-12-01 11:26:34 +0100 | [diff] [blame] | 19 | 'webrtc/audio', |
| 20 | 'webrtc/call', |
jbauch | 0f2e939 | 2015-12-10 03:11:42 -0800 | [diff] [blame] | 21 | 'webrtc/common_video', |
jbauch | 70625e5 | 2015-12-09 14:18:14 -0800 | [diff] [blame] | 22 | 'webrtc/examples', |
jbauch | f91e6d0 | 2016-01-24 23:05:21 -0800 | [diff] [blame] | 23 | 'webrtc/modules/bitrate_controller', |
Stefan Holmer | 80e1207 | 2016-02-23 13:30:42 +0100 | [diff] [blame^] | 24 | 'webrtc/modules/congestion_controller', |
jbauch | d2a2296 | 2016-02-08 23:18:25 -0800 | [diff] [blame] | 25 | 'webrtc/modules/pacing', |
terelius | 8f09f17 | 2015-12-15 00:51:54 -0800 | [diff] [blame] | 26 | 'webrtc/modules/remote_bitrate_estimator', |
danilchap | 377b5e6 | 2015-12-15 04:33:44 -0800 | [diff] [blame] | 27 | 'webrtc/modules/rtp_rtcp', |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 28 | 'webrtc/modules/video_coding', |
mflodman | 88eeac4 | 2015-12-08 09:21:28 +0100 | [diff] [blame] | 29 | 'webrtc/modules/video_processing', |
jbauch | 0f2e939 | 2015-12-10 03:11:42 -0800 | [diff] [blame] | 30 | 'webrtc/sound', |
| 31 | 'webrtc/tools', |
mflodman | d1590b2 | 2015-12-09 07:07:59 -0800 | [diff] [blame] | 32 | 'webrtc/video', |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 33 | ] |
| 34 | |
jbauch | c4e3ead | 2016-02-19 00:25:55 -0800 | [diff] [blame] | 35 | # These filters will always be removed, even if the caller specifies a filter |
| 36 | # set, as they are problematic or broken in some way. |
| 37 | # |
| 38 | # Justifications for each filter: |
| 39 | # - build/c++11 : Rvalue ref checks are unreliable (false positives), |
| 40 | # include file and feature blacklists are |
| 41 | # google3-specific. |
| 42 | BLACKLIST_LINT_FILTERS = [ |
| 43 | '-build/c++11', |
| 44 | ] |
| 45 | |
kjellander | fd59523 | 2015-12-04 02:44:09 -0800 | [diff] [blame] | 46 | # List of directories of "supported" native APIs. That means changes to headers |
| 47 | # will be done in a compatible way following this scheme: |
| 48 | # 1. Non-breaking changes are made. |
| 49 | # 2. The old APIs as marked as deprecated (with comments). |
| 50 | # 3. Deprecation is announced to discuss-webrtc@googlegroups.com and |
| 51 | # webrtc-users@google.com (internal list). |
| 52 | # 4. (later) The deprecated APIs are removed. |
| 53 | # Directories marked as DEPRECATED should not be used. They're only present in |
| 54 | # the list to support legacy downstream code. |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 55 | NATIVE_API_DIRS = ( |
| 56 | 'talk/app/webrtc', |
| 57 | 'webrtc', |
kjellander | fd59523 | 2015-12-04 02:44:09 -0800 | [diff] [blame] | 58 | 'webrtc/base', # DEPRECATED. |
| 59 | 'webrtc/common_audio/include', # DEPRECATED. |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 60 | 'webrtc/modules/audio_coding/include', |
kjellander | fd59523 | 2015-12-04 02:44:09 -0800 | [diff] [blame] | 61 | 'webrtc/modules/audio_conference_mixer/include', # DEPRECATED. |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 62 | 'webrtc/modules/audio_device/include', |
| 63 | 'webrtc/modules/audio_processing/include', |
| 64 | 'webrtc/modules/bitrate_controller/include', |
Stefan Holmer | 80e1207 | 2016-02-23 13:30:42 +0100 | [diff] [blame^] | 65 | 'webrtc/modules/congestion_controller/include', |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 66 | 'webrtc/modules/include', |
| 67 | 'webrtc/modules/remote_bitrate_estimator/include', |
| 68 | 'webrtc/modules/rtp_rtcp/include', |
kjellander | fd59523 | 2015-12-04 02:44:09 -0800 | [diff] [blame] | 69 | 'webrtc/modules/rtp_rtcp/source', # DEPRECATED. |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 70 | 'webrtc/modules/utility/include', |
| 71 | 'webrtc/modules/video_coding/codecs/h264/include', |
| 72 | 'webrtc/modules/video_coding/codecs/i420/include', |
| 73 | 'webrtc/modules/video_coding/codecs/vp8/include', |
| 74 | 'webrtc/modules/video_coding/codecs/vp9/include', |
| 75 | 'webrtc/modules/video_coding/include', |
kjellander | fd59523 | 2015-12-04 02:44:09 -0800 | [diff] [blame] | 76 | 'webrtc/system_wrappers/include', # DEPRECATED. |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 77 | 'webrtc/voice_engine/include', |
| 78 | ) |
| 79 | |
| 80 | |
| 81 | def _VerifyNativeApiHeadersListIsValid(input_api, output_api): |
| 82 | """Ensures the list of native API header directories is up to date.""" |
| 83 | non_existing_paths = [] |
| 84 | native_api_full_paths = [ |
| 85 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 86 | *path.split('/')) for path in NATIVE_API_DIRS] |
| 87 | for path in native_api_full_paths: |
| 88 | if not os.path.isdir(path): |
| 89 | non_existing_paths.append(path) |
| 90 | if non_existing_paths: |
| 91 | return [output_api.PresubmitError( |
| 92 | 'Directories to native API headers have changed which has made the ' |
| 93 | 'list in PRESUBMIT.py outdated.\nPlease update it to the current ' |
| 94 | 'location of our native APIs.', |
| 95 | non_existing_paths)] |
| 96 | return [] |
| 97 | |
| 98 | |
| 99 | def _CheckNativeApiHeaderChanges(input_api, output_api): |
| 100 | """Checks to remind proper changing of native APIs.""" |
| 101 | files = [] |
| 102 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 103 | if f.LocalPath().endswith('.h'): |
| 104 | for path in NATIVE_API_DIRS: |
| 105 | if os.path.dirname(f.LocalPath()) == path: |
| 106 | files.append(f) |
| 107 | |
| 108 | if files: |
kjellander | ffea13c | 2015-12-08 01:57:17 -0800 | [diff] [blame] | 109 | return [output_api.PresubmitNotifyResult( |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 110 | 'You seem to be changing native API header files. Please make sure ' |
| 111 | 'you:\n' |
| 112 | ' 1. Make compatible changes that don\'t break existing clients.\n' |
| 113 | ' 2. Mark the old APIs as deprecated.\n' |
| 114 | ' 3. Create a timeline and plan for when the deprecated method will ' |
| 115 | 'be removed (preferably 3 months or so).\n' |
| 116 | ' 4. Update/inform existing downstream code owners to stop using the ' |
| 117 | 'deprecated APIs: \n' |
| 118 | 'send announcement to discuss-webrtc@googlegroups.com and ' |
| 119 | 'webrtc-users@google.com.\n' |
| 120 | ' 5. (after ~3 months) remove the deprecated API.\n' |
| 121 | 'Related files:', |
| 122 | files)] |
| 123 | return [] |
| 124 | |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 125 | |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 126 | def _CheckNoIOStreamInHeaders(input_api, output_api): |
| 127 | """Checks to make sure no .h files include <iostream>.""" |
| 128 | files = [] |
| 129 | pattern = input_api.re.compile(r'^#include\s*<iostream>', |
| 130 | input_api.re.MULTILINE) |
| 131 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 132 | if not f.LocalPath().endswith('.h'): |
| 133 | continue |
| 134 | contents = input_api.ReadFile(f) |
| 135 | if pattern.search(contents): |
| 136 | files.append(f) |
| 137 | |
| 138 | if len(files): |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 139 | return [output_api.PresubmitError( |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 140 | 'Do not #include <iostream> in header files, since it inserts static ' + |
| 141 | 'initialization into every file including the header. Instead, ' + |
| 142 | '#include <ostream>. See http://crbug.com/94794', |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 143 | files)] |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 144 | return [] |
| 145 | |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 146 | |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 147 | def _CheckNoFRIEND_TEST(input_api, output_api): |
| 148 | """Make sure that gtest's FRIEND_TEST() macro is not used, the |
| 149 | FRIEND_TEST_ALL_PREFIXES() macro from testsupport/gtest_prod_util.h should be |
| 150 | used instead since that allows for FLAKY_, FAILS_ and DISABLED_ prefixes.""" |
| 151 | problems = [] |
| 152 | |
| 153 | file_filter = lambda f: f.LocalPath().endswith(('.cc', '.h')) |
| 154 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 155 | for line_num, line in f.ChangedContents(): |
| 156 | if 'FRIEND_TEST(' in line: |
| 157 | problems.append(' %s:%d' % (f.LocalPath(), line_num)) |
| 158 | |
| 159 | if not problems: |
| 160 | return [] |
| 161 | return [output_api.PresubmitPromptWarning('WebRTC\'s code should not use ' |
| 162 | 'gtest\'s FRIEND_TEST() macro. Include testsupport/gtest_prod_util.h and ' |
| 163 | 'use FRIEND_TEST_ALL_PREFIXES() instead.\n' + '\n'.join(problems))] |
| 164 | |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 165 | |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 166 | def _IsLintWhitelisted(whitelist_dirs, file_path): |
| 167 | """ Checks if a file is whitelisted for lint check.""" |
| 168 | for path in whitelist_dirs: |
| 169 | if os.path.dirname(file_path).startswith(path): |
| 170 | return True |
| 171 | return False |
| 172 | |
| 173 | |
mflodman@webrtc.org | 2a45209 | 2012-07-01 05:55:23 +0000 | [diff] [blame] | 174 | def _CheckApprovedFilesLintClean(input_api, output_api, |
| 175 | source_file_filter=None): |
| 176 | """Checks that all new or whitelisted .cc and .h files pass cpplint.py. |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 177 | This check is based on _CheckChangeLintsClean in |
| 178 | depot_tools/presubmit_canned_checks.py but has less filters and only checks |
| 179 | added files.""" |
| 180 | result = [] |
| 181 | |
| 182 | # Initialize cpplint. |
| 183 | import cpplint |
| 184 | # Access to a protected member _XX of a client class |
| 185 | # pylint: disable=W0212 |
| 186 | cpplint._cpplint_state.ResetErrorCounts() |
| 187 | |
jbauch | c4e3ead | 2016-02-19 00:25:55 -0800 | [diff] [blame] | 188 | lint_filters = cpplint._Filters() |
| 189 | lint_filters.extend(BLACKLIST_LINT_FILTERS) |
| 190 | cpplint._SetFilters(','.join(lint_filters)) |
| 191 | |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 192 | # Create a platform independent whitelist for the CPPLINT_DIRS. |
| 193 | whitelist_dirs = [input_api.os_path.join(*path.split('/')) |
| 194 | for path in CPPLINT_DIRS] |
| 195 | |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 196 | # Use the strictest verbosity level for cpplint.py (level 1) which is the |
| 197 | # default when running cpplint.py from command line. |
| 198 | # To make it possible to work with not-yet-converted code, we're only applying |
mflodman@webrtc.org | 2a45209 | 2012-07-01 05:55:23 +0000 | [diff] [blame] | 199 | # it to new (or moved/renamed) files and files listed in LINT_FOLDERS. |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 200 | verbosity_level = 1 |
| 201 | files = [] |
| 202 | for f in input_api.AffectedSourceFiles(source_file_filter): |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 203 | # Note that moved/renamed files also count as added. |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 204 | if f.Action() == 'A' or _IsLintWhitelisted(whitelist_dirs, f.LocalPath()): |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 205 | files.append(f.AbsoluteLocalPath()) |
mflodman@webrtc.org | 2a45209 | 2012-07-01 05:55:23 +0000 | [diff] [blame] | 206 | |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 207 | for file_name in files: |
| 208 | cpplint.ProcessFile(file_name, verbosity_level) |
| 209 | |
| 210 | if cpplint._cpplint_state.error_count > 0: |
| 211 | if input_api.is_committing: |
| 212 | # TODO(kjellander): Change back to PresubmitError below when we're |
| 213 | # confident with the lint settings. |
| 214 | res_type = output_api.PresubmitPromptWarning |
| 215 | else: |
| 216 | res_type = output_api.PresubmitPromptWarning |
| 217 | result = [res_type('Changelist failed cpplint.py check.')] |
| 218 | |
| 219 | return result |
| 220 | |
henrike@webrtc.org | 83fe69d | 2014-09-30 21:54:26 +0000 | [diff] [blame] | 221 | def _CheckNoRtcBaseDeps(input_api, gyp_files, output_api): |
| 222 | pattern = input_api.re.compile(r"base.gyp:rtc_base\s*'") |
| 223 | violating_files = [] |
| 224 | for f in gyp_files: |
henrike@webrtc.org | 36b0c1a | 2014-10-01 14:40:58 +0000 | [diff] [blame] | 225 | gyp_exceptions = ( |
| 226 | 'base_tests.gyp', |
| 227 | 'desktop_capture.gypi', |
kjellander@webrtc.org | e723728 | 2015-02-26 11:12:17 +0000 | [diff] [blame] | 228 | 'p2p.gyp', |
henrike@webrtc.org | 36b0c1a | 2014-10-01 14:40:58 +0000 | [diff] [blame] | 229 | 'sound.gyp', |
| 230 | 'webrtc_test_common.gyp', |
| 231 | 'webrtc_tests.gypi', |
| 232 | ) |
| 233 | if f.LocalPath().endswith(gyp_exceptions): |
| 234 | continue |
henrike@webrtc.org | 83fe69d | 2014-09-30 21:54:26 +0000 | [diff] [blame] | 235 | contents = input_api.ReadFile(f) |
| 236 | if pattern.search(contents): |
| 237 | violating_files.append(f) |
| 238 | if violating_files: |
| 239 | return [output_api.PresubmitError( |
| 240 | 'Depending on rtc_base is not allowed. Change your dependency to ' |
| 241 | 'rtc_base_approved and possibly sanitize and move the desired source ' |
| 242 | 'file(s) to rtc_base_approved.\nChanged GYP files:', |
| 243 | items=violating_files)] |
| 244 | return [] |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 245 | |
kjellander@webrtc.org | f68ffca | 2015-01-27 13:13:24 +0000 | [diff] [blame] | 246 | def _CheckNoSourcesAboveGyp(input_api, gyp_files, output_api): |
| 247 | # Disallow referencing source files with paths above the GYP file location. |
| 248 | source_pattern = input_api.re.compile(r'sources.*?\[(.*?)\]', |
| 249 | re.MULTILINE | re.DOTALL) |
kjellander@webrtc.org | a33f05e | 2015-01-29 14:29:45 +0000 | [diff] [blame] | 250 | file_pattern = input_api.re.compile(r"'((\.\./.*?)|(<\(webrtc_root\).*?))'") |
kjellander@webrtc.org | f68ffca | 2015-01-27 13:13:24 +0000 | [diff] [blame] | 251 | violating_gyp_files = set() |
| 252 | violating_source_entries = [] |
| 253 | for gyp_file in gyp_files: |
kjellander | c61635c | 2016-02-02 02:30:07 -0800 | [diff] [blame] | 254 | if 'supplement.gypi' in gyp_file.LocalPath(): |
| 255 | # Exclude supplement.gypi from this check, as the LSan and TSan |
| 256 | # suppression files are located in a different location. |
| 257 | continue |
kjellander@webrtc.org | f68ffca | 2015-01-27 13:13:24 +0000 | [diff] [blame] | 258 | contents = input_api.ReadFile(gyp_file) |
| 259 | for source_block_match in source_pattern.finditer(contents): |
kjellander@webrtc.org | c98f6f3 | 2015-03-04 07:08:11 +0000 | [diff] [blame] | 260 | # Find all source list entries starting with ../ in the source block |
| 261 | # (exclude overrides entries). |
kjellander@webrtc.org | f68ffca | 2015-01-27 13:13:24 +0000 | [diff] [blame] | 262 | for file_list_match in file_pattern.finditer(source_block_match.group(0)): |
kjellander@webrtc.org | c98f6f3 | 2015-03-04 07:08:11 +0000 | [diff] [blame] | 263 | source_file = file_list_match.group(0) |
| 264 | if 'overrides/' not in source_file: |
| 265 | violating_source_entries.append(source_file) |
| 266 | violating_gyp_files.add(gyp_file) |
kjellander@webrtc.org | f68ffca | 2015-01-27 13:13:24 +0000 | [diff] [blame] | 267 | if violating_gyp_files: |
| 268 | return [output_api.PresubmitError( |
| 269 | 'Referencing source files above the directory of the GYP file is not ' |
| 270 | 'allowed. Please introduce new GYP targets and/or GYP files in the ' |
| 271 | 'proper location instead.\n' |
| 272 | 'Invalid source entries:\n' |
| 273 | '%s\n' |
| 274 | 'Violating GYP files:' % '\n'.join(violating_source_entries), |
| 275 | items=violating_gyp_files)] |
| 276 | return [] |
| 277 | |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 278 | def _CheckGypChanges(input_api, output_api): |
| 279 | source_file_filter = lambda x: input_api.FilterSourceFile( |
| 280 | x, white_list=(r'.+\.(gyp|gypi)$',)) |
| 281 | |
| 282 | gyp_files = [] |
| 283 | for f in input_api.AffectedSourceFiles(source_file_filter): |
kjellander@webrtc.org | 3398a4a | 2014-11-24 10:05:37 +0000 | [diff] [blame] | 284 | if f.LocalPath().startswith('webrtc'): |
| 285 | gyp_files.append(f) |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 286 | |
| 287 | result = [] |
| 288 | if gyp_files: |
| 289 | result.append(output_api.PresubmitNotifyResult( |
| 290 | 'As you\'re changing GYP files: please make sure corresponding ' |
| 291 | 'BUILD.gn files are also updated.\nChanged GYP files:', |
| 292 | items=gyp_files)) |
henrike@webrtc.org | 83fe69d | 2014-09-30 21:54:26 +0000 | [diff] [blame] | 293 | result.extend(_CheckNoRtcBaseDeps(input_api, gyp_files, output_api)) |
kjellander@webrtc.org | f68ffca | 2015-01-27 13:13:24 +0000 | [diff] [blame] | 294 | result.extend(_CheckNoSourcesAboveGyp(input_api, gyp_files, output_api)) |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 295 | return result |
| 296 | |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 297 | def _CheckUnwantedDependencies(input_api, output_api): |
| 298 | """Runs checkdeps on #include statements added in this |
| 299 | change. Breaking - rules is an error, breaking ! rules is a |
| 300 | warning. |
| 301 | """ |
| 302 | # Copied from Chromium's src/PRESUBMIT.py. |
| 303 | |
| 304 | # We need to wait until we have an input_api object and use this |
| 305 | # roundabout construct to import checkdeps because this file is |
| 306 | # eval-ed and thus doesn't have __file__. |
| 307 | original_sys_path = sys.path |
| 308 | try: |
kjellander@webrtc.org | aefe61a | 2014-12-08 13:00:30 +0000 | [diff] [blame] | 309 | checkdeps_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 310 | 'buildtools', 'checkdeps') |
| 311 | if not os.path.exists(checkdeps_path): |
| 312 | return [output_api.PresubmitError( |
| 313 | 'Cannot find checkdeps at %s\nHave you run "gclient sync" to ' |
| 314 | 'download Chromium and setup the symlinks?' % checkdeps_path)] |
| 315 | sys.path.append(checkdeps_path) |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 316 | import checkdeps |
| 317 | from cpp_checker import CppChecker |
| 318 | from rules import Rule |
| 319 | finally: |
| 320 | # Restore sys.path to what it was before. |
| 321 | sys.path = original_sys_path |
| 322 | |
| 323 | added_includes = [] |
| 324 | for f in input_api.AffectedFiles(): |
| 325 | if not CppChecker.IsCppFile(f.LocalPath()): |
| 326 | continue |
| 327 | |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 328 | changed_lines = [line for _, line in f.ChangedContents()] |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 329 | added_includes.append([f.LocalPath(), changed_lines]) |
| 330 | |
| 331 | deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath()) |
| 332 | |
| 333 | error_descriptions = [] |
| 334 | warning_descriptions = [] |
| 335 | for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes( |
| 336 | added_includes): |
| 337 | description_with_path = '%s\n %s' % (path, rule_description) |
| 338 | if rule_type == Rule.DISALLOW: |
| 339 | error_descriptions.append(description_with_path) |
| 340 | else: |
| 341 | warning_descriptions.append(description_with_path) |
| 342 | |
| 343 | results = [] |
| 344 | if error_descriptions: |
| 345 | results.append(output_api.PresubmitError( |
| 346 | 'You added one or more #includes that violate checkdeps rules.', |
| 347 | error_descriptions)) |
| 348 | if warning_descriptions: |
| 349 | results.append(output_api.PresubmitPromptOrNotify( |
| 350 | 'You added one or more #includes of files that are temporarily\n' |
| 351 | 'allowed but being removed. Can you avoid introducing the\n' |
| 352 | '#include? See relevant DEPS file(s) for details and contacts.', |
| 353 | warning_descriptions)) |
| 354 | return results |
| 355 | |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 356 | |
kjellander | 569cf94 | 2016-02-11 05:02:59 -0800 | [diff] [blame] | 357 | def _CheckJSONParseErrors(input_api, output_api): |
| 358 | """Check that JSON files do not contain syntax errors.""" |
| 359 | |
| 360 | def FilterFile(affected_file): |
| 361 | return input_api.os_path.splitext(affected_file.LocalPath())[1] == '.json' |
| 362 | |
| 363 | def GetJSONParseError(input_api, filename): |
| 364 | try: |
| 365 | contents = input_api.ReadFile(filename) |
| 366 | input_api.json.loads(contents) |
| 367 | except ValueError as e: |
| 368 | return e |
| 369 | return None |
| 370 | |
| 371 | results = [] |
| 372 | for affected_file in input_api.AffectedFiles( |
| 373 | file_filter=FilterFile, include_deletes=False): |
| 374 | parse_error = GetJSONParseError(input_api, |
| 375 | affected_file.AbsoluteLocalPath()) |
| 376 | if parse_error: |
| 377 | results.append(output_api.PresubmitError('%s could not be parsed: %s' % |
| 378 | (affected_file.LocalPath(), parse_error))) |
| 379 | return results |
| 380 | |
| 381 | |
Henrik Kjellander | 8d3ad82 | 2015-05-26 19:52:05 +0200 | [diff] [blame] | 382 | def _RunPythonTests(input_api, output_api): |
| 383 | def join(*args): |
| 384 | return input_api.os_path.join(input_api.PresubmitLocalPath(), *args) |
| 385 | |
| 386 | test_directories = [ |
| 387 | join('tools', 'autoroller', 'unittests'), |
| 388 | ] |
| 389 | |
| 390 | tests = [] |
| 391 | for directory in test_directories: |
| 392 | tests.extend( |
| 393 | input_api.canned_checks.GetUnitTestsInDirectory( |
| 394 | input_api, |
| 395 | output_api, |
| 396 | directory, |
| 397 | whitelist=[r'.+_test\.py$'])) |
| 398 | return input_api.RunTests(tests, parallel=True) |
| 399 | |
| 400 | |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 401 | def _CommonChecks(input_api, output_api): |
| 402 | """Checks common to both upload and commit.""" |
niklase@google.com | da159d6 | 2011-05-30 11:51:34 +0000 | [diff] [blame] | 403 | results = [] |
tkchin | 42f580e | 2015-11-26 23:18:23 -0800 | [diff] [blame] | 404 | # Filter out files that are in objc or ios dirs from being cpplint-ed since |
| 405 | # they do not follow C++ lint rules. |
| 406 | black_list = input_api.DEFAULT_BLACK_LIST + ( |
| 407 | r".*\bobjc[\\\/].*", |
| 408 | ) |
| 409 | source_file_filter = lambda x: input_api.FilterSourceFile(x, None, black_list) |
| 410 | results.extend(_CheckApprovedFilesLintClean( |
| 411 | input_api, output_api, source_file_filter)) |
phoglund@webrtc.org | 5d371393 | 2013-03-07 09:59:43 +0000 | [diff] [blame] | 412 | results.extend(input_api.canned_checks.RunPylint(input_api, output_api, |
| 413 | black_list=(r'^.*gviz_api\.py$', |
| 414 | r'^.*gaeunit\.py$', |
fischman@webrtc.org | 33584f9 | 2013-07-25 16:43:30 +0000 | [diff] [blame] | 415 | # Embedded shell-script fakes out pylint. |
Henrik Kjellander | 14771ac | 2015-06-02 13:10:04 +0200 | [diff] [blame] | 416 | r'^build[\\\/].*\.py$', |
| 417 | r'^buildtools[\\\/].*\.py$', |
| 418 | r'^chromium[\\\/].*\.py$', |
| 419 | r'^google_apis[\\\/].*\.py$', |
| 420 | r'^net.*[\\\/].*\.py$', |
| 421 | r'^out.*[\\\/].*\.py$', |
| 422 | r'^testing[\\\/].*\.py$', |
| 423 | r'^third_party[\\\/].*\.py$', |
| 424 | r'^tools[\\\/]find_depot_tools.py$', |
| 425 | r'^tools[\\\/]clang[\\\/].*\.py$', |
| 426 | r'^tools[\\\/]generate_library_loader[\\\/].*\.py$', |
| 427 | r'^tools[\\\/]gn[\\\/].*\.py$', |
| 428 | r'^tools[\\\/]gyp[\\\/].*\.py$', |
Henrik Kjellander | d6d27e7 | 2015-09-25 22:19:11 +0200 | [diff] [blame] | 429 | r'^tools[\\\/]isolate_driver.py$', |
Henrik Kjellander | 14771ac | 2015-06-02 13:10:04 +0200 | [diff] [blame] | 430 | r'^tools[\\\/]protoc_wrapper[\\\/].*\.py$', |
| 431 | r'^tools[\\\/]python[\\\/].*\.py$', |
| 432 | r'^tools[\\\/]python_charts[\\\/]data[\\\/].*\.py$', |
| 433 | r'^tools[\\\/]refactoring[\\\/].*\.py$', |
| 434 | r'^tools[\\\/]swarming_client[\\\/].*\.py$', |
| 435 | r'^tools[\\\/]vim[\\\/].*\.py$', |
phoglund@webrtc.org | 5d371393 | 2013-03-07 09:59:43 +0000 | [diff] [blame] | 436 | # TODO(phoglund): should arguably be checked. |
Henrik Kjellander | 14771ac | 2015-06-02 13:10:04 +0200 | [diff] [blame] | 437 | r'^tools[\\\/]valgrind-webrtc[\\\/].*\.py$', |
| 438 | r'^tools[\\\/]valgrind[\\\/].*\.py$', |
| 439 | r'^tools[\\\/]win[\\\/].*\.py$', |
| 440 | r'^xcodebuild.*[\\\/].*\.py$',), |
phoglund@webrtc.org | 5d371393 | 2013-03-07 09:59:43 +0000 | [diff] [blame] | 441 | disabled_warnings=['F0401', # Failed to import x |
| 442 | 'E0611', # No package y in x |
| 443 | 'W0232', # Class has no __init__ method |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 444 | ], |
| 445 | pylintrc='pylintrc')) |
kjellander | 569cf94 | 2016-02-11 05:02:59 -0800 | [diff] [blame] | 446 | |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 447 | # WebRTC can't use the presubmit_canned_checks.PanProjectChecks function since |
| 448 | # we need to have different license checks in talk/ and webrtc/ directories. |
| 449 | # Instead, hand-picked checks are included below. |
Henrik Kjellander | 6322467 | 2015-09-08 08:03:56 +0200 | [diff] [blame] | 450 | |
| 451 | # Skip long-lines check for DEPS, GN and GYP files. |
| 452 | long_lines_sources = lambda x: input_api.FilterSourceFile(x, |
| 453 | black_list=(r'.+\.gyp$', r'.+\.gypi$', r'.+\.gn$', r'.+\.gni$', 'DEPS')) |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 454 | results.extend(input_api.canned_checks.CheckLongLines( |
Henrik Kjellander | 6322467 | 2015-09-08 08:03:56 +0200 | [diff] [blame] | 455 | input_api, output_api, maxlen=80, source_file_filter=long_lines_sources)) |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 456 | results.extend(input_api.canned_checks.CheckChangeHasNoTabs( |
| 457 | input_api, output_api)) |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 458 | results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace( |
| 459 | input_api, output_api)) |
| 460 | results.extend(input_api.canned_checks.CheckChangeTodoHasOwner( |
| 461 | input_api, output_api)) |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 462 | results.extend(_CheckNativeApiHeaderChanges(input_api, output_api)) |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 463 | results.extend(_CheckNoIOStreamInHeaders(input_api, output_api)) |
| 464 | results.extend(_CheckNoFRIEND_TEST(input_api, output_api)) |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 465 | results.extend(_CheckGypChanges(input_api, output_api)) |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 466 | results.extend(_CheckUnwantedDependencies(input_api, output_api)) |
kjellander | 569cf94 | 2016-02-11 05:02:59 -0800 | [diff] [blame] | 467 | results.extend(_CheckJSONParseErrors(input_api, output_api)) |
Henrik Kjellander | 8d3ad82 | 2015-05-26 19:52:05 +0200 | [diff] [blame] | 468 | results.extend(_RunPythonTests(input_api, output_api)) |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 469 | return results |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 470 | |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 471 | |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 472 | def CheckChangeOnUpload(input_api, output_api): |
| 473 | results = [] |
| 474 | results.extend(_CommonChecks(input_api, output_api)) |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 475 | results.extend( |
| 476 | input_api.canned_checks.CheckGNFormatted(input_api, output_api)) |
niklase@google.com | da159d6 | 2011-05-30 11:51:34 +0000 | [diff] [blame] | 477 | return results |
| 478 | |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 479 | |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 480 | def CheckChangeOnCommit(input_api, output_api): |
niklase@google.com | 1198db9 | 2011-06-09 07:07:24 +0000 | [diff] [blame] | 481 | results = [] |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 482 | results.extend(_CommonChecks(input_api, output_api)) |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 483 | results.extend(_VerifyNativeApiHeadersListIsValid(input_api, output_api)) |
niklase@google.com | 1198db9 | 2011-06-09 07:07:24 +0000 | [diff] [blame] | 484 | results.extend(input_api.canned_checks.CheckOwners(input_api, output_api)) |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 485 | results.extend(input_api.canned_checks.CheckChangeWasUploaded( |
| 486 | input_api, output_api)) |
| 487 | results.extend(input_api.canned_checks.CheckChangeHasDescription( |
| 488 | input_api, output_api)) |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 489 | results.extend(input_api.canned_checks.CheckChangeHasBugField( |
| 490 | input_api, output_api)) |
| 491 | results.extend(input_api.canned_checks.CheckChangeHasTestField( |
| 492 | input_api, output_api)) |
kjellander@webrtc.org | 12cb88c | 2014-02-13 11:53:43 +0000 | [diff] [blame] | 493 | results.extend(input_api.canned_checks.CheckTreeIsOpen( |
| 494 | input_api, output_api, |
| 495 | json_url='http://webrtc-status.appspot.com/current?format=json')) |
niklase@google.com | 1198db9 | 2011-06-09 07:07:24 +0000 | [diff] [blame] | 496 | return results |
kjellander@webrtc.org | 8575980 | 2013-10-22 16:47:40 +0000 | [diff] [blame] | 497 | |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 498 | |
kjellander@webrtc.org | 8575980 | 2013-10-22 16:47:40 +0000 | [diff] [blame] | 499 | # pylint: disable=W0613 |
kjellander@webrtc.org | c7b8b2f | 2014-04-03 20:19:36 +0000 | [diff] [blame] | 500 | def GetPreferredTryMasters(project, change): |
kjellander | 986ee08 | 2015-06-16 04:32:13 -0700 | [diff] [blame] | 501 | cq_config_path = os.path.join( |
tandrii | 04465d2 | 2015-06-20 04:00:49 -0700 | [diff] [blame] | 502 | change.RepositoryRoot(), 'infra', 'config', 'cq.cfg') |
kjellander | 986ee08 | 2015-06-16 04:32:13 -0700 | [diff] [blame] | 503 | # commit_queue.py below is a script in depot_tools directory, which has a |
| 504 | # 'builders' command to retrieve a list of CQ builders from the CQ config. |
| 505 | is_win = platform.system() == 'Windows' |
| 506 | masters = json.loads(subprocess.check_output( |
| 507 | ['commit_queue', 'builders', cq_config_path], shell=is_win)) |
kjellander@webrtc.org | 8575980 | 2013-10-22 16:47:40 +0000 | [diff] [blame] | 508 | |
kjellander | 986ee08 | 2015-06-16 04:32:13 -0700 | [diff] [blame] | 509 | try_config = {} |
| 510 | for master in masters: |
| 511 | try_config.setdefault(master, {}) |
| 512 | for builder in masters[master]: |
| 513 | if 'presubmit' in builder: |
| 514 | # Do not trigger presubmit builders, since they're likely to fail |
| 515 | # (e.g. OWNERS checks before finished code review), and we're running |
| 516 | # local presubmit anyway. |
| 517 | pass |
| 518 | else: |
| 519 | try_config[master][builder] = ['defaulttests'] |
kjellander@webrtc.org | 8575980 | 2013-10-22 16:47:40 +0000 | [diff] [blame] | 520 | |
kjellander | 986ee08 | 2015-06-16 04:32:13 -0700 | [diff] [blame] | 521 | return try_config |