blob: 3405ef9116fcd9e654af577ba49538e08ca945d3 [file] [log] [blame]
andrew@webrtc.org2442de12012-01-23 17:45:41 +00001# 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.comda159d62011-05-30 11:51:34 +00008
kjellander7439f972016-12-05 22:47:46 -08009import json
kjellander@webrtc.orgaefe61a2014-12-08 13:00:30 +000010import os
kjellander@webrtc.org85759802013-10-22 16:47:40 +000011import re
ehmaldonado4fb97462017-01-30 05:27:22 -080012import subprocess
kjellander@webrtc.org3bd41562014-09-01 11:06:37 +000013import sys
kjellander@webrtc.org85759802013-10-22 16:47:40 +000014
15
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +010016# Directories that will be scanned by cpplint by the presubmit script.
17CPPLINT_DIRS = [
oprypin803dc292017-02-01 01:55:59 -080018 'webrtc/api',
Fredrik Solenbergea073732015-12-01 11:26:34 +010019 'webrtc/audio',
20 'webrtc/call',
jbauch0f2e9392015-12-10 03:11:42 -080021 'webrtc/common_video',
jbauch70625e52015-12-09 14:18:14 -080022 'webrtc/examples',
aleloidf9e4d92016-08-08 10:26:09 -070023 'webrtc/modules/audio_mixer',
jbauchf91e6d02016-01-24 23:05:21 -080024 'webrtc/modules/bitrate_controller',
Stefan Holmer80e12072016-02-23 13:30:42 +010025 'webrtc/modules/congestion_controller',
jbauchd2a22962016-02-08 23:18:25 -080026 'webrtc/modules/pacing',
terelius8f09f172015-12-15 00:51:54 -080027 'webrtc/modules/remote_bitrate_estimator',
danilchap377b5e62015-12-15 04:33:44 -080028 'webrtc/modules/rtp_rtcp',
philipel5908c712015-12-21 08:23:20 -080029 'webrtc/modules/video_coding',
mflodman88eeac42015-12-08 09:21:28 +010030 'webrtc/modules/video_processing',
jbauch0f2e9392015-12-10 03:11:42 -080031 'webrtc/tools',
mflodmand1590b22015-12-09 07:07:59 -080032 'webrtc/video',
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +010033]
34
jbauchc4e3ead2016-02-19 00:25:55 -080035# These filters will always be removed, even if the caller specifies a filter
36# set, as they are problematic or broken in some way.
37#
38# Justifications for each filter:
39# - build/c++11 : Rvalue ref checks are unreliable (false positives),
40# include file and feature blacklists are
41# google3-specific.
kjellandere5a87a52016-04-27 02:32:12 -070042# - whitespace/operators: Same as above (doesn't seem sufficient to eliminate
43# all move-related errors).
jbauchc4e3ead2016-02-19 00:25:55 -080044BLACKLIST_LINT_FILTERS = [
45 '-build/c++11',
kjellandere5a87a52016-04-27 02:32:12 -070046 '-whitespace/operators',
jbauchc4e3ead2016-02-19 00:25:55 -080047]
48
kjellanderfd595232015-12-04 02:44:09 -080049# List of directories of "supported" native APIs. That means changes to headers
50# will be done in a compatible way following this scheme:
51# 1. Non-breaking changes are made.
52# 2. The old APIs as marked as deprecated (with comments).
53# 3. Deprecation is announced to discuss-webrtc@googlegroups.com and
54# webrtc-users@google.com (internal list).
55# 4. (later) The deprecated APIs are removed.
kjellander53047c92015-12-02 23:56:14 -080056NATIVE_API_DIRS = (
kjellander53047c92015-12-02 23:56:14 -080057 'webrtc',
kjellanderdd705472016-06-09 11:17:27 -070058 'webrtc/api',
59 'webrtc/media',
kjellander53047c92015-12-02 23:56:14 -080060 'webrtc/modules/audio_device/include',
kjellanderdd705472016-06-09 11:17:27 -070061 'webrtc/pc',
62)
63# These directories should not be used but are maintained only to avoid breaking
64# some legacy downstream code.
65LEGACY_API_DIRS = (
kjellanderdd705472016-06-09 11:17:27 -070066 'webrtc/base',
67 'webrtc/common_audio/include',
68 'webrtc/modules/audio_coding/include',
69 'webrtc/modules/audio_conference_mixer/include',
kjellander53047c92015-12-02 23:56:14 -080070 'webrtc/modules/audio_processing/include',
71 'webrtc/modules/bitrate_controller/include',
Stefan Holmer80e12072016-02-23 13:30:42 +010072 'webrtc/modules/congestion_controller/include',
kjellander53047c92015-12-02 23:56:14 -080073 'webrtc/modules/include',
74 'webrtc/modules/remote_bitrate_estimator/include',
75 'webrtc/modules/rtp_rtcp/include',
kjellanderdd705472016-06-09 11:17:27 -070076 'webrtc/modules/rtp_rtcp/source',
kjellander53047c92015-12-02 23:56:14 -080077 'webrtc/modules/utility/include',
78 'webrtc/modules/video_coding/codecs/h264/include',
79 'webrtc/modules/video_coding/codecs/i420/include',
80 'webrtc/modules/video_coding/codecs/vp8/include',
81 'webrtc/modules/video_coding/codecs/vp9/include',
82 'webrtc/modules/video_coding/include',
kjellanderdd705472016-06-09 11:17:27 -070083 'webrtc/system_wrappers/include',
kjellander53047c92015-12-02 23:56:14 -080084 'webrtc/voice_engine/include',
85)
kjellanderdd705472016-06-09 11:17:27 -070086API_DIRS = NATIVE_API_DIRS[:] + LEGACY_API_DIRS[:]
kjellander53047c92015-12-02 23:56:14 -080087
88
ehmaldonado4fb97462017-01-30 05:27:22 -080089def _RunCommand(command, cwd):
90 """Runs a command and returns the output from that command."""
91 p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
92 cwd=cwd)
93 stdout = p.stdout.read()
94 stderr = p.stderr.read()
95 p.wait()
96 p.stdout.close()
97 p.stderr.close()
98 return p.returncode, stdout, stderr
99
100
kjellander53047c92015-12-02 23:56:14 -0800101def _VerifyNativeApiHeadersListIsValid(input_api, output_api):
102 """Ensures the list of native API header directories is up to date."""
103 non_existing_paths = []
104 native_api_full_paths = [
105 input_api.os_path.join(input_api.PresubmitLocalPath(),
kjellanderdd705472016-06-09 11:17:27 -0700106 *path.split('/')) for path in API_DIRS]
kjellander53047c92015-12-02 23:56:14 -0800107 for path in native_api_full_paths:
108 if not os.path.isdir(path):
109 non_existing_paths.append(path)
110 if non_existing_paths:
111 return [output_api.PresubmitError(
112 'Directories to native API headers have changed which has made the '
113 'list in PRESUBMIT.py outdated.\nPlease update it to the current '
114 'location of our native APIs.',
115 non_existing_paths)]
116 return []
117
kwibergeb133022016-04-07 07:41:48 -0700118api_change_msg = """
119You seem to be changing native API header files. Please make sure that you:
oprypin375b9ac2017-02-13 04:13:23 -0800120 1. Make compatible changes that don't break existing clients. Usually
121 this is done by keeping the existing method signatures unchanged.
122 2. Mark the old stuff as deprecated (see RTC_DEPRECATED macro).
kwibergeb133022016-04-07 07:41:48 -0700123 3. Create a timeline and plan for when the deprecated stuff will be
124 removed. (The amount of time we give users to change their code
125 should be informed by how much work it is for them. If they just
126 need to replace one name with another or something equally
127 simple, 1-2 weeks might be good; if they need to do serious work,
128 up to 3 months may be called for.)
129 4. Update/inform existing downstream code owners to stop using the
130 deprecated stuff. (Send announcements to
131 discuss-webrtc@googlegroups.com and webrtc-users@google.com.)
132 5. Remove the deprecated stuff, once the agreed-upon amount of time
133 has passed.
134Related files:
135"""
kjellander53047c92015-12-02 23:56:14 -0800136
137def _CheckNativeApiHeaderChanges(input_api, output_api):
138 """Checks to remind proper changing of native APIs."""
139 files = []
140 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
141 if f.LocalPath().endswith('.h'):
kjellanderdd705472016-06-09 11:17:27 -0700142 for path in API_DIRS:
kjellander53047c92015-12-02 23:56:14 -0800143 if os.path.dirname(f.LocalPath()) == path:
144 files.append(f)
145
146 if files:
kwibergeb133022016-04-07 07:41:48 -0700147 return [output_api.PresubmitNotifyResult(api_change_msg, files)]
kjellander53047c92015-12-02 23:56:14 -0800148 return []
149
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +0100150
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000151def _CheckNoIOStreamInHeaders(input_api, output_api):
152 """Checks to make sure no .h files include <iostream>."""
153 files = []
154 pattern = input_api.re.compile(r'^#include\s*<iostream>',
155 input_api.re.MULTILINE)
156 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
157 if not f.LocalPath().endswith('.h'):
158 continue
159 contents = input_api.ReadFile(f)
160 if pattern.search(contents):
161 files.append(f)
162
163 if len(files):
Henrik Kjellander57e5fd22015-05-25 12:55:39 +0200164 return [output_api.PresubmitError(
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000165 'Do not #include <iostream> in header files, since it inserts static ' +
166 'initialization into every file including the header. Instead, ' +
167 '#include <ostream>. See http://crbug.com/94794',
Henrik Kjellander57e5fd22015-05-25 12:55:39 +0200168 files)]
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000169 return []
170
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000171
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000172def _CheckNoFRIEND_TEST(input_api, output_api):
173 """Make sure that gtest's FRIEND_TEST() macro is not used, the
174 FRIEND_TEST_ALL_PREFIXES() macro from testsupport/gtest_prod_util.h should be
175 used instead since that allows for FLAKY_, FAILS_ and DISABLED_ prefixes."""
176 problems = []
177
178 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.h'))
179 for f in input_api.AffectedFiles(file_filter=file_filter):
180 for line_num, line in f.ChangedContents():
181 if 'FRIEND_TEST(' in line:
182 problems.append(' %s:%d' % (f.LocalPath(), line_num))
183
184 if not problems:
185 return []
186 return [output_api.PresubmitPromptWarning('WebRTC\'s code should not use '
187 'gtest\'s FRIEND_TEST() macro. Include testsupport/gtest_prod_util.h and '
188 'use FRIEND_TEST_ALL_PREFIXES() instead.\n' + '\n'.join(problems))]
189
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000190
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +0100191def _IsLintWhitelisted(whitelist_dirs, file_path):
192 """ Checks if a file is whitelisted for lint check."""
193 for path in whitelist_dirs:
194 if os.path.dirname(file_path).startswith(path):
195 return True
196 return False
197
198
mflodman@webrtc.org2a452092012-07-01 05:55:23 +0000199def _CheckApprovedFilesLintClean(input_api, output_api,
200 source_file_filter=None):
201 """Checks that all new or whitelisted .cc and .h files pass cpplint.py.
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000202 This check is based on _CheckChangeLintsClean in
203 depot_tools/presubmit_canned_checks.py but has less filters and only checks
204 added files."""
205 result = []
206
207 # Initialize cpplint.
208 import cpplint
209 # Access to a protected member _XX of a client class
210 # pylint: disable=W0212
211 cpplint._cpplint_state.ResetErrorCounts()
212
jbauchc4e3ead2016-02-19 00:25:55 -0800213 lint_filters = cpplint._Filters()
214 lint_filters.extend(BLACKLIST_LINT_FILTERS)
215 cpplint._SetFilters(','.join(lint_filters))
216
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +0100217 # Create a platform independent whitelist for the CPPLINT_DIRS.
218 whitelist_dirs = [input_api.os_path.join(*path.split('/'))
219 for path in CPPLINT_DIRS]
220
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000221 # Use the strictest verbosity level for cpplint.py (level 1) which is the
222 # default when running cpplint.py from command line.
223 # To make it possible to work with not-yet-converted code, we're only applying
mflodman@webrtc.org2a452092012-07-01 05:55:23 +0000224 # it to new (or moved/renamed) files and files listed in LINT_FOLDERS.
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000225 verbosity_level = 1
226 files = []
227 for f in input_api.AffectedSourceFiles(source_file_filter):
Henrik Kjellander57e5fd22015-05-25 12:55:39 +0200228 # Note that moved/renamed files also count as added.
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +0100229 if f.Action() == 'A' or _IsLintWhitelisted(whitelist_dirs, f.LocalPath()):
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000230 files.append(f.AbsoluteLocalPath())
mflodman@webrtc.org2a452092012-07-01 05:55:23 +0000231
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000232 for file_name in files:
233 cpplint.ProcessFile(file_name, verbosity_level)
234
235 if cpplint._cpplint_state.error_count > 0:
236 if input_api.is_committing:
237 # TODO(kjellander): Change back to PresubmitError below when we're
238 # confident with the lint settings.
239 res_type = output_api.PresubmitPromptWarning
240 else:
241 res_type = output_api.PresubmitPromptWarning
242 result = [res_type('Changelist failed cpplint.py check.')]
243
244 return result
245
Henrik Kjellanderb4af3d62016-11-16 20:11:29 +0100246def _CheckNoSourcesAbove(input_api, gn_files, output_api):
ehmaldonado5b1ba082016-09-02 05:51:08 -0700247 # Disallow referencing source files with paths above the GN file location.
248 source_pattern = input_api.re.compile(r' +sources \+?= \[(.*?)\]',
249 re.MULTILINE | re.DOTALL)
250 file_pattern = input_api.re.compile(r'"((\.\./.*?)|(//.*?))"')
251 violating_gn_files = set()
252 violating_source_entries = []
253 for gn_file in gn_files:
254 contents = input_api.ReadFile(gn_file)
255 for source_block_match in source_pattern.finditer(contents):
256 # Find all source list entries starting with ../ in the source block
257 # (exclude overrides entries).
258 for file_list_match in file_pattern.finditer(source_block_match.group(1)):
259 source_file = file_list_match.group(1)
260 if 'overrides/' not in source_file:
261 violating_source_entries.append(source_file)
262 violating_gn_files.add(gn_file)
263 if violating_gn_files:
264 return [output_api.PresubmitError(
265 'Referencing source files above the directory of the GN file is not '
Henrik Kjellanderb4af3d62016-11-16 20:11:29 +0100266 'allowed. Please introduce new GN targets in the proper location '
267 'instead.\n'
ehmaldonado5b1ba082016-09-02 05:51:08 -0700268 'Invalid source entries:\n'
269 '%s\n'
270 'Violating GN files:' % '\n'.join(violating_source_entries),
271 items=violating_gn_files)]
272 return []
273
kjellander7439f972016-12-05 22:47:46 -0800274def _CheckNoMixingCAndCCSources(input_api, gn_files, output_api):
275 # Disallow mixing .c and .cc source files in the same target.
276 source_pattern = input_api.re.compile(r' +sources \+?= \[(.*?)\]',
277 re.MULTILINE | re.DOTALL)
278 file_pattern = input_api.re.compile(r'"(.*)"')
279 violating_gn_files = dict()
280 for gn_file in gn_files:
281 contents = input_api.ReadFile(gn_file)
282 for source_block_match in source_pattern.finditer(contents):
283 c_files = []
284 cc_files = []
285 for file_list_match in file_pattern.finditer(source_block_match.group(1)):
286 source_file = file_list_match.group(1)
287 if source_file.endswith('.c'):
288 c_files.append(source_file)
289 if source_file.endswith('.cc'):
290 cc_files.append(source_file)
291 if c_files and cc_files:
292 violating_gn_files[gn_file.LocalPath()] = sorted(c_files + cc_files)
293 if violating_gn_files:
294 return [output_api.PresubmitError(
295 'GN targets cannot mix .cc and .c source files. Please create a '
296 'separate target for each collection of sources.\n'
297 'Mixed sources: \n'
298 '%s\n'
299 'Violating GN files:' % json.dumps(violating_gn_files, indent=2),
300 items=violating_gn_files.keys())]
301 return []
302
ehmaldonado4fb97462017-01-30 05:27:22 -0800303def _CheckNoPackageBoundaryViolations(input_api, gn_files, output_api):
304 cwd = input_api.PresubmitLocalPath()
305 script_path = os.path.join('tools-webrtc', 'check_package_boundaries.py')
306 webrtc_path = os.path.join('webrtc')
307 command = [sys.executable, script_path, webrtc_path]
308 command += [gn_file.LocalPath() for gn_file in gn_files]
309 returncode, _, stderr = _RunCommand(command, cwd)
310 if returncode:
311 return [output_api.PresubmitError(
312 'There are package boundary violations in the following GN files:\n\n'
313 '%s' % stderr)]
314 return []
315
ehmaldonado5b1ba082016-09-02 05:51:08 -0700316def _CheckGnChanges(input_api, output_api):
317 source_file_filter = lambda x: input_api.FilterSourceFile(
318 x, white_list=(r'.+\.(gn|gni)$',))
319
320 gn_files = []
321 for f in input_api.AffectedSourceFiles(source_file_filter):
322 if f.LocalPath().startswith('webrtc'):
323 gn_files.append(f)
324
325 result = []
326 if gn_files:
Henrik Kjellanderb4af3d62016-11-16 20:11:29 +0100327 result.extend(_CheckNoSourcesAbove(input_api, gn_files, output_api))
kjellander7439f972016-12-05 22:47:46 -0800328 result.extend(_CheckNoMixingCAndCCSources(input_api, gn_files, output_api))
ehmaldonado4fb97462017-01-30 05:27:22 -0800329 result.extend(_CheckNoPackageBoundaryViolations(
330 input_api, gn_files, output_api))
ehmaldonado5b1ba082016-09-02 05:51:08 -0700331 return result
332
kjellander@webrtc.org3bd41562014-09-01 11:06:37 +0000333def _CheckUnwantedDependencies(input_api, output_api):
334 """Runs checkdeps on #include statements added in this
335 change. Breaking - rules is an error, breaking ! rules is a
336 warning.
337 """
338 # Copied from Chromium's src/PRESUBMIT.py.
339
340 # We need to wait until we have an input_api object and use this
341 # roundabout construct to import checkdeps because this file is
342 # eval-ed and thus doesn't have __file__.
343 original_sys_path = sys.path
344 try:
kjellander@webrtc.orgaefe61a2014-12-08 13:00:30 +0000345 checkdeps_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
346 'buildtools', 'checkdeps')
347 if not os.path.exists(checkdeps_path):
348 return [output_api.PresubmitError(
349 'Cannot find checkdeps at %s\nHave you run "gclient sync" to '
350 'download Chromium and setup the symlinks?' % checkdeps_path)]
351 sys.path.append(checkdeps_path)
kjellander@webrtc.org3bd41562014-09-01 11:06:37 +0000352 import checkdeps
353 from cpp_checker import CppChecker
354 from rules import Rule
355 finally:
356 # Restore sys.path to what it was before.
357 sys.path = original_sys_path
358
359 added_includes = []
360 for f in input_api.AffectedFiles():
361 if not CppChecker.IsCppFile(f.LocalPath()):
362 continue
363
Henrik Kjellander57e5fd22015-05-25 12:55:39 +0200364 changed_lines = [line for _, line in f.ChangedContents()]
kjellander@webrtc.org3bd41562014-09-01 11:06:37 +0000365 added_includes.append([f.LocalPath(), changed_lines])
366
367 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
368
369 error_descriptions = []
370 warning_descriptions = []
371 for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
372 added_includes):
373 description_with_path = '%s\n %s' % (path, rule_description)
374 if rule_type == Rule.DISALLOW:
375 error_descriptions.append(description_with_path)
376 else:
377 warning_descriptions.append(description_with_path)
378
379 results = []
380 if error_descriptions:
381 results.append(output_api.PresubmitError(
382 'You added one or more #includes that violate checkdeps rules.',
383 error_descriptions))
384 if warning_descriptions:
385 results.append(output_api.PresubmitPromptOrNotify(
386 'You added one or more #includes of files that are temporarily\n'
387 'allowed but being removed. Can you avoid introducing the\n'
388 '#include? See relevant DEPS file(s) for details and contacts.',
389 warning_descriptions))
390 return results
391
kjellanderd1e26a92016-09-19 08:11:16 -0700392def _CheckChangeHasBugField(input_api, output_api):
393 """Requires that the changelist have a BUG= field.
394
395 This check is stricter than the one in depot_tools/presubmit_canned_checks.py
396 since it fails the presubmit if the BUG= field is missing or doesn't contain
397 a bug reference.
398 """
399 if input_api.change.BUG:
400 return []
401 else:
402 return [output_api.PresubmitError(
403 'The BUG=[bug number] field is mandatory. Please create a bug and '
404 'reference it using either of:\n'
405 ' * https://bugs.webrtc.org - reference it using BUG=webrtc:XXXX\n'
406 ' * https://crbug.com - reference it using BUG=chromium:XXXXXX')]
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000407
kjellander569cf942016-02-11 05:02:59 -0800408def _CheckJSONParseErrors(input_api, output_api):
409 """Check that JSON files do not contain syntax errors."""
410
411 def FilterFile(affected_file):
412 return input_api.os_path.splitext(affected_file.LocalPath())[1] == '.json'
413
414 def GetJSONParseError(input_api, filename):
415 try:
416 contents = input_api.ReadFile(filename)
417 input_api.json.loads(contents)
418 except ValueError as e:
419 return e
420 return None
421
422 results = []
423 for affected_file in input_api.AffectedFiles(
424 file_filter=FilterFile, include_deletes=False):
425 parse_error = GetJSONParseError(input_api,
426 affected_file.AbsoluteLocalPath())
427 if parse_error:
428 results.append(output_api.PresubmitError('%s could not be parsed: %s' %
429 (affected_file.LocalPath(), parse_error)))
430 return results
431
432
Henrik Kjellander8d3ad822015-05-26 19:52:05 +0200433def _RunPythonTests(input_api, output_api):
434 def join(*args):
435 return input_api.os_path.join(input_api.PresubmitLocalPath(), *args)
436
437 test_directories = [
ehmaldonado4fb97462017-01-30 05:27:22 -0800438 join('webrtc', 'tools', 'py_event_log_analyzer')
439 ] + [
440 root for root, _, files in os.walk(join('tools-webrtc'))
441 if any(f.endswith('_test.py') for f in files)
Henrik Kjellander8d3ad822015-05-26 19:52:05 +0200442 ]
443
444 tests = []
445 for directory in test_directories:
446 tests.extend(
447 input_api.canned_checks.GetUnitTestsInDirectory(
448 input_api,
449 output_api,
450 directory,
451 whitelist=[r'.+_test\.py$']))
452 return input_api.RunTests(tests, parallel=True)
453
454
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000455def _CommonChecks(input_api, output_api):
456 """Checks common to both upload and commit."""
niklase@google.comda159d62011-05-30 11:51:34 +0000457 results = []
tkchin42f580e2015-11-26 23:18:23 -0800458 # Filter out files that are in objc or ios dirs from being cpplint-ed since
459 # they do not follow C++ lint rules.
460 black_list = input_api.DEFAULT_BLACK_LIST + (
461 r".*\bobjc[\\\/].*",
Kári Tristan Helgason3fa35172016-09-09 08:55:05 +0000462 r".*objc\.[hcm]+$",
hjon65ae2d82016-08-02 23:55:44 -0700463 r"webrtc\/build\/ios\/SDK\/.*",
tkchin42f580e2015-11-26 23:18:23 -0800464 )
465 source_file_filter = lambda x: input_api.FilterSourceFile(x, None, black_list)
466 results.extend(_CheckApprovedFilesLintClean(
467 input_api, output_api, source_file_filter))
phoglund@webrtc.org5d3713932013-03-07 09:59:43 +0000468 results.extend(input_api.canned_checks.RunPylint(input_api, output_api,
kjellander@webrtc.org177567c2016-12-22 10:40:28 +0100469 black_list=(r'^base[\\\/].*\.py$',
Henrik Kjellander14771ac2015-06-02 13:10:04 +0200470 r'^build[\\\/].*\.py$',
471 r'^buildtools[\\\/].*\.py$',
Henrik Kjellander0779e8f2016-12-22 12:01:17 +0100472 r'^ios[\\\/].*\.py$',
Henrik Kjellander14771ac2015-06-02 13:10:04 +0200473 r'^out.*[\\\/].*\.py$',
474 r'^testing[\\\/].*\.py$',
475 r'^third_party[\\\/].*\.py$',
kjellander@webrtc.org177567c2016-12-22 10:40:28 +0100476 r'^tools[\\\/].*\.py$',
kjellanderafd54942016-12-17 12:21:39 -0800477 # TODO(phoglund): should arguably be checked.
Henrik Kjellanderb2d55772016-12-18 22:14:50 +0100478 r'^tools-webrtc[\\\/]mb[\\\/].*\.py$',
kjellanderafd54942016-12-17 12:21:39 -0800479 r'^tools-webrtc[\\\/]valgrind[\\\/].*\.py$',
Henrik Kjellander14771ac2015-06-02 13:10:04 +0200480 r'^xcodebuild.*[\\\/].*\.py$',),
phoglund@webrtc.org5d3713932013-03-07 09:59:43 +0000481 disabled_warnings=['F0401', # Failed to import x
482 'E0611', # No package y in x
483 'W0232', # Class has no __init__ method
Henrik Kjellander57e5fd22015-05-25 12:55:39 +0200484 ],
485 pylintrc='pylintrc'))
kjellander569cf942016-02-11 05:02:59 -0800486
nisse3d21e232016-09-02 03:07:06 -0700487 # TODO(nisse): talk/ is no more, so make below checks simpler?
Henrik Kjellander57e5fd22015-05-25 12:55:39 +0200488 # WebRTC can't use the presubmit_canned_checks.PanProjectChecks function since
489 # we need to have different license checks in talk/ and webrtc/ directories.
490 # Instead, hand-picked checks are included below.
Henrik Kjellander63224672015-09-08 08:03:56 +0200491
tkchin3cd9a302016-06-08 12:40:28 -0700492 # .m and .mm files are ObjC files. For simplicity we will consider .h files in
493 # ObjC subdirectories ObjC headers.
494 objc_filter_list = (r'.+\.m$', r'.+\.mm$', r'.+objc\/.+\.h$')
Henrik Kjellanderb4af3d62016-11-16 20:11:29 +0100495 # Skip long-lines check for DEPS and GN files.
496 build_file_filter_list = (r'.+\.gn$', r'.+\.gni$', 'DEPS')
tkchin3cd9a302016-06-08 12:40:28 -0700497 eighty_char_sources = lambda x: input_api.FilterSourceFile(x,
498 black_list=build_file_filter_list + objc_filter_list)
499 hundred_char_sources = lambda x: input_api.FilterSourceFile(x,
500 white_list=objc_filter_list)
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000501 results.extend(input_api.canned_checks.CheckLongLines(
tkchin3cd9a302016-06-08 12:40:28 -0700502 input_api, output_api, maxlen=80, source_file_filter=eighty_char_sources))
503 results.extend(input_api.canned_checks.CheckLongLines(
504 input_api, output_api, maxlen=100,
505 source_file_filter=hundred_char_sources))
506
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000507 results.extend(input_api.canned_checks.CheckChangeHasNoTabs(
508 input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000509 results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace(
510 input_api, output_api))
kjellandere5dc62a2016-12-14 00:16:21 -0800511 results.extend(input_api.canned_checks.CheckAuthorizedAuthor(
512 input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000513 results.extend(input_api.canned_checks.CheckChangeTodoHasOwner(
514 input_api, output_api))
kjellander53047c92015-12-02 23:56:14 -0800515 results.extend(_CheckNativeApiHeaderChanges(input_api, output_api))
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000516 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
517 results.extend(_CheckNoFRIEND_TEST(input_api, output_api))
ehmaldonado5b1ba082016-09-02 05:51:08 -0700518 results.extend(_CheckGnChanges(input_api, output_api))
kjellander@webrtc.org3bd41562014-09-01 11:06:37 +0000519 results.extend(_CheckUnwantedDependencies(input_api, output_api))
kjellander569cf942016-02-11 05:02:59 -0800520 results.extend(_CheckJSONParseErrors(input_api, output_api))
Henrik Kjellander8d3ad822015-05-26 19:52:05 +0200521 results.extend(_RunPythonTests(input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000522 return results
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000523
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000524
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000525def CheckChangeOnUpload(input_api, output_api):
526 results = []
527 results.extend(_CommonChecks(input_api, output_api))
Henrik Kjellander57e5fd22015-05-25 12:55:39 +0200528 results.extend(
529 input_api.canned_checks.CheckGNFormatted(input_api, output_api))
niklase@google.comda159d62011-05-30 11:51:34 +0000530 return results
531
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000532
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000533def CheckChangeOnCommit(input_api, output_api):
niklase@google.com1198db92011-06-09 07:07:24 +0000534 results = []
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000535 results.extend(_CommonChecks(input_api, output_api))
kjellander53047c92015-12-02 23:56:14 -0800536 results.extend(_VerifyNativeApiHeadersListIsValid(input_api, output_api))
niklase@google.com1198db92011-06-09 07:07:24 +0000537 results.extend(input_api.canned_checks.CheckOwners(input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000538 results.extend(input_api.canned_checks.CheckChangeWasUploaded(
539 input_api, output_api))
540 results.extend(input_api.canned_checks.CheckChangeHasDescription(
541 input_api, output_api))
kjellanderd1e26a92016-09-19 08:11:16 -0700542 results.extend(_CheckChangeHasBugField(input_api, output_api))
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000543 results.extend(input_api.canned_checks.CheckChangeHasTestField(
544 input_api, output_api))
kjellander@webrtc.org12cb88c2014-02-13 11:53:43 +0000545 results.extend(input_api.canned_checks.CheckTreeIsOpen(
546 input_api, output_api,
547 json_url='http://webrtc-status.appspot.com/current?format=json'))
niklase@google.com1198db92011-06-09 07:07:24 +0000548 return results