WebKit merge 81934:81946.
[chromium-blink-merge.git] / PRESUBMIT.py
blob8b7999da1e000eb1a98cfc903f8bb6acf88d491e
1 # Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Top-level presubmit script for Chromium.
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8 for more details about the presubmit API built into gcl.
9 """
11 import time
13 _EXCLUDED_PATHS = (
14 r"^breakpad[\\\/].*",
15 r"^net/tools/spdyshark/[\\\/].*",
16 r"^skia[\\\/].*",
17 r"^v8[\\\/].*",
18 r".*MakeFile$",
21 _TEXT_FILES = (
22 r".*\.txt",
23 r".*\.json",
26 _LICENSE_HEADER = (
27 r".*? Copyright \(c\) %s The Chromium Authors\. All rights reserved\.\n"
28 r".*? Use of this source code is governed by a BSD-style license that can "
29 "be\n"
30 r".*? found in the LICENSE file\."
31 "\n"
32 ) % time.strftime("%Y")
34 def _CheckNoInterfacesInBase(input_api, output_api, source_file_filter):
35 """Checks to make sure no files in libbase.a have |@interface|."""
36 pattern = input_api.re.compile(r'@interface')
37 files = []
38 for f in input_api.AffectedSourceFiles(source_file_filter):
39 if (f.LocalPath().find('base/') != -1 and
40 f.LocalPath().find('base/test/') == -1):
41 contents = input_api.ReadFile(f)
42 if pattern.search(contents):
43 files.append(f)
45 if len(files):
46 return [ output_api.PresubmitError(
47 'Objective-C interfaces or categories are forbidden in libbase. ' +
48 'See http://groups.google.com/a/chromium.org/group/chromium-dev/' +
49 'browse_thread/thread/efb28c10435987fd',
50 files) ]
51 return []
53 def _CheckSingletonInHeaders(input_api, output_api, source_file_filter):
54 """Checks to make sure no header files have |Singleton<|."""
55 pattern = input_api.re.compile(r'Singleton<')
56 files = []
57 for f in input_api.AffectedSourceFiles(source_file_filter):
58 if (f.LocalPath().endswith('.h') or f.LocalPath().endswith('.hxx') or
59 f.LocalPath().endswith('.hpp') or f.LocalPath().endswith('.inl')):
60 contents = input_api.ReadFile(f)
61 if pattern.search(contents):
62 files.append(f)
64 if len(files):
65 return [ output_api.PresubmitError(
66 'Found Singleton<T> in the following header files.\n' +
67 'Please move them to an appropriate source file so that the ' +
68 'template gets instantiated in a single compilation unit.',
69 files) ]
70 return []
73 def _CheckSubversionConfig(input_api, output_api):
74 """Verifies the subversion config file is correctly setup.
76 Checks that autoprops are enabled, returns an error otherwise.
77 """
78 join = input_api.os_path.join
79 if input_api.platform == 'win32':
80 appdata = input_api.environ.get('APPDATA', '')
81 if not appdata:
82 return [output_api.PresubmitError('%APPDATA% is not configured.')]
83 path = join(appdata, 'Subversion', 'config')
84 else:
85 home = input_api.environ.get('HOME', '')
86 if not home:
87 return [output_api.PresubmitError('$HOME is not configured.')]
88 path = join(home, '.subversion', 'config')
90 error_msg = (
91 'Please look at http://dev.chromium.org/developers/coding-style to\n'
92 'configure your subversion configuration file. This enables automatic\n'
93 'properties to simplify the project maintenance.\n'
94 'Pro-tip: just download and install\n'
95 'http://src.chromium.org/viewvc/chrome/trunk/tools/build/slave/config\n')
97 try:
98 lines = open(path, 'r').read().splitlines()
99 # Make sure auto-props is enabled and check for 2 Chromium standard
100 # auto-prop.
101 if (not '*.cc = svn:eol-style=LF' in lines or
102 not '*.pdf = svn:mime-type=application/pdf' in lines or
103 not 'enable-auto-props = yes' in lines):
104 return [
105 output_api.PresubmitNotifyResult(
106 'It looks like you have not configured your subversion config '
107 'file or it is not up-to-date.\n' + error_msg)
109 except (OSError, IOError):
110 return [
111 output_api.PresubmitNotifyResult(
112 'Can\'t find your subversion config file.\n' + error_msg)
114 return []
117 def _CheckConstNSObject(input_api, output_api, source_file_filter):
118 """Checks to make sure no objective-c files have |const NSSomeClass*|."""
119 pattern = input_api.re.compile(r'const\s+NS\w*\s*\*')
120 files = []
121 for f in input_api.AffectedSourceFiles(source_file_filter):
122 if f.LocalPath().endswith('.h') or f.LocalPath().endswith('.mm'):
123 contents = input_api.ReadFile(f)
124 if pattern.search(contents):
125 files.append(f)
127 if len(files):
128 if input_api.is_committing:
129 res_type = output_api.PresubmitPromptWarning
130 else:
131 res_type = output_api.PresubmitNotifyResult
132 return [ res_type('|const NSClass*| is wrong, see ' +
133 'http://dev.chromium.org/developers/clang-mac',
134 files) ]
135 return []
138 def _CommonChecks(input_api, output_api):
139 results = []
140 # What does this code do?
141 # It loads the default black list (e.g. third_party, experimental, etc) and
142 # add our black list (breakpad, skia and v8 are still not following
143 # google style and are not really living this repository).
144 # See presubmit_support.py InputApi.FilterSourceFile for the (simple) usage.
145 black_list = input_api.DEFAULT_BLACK_LIST + _EXCLUDED_PATHS
146 white_list = input_api.DEFAULT_WHITE_LIST + _TEXT_FILES
147 sources = lambda x: input_api.FilterSourceFile(x, black_list=black_list)
148 text_files = lambda x: input_api.FilterSourceFile(x, black_list=black_list,
149 white_list=white_list)
151 # TODO(dpranke): enable upload as well
152 if input_api.is_committing:
153 results.extend(input_api.canned_checks.CheckOwners(
154 input_api, output_api, source_file_filter=sources))
156 results.extend(input_api.canned_checks.CheckLongLines(
157 input_api, output_api, source_file_filter=sources))
158 results.extend(input_api.canned_checks.CheckChangeHasNoTabs(
159 input_api, output_api, source_file_filter=sources))
160 results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace(
161 input_api, output_api, source_file_filter=sources))
162 results.extend(input_api.canned_checks.CheckChangeSvnEolStyle(
163 input_api, output_api, source_file_filter=text_files))
164 results.extend(input_api.canned_checks.CheckSvnForCommonMimeTypes(
165 input_api, output_api))
166 results.extend(input_api.canned_checks.CheckLicense(
167 input_api, output_api, _LICENSE_HEADER, source_file_filter=sources))
168 results.extend(_CheckConstNSObject(
169 input_api, output_api, source_file_filter=sources))
170 results.extend(_CheckSingletonInHeaders(
171 input_api, output_api, source_file_filter=sources))
172 results.extend(_CheckNoInterfacesInBase(
173 input_api, output_api, source_file_filter=sources))
174 return results
177 def CheckChangeOnUpload(input_api, output_api):
178 results = []
179 results.extend(_CommonChecks(input_api, output_api))
180 return results
183 def CheckChangeOnCommit(input_api, output_api):
184 results = []
185 if not input_api.json:
186 results.append(output_api.PresubmitNotifyResult(
187 'You don\'t have json nor simplejson installed.\n'
188 ' This is a warning that you will need to upgrade your python '
189 'installation.\n'
190 ' This is no big deal but you\'ll eventually need to '
191 'upgrade.\n'
192 ' How? Easy! You can do it right now and shut me off! Just:\n'
193 ' del depot_tools\\python.bat\n'
194 ' gclient\n'
195 ' Thanks for your patience.'))
196 results.extend(_CommonChecks(input_api, output_api))
197 # TODO(thestig) temporarily disabled, doesn't work in third_party/
198 #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
199 # input_api, output_api, sources))
200 # Make sure the tree is 'open'.
201 results.extend(input_api.canned_checks.CheckTreeIsOpen(
202 input_api,
203 output_api,
204 json_url='http://chromium-status.appspot.com/current?format=json'))
205 results.extend(input_api.canned_checks.CheckRietveldTryJobExecution(input_api,
206 output_api, 'http://codereview.chromium.org', ('win', 'linux', 'mac'),
207 'tryserver@chromium.org'))
209 # These builders are just too slow.
210 IGNORED_BUILDERS = [
211 'Chromium XP',
212 'Chromium Mac',
213 'Chromium Arm (dbg)',
214 'Chromium Linux',
215 'Chromium Linux x64',
217 results.extend(input_api.canned_checks.CheckBuildbotPendingBuilds(
218 input_api,
219 output_api,
220 'http://build.chromium.org/p/chromium/json/builders?filter=1',
222 IGNORED_BUILDERS))
223 results.extend(input_api.canned_checks.CheckChangeHasBugField(
224 input_api, output_api))
225 results.extend(input_api.canned_checks.CheckChangeHasTestField(
226 input_api, output_api))
227 results.extend(_CheckSubversionConfig(input_api, output_api))
228 return results
231 def GetPreferredTrySlaves():
232 return ['win', 'linux', 'mac']