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