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