blob: 7d6331e0892671dc581991ab9ba0589a6811cb4e [file] [log] [blame]
[email protected]377ab1da2011-03-17 15:36:281# Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]ca8d1982009-02-19 16:33:122# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Top-level presubmit script for Chromium.
6
[email protected]f1293792009-07-31 18:09:567See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
[email protected]50d7d721e2009-11-15 17:56:188for more details about the presubmit API built into gcl.
[email protected]ca8d1982009-02-19 16:33:129"""
10
[email protected]379e7dd2010-01-28 17:39:2111_EXCLUDED_PATHS = (
[email protected]3e4eb112011-01-18 03:29:5412 r"^breakpad[\\\/].*",
13 r"^net/tools/spdyshark/[\\\/].*",
14 r"^skia[\\\/].*",
15 r"^v8[\\\/].*",
16 r".*MakeFile$",
[email protected]4306417642009-06-11 00:33:4017)
[email protected]ca8d1982009-02-19 16:33:1218
[email protected]ca8d1982009-02-19 16:33:1219
[email protected]22c9bd72011-03-27 16:47:3920def _CheckNoInterfacesInBase(input_api, output_api):
[email protected]6a4c8e682010-12-19 03:31:3421 """Checks to make sure no files in libbase.a have |@interface|."""
[email protected]839c1392011-04-29 20:15:1922 pattern = input_api.re.compile(r'^\s*@interface', input_api.re.MULTILINE)
[email protected]6a4c8e682010-12-19 03:31:3423 files = []
[email protected]22c9bd72011-03-27 16:47:3924 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
[email protected]a766a1322011-09-08 20:46:0525 if (f.LocalPath().startswith('base/') and
[email protected]0b2f07b02011-05-02 17:29:0026 not f.LocalPath().endswith('_unittest.mm')):
[email protected]6a4c8e682010-12-19 03:31:3427 contents = input_api.ReadFile(f)
28 if pattern.search(contents):
29 files.append(f)
30
31 if len(files):
32 return [ output_api.PresubmitError(
33 'Objective-C interfaces or categories are forbidden in libbase. ' +
34 'See http://groups.google.com/a/chromium.org/group/chromium-dev/' +
35 'browse_thread/thread/efb28c10435987fd',
36 files) ]
37 return []
38
[email protected]650c9082010-12-14 14:33:4439
[email protected]55459852011-08-10 15:17:1940def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api):
41 """Attempts to prevent use of functions intended only for testing in
42 non-testing code. For now this is just a best-effort implementation
43 that ignores header files and may have some false positives. A
44 better implementation would probably need a proper C++ parser.
45 """
46 # We only scan .cc files and the like, as the declaration of
47 # for-testing functions in header files are hard to distinguish from
48 # calls to such functions without a proper C++ parser.
49 source_extensions = r'\.(cc|cpp|cxx|mm)$'
50 file_inclusion_pattern = r'.+%s' % source_extensions
51 file_exclusion_pattern = (r'.+(_test_support|_(unit|browser|ui|perf)test)%s' %
52 source_extensions)
[email protected]3afb12a42011-08-15 13:48:3353 path_exclusion_pattern = r'.*[/\\](test|tool(s)?)[/\\].*'
[email protected]55459852011-08-10 15:17:1954
55 base_function_pattern = r'ForTest(ing)?|for_test(ing)?'
56 inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern)
57 exclusion_pattern = input_api.re.compile(
58 r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
59 base_function_pattern, base_function_pattern))
60
61 def FilterFile(affected_file):
[email protected]3afb12a42011-08-15 13:48:3362 black_list = ((file_exclusion_pattern, path_exclusion_pattern, ) +
63 _EXCLUDED_PATHS + input_api.DEFAULT_BLACK_LIST)
[email protected]55459852011-08-10 15:17:1964 return input_api.FilterSourceFile(
65 affected_file,
66 white_list=(file_inclusion_pattern, ),
67 black_list=black_list)
68
69 problems = []
70 for f in input_api.AffectedSourceFiles(FilterFile):
71 local_path = f.LocalPath()
72 lines = input_api.ReadFile(f).splitlines()
73 line_number = 0
74 for line in lines:
75 if (inclusion_pattern.search(line) and
76 not exclusion_pattern.search(line)):
77 problems.append(
78 '%s:%d\n %s' % (local_path, line_number, line.strip()))
79 line_number += 1
80
81 if problems:
82 return [output_api.PresubmitPromptWarning(
83 'You might be calling functions intended only for testing from\n'
84 'production code. Please verify that the following usages are OK,\n'
85 'and email [email protected] if you are seeing false positives:',
86 problems)]
87 else:
88 return []
89
90
[email protected]10689ca2011-09-02 02:31:5491def _CheckNoIOStreamInHeaders(input_api, output_api):
92 """Checks to make sure no .h files include <iostream>."""
93 files = []
94 pattern = input_api.re.compile(r'^#include\s*<iostream>',
95 input_api.re.MULTILINE)
96 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
97 if not f.LocalPath().endswith('.h'):
98 continue
99 contents = input_api.ReadFile(f)
100 if pattern.search(contents):
101 files.append(f)
102
103 if len(files):
104 return [ output_api.PresubmitError(
105 'Do not #include <iostream> in header files, since it inserts static ' +
106 'initialization into every file including the header. Instead, ' +
107 '#include <ostream>. See http://crbug.com/94794',
108 files) ]
109 return []
110
111
[email protected]22c9bd72011-03-27 16:47:39112def _CommonChecks(input_api, output_api):
113 """Checks common to both upload and commit."""
114 results = []
115 results.extend(input_api.canned_checks.PanProjectChecks(
116 input_api, output_api, excluded_paths=_EXCLUDED_PATHS))
117 results.extend(_CheckNoInterfacesInBase(input_api, output_api))
[email protected]66daa702011-05-28 14:41:46118 results.extend(_CheckAuthorizedAuthor(input_api, output_api))
[email protected]55459852011-08-10 15:17:19119 results.extend(
120 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api))
[email protected]10689ca2011-09-02 02:31:54121 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
[email protected]22c9bd72011-03-27 16:47:39122 return results
[email protected]1f7b4172010-01-28 01:17:34123
[email protected]b337cb5b2011-01-23 21:24:05124
125def _CheckSubversionConfig(input_api, output_api):
126 """Verifies the subversion config file is correctly setup.
127
128 Checks that autoprops are enabled, returns an error otherwise.
129 """
130 join = input_api.os_path.join
131 if input_api.platform == 'win32':
132 appdata = input_api.environ.get('APPDATA', '')
133 if not appdata:
134 return [output_api.PresubmitError('%APPDATA% is not configured.')]
135 path = join(appdata, 'Subversion', 'config')
136 else:
137 home = input_api.environ.get('HOME', '')
138 if not home:
139 return [output_api.PresubmitError('$HOME is not configured.')]
140 path = join(home, '.subversion', 'config')
141
142 error_msg = (
143 'Please look at http://dev.chromium.org/developers/coding-style to\n'
144 'configure your subversion configuration file. This enables automatic\n'
[email protected]c6a3c10b2011-01-24 16:14:20145 'properties to simplify the project maintenance.\n'
146 'Pro-tip: just download and install\n'
147 'http://src.chromium.org/viewvc/chrome/trunk/tools/build/slave/config\n')
[email protected]b337cb5b2011-01-23 21:24:05148
149 try:
150 lines = open(path, 'r').read().splitlines()
151 # Make sure auto-props is enabled and check for 2 Chromium standard
152 # auto-prop.
153 if (not '*.cc = svn:eol-style=LF' in lines or
154 not '*.pdf = svn:mime-type=application/pdf' in lines or
155 not 'enable-auto-props = yes' in lines):
156 return [
[email protected]79ed7e62011-02-21 21:08:53157 output_api.PresubmitNotifyResult(
[email protected]b337cb5b2011-01-23 21:24:05158 'It looks like you have not configured your subversion config '
[email protected]b5359c02011-02-01 20:29:56159 'file or it is not up-to-date.\n' + error_msg)
[email protected]b337cb5b2011-01-23 21:24:05160 ]
161 except (OSError, IOError):
162 return [
[email protected]79ed7e62011-02-21 21:08:53163 output_api.PresubmitNotifyResult(
[email protected]b337cb5b2011-01-23 21:24:05164 'Can\'t find your subversion config file.\n' + error_msg)
165 ]
166 return []
167
168
[email protected]66daa702011-05-28 14:41:46169def _CheckAuthorizedAuthor(input_api, output_api):
170 """For non-googler/chromites committers, verify the author's email address is
171 in AUTHORS.
172 """
[email protected]9bb9cb82011-06-13 20:43:01173 # TODO(maruel): Add it to input_api?
174 import fnmatch
175
[email protected]66daa702011-05-28 14:41:46176 author = input_api.change.author_email
[email protected]9bb9cb82011-06-13 20:43:01177 if not author:
178 input_api.logging.info('No author, skipping AUTHOR check')
[email protected]66daa702011-05-28 14:41:46179 return []
[email protected]c99663292011-05-31 19:46:08180 authors_path = input_api.os_path.join(
[email protected]66daa702011-05-28 14:41:46181 input_api.PresubmitLocalPath(), 'AUTHORS')
182 valid_authors = (
183 input_api.re.match(r'[^#]+\s+\<(.+?)\>\s*$', line)
184 for line in open(authors_path))
[email protected]ac54b132011-06-06 18:11:18185 valid_authors = [item.group(1).lower() for item in valid_authors if item]
[email protected]9bb9cb82011-06-13 20:43:01186 if input_api.verbose:
187 print 'Valid authors are %s' % ', '.join(valid_authors)
[email protected]d8b50be2011-06-15 14:19:44188 if not any(fnmatch.fnmatch(author.lower(), valid) for valid in valid_authors):
[email protected]66daa702011-05-28 14:41:46189 return [output_api.PresubmitPromptWarning(
190 ('%s is not in AUTHORS file. If you are a new contributor, please visit'
191 '\n'
192 'http://www.chromium.org/developers/contributing-code and read the '
193 '"Legal" section\n'
194 'If you are a chromite, verify the contributor signed the CLA.') %
195 author)]
196 return []
197
198
[email protected]1f7b4172010-01-28 01:17:34199def CheckChangeOnUpload(input_api, output_api):
200 results = []
201 results.extend(_CommonChecks(input_api, output_api))
[email protected]fe5f57c52009-06-05 14:25:54202 return results
[email protected]ca8d1982009-02-19 16:33:12203
204
205def CheckChangeOnCommit(input_api, output_api):
[email protected]fe5f57c52009-06-05 14:25:54206 results = []
[email protected]1f7b4172010-01-28 01:17:34207 results.extend(_CommonChecks(input_api, output_api))
[email protected]dd805fe2009-10-01 08:11:51208 # TODO(thestig) temporarily disabled, doesn't work in third_party/
209 #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
210 # input_api, output_api, sources))
[email protected]fe5f57c52009-06-05 14:25:54211 # Make sure the tree is 'open'.
[email protected]806e98e2010-03-19 17:49:27212 results.extend(input_api.canned_checks.CheckTreeIsOpen(
[email protected]7f238152009-08-12 19:00:34213 input_api,
214 output_api,
[email protected]4efa42142010-08-26 01:29:26215 json_url='http://chromium-status.appspot.com/current?format=json'))
[email protected]806e98e2010-03-19 17:49:27216 results.extend(input_api.canned_checks.CheckRietveldTryJobExecution(input_api,
217 output_api, 'http://codereview.chromium.org', ('win', 'linux', 'mac'),
218 '[email protected]'))
219
[email protected]3e4eb112011-01-18 03:29:54220 results.extend(input_api.canned_checks.CheckChangeHasBugField(
221 input_api, output_api))
222 results.extend(input_api.canned_checks.CheckChangeHasTestField(
223 input_api, output_api))
[email protected]b337cb5b2011-01-23 21:24:05224 results.extend(_CheckSubversionConfig(input_api, output_api))
[email protected]fe5f57c52009-06-05 14:25:54225 return results
[email protected]ca8d1982009-02-19 16:33:12226
227
[email protected]5fa06292009-09-29 01:55:00228def GetPreferredTrySlaves():
229 return ['win', 'linux', 'mac']