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