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