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