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