[email protected] | 377ab1da | 2011-03-17 15:36:28 | [diff] [blame] | 1 | # Copyright (c) 2011 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] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 19 | |
[email protected] | 22c9bd7 | 2011-03-27 16:47:39 | [diff] [blame] | 20 | def _CheckNoInterfacesInBase(input_api, output_api): |
[email protected] | 6a4c8e68 | 2010-12-19 03:31:34 | [diff] [blame] | 21 | """Checks to make sure no files in libbase.a have |@interface|.""" |
[email protected] | 839c139 | 2011-04-29 20:15:19 | [diff] [blame] | 22 | pattern = input_api.re.compile(r'^\s*@interface', input_api.re.MULTILINE) |
[email protected] | 6a4c8e68 | 2010-12-19 03:31:34 | [diff] [blame] | 23 | files = [] |
[email protected] | 22c9bd7 | 2011-03-27 16:47:39 | [diff] [blame] | 24 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
[email protected] | 6a4c8e68 | 2010-12-19 03:31:34 | [diff] [blame] | 25 | if (f.LocalPath().find('base/') != -1 and |
[email protected] | 0b2f07b0 | 2011-05-02 17:29:00 | [diff] [blame] | 26 | f.LocalPath().find('base/test/') == -1 and |
| 27 | not f.LocalPath().endswith('_unittest.mm')): |
[email protected] | 6a4c8e68 | 2010-12-19 03:31:34 | [diff] [blame] | 28 | contents = input_api.ReadFile(f) |
| 29 | if pattern.search(contents): |
| 30 | files.append(f) |
| 31 | |
| 32 | if len(files): |
| 33 | return [ output_api.PresubmitError( |
| 34 | 'Objective-C interfaces or categories are forbidden in libbase. ' + |
| 35 | 'See http://groups.google.com/a/chromium.org/group/chromium-dev/' + |
| 36 | 'browse_thread/thread/efb28c10435987fd', |
| 37 | files) ] |
| 38 | return [] |
| 39 | |
[email protected] | 650c908 | 2010-12-14 14:33:44 | [diff] [blame] | 40 | |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 41 | def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api): |
| 42 | """Attempts to prevent use of functions intended only for testing in |
| 43 | non-testing code. For now this is just a best-effort implementation |
| 44 | that ignores header files and may have some false positives. A |
| 45 | better implementation would probably need a proper C++ parser. |
| 46 | """ |
| 47 | # We only scan .cc files and the like, as the declaration of |
| 48 | # for-testing functions in header files are hard to distinguish from |
| 49 | # calls to such functions without a proper C++ parser. |
| 50 | source_extensions = r'\.(cc|cpp|cxx|mm)$' |
| 51 | file_inclusion_pattern = r'.+%s' % source_extensions |
| 52 | file_exclusion_pattern = (r'.+(_test_support|_(unit|browser|ui|perf)test)%s' % |
| 53 | source_extensions) |
| 54 | |
| 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): |
| 62 | black_list = ((file_exclusion_pattern, ) + _EXCLUDED_PATHS + |
| 63 | input_api.DEFAULT_BLACK_LIST) |
| 64 | 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] | 22c9bd7 | 2011-03-27 16:47:39 | [diff] [blame] | 91 | def _CommonChecks(input_api, output_api): |
| 92 | """Checks common to both upload and commit.""" |
| 93 | results = [] |
| 94 | results.extend(input_api.canned_checks.PanProjectChecks( |
| 95 | input_api, output_api, excluded_paths=_EXCLUDED_PATHS)) |
| 96 | results.extend(_CheckNoInterfacesInBase(input_api, output_api)) |
[email protected] | 66daa70 | 2011-05-28 14:41:46 | [diff] [blame] | 97 | results.extend(_CheckAuthorizedAuthor(input_api, output_api)) |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 98 | results.extend( |
| 99 | _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api)) |
[email protected] | 22c9bd7 | 2011-03-27 16:47:39 | [diff] [blame] | 100 | return results |
[email protected] | 1f7b417 | 2010-01-28 01:17:34 | [diff] [blame] | 101 | |
[email protected] | b337cb5b | 2011-01-23 21:24:05 | [diff] [blame] | 102 | |
| 103 | def _CheckSubversionConfig(input_api, output_api): |
| 104 | """Verifies the subversion config file is correctly setup. |
| 105 | |
| 106 | Checks that autoprops are enabled, returns an error otherwise. |
| 107 | """ |
| 108 | join = input_api.os_path.join |
| 109 | if input_api.platform == 'win32': |
| 110 | appdata = input_api.environ.get('APPDATA', '') |
| 111 | if not appdata: |
| 112 | return [output_api.PresubmitError('%APPDATA% is not configured.')] |
| 113 | path = join(appdata, 'Subversion', 'config') |
| 114 | else: |
| 115 | home = input_api.environ.get('HOME', '') |
| 116 | if not home: |
| 117 | return [output_api.PresubmitError('$HOME is not configured.')] |
| 118 | path = join(home, '.subversion', 'config') |
| 119 | |
| 120 | error_msg = ( |
| 121 | 'Please look at http://dev.chromium.org/developers/coding-style to\n' |
| 122 | 'configure your subversion configuration file. This enables automatic\n' |
[email protected] | c6a3c10b | 2011-01-24 16:14:20 | [diff] [blame] | 123 | 'properties to simplify the project maintenance.\n' |
| 124 | 'Pro-tip: just download and install\n' |
| 125 | 'http://src.chromium.org/viewvc/chrome/trunk/tools/build/slave/config\n') |
[email protected] | b337cb5b | 2011-01-23 21:24:05 | [diff] [blame] | 126 | |
| 127 | try: |
| 128 | lines = open(path, 'r').read().splitlines() |
| 129 | # Make sure auto-props is enabled and check for 2 Chromium standard |
| 130 | # auto-prop. |
| 131 | if (not '*.cc = svn:eol-style=LF' in lines or |
| 132 | not '*.pdf = svn:mime-type=application/pdf' in lines or |
| 133 | not 'enable-auto-props = yes' in lines): |
| 134 | return [ |
[email protected] | 79ed7e6 | 2011-02-21 21:08:53 | [diff] [blame] | 135 | output_api.PresubmitNotifyResult( |
[email protected] | b337cb5b | 2011-01-23 21:24:05 | [diff] [blame] | 136 | 'It looks like you have not configured your subversion config ' |
[email protected] | b5359c0 | 2011-02-01 20:29:56 | [diff] [blame] | 137 | 'file or it is not up-to-date.\n' + error_msg) |
[email protected] | b337cb5b | 2011-01-23 21:24:05 | [diff] [blame] | 138 | ] |
| 139 | except (OSError, IOError): |
| 140 | return [ |
[email protected] | 79ed7e6 | 2011-02-21 21:08:53 | [diff] [blame] | 141 | output_api.PresubmitNotifyResult( |
[email protected] | b337cb5b | 2011-01-23 21:24:05 | [diff] [blame] | 142 | 'Can\'t find your subversion config file.\n' + error_msg) |
| 143 | ] |
| 144 | return [] |
| 145 | |
| 146 | |
[email protected] | 66daa70 | 2011-05-28 14:41:46 | [diff] [blame] | 147 | def _CheckAuthorizedAuthor(input_api, output_api): |
| 148 | """For non-googler/chromites committers, verify the author's email address is |
| 149 | in AUTHORS. |
| 150 | """ |
[email protected] | 9bb9cb8 | 2011-06-13 20:43:01 | [diff] [blame] | 151 | # TODO(maruel): Add it to input_api? |
| 152 | import fnmatch |
| 153 | |
[email protected] | 66daa70 | 2011-05-28 14:41:46 | [diff] [blame] | 154 | author = input_api.change.author_email |
[email protected] | 9bb9cb8 | 2011-06-13 20:43:01 | [diff] [blame] | 155 | if not author: |
| 156 | input_api.logging.info('No author, skipping AUTHOR check') |
[email protected] | 66daa70 | 2011-05-28 14:41:46 | [diff] [blame] | 157 | return [] |
[email protected] | c9966329 | 2011-05-31 19:46:08 | [diff] [blame] | 158 | authors_path = input_api.os_path.join( |
[email protected] | 66daa70 | 2011-05-28 14:41:46 | [diff] [blame] | 159 | input_api.PresubmitLocalPath(), 'AUTHORS') |
| 160 | valid_authors = ( |
| 161 | input_api.re.match(r'[^#]+\s+\<(.+?)\>\s*$', line) |
| 162 | for line in open(authors_path)) |
[email protected] | ac54b13 | 2011-06-06 18:11:18 | [diff] [blame] | 163 | valid_authors = [item.group(1).lower() for item in valid_authors if item] |
[email protected] | 9bb9cb8 | 2011-06-13 20:43:01 | [diff] [blame] | 164 | if input_api.verbose: |
| 165 | print 'Valid authors are %s' % ', '.join(valid_authors) |
[email protected] | d8b50be | 2011-06-15 14:19:44 | [diff] [blame] | 166 | if not any(fnmatch.fnmatch(author.lower(), valid) for valid in valid_authors): |
[email protected] | 66daa70 | 2011-05-28 14:41:46 | [diff] [blame] | 167 | return [output_api.PresubmitPromptWarning( |
| 168 | ('%s is not in AUTHORS file. If you are a new contributor, please visit' |
| 169 | '\n' |
| 170 | 'http://www.chromium.org/developers/contributing-code and read the ' |
| 171 | '"Legal" section\n' |
| 172 | 'If you are a chromite, verify the contributor signed the CLA.') % |
| 173 | author)] |
| 174 | return [] |
| 175 | |
| 176 | |
[email protected] | 1f7b417 | 2010-01-28 01:17:34 | [diff] [blame] | 177 | def CheckChangeOnUpload(input_api, output_api): |
| 178 | results = [] |
| 179 | results.extend(_CommonChecks(input_api, output_api)) |
[email protected] | fe5f57c5 | 2009-06-05 14:25:54 | [diff] [blame] | 180 | return results |
[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 181 | |
| 182 | |
| 183 | def CheckChangeOnCommit(input_api, output_api): |
[email protected] | fe5f57c5 | 2009-06-05 14:25:54 | [diff] [blame] | 184 | results = [] |
[email protected] | 1f7b417 | 2010-01-28 01:17:34 | [diff] [blame] | 185 | results.extend(_CommonChecks(input_api, output_api)) |
[email protected] | dd805fe | 2009-10-01 08:11:51 | [diff] [blame] | 186 | # TODO(thestig) temporarily disabled, doesn't work in third_party/ |
| 187 | #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories( |
| 188 | # input_api, output_api, sources)) |
[email protected] | fe5f57c5 | 2009-06-05 14:25:54 | [diff] [blame] | 189 | # Make sure the tree is 'open'. |
[email protected] | 806e98e | 2010-03-19 17:49:27 | [diff] [blame] | 190 | results.extend(input_api.canned_checks.CheckTreeIsOpen( |
[email protected] | 7f23815 | 2009-08-12 19:00:34 | [diff] [blame] | 191 | input_api, |
| 192 | output_api, |
[email protected] | 4efa4214 | 2010-08-26 01:29:26 | [diff] [blame] | 193 | json_url='http://chromium-status.appspot.com/current?format=json')) |
[email protected] | 806e98e | 2010-03-19 17:49:27 | [diff] [blame] | 194 | results.extend(input_api.canned_checks.CheckRietveldTryJobExecution(input_api, |
| 195 | output_api, 'http://codereview.chromium.org', ('win', 'linux', 'mac'), |
| 196 | '[email protected]')) |
| 197 | |
[email protected] | 3e4eb11 | 2011-01-18 03:29:54 | [diff] [blame] | 198 | results.extend(input_api.canned_checks.CheckChangeHasBugField( |
| 199 | input_api, output_api)) |
| 200 | results.extend(input_api.canned_checks.CheckChangeHasTestField( |
| 201 | input_api, output_api)) |
[email protected] | b337cb5b | 2011-01-23 21:24:05 | [diff] [blame] | 202 | results.extend(_CheckSubversionConfig(input_api, output_api)) |
[email protected] | fe5f57c5 | 2009-06-05 14:25:54 | [diff] [blame] | 203 | return results |
[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 204 | |
| 205 | |
[email protected] | 5fa0629 | 2009-09-29 01:55:00 | [diff] [blame] | 206 | def GetPreferredTrySlaves(): |
| 207 | return ['win', 'linux', 'mac'] |