blob: 08e6024da0b2603d260832cb54e293ef13243331 [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
kjellander@webrtc.org3bd41562014-09-01 11:06:37 +000012import sys
Mirko Bonadei4dc4e252017-09-19 13:49:16 +020013from collections import defaultdict
Oleh Prypin2f33a562017-10-04 20:17:54 +020014from contextlib import contextmanager
kjellander@webrtc.org85759802013-10-22 16:47:40 +000015
oprypin2aa463f2017-03-23 03:17:02 -070016# Files and directories that are *skipped* by cpplint in the presubmit script.
17CPPLINT_BLACKLIST = [
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018 'api/video_codecs/video_decoder.h',
19 'common_types.cc',
20 'common_types.h',
21 'examples/objc',
Amit Hilbuchce7032b2019-01-22 16:19:35 -080022 'media/base/stream_params.h',
23 'media/base/video_common.h',
24 'media/sctp/sctp_transport.cc',
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025 'modules/audio_coding',
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026 'modules/audio_device',
27 'modules/audio_processing',
28 'modules/desktop_capture',
29 'modules/include/module_common_types.h',
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030 'modules/utility',
31 'modules/video_capture',
Amit Hilbuchce7032b2019-01-22 16:19:35 -080032 'p2p/base/pseudo_tcp.cc',
33 'p2p/base/pseudo_tcp.h',
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034 'rtc_base',
35 'sdk/android/src/jni',
36 'sdk/objc',
37 'system_wrappers',
38 'test',
Henrik Kjellander90fd7d82017-05-09 08:30:10 +020039 'tools_webrtc',
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020040 'voice_engine',
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +010041]
42
jbauchc4e3ead2016-02-19 00:25:55 -080043# These filters will always be removed, even if the caller specifies a filter
44# set, as they are problematic or broken in some way.
45#
46# Justifications for each filter:
47# - build/c++11 : Rvalue ref checks are unreliable (false positives),
48# include file and feature blacklists are
49# google3-specific.
kjellandere5a87a52016-04-27 02:32:12 -070050# - whitespace/operators: Same as above (doesn't seem sufficient to eliminate
51# all move-related errors).
jbauchc4e3ead2016-02-19 00:25:55 -080052BLACKLIST_LINT_FILTERS = [
53 '-build/c++11',
kjellandere5a87a52016-04-27 02:32:12 -070054 '-whitespace/operators',
jbauchc4e3ead2016-02-19 00:25:55 -080055]
56
kjellanderfd595232015-12-04 02:44:09 -080057# List of directories of "supported" native APIs. That means changes to headers
58# will be done in a compatible way following this scheme:
59# 1. Non-breaking changes are made.
60# 2. The old APIs as marked as deprecated (with comments).
61# 3. Deprecation is announced to discuss-webrtc@googlegroups.com and
62# webrtc-users@google.com (internal list).
63# 4. (later) The deprecated APIs are removed.
kjellander53047c92015-12-02 23:56:14 -080064NATIVE_API_DIRS = (
Karl Wibergef52d8b82017-10-25 13:20:03 +020065 'api', # All subdirectories of api/ are included as well.
Mirko Bonadeia4eeeff2018-01-11 13:16:52 +010066 'media/base',
67 'media/engine',
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020068 'modules/audio_device/include',
69 'pc',
kjellanderdd705472016-06-09 11:17:27 -070070)
Mirko Bonadei4dc4e252017-09-19 13:49:16 +020071
kjellanderdd705472016-06-09 11:17:27 -070072# These directories should not be used but are maintained only to avoid breaking
73# some legacy downstream code.
74LEGACY_API_DIRS = (
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020075 'common_audio/include',
76 'modules/audio_coding/include',
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020077 'modules/audio_processing/include',
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020078 'modules/congestion_controller/include',
79 'modules/include',
80 'modules/remote_bitrate_estimator/include',
81 'modules/rtp_rtcp/include',
82 'modules/rtp_rtcp/source',
83 'modules/utility/include',
84 'modules/video_coding/codecs/h264/include',
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020085 'modules/video_coding/codecs/vp8/include',
86 'modules/video_coding/codecs/vp9/include',
87 'modules/video_coding/include',
88 'rtc_base',
89 'system_wrappers/include',
kjellander53047c92015-12-02 23:56:14 -080090)
Mirko Bonadei4dc4e252017-09-19 13:49:16 +020091
Karl Wibergd4f01c12017-11-10 10:55:45 +010092# NOTE: The set of directories in API_DIRS should be the same as those
93# listed in the table in native-api.md.
kjellanderdd705472016-06-09 11:17:27 -070094API_DIRS = NATIVE_API_DIRS[:] + LEGACY_API_DIRS[:]
kjellander53047c92015-12-02 23:56:14 -080095
Mirko Bonadei4dc4e252017-09-19 13:49:16 +020096# TARGET_RE matches a GN target, and extracts the target name and the contents.
97TARGET_RE = re.compile(r'(?P<indent>\s*)\w+\("(?P<target_name>\w+)"\) {'
98 r'(?P<target_contents>.*?)'
99 r'(?P=indent)}',
100 re.MULTILINE | re.DOTALL)
101
102# SOURCES_RE matches a block of sources inside a GN target.
103SOURCES_RE = re.compile(r'sources \+?= \[(?P<sources>.*?)\]',
104 re.MULTILINE | re.DOTALL)
105
106# FILE_PATH_RE matchies a file path.
107FILE_PATH_RE = re.compile(r'"(?P<file_path>(\w|\/)+)(?P<extension>\.\w+)"')
108
kjellander53047c92015-12-02 23:56:14 -0800109
Mirko Bonadeid8665442018-09-04 12:17:27 +0200110def FindSrcDirPath(starting_dir):
111 """Returns the abs path to the src/ dir of the project."""
112 src_dir = starting_dir
113 while os.path.basename(src_dir) != 'src':
114 src_dir = os.path.normpath(os.path.join(src_dir, os.pardir))
115 return src_dir
116
117
Oleh Prypin2f33a562017-10-04 20:17:54 +0200118@contextmanager
119def _AddToPath(*paths):
120 original_sys_path = sys.path
121 sys.path.extend(paths)
122 try:
123 yield
124 finally:
125 # Restore sys.path to what it was before.
126 sys.path = original_sys_path
ehmaldonado4fb97462017-01-30 05:27:22 -0800127
128
charujain9893e252017-09-14 13:33:22 +0200129def VerifyNativeApiHeadersListIsValid(input_api, output_api):
kjellander53047c92015-12-02 23:56:14 -0800130 """Ensures the list of native API header directories is up to date."""
131 non_existing_paths = []
132 native_api_full_paths = [
133 input_api.os_path.join(input_api.PresubmitLocalPath(),
kjellanderdd705472016-06-09 11:17:27 -0700134 *path.split('/')) for path in API_DIRS]
kjellander53047c92015-12-02 23:56:14 -0800135 for path in native_api_full_paths:
136 if not os.path.isdir(path):
137 non_existing_paths.append(path)
138 if non_existing_paths:
139 return [output_api.PresubmitError(
140 'Directories to native API headers have changed which has made the '
141 'list in PRESUBMIT.py outdated.\nPlease update it to the current '
142 'location of our native APIs.',
143 non_existing_paths)]
144 return []
145
Artem Titove92675b2018-05-22 10:21:27 +0200146
kjellanderc88b5d52017-04-05 06:42:43 -0700147API_CHANGE_MSG = """
kwibergeb133022016-04-07 07:41:48 -0700148You seem to be changing native API header files. Please make sure that you:
oprypin375b9ac2017-02-13 04:13:23 -0800149 1. Make compatible changes that don't break existing clients. Usually
150 this is done by keeping the existing method signatures unchanged.
151 2. Mark the old stuff as deprecated (see RTC_DEPRECATED macro).
kwibergeb133022016-04-07 07:41:48 -0700152 3. Create a timeline and plan for when the deprecated stuff will be
153 removed. (The amount of time we give users to change their code
154 should be informed by how much work it is for them. If they just
155 need to replace one name with another or something equally
156 simple, 1-2 weeks might be good; if they need to do serious work,
157 up to 3 months may be called for.)
158 4. Update/inform existing downstream code owners to stop using the
159 deprecated stuff. (Send announcements to
160 discuss-webrtc@googlegroups.com and webrtc-users@google.com.)
161 5. Remove the deprecated stuff, once the agreed-upon amount of time
162 has passed.
163Related files:
164"""
kjellander53047c92015-12-02 23:56:14 -0800165
Artem Titove92675b2018-05-22 10:21:27 +0200166
charujain9893e252017-09-14 13:33:22 +0200167def CheckNativeApiHeaderChanges(input_api, output_api):
kjellander53047c92015-12-02 23:56:14 -0800168 """Checks to remind proper changing of native APIs."""
169 files = []
Karl Wiberg6bfac032017-10-27 15:14:20 +0200170 source_file_filter = lambda x: input_api.FilterSourceFile(
171 x, white_list=[r'.+\.(gn|gni|h)$'])
172 for f in input_api.AffectedSourceFiles(source_file_filter):
173 for path in API_DIRS:
174 dn = os.path.dirname(f.LocalPath())
175 if path == 'api':
176 # Special case: Subdirectories included.
177 if dn == 'api' or dn.startswith('api/'):
Niels Möller1d201852019-06-26 12:58:27 +0200178 files.append(f.LocalPath())
Karl Wiberg6bfac032017-10-27 15:14:20 +0200179 else:
180 # Normal case: Subdirectories not included.
181 if dn == path:
Niels Möller1d201852019-06-26 12:58:27 +0200182 files.append(f.LocalPath())
kjellander53047c92015-12-02 23:56:14 -0800183
184 if files:
kjellanderc88b5d52017-04-05 06:42:43 -0700185 return [output_api.PresubmitNotifyResult(API_CHANGE_MSG, files)]
kjellander53047c92015-12-02 23:56:14 -0800186 return []
187
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +0100188
Artem Titova04d1402018-05-11 11:23:00 +0200189def CheckNoIOStreamInHeaders(input_api, output_api,
190 source_file_filter):
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000191 """Checks to make sure no .h files include <iostream>."""
192 files = []
193 pattern = input_api.re.compile(r'^#include\s*<iostream>',
194 input_api.re.MULTILINE)
Artem Titova04d1402018-05-11 11:23:00 +0200195 file_filter = lambda x: (input_api.FilterSourceFile(x)
196 and source_file_filter(x))
197 for f in input_api.AffectedSourceFiles(file_filter):
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000198 if not f.LocalPath().endswith('.h'):
199 continue
200 contents = input_api.ReadFile(f)
201 if pattern.search(contents):
202 files.append(f)
203
204 if len(files):
Henrik Kjellander57e5fd22015-05-25 12:55:39 +0200205 return [output_api.PresubmitError(
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000206 'Do not #include <iostream> in header files, since it inserts static ' +
207 'initialization into every file including the header. Instead, ' +
208 '#include <ostream>. See http://crbug.com/94794',
Henrik Kjellander57e5fd22015-05-25 12:55:39 +0200209 files)]
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000210 return []
211
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000212
Artem Titova04d1402018-05-11 11:23:00 +0200213def CheckNoPragmaOnce(input_api, output_api,
214 source_file_filter):
kjellander6aeef742017-02-20 01:13:18 -0800215 """Make sure that banned functions are not used."""
216 files = []
217 pattern = input_api.re.compile(r'^#pragma\s+once',
218 input_api.re.MULTILINE)
Artem Titova04d1402018-05-11 11:23:00 +0200219 file_filter = lambda x: (input_api.FilterSourceFile(x)
220 and source_file_filter(x))
221 for f in input_api.AffectedSourceFiles(file_filter):
kjellander6aeef742017-02-20 01:13:18 -0800222 if not f.LocalPath().endswith('.h'):
223 continue
224 contents = input_api.ReadFile(f)
225 if pattern.search(contents):
226 files.append(f)
227
228 if files:
229 return [output_api.PresubmitError(
230 'Do not use #pragma once in header files.\n'
231 'See http://www.chromium.org/developers/coding-style#TOC-File-headers',
232 files)]
233 return []
234
235
Artem Titova04d1402018-05-11 11:23:00 +0200236def CheckNoFRIEND_TEST(input_api, output_api, # pylint: disable=invalid-name
237 source_file_filter):
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000238 """Make sure that gtest's FRIEND_TEST() macro is not used, the
239 FRIEND_TEST_ALL_PREFIXES() macro from testsupport/gtest_prod_util.h should be
240 used instead since that allows for FLAKY_, FAILS_ and DISABLED_ prefixes."""
241 problems = []
242
Artem Titova04d1402018-05-11 11:23:00 +0200243 file_filter = lambda f: (f.LocalPath().endswith(('.cc', '.h'))
244 and source_file_filter(f))
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000245 for f in input_api.AffectedFiles(file_filter=file_filter):
246 for line_num, line in f.ChangedContents():
247 if 'FRIEND_TEST(' in line:
248 problems.append(' %s:%d' % (f.LocalPath(), line_num))
249
250 if not problems:
251 return []
252 return [output_api.PresubmitPromptWarning('WebRTC\'s code should not use '
253 'gtest\'s FRIEND_TEST() macro. Include testsupport/gtest_prod_util.h and '
254 'use FRIEND_TEST_ALL_PREFIXES() instead.\n' + '\n'.join(problems))]
255
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000256
charujain9893e252017-09-14 13:33:22 +0200257def IsLintBlacklisted(blacklist_paths, file_path):
oprypin2aa463f2017-03-23 03:17:02 -0700258 """ Checks if a file is blacklisted for lint check."""
259 for path in blacklist_paths:
260 if file_path == path or os.path.dirname(file_path).startswith(path):
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +0100261 return True
262 return False
263
264
charujain9893e252017-09-14 13:33:22 +0200265def CheckApprovedFilesLintClean(input_api, output_api,
Artem Titova04d1402018-05-11 11:23:00 +0200266 source_file_filter=None):
oprypin2aa463f2017-03-23 03:17:02 -0700267 """Checks that all new or non-blacklisted .cc and .h files pass cpplint.py.
charujain9893e252017-09-14 13:33:22 +0200268 This check is based on CheckChangeLintsClean in
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000269 depot_tools/presubmit_canned_checks.py but has less filters and only checks
270 added files."""
271 result = []
272
273 # Initialize cpplint.
274 import cpplint
275 # Access to a protected member _XX of a client class
276 # pylint: disable=W0212
277 cpplint._cpplint_state.ResetErrorCounts()
278
jbauchc4e3ead2016-02-19 00:25:55 -0800279 lint_filters = cpplint._Filters()
280 lint_filters.extend(BLACKLIST_LINT_FILTERS)
281 cpplint._SetFilters(','.join(lint_filters))
282
oprypin2aa463f2017-03-23 03:17:02 -0700283 # Create a platform independent blacklist for cpplint.
284 blacklist_paths = [input_api.os_path.join(*path.split('/'))
285 for path in CPPLINT_BLACKLIST]
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +0100286
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000287 # Use the strictest verbosity level for cpplint.py (level 1) which is the
oprypin2aa463f2017-03-23 03:17:02 -0700288 # default when running cpplint.py from command line. To make it possible to
289 # work with not-yet-converted code, we're only applying it to new (or
290 # moved/renamed) files and files not listed in CPPLINT_BLACKLIST.
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000291 verbosity_level = 1
292 files = []
293 for f in input_api.AffectedSourceFiles(source_file_filter):
Henrik Kjellander57e5fd22015-05-25 12:55:39 +0200294 # Note that moved/renamed files also count as added.
charujain9893e252017-09-14 13:33:22 +0200295 if f.Action() == 'A' or not IsLintBlacklisted(blacklist_paths,
Artem Titove92675b2018-05-22 10:21:27 +0200296 f.LocalPath()):
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000297 files.append(f.AbsoluteLocalPath())
mflodman@webrtc.org2a452092012-07-01 05:55:23 +0000298
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000299 for file_name in files:
300 cpplint.ProcessFile(file_name, verbosity_level)
301
302 if cpplint._cpplint_state.error_count > 0:
303 if input_api.is_committing:
oprypin8e58d652017-03-21 07:52:41 -0700304 res_type = output_api.PresubmitError
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000305 else:
306 res_type = output_api.PresubmitPromptWarning
307 result = [res_type('Changelist failed cpplint.py check.')]
308
309 return result
310
Artem Titove92675b2018-05-22 10:21:27 +0200311
charujain9893e252017-09-14 13:33:22 +0200312def CheckNoSourcesAbove(input_api, gn_files, output_api):
ehmaldonado5b1ba082016-09-02 05:51:08 -0700313 # Disallow referencing source files with paths above the GN file location.
314 source_pattern = input_api.re.compile(r' +sources \+?= \[(.*?)\]',
315 re.MULTILINE | re.DOTALL)
316 file_pattern = input_api.re.compile(r'"((\.\./.*?)|(//.*?))"')
317 violating_gn_files = set()
318 violating_source_entries = []
319 for gn_file in gn_files:
320 contents = input_api.ReadFile(gn_file)
321 for source_block_match in source_pattern.finditer(contents):
322 # Find all source list entries starting with ../ in the source block
323 # (exclude overrides entries).
324 for file_list_match in file_pattern.finditer(source_block_match.group(1)):
325 source_file = file_list_match.group(1)
326 if 'overrides/' not in source_file:
327 violating_source_entries.append(source_file)
328 violating_gn_files.add(gn_file)
329 if violating_gn_files:
330 return [output_api.PresubmitError(
331 'Referencing source files above the directory of the GN file is not '
Henrik Kjellanderb4af3d62016-11-16 20:11:29 +0100332 'allowed. Please introduce new GN targets in the proper location '
333 'instead.\n'
ehmaldonado5b1ba082016-09-02 05:51:08 -0700334 'Invalid source entries:\n'
335 '%s\n'
336 'Violating GN files:' % '\n'.join(violating_source_entries),
337 items=violating_gn_files)]
338 return []
339
Artem Titove92675b2018-05-22 10:21:27 +0200340
Mirko Bonadei4dc4e252017-09-19 13:49:16 +0200341def CheckNoMixingSources(input_api, gn_files, output_api):
342 """Disallow mixing C, C++ and Obj-C/Obj-C++ in the same target.
343
344 See bugs.webrtc.org/7743 for more context.
345 """
Artem Titove92675b2018-05-22 10:21:27 +0200346
Mirko Bonadei4dc4e252017-09-19 13:49:16 +0200347 def _MoreThanOneSourceUsed(*sources_lists):
348 sources_used = 0
349 for source_list in sources_lists:
350 if len(source_list):
351 sources_used += 1
352 return sources_used > 1
353
354 errors = defaultdict(lambda: [])
kjellander7439f972016-12-05 22:47:46 -0800355 for gn_file in gn_files:
Mirko Bonadei4dc4e252017-09-19 13:49:16 +0200356 gn_file_content = input_api.ReadFile(gn_file)
357 for target_match in TARGET_RE.finditer(gn_file_content):
358 # list_of_sources is a list of tuples of the form
359 # (c_files, cc_files, objc_files) that keeps track of all the sources
360 # defined in a target. A GN target can have more that on definition of
361 # sources (since it supports if/else statements).
362 # E.g.:
363 # rtc_static_library("foo") {
364 # if (is_win) {
365 # sources = [ "foo.cc" ]
366 # } else {
367 # sources = [ "foo.mm" ]
368 # }
369 # }
370 # This is allowed and the presubmit check should support this case.
371 list_of_sources = []
kjellander7439f972016-12-05 22:47:46 -0800372 c_files = []
373 cc_files = []
Mirko Bonadei4dc4e252017-09-19 13:49:16 +0200374 objc_files = []
375 target_name = target_match.group('target_name')
376 target_contents = target_match.group('target_contents')
377 for sources_match in SOURCES_RE.finditer(target_contents):
378 if '+=' not in sources_match.group(0):
379 if c_files or cc_files or objc_files:
380 list_of_sources.append((c_files, cc_files, objc_files))
381 c_files = []
382 cc_files = []
383 objc_files = []
384 for file_match in FILE_PATH_RE.finditer(sources_match.group(1)):
385 file_path = file_match.group('file_path')
386 extension = file_match.group('extension')
387 if extension == '.c':
388 c_files.append(file_path + extension)
389 if extension == '.cc':
390 cc_files.append(file_path + extension)
391 if extension in ['.m', '.mm']:
392 objc_files.append(file_path + extension)
393 list_of_sources.append((c_files, cc_files, objc_files))
394 for c_files_list, cc_files_list, objc_files_list in list_of_sources:
395 if _MoreThanOneSourceUsed(c_files_list, cc_files_list, objc_files_list):
396 all_sources = sorted(c_files_list + cc_files_list + objc_files_list)
397 errors[gn_file.LocalPath()].append((target_name, all_sources))
398 if errors:
kjellander7439f972016-12-05 22:47:46 -0800399 return [output_api.PresubmitError(
Mirko Bonadei4dc4e252017-09-19 13:49:16 +0200400 'GN targets cannot mix .c, .cc and .m (or .mm) source files.\n'
401 'Please create a separate target for each collection of sources.\n'
kjellander7439f972016-12-05 22:47:46 -0800402 'Mixed sources: \n'
403 '%s\n'
Mirko Bonadei4dc4e252017-09-19 13:49:16 +0200404 'Violating GN files:\n%s\n' % (json.dumps(errors, indent=2),
405 '\n'.join(errors.keys())))]
kjellander7439f972016-12-05 22:47:46 -0800406 return []
407
Artem Titove92675b2018-05-22 10:21:27 +0200408
charujain9893e252017-09-14 13:33:22 +0200409def CheckNoPackageBoundaryViolations(input_api, gn_files, output_api):
ehmaldonado4fb97462017-01-30 05:27:22 -0800410 cwd = input_api.PresubmitLocalPath()
Oleh Prypin2f33a562017-10-04 20:17:54 +0200411 with _AddToPath(input_api.os_path.join(
412 cwd, 'tools_webrtc', 'presubmit_checks_lib')):
413 from check_package_boundaries import CheckPackageBoundaries
414 build_files = [os.path.join(cwd, gn_file.LocalPath()) for gn_file in gn_files]
415 errors = CheckPackageBoundaries(cwd, build_files)[:5]
416 if errors:
ehmaldonado4fb97462017-01-30 05:27:22 -0800417 return [output_api.PresubmitError(
Oleh Prypin2f33a562017-10-04 20:17:54 +0200418 'There are package boundary violations in the following GN files:',
419 long_text='\n\n'.join(str(err) for err in errors))]
ehmaldonado4fb97462017-01-30 05:27:22 -0800420 return []
421
Mirko Bonadeia51bbd82018-03-08 16:15:45 +0100422
Mirko Bonadeif0e0d752018-07-04 08:48:18 +0200423def _ReportFileAndLine(filename, line_num):
Mirko Bonadeia51bbd82018-03-08 16:15:45 +0100424 """Default error formatter for _FindNewViolationsOfRule."""
425 return '%s (line %s)' % (filename, line_num)
426
427
Mirko Bonadeif0e0d752018-07-04 08:48:18 +0200428def CheckNoWarningSuppressionFlagsAreAdded(gn_files, input_api, output_api,
429 error_formatter=_ReportFileAndLine):
430 """Make sure that warning suppression flags are not added wihtout a reason."""
431 msg = ('Usage of //build/config/clang:extra_warnings is discouraged '
432 'in WebRTC.\n'
433 'If you are not adding this code (e.g. you are just moving '
434 'existing code) or you want to add an exception,\n'
435 'you can add a comment on the line that causes the problem:\n\n'
436 '"-Wno-odr" # no-presubmit-check TODO(bugs.webrtc.org/BUG_ID)\n'
437 '\n'
438 'Affected files:\n')
439 errors = [] # 2-element tuples with (file, line number)
440 clang_warn_re = input_api.re.compile(r'//build/config/clang:extra_warnings')
441 no_presubmit_re = input_api.re.compile(
442 r'# no-presubmit-check TODO\(bugs\.webrtc\.org/\d+\)')
443 for f in gn_files:
444 for line_num, line in f.ChangedContents():
445 if clang_warn_re.search(line) and not no_presubmit_re.search(line):
446 errors.append(error_formatter(f.LocalPath(), line_num))
447 if errors:
448 return [output_api.PresubmitError(msg, errors)]
449 return []
450
Mirko Bonadei9ce800d2019-02-05 16:48:13 +0100451
452def CheckNoTestCaseUsageIsAdded(input_api, output_api, source_file_filter,
453 error_formatter=_ReportFileAndLine):
454 error_msg = ('Usage of legacy GoogleTest API detected!\nPlease use the '
455 'new API: https://github.com/google/googletest/blob/master/'
456 'googletest/docs/primer.md#beware-of-the-nomenclature.\n'
457 'Affected files:\n')
458 errors = [] # 2-element tuples with (file, line number)
459 test_case_re = input_api.re.compile(r'TEST_CASE')
460 file_filter = lambda f: (source_file_filter(f)
461 and f.LocalPath().endswith('.cc'))
462 for f in input_api.AffectedSourceFiles(file_filter):
463 for line_num, line in f.ChangedContents():
464 if test_case_re.search(line):
465 errors.append(error_formatter(f.LocalPath(), line_num))
466 if errors:
467 return [output_api.PresubmitError(error_msg, errors)]
468 return []
469
470
Mirko Bonadeia51bbd82018-03-08 16:15:45 +0100471def CheckNoStreamUsageIsAdded(input_api, output_api,
Artem Titov739351d2018-05-11 12:21:36 +0200472 source_file_filter,
Mirko Bonadeif0e0d752018-07-04 08:48:18 +0200473 error_formatter=_ReportFileAndLine):
Mirko Bonadeia51bbd82018-03-08 16:15:45 +0100474 """Make sure that no more dependencies on stringstream are added."""
475 error_msg = ('Usage of <sstream>, <istream> and <ostream> in WebRTC is '
476 'deprecated.\n'
477 'This includes the following types:\n'
478 'std::istringstream, std::ostringstream, std::wistringstream, '
479 'std::wostringstream,\n'
480 'std::wstringstream, std::ostream, std::wostream, std::istream,'
481 'std::wistream,\n'
482 'std::iostream, std::wiostream.\n'
483 'If you are not adding this code (e.g. you are just moving '
484 'existing code),\n'
485 'you can add a comment on the line that causes the problem:\n\n'
486 '#include <sstream> // no-presubmit-check TODO(webrtc:8982)\n'
487 'std::ostream& F() { // no-presubmit-check TODO(webrtc:8982)\n'
488 '\n'
Karl Wibergebd01e82018-03-14 15:08:39 +0100489 'If you are adding new code, consider using '
490 'rtc::SimpleStringBuilder\n'
491 '(in rtc_base/strings/string_builder.h).\n'
Mirko Bonadeia51bbd82018-03-08 16:15:45 +0100492 'Affected files:\n')
493 errors = [] # 2-element tuples with (file, line number)
494 include_re = input_api.re.compile(r'#include <(i|o|s)stream>')
495 usage_re = input_api.re.compile(r'std::(w|i|o|io|wi|wo|wio)(string)*stream')
496 no_presubmit_re = input_api.re.compile(
Jonas Olsson74395342018-04-03 12:22:07 +0200497 r'// no-presubmit-check TODO\(webrtc:8982\)')
Artem Titova04d1402018-05-11 11:23:00 +0200498 file_filter = lambda x: (input_api.FilterSourceFile(x)
499 and source_file_filter(x))
Mirko Bonadei571791a2019-05-07 14:08:05 +0200500
501 def _IsException(file_path):
502 is_test = any(file_path.endswith(x) for x in ['_test.cc', '_tests.cc',
503 '_unittest.cc',
504 '_unittests.cc'])
Patrik Höglund2ea27962020-01-13 15:10:40 +0100505 return (file_path.startswith('examples') or
506 file_path.startswith('test') or
507 is_test)
508
Mirko Bonadei571791a2019-05-07 14:08:05 +0200509
Artem Titova04d1402018-05-11 11:23:00 +0200510 for f in input_api.AffectedSourceFiles(file_filter):
Mirko Bonadei571791a2019-05-07 14:08:05 +0200511 # Usage of stringstream is allowed under examples/ and in tests.
512 if f.LocalPath() == 'PRESUBMIT.py' or _IsException(f.LocalPath()):
Mirko Bonadeid2c83322018-03-19 10:31:47 +0000513 continue
514 for line_num, line in f.ChangedContents():
515 if ((include_re.search(line) or usage_re.search(line))
516 and not no_presubmit_re.search(line)):
517 errors.append(error_formatter(f.LocalPath(), line_num))
Mirko Bonadeia51bbd82018-03-08 16:15:45 +0100518 if errors:
519 return [output_api.PresubmitError(error_msg, errors)]
520 return []
521
Artem Titove92675b2018-05-22 10:21:27 +0200522
Mirko Bonadeia05d47e2018-05-09 11:03:38 +0200523def CheckPublicDepsIsNotUsed(gn_files, input_api, output_api):
524 """Checks that public_deps is not used without a good reason."""
Mirko Bonadei5c1ad592017-12-12 11:52:27 +0100525 result = []
Mirko Bonadeia05d47e2018-05-09 11:03:38 +0200526 no_presubmit_check_re = input_api.re.compile(
Joe Chen0b3a6e32019-12-26 23:01:42 -0800527 r'# no-presubmit-check TODO\(webrtc:\d+\)')
Mirko Bonadeia05d47e2018-05-09 11:03:38 +0200528 error_msg = ('public_deps is not recommended in WebRTC BUILD.gn files '
529 'because it doesn\'t map well to downstream build systems.\n'
530 'Used in: %s (line %d).\n'
531 'If you are not adding this code (e.g. you are just moving '
532 'existing code) or you have a good reason, you can add a '
533 'comment on the line that causes the problem:\n\n'
534 'public_deps = [ # no-presubmit-check TODO(webrtc:8603)\n')
Mirko Bonadei5c1ad592017-12-12 11:52:27 +0100535 for affected_file in gn_files:
536 for (line_number, affected_line) in affected_file.ChangedContents():
Mirko Bonadeia05d47e2018-05-09 11:03:38 +0200537 if ('public_deps' in affected_line
538 and not no_presubmit_check_re.search(affected_line)):
Mirko Bonadei5c1ad592017-12-12 11:52:27 +0100539 result.append(
540 output_api.PresubmitError(error_msg % (affected_file.LocalPath(),
541 line_number)))
542 return result
543
Artem Titove92675b2018-05-22 10:21:27 +0200544
Mirko Bonadei05691dd2019-10-22 07:34:24 -0700545def CheckCheckIncludesIsNotUsed(gn_files, input_api, output_api):
Patrik Höglund6f491062018-01-11 12:04:23 +0100546 result = []
547 error_msg = ('check_includes overrides are not allowed since it can cause '
548 'incorrect dependencies to form. It effectively means that your '
549 'module can include any .h file without depending on its '
550 'corresponding target. There are some exceptional cases when '
Mirko Bonadei05691dd2019-10-22 07:34:24 -0700551 'this is allowed: if so, get approval from a .gn owner in the '
Patrik Höglund6f491062018-01-11 12:04:23 +0100552 'root OWNERS file.\n'
553 'Used in: %s (line %d).')
Mirko Bonadei05691dd2019-10-22 07:34:24 -0700554 no_presubmit_re = input_api.re.compile(
555 r'# no-presubmit-check TODO\(bugs\.webrtc\.org/\d+\)')
Patrik Höglund6f491062018-01-11 12:04:23 +0100556 for affected_file in gn_files:
557 for (line_number, affected_line) in affected_file.ChangedContents():
Mirko Bonadei05691dd2019-10-22 07:34:24 -0700558 if ('check_includes' in affected_line
559 and not no_presubmit_re.search(affected_line)):
Patrik Höglund6f491062018-01-11 12:04:23 +0100560 result.append(
561 output_api.PresubmitError(error_msg % (affected_file.LocalPath(),
562 line_number)))
563 return result
564
Artem Titove92675b2018-05-22 10:21:27 +0200565
Mirko Bonadeif0e0d752018-07-04 08:48:18 +0200566def CheckGnChanges(input_api, output_api):
Artem Titova04d1402018-05-11 11:23:00 +0200567 file_filter = lambda x: (input_api.FilterSourceFile(
Oleh Prypinafe01652017-10-04 15:56:08 +0200568 x, white_list=(r'.+\.(gn|gni)$',),
Mirko Bonadeif0e0d752018-07-04 08:48:18 +0200569 black_list=(r'.*/presubmit_checks_lib/testdata/.*',)))
ehmaldonado5b1ba082016-09-02 05:51:08 -0700570
571 gn_files = []
Artem Titova04d1402018-05-11 11:23:00 +0200572 for f in input_api.AffectedSourceFiles(file_filter):
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200573 gn_files.append(f)
ehmaldonado5b1ba082016-09-02 05:51:08 -0700574
575 result = []
576 if gn_files:
charujain9893e252017-09-14 13:33:22 +0200577 result.extend(CheckNoSourcesAbove(input_api, gn_files, output_api))
Mirko Bonadei4dc4e252017-09-19 13:49:16 +0200578 result.extend(CheckNoMixingSources(input_api, gn_files, output_api))
579 result.extend(CheckNoPackageBoundaryViolations(input_api, gn_files,
580 output_api))
Mirko Bonadeia05d47e2018-05-09 11:03:38 +0200581 result.extend(CheckPublicDepsIsNotUsed(gn_files, input_api, output_api))
Mirko Bonadei05691dd2019-10-22 07:34:24 -0700582 result.extend(CheckCheckIncludesIsNotUsed(gn_files, input_api, output_api))
Mirko Bonadeif0e0d752018-07-04 08:48:18 +0200583 result.extend(CheckNoWarningSuppressionFlagsAreAdded(gn_files, input_api,
584 output_api))
ehmaldonado5b1ba082016-09-02 05:51:08 -0700585 return result
586
Artem Titove92675b2018-05-22 10:21:27 +0200587
Oleh Prypin920b6532017-10-05 11:28:51 +0200588def CheckGnGen(input_api, output_api):
589 """Runs `gn gen --check` with default args to detect mismatches between
590 #includes and dependencies in the BUILD.gn files, as well as general build
591 errors.
592 """
593 with _AddToPath(input_api.os_path.join(
594 input_api.PresubmitLocalPath(), 'tools_webrtc', 'presubmit_checks_lib')):
Yves Gerey546ee612019-02-26 17:04:16 +0100595 from build_helpers import RunGnCheck
Mirko Bonadeid8665442018-09-04 12:17:27 +0200596 errors = RunGnCheck(FindSrcDirPath(input_api.PresubmitLocalPath()))[:5]
Oleh Prypin920b6532017-10-05 11:28:51 +0200597 if errors:
598 return [output_api.PresubmitPromptWarning(
599 'Some #includes do not match the build dependency graph. Please run:\n'
600 ' gn gen --check <out_dir>',
601 long_text='\n\n'.join(errors))]
602 return []
603
Artem Titove92675b2018-05-22 10:21:27 +0200604
Artem Titova04d1402018-05-11 11:23:00 +0200605def CheckUnwantedDependencies(input_api, output_api, source_file_filter):
kjellander@webrtc.org3bd41562014-09-01 11:06:37 +0000606 """Runs checkdeps on #include statements added in this
607 change. Breaking - rules is an error, breaking ! rules is a
608 warning.
609 """
610 # Copied from Chromium's src/PRESUBMIT.py.
611
612 # We need to wait until we have an input_api object and use this
613 # roundabout construct to import checkdeps because this file is
614 # eval-ed and thus doesn't have __file__.
Mirko Bonadeid8665442018-09-04 12:17:27 +0200615 src_path = FindSrcDirPath(input_api.PresubmitLocalPath())
616 checkdeps_path = input_api.os_path.join(src_path, 'buildtools', 'checkdeps')
Oleh Prypin2f33a562017-10-04 20:17:54 +0200617 if not os.path.exists(checkdeps_path):
618 return [output_api.PresubmitError(
619 'Cannot find checkdeps at %s\nHave you run "gclient sync" to '
620 'download all the DEPS entries?' % checkdeps_path)]
621 with _AddToPath(checkdeps_path):
kjellander@webrtc.org3bd41562014-09-01 11:06:37 +0000622 import checkdeps
623 from cpp_checker import CppChecker
624 from rules import Rule
kjellander@webrtc.org3bd41562014-09-01 11:06:37 +0000625
626 added_includes = []
Artem Titova04d1402018-05-11 11:23:00 +0200627 for f in input_api.AffectedFiles(file_filter=source_file_filter):
kjellander@webrtc.org3bd41562014-09-01 11:06:37 +0000628 if not CppChecker.IsCppFile(f.LocalPath()):
629 continue
630
Henrik Kjellander57e5fd22015-05-25 12:55:39 +0200631 changed_lines = [line for _, line in f.ChangedContents()]
kjellander@webrtc.org3bd41562014-09-01 11:06:37 +0000632 added_includes.append([f.LocalPath(), changed_lines])
633
634 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
635
636 error_descriptions = []
637 warning_descriptions = []
638 for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
639 added_includes):
640 description_with_path = '%s\n %s' % (path, rule_description)
641 if rule_type == Rule.DISALLOW:
642 error_descriptions.append(description_with_path)
643 else:
644 warning_descriptions.append(description_with_path)
645
646 results = []
647 if error_descriptions:
648 results.append(output_api.PresubmitError(
kjellandera7066a32017-03-23 03:47:05 -0700649 'You added one or more #includes that violate checkdeps rules.\n'
650 'Check that the DEPS files in these locations contain valid rules.\n'
651 'See https://cs.chromium.org/chromium/src/buildtools/checkdeps/ for '
652 'more details about checkdeps.',
kjellander@webrtc.org3bd41562014-09-01 11:06:37 +0000653 error_descriptions))
654 if warning_descriptions:
655 results.append(output_api.PresubmitPromptOrNotify(
656 'You added one or more #includes of files that are temporarily\n'
657 'allowed but being removed. Can you avoid introducing the\n'
kjellandera7066a32017-03-23 03:47:05 -0700658 '#include? See relevant DEPS file(s) for details and contacts.\n'
659 'See https://cs.chromium.org/chromium/src/buildtools/checkdeps/ for '
660 'more details about checkdeps.',
kjellander@webrtc.org3bd41562014-09-01 11:06:37 +0000661 warning_descriptions))
662 return results
663
Artem Titove92675b2018-05-22 10:21:27 +0200664
charujain9893e252017-09-14 13:33:22 +0200665def CheckCommitMessageBugEntry(input_api, output_api):
666 """Check that bug entries are well-formed in commit message."""
667 bogus_bug_msg = (
Mirko Bonadei61880182017-10-12 15:12:35 +0200668 'Bogus Bug entry: %s. Please specify the issue tracker prefix and the '
charujain9893e252017-09-14 13:33:22 +0200669 'issue number, separated by a colon, e.g. webrtc:123 or chromium:12345.')
670 results = []
Mirko Bonadei61880182017-10-12 15:12:35 +0200671 for bug in input_api.change.BugsFromDescription():
charujain9893e252017-09-14 13:33:22 +0200672 bug = bug.strip()
673 if bug.lower() == 'none':
674 continue
charujain81a58c72017-09-25 13:25:45 +0200675 if 'b/' not in bug and ':' not in bug:
charujain9893e252017-09-14 13:33:22 +0200676 try:
677 if int(bug) > 100000:
678 # Rough indicator for current chromium bugs.
679 prefix_guess = 'chromium'
680 else:
681 prefix_guess = 'webrtc'
Mirko Bonadei61880182017-10-12 15:12:35 +0200682 results.append('Bug entry requires issue tracker prefix, e.g. %s:%s' %
charujain9893e252017-09-14 13:33:22 +0200683 (prefix_guess, bug))
684 except ValueError:
685 results.append(bogus_bug_msg % bug)
charujain81a58c72017-09-25 13:25:45 +0200686 elif not (re.match(r'\w+:\d+', bug) or re.match(r'b/\d+', bug)):
charujain9893e252017-09-14 13:33:22 +0200687 results.append(bogus_bug_msg % bug)
688 return [output_api.PresubmitError(r) for r in results]
689
Artem Titove92675b2018-05-22 10:21:27 +0200690
charujain9893e252017-09-14 13:33:22 +0200691def CheckChangeHasBugField(input_api, output_api):
Mirko Bonadei61880182017-10-12 15:12:35 +0200692 """Requires that the changelist is associated with a bug.
kjellanderd1e26a92016-09-19 08:11:16 -0700693
694 This check is stricter than the one in depot_tools/presubmit_canned_checks.py
Mirko Bonadei61880182017-10-12 15:12:35 +0200695 since it fails the presubmit if the bug field is missing or doesn't contain
kjellanderd1e26a92016-09-19 08:11:16 -0700696 a bug reference.
Mirko Bonadei61880182017-10-12 15:12:35 +0200697
698 This supports both 'BUG=' and 'Bug:' since we are in the process of migrating
699 to Gerrit and it encourages the usage of 'Bug:'.
kjellanderd1e26a92016-09-19 08:11:16 -0700700 """
Mirko Bonadei61880182017-10-12 15:12:35 +0200701 if input_api.change.BugsFromDescription():
kjellanderd1e26a92016-09-19 08:11:16 -0700702 return []
703 else:
704 return [output_api.PresubmitError(
Mirko Bonadei61880182017-10-12 15:12:35 +0200705 'The "Bug: [bug number]" footer is mandatory. Please create a bug and '
kjellanderd1e26a92016-09-19 08:11:16 -0700706 'reference it using either of:\n'
Mirko Bonadei61880182017-10-12 15:12:35 +0200707 ' * https://bugs.webrtc.org - reference it using Bug: webrtc:XXXX\n'
708 ' * https://crbug.com - reference it using Bug: chromium:XXXXXX')]
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000709
Artem Titove92675b2018-05-22 10:21:27 +0200710
Artem Titova04d1402018-05-11 11:23:00 +0200711def CheckJSONParseErrors(input_api, output_api, source_file_filter):
kjellander569cf942016-02-11 05:02:59 -0800712 """Check that JSON files do not contain syntax errors."""
713
714 def FilterFile(affected_file):
Artem Titova04d1402018-05-11 11:23:00 +0200715 return (input_api.os_path.splitext(affected_file.LocalPath())[1] == '.json'
716 and source_file_filter(affected_file))
kjellander569cf942016-02-11 05:02:59 -0800717
718 def GetJSONParseError(input_api, filename):
719 try:
720 contents = input_api.ReadFile(filename)
721 input_api.json.loads(contents)
722 except ValueError as e:
723 return e
724 return None
725
726 results = []
727 for affected_file in input_api.AffectedFiles(
728 file_filter=FilterFile, include_deletes=False):
729 parse_error = GetJSONParseError(input_api,
730 affected_file.AbsoluteLocalPath())
731 if parse_error:
732 results.append(output_api.PresubmitError('%s could not be parsed: %s' %
Artem Titove92675b2018-05-22 10:21:27 +0200733 (affected_file.LocalPath(),
734 parse_error)))
kjellander569cf942016-02-11 05:02:59 -0800735 return results
736
737
charujain9893e252017-09-14 13:33:22 +0200738def RunPythonTests(input_api, output_api):
kjellanderc88b5d52017-04-05 06:42:43 -0700739 def Join(*args):
Henrik Kjellander8d3ad822015-05-26 19:52:05 +0200740 return input_api.os_path.join(input_api.PresubmitLocalPath(), *args)
741
742 test_directories = [
Edward Lemur6d01f6d2017-09-14 17:02:01 +0200743 input_api.PresubmitLocalPath(),
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200744 Join('rtc_tools', 'py_event_log_analyzer'),
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200745 Join('audio', 'test', 'unittests'),
ehmaldonado4fb97462017-01-30 05:27:22 -0800746 ] + [
Henrik Kjellander90fd7d82017-05-09 08:30:10 +0200747 root for root, _, files in os.walk(Join('tools_webrtc'))
ehmaldonado4fb97462017-01-30 05:27:22 -0800748 if any(f.endswith('_test.py') for f in files)
Henrik Kjellander8d3ad822015-05-26 19:52:05 +0200749 ]
750
751 tests = []
752 for directory in test_directories:
753 tests.extend(
754 input_api.canned_checks.GetUnitTestsInDirectory(
755 input_api,
756 output_api,
757 directory,
758 whitelist=[r'.+_test\.py$']))
759 return input_api.RunTests(tests, parallel=True)
760
761
Artem Titova04d1402018-05-11 11:23:00 +0200762def CheckUsageOfGoogleProtobufNamespace(input_api, output_api,
763 source_file_filter):
mbonadei38415b22017-04-07 05:38:01 -0700764 """Checks that the namespace google::protobuf has not been used."""
765 files = []
766 pattern = input_api.re.compile(r'google::protobuf')
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200767 proto_utils_path = os.path.join('rtc_base', 'protobuf_utils.h')
Artem Titova04d1402018-05-11 11:23:00 +0200768 file_filter = lambda x: (input_api.FilterSourceFile(x)
769 and source_file_filter(x))
770 for f in input_api.AffectedSourceFiles(file_filter):
mbonadei38415b22017-04-07 05:38:01 -0700771 if f.LocalPath() in [proto_utils_path, 'PRESUBMIT.py']:
772 continue
773 contents = input_api.ReadFile(f)
774 if pattern.search(contents):
775 files.append(f)
776
777 if files:
778 return [output_api.PresubmitError(
779 'Please avoid to use namespace `google::protobuf` directly.\n'
780 'Add a using directive in `%s` and include that header instead.'
781 % proto_utils_path, files)]
782 return []
783
784
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200785def _LicenseHeader(input_api):
786 """Returns the license header regexp."""
787 # Accept any year number from 2003 to the current year
788 current_year = int(input_api.time.strftime('%Y'))
789 allowed_years = (str(s) for s in reversed(xrange(2003, current_year + 1)))
790 years_re = '(' + '|'.join(allowed_years) + ')'
791 license_header = (
792 r'.*? Copyright( \(c\))? %(year)s The WebRTC [Pp]roject [Aa]uthors\. '
793 r'All [Rr]ights [Rr]eserved\.\n'
794 r'.*?\n'
795 r'.*? Use of this source code is governed by a BSD-style license\n'
796 r'.*? that can be found in the LICENSE file in the root of the source\n'
797 r'.*? tree\. An additional intellectual property rights grant can be '
798 r'found\n'
799 r'.*? in the file PATENTS\. All contributing project authors may\n'
800 r'.*? be found in the AUTHORS file in the root of the source tree\.\n'
801 ) % {
802 'year': years_re,
803 }
804 return license_header
805
806
charujain9893e252017-09-14 13:33:22 +0200807def CommonChecks(input_api, output_api):
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000808 """Checks common to both upload and commit."""
niklase@google.comda159d62011-05-30 11:51:34 +0000809 results = []
tkchin42f580e2015-11-26 23:18:23 -0800810 # Filter out files that are in objc or ios dirs from being cpplint-ed since
811 # they do not follow C++ lint rules.
812 black_list = input_api.DEFAULT_BLACK_LIST + (
813 r".*\bobjc[\\\/].*",
Kári Tristan Helgason3fa35172016-09-09 08:55:05 +0000814 r".*objc\.[hcm]+$",
tkchin42f580e2015-11-26 23:18:23 -0800815 )
816 source_file_filter = lambda x: input_api.FilterSourceFile(x, None, black_list)
charujain9893e252017-09-14 13:33:22 +0200817 results.extend(CheckApprovedFilesLintClean(
tkchin42f580e2015-11-26 23:18:23 -0800818 input_api, output_api, source_file_filter))
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200819 results.extend(input_api.canned_checks.CheckLicense(
820 input_api, output_api, _LicenseHeader(input_api)))
phoglund@webrtc.org5d3713932013-03-07 09:59:43 +0000821 results.extend(input_api.canned_checks.RunPylint(input_api, output_api,
kjellander@webrtc.org177567c2016-12-22 10:40:28 +0100822 black_list=(r'^base[\\\/].*\.py$',
Henrik Kjellander14771ac2015-06-02 13:10:04 +0200823 r'^build[\\\/].*\.py$',
824 r'^buildtools[\\\/].*\.py$',
kjellander38c65c82017-04-12 22:43:38 -0700825 r'^infra[\\\/].*\.py$',
Henrik Kjellander0779e8f2016-12-22 12:01:17 +0100826 r'^ios[\\\/].*\.py$',
Henrik Kjellander14771ac2015-06-02 13:10:04 +0200827 r'^out.*[\\\/].*\.py$',
828 r'^testing[\\\/].*\.py$',
829 r'^third_party[\\\/].*\.py$',
kjellander@webrtc.org177567c2016-12-22 10:40:28 +0100830 r'^tools[\\\/].*\.py$',
kjellanderafd54942016-12-17 12:21:39 -0800831 # TODO(phoglund): should arguably be checked.
Henrik Kjellander90fd7d82017-05-09 08:30:10 +0200832 r'^tools_webrtc[\\\/]mb[\\\/].*\.py$',
Henrik Kjellander14771ac2015-06-02 13:10:04 +0200833 r'^xcodebuild.*[\\\/].*\.py$',),
Henrik Kjellander57e5fd22015-05-25 12:55:39 +0200834 pylintrc='pylintrc'))
kjellander569cf942016-02-11 05:02:59 -0800835
nisse3d21e232016-09-02 03:07:06 -0700836 # TODO(nisse): talk/ is no more, so make below checks simpler?
Henrik Kjellander57e5fd22015-05-25 12:55:39 +0200837 # WebRTC can't use the presubmit_canned_checks.PanProjectChecks function since
838 # we need to have different license checks in talk/ and webrtc/ directories.
839 # Instead, hand-picked checks are included below.
Henrik Kjellander63224672015-09-08 08:03:56 +0200840
tkchin3cd9a302016-06-08 12:40:28 -0700841 # .m and .mm files are ObjC files. For simplicity we will consider .h files in
842 # ObjC subdirectories ObjC headers.
843 objc_filter_list = (r'.+\.m$', r'.+\.mm$', r'.+objc\/.+\.h$')
Henrik Kjellanderb4af3d62016-11-16 20:11:29 +0100844 # Skip long-lines check for DEPS and GN files.
845 build_file_filter_list = (r'.+\.gn$', r'.+\.gni$', 'DEPS')
Artem Titova04d1402018-05-11 11:23:00 +0200846 # Also we will skip most checks for third_party directory.
Artem Titov42f0d782018-06-27 13:23:17 +0200847 third_party_filter_list = (r'^third_party[\\\/].+',)
tkchin3cd9a302016-06-08 12:40:28 -0700848 eighty_char_sources = lambda x: input_api.FilterSourceFile(x,
Artem Titova04d1402018-05-11 11:23:00 +0200849 black_list=build_file_filter_list + objc_filter_list +
850 third_party_filter_list)
tkchin3cd9a302016-06-08 12:40:28 -0700851 hundred_char_sources = lambda x: input_api.FilterSourceFile(x,
852 white_list=objc_filter_list)
Artem Titove92675b2018-05-22 10:21:27 +0200853 non_third_party_sources = lambda x: input_api.FilterSourceFile(x,
854 black_list=third_party_filter_list)
855
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000856 results.extend(input_api.canned_checks.CheckLongLines(
tkchin3cd9a302016-06-08 12:40:28 -0700857 input_api, output_api, maxlen=80, source_file_filter=eighty_char_sources))
858 results.extend(input_api.canned_checks.CheckLongLines(
859 input_api, output_api, maxlen=100,
860 source_file_filter=hundred_char_sources))
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000861 results.extend(input_api.canned_checks.CheckChangeHasNoTabs(
Artem Titova04d1402018-05-11 11:23:00 +0200862 input_api, output_api, source_file_filter=non_third_party_sources))
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000863 results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace(
Artem Titova04d1402018-05-11 11:23:00 +0200864 input_api, output_api, source_file_filter=non_third_party_sources))
kjellandere5dc62a2016-12-14 00:16:21 -0800865 results.extend(input_api.canned_checks.CheckAuthorizedAuthor(
Oleh Prypine0735142018-10-04 11:15:54 +0200866 input_api, output_api, bot_whitelist=[
867 'chromium-webrtc-autoroll@webrtc-ci.iam.gserviceaccount.com'
868 ]))
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000869 results.extend(input_api.canned_checks.CheckChangeTodoHasOwner(
Artem Titova04d1402018-05-11 11:23:00 +0200870 input_api, output_api, source_file_filter=non_third_party_sources))
Yves Gerey87a93532018-06-20 15:51:49 +0200871 results.extend(input_api.canned_checks.CheckPatchFormatted(
872 input_api, output_api))
charujain9893e252017-09-14 13:33:22 +0200873 results.extend(CheckNativeApiHeaderChanges(input_api, output_api))
Artem Titova04d1402018-05-11 11:23:00 +0200874 results.extend(CheckNoIOStreamInHeaders(
875 input_api, output_api, source_file_filter=non_third_party_sources))
876 results.extend(CheckNoPragmaOnce(
877 input_api, output_api, source_file_filter=non_third_party_sources))
878 results.extend(CheckNoFRIEND_TEST(
879 input_api, output_api, source_file_filter=non_third_party_sources))
Mirko Bonadeif0e0d752018-07-04 08:48:18 +0200880 results.extend(CheckGnChanges(input_api, output_api))
Artem Titova04d1402018-05-11 11:23:00 +0200881 results.extend(CheckUnwantedDependencies(
882 input_api, output_api, source_file_filter=non_third_party_sources))
883 results.extend(CheckJSONParseErrors(
884 input_api, output_api, source_file_filter=non_third_party_sources))
charujain9893e252017-09-14 13:33:22 +0200885 results.extend(RunPythonTests(input_api, output_api))
Artem Titova04d1402018-05-11 11:23:00 +0200886 results.extend(CheckUsageOfGoogleProtobufNamespace(
887 input_api, output_api, source_file_filter=non_third_party_sources))
888 results.extend(CheckOrphanHeaders(
889 input_api, output_api, source_file_filter=non_third_party_sources))
890 results.extend(CheckNewlineAtTheEndOfProtoFiles(
891 input_api, output_api, source_file_filter=non_third_party_sources))
892 results.extend(CheckNoStreamUsageIsAdded(
Artem Titov739351d2018-05-11 12:21:36 +0200893 input_api, output_api, non_third_party_sources))
Mirko Bonadei9ce800d2019-02-05 16:48:13 +0100894 results.extend(CheckNoTestCaseUsageIsAdded(
895 input_api, output_api, non_third_party_sources))
Mirko Bonadei7e4ee6e2018-09-28 11:45:23 +0200896 results.extend(CheckAddedDepsHaveTargetApprovals(input_api, output_api))
Mirko Bonadeia418e672018-10-24 13:57:25 +0200897 results.extend(CheckApiDepsFileIsUpToDate(input_api, output_api))
tzika06bf852018-11-15 20:37:35 +0900898 results.extend(CheckAbslMemoryInclude(
899 input_api, output_api, non_third_party_sources))
Mirko Bonadei9fa8ef12019-09-17 19:14:13 +0200900 results.extend(CheckBannedAbslMakeUnique(
901 input_api, output_api, non_third_party_sources))
Mirko Bonadeia418e672018-10-24 13:57:25 +0200902 return results
903
904
905def CheckApiDepsFileIsUpToDate(input_api, output_api):
Mirko Bonadei90490372018-10-26 13:17:47 +0200906 """Check that 'include_rules' in api/DEPS is up to date.
907
908 The file api/DEPS must be kept up to date in order to avoid to avoid to
909 include internal header from WebRTC's api/ headers.
910
911 This check is focused on ensuring that 'include_rules' contains a deny
912 rule for each root level directory. More focused allow rules can be
913 added to 'specific_include_rules'.
914 """
Mirko Bonadeia418e672018-10-24 13:57:25 +0200915 results = []
916 api_deps = os.path.join(input_api.PresubmitLocalPath(), 'api', 'DEPS')
917 with open(api_deps) as f:
918 deps_content = _ParseDeps(f.read())
919
920 include_rules = deps_content.get('include_rules', [])
Mirko Bonadei01e97ae2019-09-05 14:36:42 +0200921 dirs_to_skip = set(['api', 'docs'])
Mirko Bonadeia418e672018-10-24 13:57:25 +0200922
Mirko Bonadei90490372018-10-26 13:17:47 +0200923 # Only check top level directories affected by the current CL.
924 dirs_to_check = set()
925 for f in input_api.AffectedFiles():
926 path_tokens = [t for t in f.LocalPath().split(os.sep) if t]
927 if len(path_tokens) > 1:
Mirko Bonadei01e97ae2019-09-05 14:36:42 +0200928 if (path_tokens[0] not in dirs_to_skip and
Mirko Bonadei90490372018-10-26 13:17:47 +0200929 os.path.isdir(os.path.join(input_api.PresubmitLocalPath(),
930 path_tokens[0]))):
931 dirs_to_check.add(path_tokens[0])
Mirko Bonadeia418e672018-10-24 13:57:25 +0200932
Mirko Bonadei90490372018-10-26 13:17:47 +0200933 missing_include_rules = set()
934 for p in dirs_to_check:
Mirko Bonadeia418e672018-10-24 13:57:25 +0200935 rule = '-%s' % p
936 if rule not in include_rules:
Mirko Bonadei90490372018-10-26 13:17:47 +0200937 missing_include_rules.add(rule)
938
Mirko Bonadeia418e672018-10-24 13:57:25 +0200939 if missing_include_rules:
Mirko Bonadei90490372018-10-26 13:17:47 +0200940 error_msg = [
941 'include_rules = [\n',
942 ' ...\n',
943 ]
Mirko Bonadeia418e672018-10-24 13:57:25 +0200944
Mirko Bonadei90490372018-10-26 13:17:47 +0200945 for r in sorted(missing_include_rules):
946 error_msg.append(' "%s",\n' % str(r))
Mirko Bonadeia418e672018-10-24 13:57:25 +0200947
Mirko Bonadei90490372018-10-26 13:17:47 +0200948 error_msg.append(' ...\n')
949 error_msg.append(']\n')
950
Mirko Bonadeia418e672018-10-24 13:57:25 +0200951 results.append(output_api.PresubmitError(
Mirko Bonadei90490372018-10-26 13:17:47 +0200952 'New root level directory detected! WebRTC api/ headers should '
953 'not #include headers from \n'
954 'the new directory, so please update "include_rules" in file\n'
955 '"%s". Example:\n%s\n' % (api_deps, ''.join(error_msg))))
956
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000957 return results
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000958
Mirko Bonadei9fa8ef12019-09-17 19:14:13 +0200959def CheckBannedAbslMakeUnique(input_api, output_api, source_file_filter):
960 file_filter = lambda f: (f.LocalPath().endswith(('.cc', '.h'))
961 and source_file_filter(f))
962
963 files = []
964 for f in input_api.AffectedFiles(
965 include_deletes=False, file_filter=file_filter):
966 for _, line in f.ChangedContents():
967 if 'absl::make_unique' in line:
968 files.append(f)
969 break
970
971 if len(files):
972 return [output_api.PresubmitError(
973 'Please use std::make_unique instead of absl::make_unique.\n'
974 'Affected files:',
975 files)]
976 return []
977
tzika06bf852018-11-15 20:37:35 +0900978def CheckAbslMemoryInclude(input_api, output_api, source_file_filter):
979 pattern = input_api.re.compile(
980 r'^#include\s*"absl/memory/memory.h"', input_api.re.MULTILINE)
981 file_filter = lambda f: (f.LocalPath().endswith(('.cc', '.h'))
982 and source_file_filter(f))
983
984 files = []
985 for f in input_api.AffectedFiles(
986 include_deletes=False, file_filter=file_filter):
987 contents = input_api.ReadFile(f)
988 if pattern.search(contents):
989 continue
990 for _, line in f.ChangedContents():
Mirko Bonadei9fa8ef12019-09-17 19:14:13 +0200991 if 'absl::WrapUnique' in line:
tzika06bf852018-11-15 20:37:35 +0900992 files.append(f)
993 break
994
995 if len(files):
996 return [output_api.PresubmitError(
Mirko Bonadei9fa8ef12019-09-17 19:14:13 +0200997 'Please include "absl/memory/memory.h" header for absl::WrapUnique.\n'
998 'This header may or may not be included transitively depending on the '
999 'C++ standard version.',
tzika06bf852018-11-15 20:37:35 +09001000 files)]
1001 return []
kjellander@webrtc.orge4158642014-08-06 09:11:18 +00001002
andrew@webrtc.org53df1362012-01-26 21:24:23 +00001003def CheckChangeOnUpload(input_api, output_api):
1004 results = []
charujain9893e252017-09-14 13:33:22 +02001005 results.extend(CommonChecks(input_api, output_api))
Oleh Prypin920b6532017-10-05 11:28:51 +02001006 results.extend(CheckGnGen(input_api, output_api))
Henrik Kjellander57e5fd22015-05-25 12:55:39 +02001007 results.extend(
1008 input_api.canned_checks.CheckGNFormatted(input_api, output_api))
niklase@google.comda159d62011-05-30 11:51:34 +00001009 return results
1010
kjellander@webrtc.orge4158642014-08-06 09:11:18 +00001011
andrew@webrtc.org2442de12012-01-23 17:45:41 +00001012def CheckChangeOnCommit(input_api, output_api):
niklase@google.com1198db92011-06-09 07:07:24 +00001013 results = []
charujain9893e252017-09-14 13:33:22 +02001014 results.extend(CommonChecks(input_api, output_api))
1015 results.extend(VerifyNativeApiHeadersListIsValid(input_api, output_api))
Artem Titov42f0d782018-06-27 13:23:17 +02001016 results.extend(input_api.canned_checks.CheckOwners(input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23 +00001017 results.extend(input_api.canned_checks.CheckChangeWasUploaded(
1018 input_api, output_api))
1019 results.extend(input_api.canned_checks.CheckChangeHasDescription(
1020 input_api, output_api))
charujain9893e252017-09-14 13:33:22 +02001021 results.extend(CheckChangeHasBugField(input_api, output_api))
1022 results.extend(CheckCommitMessageBugEntry(input_api, output_api))
kjellander@webrtc.org12cb88c2014-02-13 11:53:43 +00001023 results.extend(input_api.canned_checks.CheckTreeIsOpen(
1024 input_api, output_api,
1025 json_url='http://webrtc-status.appspot.com/current?format=json'))
niklase@google.com1198db92011-06-09 07:07:24 +00001026 return results
mbonadei74973ed2017-05-09 07:58:05 -07001027
1028
Artem Titova04d1402018-05-11 11:23:00 +02001029def CheckOrphanHeaders(input_api, output_api, source_file_filter):
mbonadei74973ed2017-05-09 07:58:05 -07001030 # We need to wait until we have an input_api object and use this
1031 # roundabout construct to import prebubmit_checks_lib because this file is
1032 # eval-ed and thus doesn't have __file__.
Patrik Höglund2f3f7222017-12-19 11:08:56 +01001033 error_msg = """{} should be listed in {}."""
mbonadei74973ed2017-05-09 07:58:05 -07001034 results = []
Patrik Höglund7e60de22018-01-09 14:22:00 +01001035 orphan_blacklist = [
1036 os.path.join('tools_webrtc', 'ios', 'SDK'),
1037 ]
Oleh Prypin2f33a562017-10-04 20:17:54 +02001038 with _AddToPath(input_api.os_path.join(
1039 input_api.PresubmitLocalPath(), 'tools_webrtc', 'presubmit_checks_lib')):
mbonadei74973ed2017-05-09 07:58:05 -07001040 from check_orphan_headers import GetBuildGnPathFromFilePath
1041 from check_orphan_headers import IsHeaderInBuildGn
mbonadei74973ed2017-05-09 07:58:05 -07001042
Artem Titova04d1402018-05-11 11:23:00 +02001043 file_filter = lambda x: input_api.FilterSourceFile(
1044 x, black_list=orphan_blacklist) and source_file_filter(x)
1045 for f in input_api.AffectedSourceFiles(file_filter):
Patrik Höglund7e60de22018-01-09 14:22:00 +01001046 if f.LocalPath().endswith('.h'):
mbonadei74973ed2017-05-09 07:58:05 -07001047 file_path = os.path.abspath(f.LocalPath())
1048 root_dir = os.getcwd()
1049 gn_file_path = GetBuildGnPathFromFilePath(file_path, os.path.exists,
1050 root_dir)
1051 in_build_gn = IsHeaderInBuildGn(file_path, gn_file_path)
1052 if not in_build_gn:
1053 results.append(output_api.PresubmitError(error_msg.format(
Patrik Höglund2f3f7222017-12-19 11:08:56 +01001054 f.LocalPath(), os.path.relpath(gn_file_path))))
mbonadei74973ed2017-05-09 07:58:05 -07001055 return results
Mirko Bonadei960fd5b2017-06-29 14:59:36 +02001056
1057
Artem Titove92675b2018-05-22 10:21:27 +02001058def CheckNewlineAtTheEndOfProtoFiles(input_api, output_api, source_file_filter):
Mirko Bonadei960fd5b2017-06-29 14:59:36 +02001059 """Checks that all .proto files are terminated with a newline."""
1060 error_msg = 'File {} must end with exactly one newline.'
1061 results = []
Artem Titova04d1402018-05-11 11:23:00 +02001062 file_filter = lambda x: input_api.FilterSourceFile(
1063 x, white_list=(r'.+\.proto$',)) and source_file_filter(x)
1064 for f in input_api.AffectedSourceFiles(file_filter):
Mirko Bonadei960fd5b2017-06-29 14:59:36 +02001065 file_path = f.LocalPath()
1066 with open(file_path) as f:
1067 lines = f.readlines()
Mirko Bonadeia730c1c2017-09-18 11:33:13 +02001068 if len(lines) > 0 and not lines[-1].endswith('\n'):
Mirko Bonadei960fd5b2017-06-29 14:59:36 +02001069 results.append(output_api.PresubmitError(error_msg.format(file_path)))
1070 return results
Mirko Bonadei7e4ee6e2018-09-28 11:45:23 +02001071
1072
1073def _ExtractAddRulesFromParsedDeps(parsed_deps):
1074 """Extract the rules that add dependencies from a parsed DEPS file.
1075
1076 Args:
1077 parsed_deps: the locals dictionary from evaluating the DEPS file."""
1078 add_rules = set()
1079 add_rules.update([
1080 rule[1:] for rule in parsed_deps.get('include_rules', [])
1081 if rule.startswith('+') or rule.startswith('!')
1082 ])
1083 for _, rules in parsed_deps.get('specific_include_rules',
1084 {}).iteritems():
1085 add_rules.update([
1086 rule[1:] for rule in rules
1087 if rule.startswith('+') or rule.startswith('!')
1088 ])
1089 return add_rules
1090
1091
1092def _ParseDeps(contents):
1093 """Simple helper for parsing DEPS files."""
1094 # Stubs for handling special syntax in the root DEPS file.
1095 class VarImpl(object):
1096
1097 def __init__(self, local_scope):
1098 self._local_scope = local_scope
1099
1100 def Lookup(self, var_name):
1101 """Implements the Var syntax."""
1102 try:
1103 return self._local_scope['vars'][var_name]
1104 except KeyError:
1105 raise Exception('Var is not defined: %s' % var_name)
1106
1107 local_scope = {}
1108 global_scope = {
1109 'Var': VarImpl(local_scope).Lookup,
1110 }
1111 exec contents in global_scope, local_scope
1112 return local_scope
1113
1114
1115def _CalculateAddedDeps(os_path, old_contents, new_contents):
1116 """Helper method for _CheckAddedDepsHaveTargetApprovals. Returns
1117 a set of DEPS entries that we should look up.
1118
1119 For a directory (rather than a specific filename) we fake a path to
1120 a specific filename by adding /DEPS. This is chosen as a file that
1121 will seldom or never be subject to per-file include_rules.
1122 """
1123 # We ignore deps entries on auto-generated directories.
1124 auto_generated_dirs = ['grit', 'jni']
1125
1126 old_deps = _ExtractAddRulesFromParsedDeps(_ParseDeps(old_contents))
1127 new_deps = _ExtractAddRulesFromParsedDeps(_ParseDeps(new_contents))
1128
1129 added_deps = new_deps.difference(old_deps)
1130
1131 results = set()
1132 for added_dep in added_deps:
1133 if added_dep.split('/')[0] in auto_generated_dirs:
1134 continue
1135 # Assume that a rule that ends in .h is a rule for a specific file.
1136 if added_dep.endswith('.h'):
1137 results.add(added_dep)
1138 else:
1139 results.add(os_path.join(added_dep, 'DEPS'))
1140 return results
1141
1142
1143def CheckAddedDepsHaveTargetApprovals(input_api, output_api):
1144 """When a dependency prefixed with + is added to a DEPS file, we
1145 want to make sure that the change is reviewed by an OWNER of the
1146 target file or directory, to avoid layering violations from being
1147 introduced. This check verifies that this happens.
1148 """
1149 virtual_depended_on_files = set()
1150
1151 file_filter = lambda f: not input_api.re.match(
1152 r"^third_party[\\\/](WebKit|blink)[\\\/].*", f.LocalPath())
1153 for f in input_api.AffectedFiles(include_deletes=False,
1154 file_filter=file_filter):
1155 filename = input_api.os_path.basename(f.LocalPath())
1156 if filename == 'DEPS':
1157 virtual_depended_on_files.update(_CalculateAddedDeps(
1158 input_api.os_path,
1159 '\n'.join(f.OldContents()),
1160 '\n'.join(f.NewContents())))
1161
1162 if not virtual_depended_on_files:
1163 return []
1164
1165 if input_api.is_committing:
1166 if input_api.tbr:
1167 return [output_api.PresubmitNotifyResult(
1168 '--tbr was specified, skipping OWNERS check for DEPS additions')]
1169 if input_api.dry_run:
1170 return [output_api.PresubmitNotifyResult(
1171 'This is a dry run, skipping OWNERS check for DEPS additions')]
1172 if not input_api.change.issue:
1173 return [output_api.PresubmitError(
1174 "DEPS approval by OWNERS check failed: this change has "
1175 "no change number, so we can't check it for approvals.")]
1176 output = output_api.PresubmitError
1177 else:
1178 output = output_api.PresubmitNotifyResult
1179
1180 owners_db = input_api.owners_db
1181 owner_email, reviewers = (
1182 input_api.canned_checks.GetCodereviewOwnerAndReviewers(
1183 input_api,
1184 owners_db.email_regexp,
1185 approval_needed=input_api.is_committing))
1186
1187 owner_email = owner_email or input_api.change.author_email
1188
1189 reviewers_plus_owner = set(reviewers)
1190 if owner_email:
1191 reviewers_plus_owner.add(owner_email)
1192 missing_files = owners_db.files_not_covered_by(virtual_depended_on_files,
1193 reviewers_plus_owner)
1194
1195 # We strip the /DEPS part that was added by
1196 # _FilesToCheckForIncomingDeps to fake a path to a file in a
1197 # directory.
1198 def StripDeps(path):
1199 start_deps = path.rfind('/DEPS')
1200 if start_deps != -1:
1201 return path[:start_deps]
1202 else:
1203 return path
1204 unapproved_dependencies = ["'+%s'," % StripDeps(path)
1205 for path in missing_files]
1206
1207 if unapproved_dependencies:
1208 output_list = [
1209 output('You need LGTM from owners of depends-on paths in DEPS that were '
1210 'modified in this CL:\n %s' %
1211 '\n '.join(sorted(unapproved_dependencies)))]
1212 suggested_owners = owners_db.reviewers_for(missing_files, owner_email)
1213 output_list.append(output(
1214 'Suggested missing target path OWNERS:\n %s' %
1215 '\n '.join(suggested_owners or [])))
1216 return output_list
1217
1218 return []