maruel@chromium.org | ba55177 | 2010-02-03 18:21:42 +0000 | [diff] [blame] | 1 | # Copyright (c) 2010 The Chromium Authors. All rights reserved. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [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 | """Generic presubmit checks that can be reused by other presubmit checks.""" |
| 6 | |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 7 | ### Description checks |
| 8 | |
kuchhal@chromium.org | 00c41e4 | 2009-05-12 21:43:13 +0000 | [diff] [blame] | 9 | def CheckChangeHasTestField(input_api, output_api): |
| 10 | """Requires that the changelist have a TEST= field.""" |
maruel@chromium.org | e1a524f | 2009-05-27 14:43:46 +0000 | [diff] [blame] | 11 | if input_api.change.TEST: |
kuchhal@chromium.org | 00c41e4 | 2009-05-12 21:43:13 +0000 | [diff] [blame] | 12 | return [] |
| 13 | else: |
| 14 | return [output_api.PresubmitNotifyResult( |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 15 | 'Changelist should have a TEST= field. TEST=none is allowed.')] |
kuchhal@chromium.org | 00c41e4 | 2009-05-12 21:43:13 +0000 | [diff] [blame] | 16 | |
| 17 | |
| 18 | def CheckChangeHasBugField(input_api, output_api): |
| 19 | """Requires that the changelist have a BUG= field.""" |
maruel@chromium.org | e1a524f | 2009-05-27 14:43:46 +0000 | [diff] [blame] | 20 | if input_api.change.BUG: |
kuchhal@chromium.org | 00c41e4 | 2009-05-12 21:43:13 +0000 | [diff] [blame] | 21 | return [] |
| 22 | else: |
| 23 | return [output_api.PresubmitNotifyResult( |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 24 | 'Changelist should have a BUG= field. BUG=none is allowed.')] |
kuchhal@chromium.org | 00c41e4 | 2009-05-12 21:43:13 +0000 | [diff] [blame] | 25 | |
| 26 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 27 | def CheckChangeHasTestedField(input_api, output_api): |
| 28 | """Requires that the changelist have a TESTED= field.""" |
maruel@chromium.org | e1a524f | 2009-05-27 14:43:46 +0000 | [diff] [blame] | 29 | if input_api.change.TESTED: |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 30 | return [] |
| 31 | else: |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 32 | return [output_api.PresubmitError('Changelist must have a TESTED= field.')] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 33 | |
| 34 | |
| 35 | def CheckChangeHasQaField(input_api, output_api): |
| 36 | """Requires that the changelist have a QA= field.""" |
| 37 | if input_api.change.QA: |
| 38 | return [] |
| 39 | else: |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 40 | return [output_api.PresubmitError('Changelist must have a QA= field.')] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 41 | |
| 42 | |
| 43 | def CheckDoNotSubmitInDescription(input_api, output_api): |
| 44 | """Checks that the user didn't add 'DO NOT ' + 'SUBMIT' to the CL description. |
| 45 | """ |
| 46 | keyword = 'DO NOT ' + 'SUBMIT' |
| 47 | if keyword in input_api.change.DescriptionText(): |
| 48 | return [output_api.PresubmitError( |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 49 | keyword + ' is present in the changelist description.')] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 50 | else: |
| 51 | return [] |
| 52 | |
| 53 | |
maruel@chromium.org | bc50eb4 | 2009-06-10 18:22:47 +0000 | [diff] [blame] | 54 | def CheckChangeHasDescription(input_api, output_api): |
| 55 | """Checks the CL description is not empty.""" |
| 56 | text = input_api.change.DescriptionText() |
| 57 | if text.strip() == '': |
| 58 | if input_api.is_committing: |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 59 | return [output_api.PresubmitError('Add a description.')] |
maruel@chromium.org | bc50eb4 | 2009-06-10 18:22:47 +0000 | [diff] [blame] | 60 | else: |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 61 | return [output_api.PresubmitNotifyResult('Add a description.')] |
maruel@chromium.org | bc50eb4 | 2009-06-10 18:22:47 +0000 | [diff] [blame] | 62 | return [] |
| 63 | |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 64 | ### Content checks |
| 65 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 66 | def CheckDoNotSubmitInFiles(input_api, output_api): |
| 67 | """Checks that the user didn't add 'DO NOT ' + 'SUBMIT' to any files.""" |
| 68 | keyword = 'DO NOT ' + 'SUBMIT' |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 69 | # We want to check every text files, not just source files. |
| 70 | for f, line_num, line in input_api.RightHandSideLines(lambda x: x): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 71 | if keyword in line: |
| 72 | text = 'Found ' + keyword + ' in %s, line %s' % (f.LocalPath(), line_num) |
| 73 | return [output_api.PresubmitError(text)] |
| 74 | return [] |
| 75 | |
| 76 | |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 77 | def CheckChangeLintsClean(input_api, output_api, source_file_filter=None): |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 78 | """Checks that all '.cc' and '.h' files pass cpplint.py.""" |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 79 | _RE_IS_TEST = input_api.re.compile(r'.*tests?.(cc|h)$') |
| 80 | result = [] |
| 81 | |
| 82 | # Initialize cpplint. |
| 83 | import cpplint |
maruel@chromium.org | b17b55b | 2010-11-03 14:42:37 +0000 | [diff] [blame] | 84 | # Access to a protected member _XX of a client class |
| 85 | # pylint: disable=W0212 |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 86 | cpplint._cpplint_state.ResetErrorCounts() |
| 87 | |
| 88 | # Justifications for each filter: |
| 89 | # |
| 90 | # - build/include : Too many; fix in the future. |
| 91 | # - build/include_order : Not happening; #ifdefed includes. |
| 92 | # - build/namespace : I'm surprised by how often we violate this rule. |
| 93 | # - readability/casting : Mistakes a whole bunch of function pointer. |
| 94 | # - runtime/int : Can be fixed long term; volume of errors too high |
| 95 | # - runtime/virtual : Broken now, but can be fixed in the future? |
| 96 | # - whitespace/braces : We have a lot of explicit scoping in chrome code. |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 97 | cpplint._SetFilters('-build/include,-build/include_order,-build/namespace,' |
| 98 | '-readability/casting,-runtime/int,-runtime/virtual,' |
| 99 | '-whitespace/braces') |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 100 | |
| 101 | # We currently are more strict with normal code than unit tests; 4 and 5 are |
| 102 | # the verbosity level that would normally be passed to cpplint.py through |
| 103 | # --verbose=#. Hopefully, in the future, we can be more verbose. |
| 104 | files = [f.AbsoluteLocalPath() for f in |
| 105 | input_api.AffectedSourceFiles(source_file_filter)] |
| 106 | for file_name in files: |
| 107 | if _RE_IS_TEST.match(file_name): |
| 108 | level = 5 |
| 109 | else: |
| 110 | level = 4 |
| 111 | |
| 112 | cpplint.ProcessFile(file_name, level) |
| 113 | |
| 114 | if cpplint._cpplint_state.error_count > 0: |
| 115 | if input_api.is_committing: |
| 116 | res_type = output_api.PresubmitError |
| 117 | else: |
| 118 | res_type = output_api.PresubmitPromptWarning |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 119 | result = [res_type('Changelist failed cpplint.py check.')] |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 120 | |
| 121 | return result |
| 122 | |
| 123 | |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 124 | def CheckChangeHasNoCR(input_api, output_api, source_file_filter=None): |
maruel@chromium.org | e9b71c9 | 2009-06-10 18:10:01 +0000 | [diff] [blame] | 125 | """Checks no '\r' (CR) character is in any source files.""" |
| 126 | cr_files = [] |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 127 | for f in input_api.AffectedSourceFiles(source_file_filter): |
maruel@chromium.org | 44a17ad | 2009-06-08 14:14:35 +0000 | [diff] [blame] | 128 | if '\r' in input_api.ReadFile(f, 'rb'): |
maruel@chromium.org | e9b71c9 | 2009-06-10 18:10:01 +0000 | [diff] [blame] | 129 | cr_files.append(f.LocalPath()) |
| 130 | if cr_files: |
| 131 | return [output_api.PresubmitPromptWarning( |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 132 | 'Found a CR character in these files:', items=cr_files)] |
maruel@chromium.org | e9b71c9 | 2009-06-10 18:10:01 +0000 | [diff] [blame] | 133 | return [] |
| 134 | |
| 135 | |
thestig@chromium.org | da8cddd | 2009-08-13 00:25:55 +0000 | [diff] [blame] | 136 | def CheckSvnModifiedDirectories(input_api, output_api, source_file_filter=None): |
| 137 | """Checks for files in svn modified directories. |
| 138 | |
| 139 | They will get submitted on accident because svn commits recursively by |
| 140 | default, and that's very dangerous. |
| 141 | """ |
| 142 | if input_api.change.scm != 'svn': |
| 143 | return [] |
| 144 | |
| 145 | errors = [] |
| 146 | current_cl_files = input_api.change.GetModifiedFiles() |
| 147 | all_modified_files = input_api.change.GetAllModifiedFiles() |
| 148 | # Filter out files in the current CL. |
| 149 | modified_files = [f for f in all_modified_files if f not in current_cl_files] |
| 150 | modified_abspaths = [input_api.os_path.abspath(f) for f in modified_files] |
| 151 | |
| 152 | for f in input_api.AffectedFiles(source_file_filter): |
| 153 | if f.Action() == 'M' and f.IsDirectory(): |
| 154 | curpath = f.AbsoluteLocalPath() |
| 155 | bad_files = [] |
| 156 | # Check if any of the modified files in other CLs are under curpath. |
| 157 | for i in xrange(len(modified_files)): |
| 158 | abspath = modified_abspaths[i] |
| 159 | if input_api.os_path.commonprefix([curpath, abspath]) == curpath: |
| 160 | bad_files.append(modified_files[i]) |
| 161 | if bad_files: |
| 162 | if input_api.is_committing: |
| 163 | error_type = output_api.PresubmitPromptWarning |
| 164 | else: |
| 165 | error_type = output_api.PresubmitNotifyResult |
| 166 | errors.append(error_type( |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 167 | 'Potential accidental commits in changelist %s:' % f.LocalPath(), |
thestig@chromium.org | da8cddd | 2009-08-13 00:25:55 +0000 | [diff] [blame] | 168 | items=bad_files)) |
| 169 | return errors |
| 170 | |
| 171 | |
maruel@chromium.org | e9b71c9 | 2009-06-10 18:10:01 +0000 | [diff] [blame] | 172 | def CheckChangeHasOnlyOneEol(input_api, output_api, source_file_filter=None): |
| 173 | """Checks the files ends with one and only one \n (LF).""" |
| 174 | eof_files = [] |
| 175 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 176 | contents = input_api.ReadFile(f, 'rb') |
| 177 | # Check that the file ends in one and only one newline character. |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 178 | if len(contents) > 1 and (contents[-1:] != '\n' or contents[-2:-1] == '\n'): |
maruel@chromium.org | e9b71c9 | 2009-06-10 18:10:01 +0000 | [diff] [blame] | 179 | eof_files.append(f.LocalPath()) |
| 180 | |
| 181 | if eof_files: |
| 182 | return [output_api.PresubmitPromptWarning( |
| 183 | 'These files should end in one (and only one) newline character:', |
| 184 | items=eof_files)] |
| 185 | return [] |
| 186 | |
| 187 | |
| 188 | def CheckChangeHasNoCrAndHasOnlyOneEol(input_api, output_api, |
| 189 | source_file_filter=None): |
| 190 | """Runs both CheckChangeHasNoCR and CheckChangeHasOnlyOneEOL in one pass. |
| 191 | |
| 192 | It is faster because it is reading the file only once. |
| 193 | """ |
| 194 | cr_files = [] |
| 195 | eof_files = [] |
| 196 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 197 | contents = input_api.ReadFile(f, 'rb') |
| 198 | if '\r' in contents: |
| 199 | cr_files.append(f.LocalPath()) |
| 200 | # Check that the file ends in one and only one newline character. |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 201 | if len(contents) > 1 and (contents[-1:] != '\n' or contents[-2:-1] == '\n'): |
maruel@chromium.org | e9b71c9 | 2009-06-10 18:10:01 +0000 | [diff] [blame] | 202 | eof_files.append(f.LocalPath()) |
| 203 | outputs = [] |
| 204 | if cr_files: |
| 205 | outputs.append(output_api.PresubmitPromptWarning( |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 206 | 'Found a CR character in these files:', items=cr_files)) |
maruel@chromium.org | e9b71c9 | 2009-06-10 18:10:01 +0000 | [diff] [blame] | 207 | if eof_files: |
| 208 | outputs.append(output_api.PresubmitPromptWarning( |
| 209 | 'These files should end in one (and only one) newline character:', |
| 210 | items=eof_files)) |
maruel@chromium.org | 44a17ad | 2009-06-08 14:14:35 +0000 | [diff] [blame] | 211 | return outputs |
| 212 | |
| 213 | |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 214 | def CheckChangeHasNoTabs(input_api, output_api, source_file_filter=None): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 215 | """Checks that there are no tab characters in any of the text files to be |
| 216 | submitted. |
| 217 | """ |
maruel@chromium.org | 115ae6c | 2010-06-18 17:11:43 +0000 | [diff] [blame] | 218 | # In addition to the filter, make sure that makefiles are blacklisted. |
| 219 | if not source_file_filter: |
| 220 | # It's the default filter. |
| 221 | source_file_filter = input_api.FilterSourceFile |
| 222 | def filter_more(affected_file): |
| 223 | return (not input_api.os_path.basename(affected_file.LocalPath()) in |
| 224 | ('Makefile', 'makefile') and |
| 225 | source_file_filter(affected_file)) |
maruel@chromium.org | e9b71c9 | 2009-06-10 18:10:01 +0000 | [diff] [blame] | 226 | tabs = [] |
maruel@chromium.org | 115ae6c | 2010-06-18 17:11:43 +0000 | [diff] [blame] | 227 | for f, line_num, line in input_api.RightHandSideLines(filter_more): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 228 | if '\t' in line: |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 229 | tabs.append('%s, line %s' % (f.LocalPath(), line_num)) |
maruel@chromium.org | e9b71c9 | 2009-06-10 18:10:01 +0000 | [diff] [blame] | 230 | if tabs: |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 231 | return [output_api.PresubmitPromptWarning('Found a tab character in:', |
| 232 | long_text='\n'.join(tabs))] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 233 | return [] |
| 234 | |
| 235 | |
maruel@chromium.org | f5888bb | 2009-06-10 20:26:37 +0000 | [diff] [blame] | 236 | def CheckChangeHasNoStrayWhitespace(input_api, output_api, |
| 237 | source_file_filter=None): |
| 238 | """Checks that there is no stray whitespace at source lines end.""" |
| 239 | errors = [] |
| 240 | for f, line_num, line in input_api.RightHandSideLines(source_file_filter): |
| 241 | if line.rstrip() != line: |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 242 | errors.append('%s, line %s' % (f.LocalPath(), line_num)) |
maruel@chromium.org | f5888bb | 2009-06-10 20:26:37 +0000 | [diff] [blame] | 243 | if errors: |
| 244 | return [output_api.PresubmitPromptWarning( |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 245 | 'Found line ending with white spaces in:', |
| 246 | long_text='\n'.join(errors))] |
maruel@chromium.org | f5888bb | 2009-06-10 20:26:37 +0000 | [diff] [blame] | 247 | return [] |
| 248 | |
| 249 | |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 250 | def CheckLongLines(input_api, output_api, maxlen=80, source_file_filter=None): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 251 | """Checks that there aren't any lines longer than maxlen characters in any of |
| 252 | the text files to be submitted. |
| 253 | """ |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 254 | bad = [] |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 255 | for f, line_num, line in input_api.RightHandSideLines(source_file_filter): |
maruel@chromium.org | 5c2720e | 2009-06-09 14:04:08 +0000 | [diff] [blame] | 256 | # Allow lines with http://, https:// and #define/#pragma/#include/#if/#endif |
| 257 | # to exceed the maxlen rule. |
| 258 | if (len(line) > maxlen and |
| 259 | not 'http://' in line and |
| 260 | not 'https://' in line and |
| 261 | not line.startswith('#define') and |
| 262 | not line.startswith('#include') and |
thakis@chromium.org | 09829bf | 2010-10-02 01:58:17 +0000 | [diff] [blame] | 263 | not line.startswith('#import') and |
maruel@chromium.org | 5c2720e | 2009-06-09 14:04:08 +0000 | [diff] [blame] | 264 | not line.startswith('#pragma') and |
| 265 | not line.startswith('#if') and |
| 266 | not line.startswith('#endif')): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 267 | bad.append( |
| 268 | '%s, line %s, %s chars' % |
maruel@chromium.org | 1487d53 | 2009-06-06 00:22:57 +0000 | [diff] [blame] | 269 | (f.LocalPath(), line_num, len(line))) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 270 | if len(bad) == 5: # Just show the first 5 errors. |
| 271 | break |
| 272 | |
| 273 | if bad: |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 274 | msg = 'Found lines longer than %s characters (first 5 shown).' % maxlen |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 275 | return [output_api.PresubmitPromptWarning(msg, items=bad)] |
| 276 | else: |
| 277 | return [] |
| 278 | |
| 279 | |
maruel@chromium.org | cb2985f | 2010-11-03 14:08:31 +0000 | [diff] [blame] | 280 | def CheckLicense(input_api, output_api, license_re, source_file_filter=None, |
maruel@chromium.org | 7162685 | 2010-11-03 13:14:25 +0000 | [diff] [blame] | 281 | accept_empty_files=True): |
maruel@chromium.org | b9e7ada | 2010-01-27 23:12:39 +0000 | [diff] [blame] | 282 | """Verifies the license header. |
| 283 | """ |
maruel@chromium.org | cb2985f | 2010-11-03 14:08:31 +0000 | [diff] [blame] | 284 | license_re = input_api.re.compile(license_re, input_api.re.MULTILINE) |
maruel@chromium.org | b9e7ada | 2010-01-27 23:12:39 +0000 | [diff] [blame] | 285 | bad_files = [] |
| 286 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 287 | contents = input_api.ReadFile(f, 'rb') |
maruel@chromium.org | 7162685 | 2010-11-03 13:14:25 +0000 | [diff] [blame] | 288 | if accept_empty_files and not contents: |
| 289 | continue |
maruel@chromium.org | b9e7ada | 2010-01-27 23:12:39 +0000 | [diff] [blame] | 290 | if not license_re.search(contents): |
| 291 | bad_files.append(f.LocalPath()) |
| 292 | if bad_files: |
| 293 | if input_api.is_committing: |
| 294 | res_type = output_api.PresubmitPromptWarning |
| 295 | else: |
| 296 | res_type = output_api.PresubmitNotifyResult |
| 297 | return [res_type( |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 298 | 'Found a bad license header in these files:', items=bad_files)] |
maruel@chromium.org | b9e7ada | 2010-01-27 23:12:39 +0000 | [diff] [blame] | 299 | return [] |
| 300 | |
| 301 | |
maruel@chromium.org | 1a0e3cb | 2009-06-10 18:03:04 +0000 | [diff] [blame] | 302 | def CheckChangeSvnEolStyle(input_api, output_api, source_file_filter=None): |
maruel@chromium.org | b7d4690 | 2009-06-10 14:12:10 +0000 | [diff] [blame] | 303 | """Checks that the source files have svn:eol-style=LF.""" |
maruel@chromium.org | 46e832a | 2009-06-18 19:58:07 +0000 | [diff] [blame] | 304 | return CheckSvnProperty(input_api, output_api, |
| 305 | 'svn:eol-style', 'LF', |
| 306 | input_api.AffectedSourceFiles(source_file_filter)) |
| 307 | |
| 308 | |
| 309 | def CheckSvnForCommonMimeTypes(input_api, output_api): |
| 310 | """Checks that common binary file types have the correct svn:mime-type.""" |
| 311 | output = [] |
| 312 | files = input_api.AffectedFiles(include_deletes=False) |
maruel@chromium.org | e49187c | 2009-06-26 22:44:53 +0000 | [diff] [blame] | 313 | def IsExts(x, exts): |
| 314 | path = x.LocalPath() |
| 315 | for extension in exts: |
| 316 | if path.endswith(extension): |
| 317 | return True |
| 318 | return False |
maruel@chromium.org | 46e832a | 2009-06-18 19:58:07 +0000 | [diff] [blame] | 319 | def FilterFiles(extension): |
maruel@chromium.org | e49187c | 2009-06-26 22:44:53 +0000 | [diff] [blame] | 320 | return filter(lambda x: IsExts(x, extension), files) |
maruel@chromium.org | 46e832a | 2009-06-18 19:58:07 +0000 | [diff] [blame] | 321 | def RunCheck(mime_type, files): |
| 322 | output.extend(CheckSvnProperty(input_api, output_api, 'svn:mime-type', |
| 323 | mime_type, files)) |
maruel@chromium.org | e49187c | 2009-06-26 22:44:53 +0000 | [diff] [blame] | 324 | RunCheck('application/pdf', FilterFiles(['.pdf'])) |
| 325 | RunCheck('image/bmp', FilterFiles(['.bmp'])) |
| 326 | RunCheck('image/gif', FilterFiles(['.gif'])) |
| 327 | RunCheck('image/png', FilterFiles(['.png'])) |
| 328 | RunCheck('image/jpeg', FilterFiles(['.jpg', '.jpeg', '.jpe'])) |
| 329 | RunCheck('image/vnd.microsoft.icon', FilterFiles(['.ico'])) |
maruel@chromium.org | 46e832a | 2009-06-18 19:58:07 +0000 | [diff] [blame] | 330 | return output |
| 331 | |
| 332 | |
| 333 | def CheckSvnProperty(input_api, output_api, prop, expected, affected_files): |
| 334 | """Checks that affected_files files have prop=expected.""" |
thestig@chromium.org | da8cddd | 2009-08-13 00:25:55 +0000 | [diff] [blame] | 335 | if input_api.change.scm != 'svn': |
| 336 | return [] |
| 337 | |
| 338 | bad = filter(lambda f: f.Property(prop) != expected, affected_files) |
maruel@chromium.org | b7d4690 | 2009-06-10 14:12:10 +0000 | [diff] [blame] | 339 | if bad: |
maruel@chromium.org | 0874d47 | 2009-06-10 19:08:33 +0000 | [diff] [blame] | 340 | if input_api.is_committing: |
maruel@chromium.org | e3608df | 2009-11-10 20:22:57 +0000 | [diff] [blame] | 341 | res_type = output_api.PresubmitError |
maruel@chromium.org | 0874d47 | 2009-06-10 19:08:33 +0000 | [diff] [blame] | 342 | else: |
maruel@chromium.org | e3608df | 2009-11-10 20:22:57 +0000 | [diff] [blame] | 343 | res_type = output_api.PresubmitNotifyResult |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 344 | message = 'Run the command: svn pset %s %s \\' % (prop, expected) |
maruel@chromium.org | e3608df | 2009-11-10 20:22:57 +0000 | [diff] [blame] | 345 | return [res_type(message, items=bad)] |
maruel@chromium.org | b7d4690 | 2009-06-10 14:12:10 +0000 | [diff] [blame] | 346 | return [] |
| 347 | |
| 348 | |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 349 | ### Other checks |
| 350 | |
| 351 | def CheckDoNotSubmit(input_api, output_api): |
| 352 | return ( |
| 353 | CheckDoNotSubmitInDescription(input_api, output_api) + |
| 354 | CheckDoNotSubmitInFiles(input_api, output_api) |
| 355 | ) |
| 356 | |
| 357 | |
bradnelson@google.com | c0b332a | 2010-08-26 00:30:37 +0000 | [diff] [blame] | 358 | def CheckTreeIsOpen(input_api, output_api, |
| 359 | url=None, closed=None, json_url=None): |
| 360 | """Check whether to allow commit without prompt. |
| 361 | |
| 362 | Supports two styles: |
| 363 | 1. Checks that an url's content doesn't match a regexp that would mean that |
| 364 | the tree is closed. (old) |
| 365 | 2. Check the json_url to decide whether to allow commit without prompt. |
| 366 | Args: |
| 367 | input_api: input related apis. |
| 368 | output_api: output related apis. |
| 369 | url: url to use for regex based tree status. |
| 370 | closed: regex to match for closed status. |
| 371 | json_url: url to download json style status. |
| 372 | """ |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 373 | if not input_api.is_committing: |
| 374 | return [] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 375 | try: |
bradnelson@google.com | c0b332a | 2010-08-26 00:30:37 +0000 | [diff] [blame] | 376 | if json_url: |
| 377 | connection = input_api.urllib2.urlopen(json_url) |
| 378 | status = input_api.json.loads(connection.read()) |
| 379 | connection.close() |
| 380 | if not status['can_commit_freely']: |
| 381 | short_text = 'Tree state is: ' + status['general_state'] |
| 382 | long_text = status['message'] + '\n' + json_url |
| 383 | return [output_api.PresubmitError(short_text, long_text=long_text)] |
| 384 | else: |
| 385 | # TODO(bradnelson): drop this once all users are gone. |
| 386 | connection = input_api.urllib2.urlopen(url) |
| 387 | status = connection.read() |
| 388 | connection.close() |
| 389 | if input_api.re.match(closed, status): |
| 390 | long_text = status + '\n' + url |
| 391 | return [output_api.PresubmitError('The tree is closed.', |
| 392 | long_text=long_text)] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 393 | except IOError: |
| 394 | pass |
| 395 | return [] |
maruel@chromium.org | 7b305e8 | 2009-05-19 18:24:20 +0000 | [diff] [blame] | 396 | |
| 397 | |
| 398 | def RunPythonUnitTests(input_api, output_api, unit_tests): |
maruel@chromium.org | c0b2297 | 2009-06-25 16:19:14 +0000 | [diff] [blame] | 399 | """Run the unit tests out of process, capture the output and use the result |
| 400 | code to determine success. |
| 401 | """ |
maruel@chromium.org | d7dccf5 | 2009-06-06 18:51:58 +0000 | [diff] [blame] | 402 | # We don't want to hinder users from uploading incomplete patches. |
| 403 | if input_api.is_committing: |
| 404 | message_type = output_api.PresubmitError |
| 405 | else: |
| 406 | message_type = output_api.PresubmitNotifyResult |
maruel@chromium.org | 7b305e8 | 2009-05-19 18:24:20 +0000 | [diff] [blame] | 407 | outputs = [] |
| 408 | for unit_test in unit_tests: |
maruel@chromium.org | c0b2297 | 2009-06-25 16:19:14 +0000 | [diff] [blame] | 409 | # Run the unit tests out of process. This is because some unit tests |
| 410 | # stub out base libraries and don't clean up their mess. It's too easy to |
| 411 | # get subtle bugs. |
| 412 | cwd = None |
| 413 | env = None |
| 414 | unit_test_name = unit_test |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 415 | # 'python -m test.unit_test' doesn't work. We need to change to the right |
maruel@chromium.org | c0b2297 | 2009-06-25 16:19:14 +0000 | [diff] [blame] | 416 | # directory instead. |
| 417 | if '.' in unit_test: |
| 418 | # Tests imported in submodules (subdirectories) assume that the current |
| 419 | # directory is in the PYTHONPATH. Manually fix that. |
| 420 | unit_test = unit_test.replace('.', '/') |
| 421 | cwd = input_api.os_path.dirname(unit_test) |
| 422 | unit_test = input_api.os_path.basename(unit_test) |
| 423 | env = input_api.environ.copy() |
kbr@google.com | ab31859 | 2009-09-04 00:54:55 +0000 | [diff] [blame] | 424 | # At least on Windows, it seems '.' must explicitly be in PYTHONPATH |
| 425 | backpath = [ |
| 426 | '.', input_api.os_path.pathsep.join(['..'] * (cwd.count('/') + 1)) |
| 427 | ] |
maruel@chromium.org | c0b2297 | 2009-06-25 16:19:14 +0000 | [diff] [blame] | 428 | if env.get('PYTHONPATH'): |
| 429 | backpath.append(env.get('PYTHONPATH')) |
ukai@chromium.org | a301f1f | 2009-08-05 10:37:33 +0000 | [diff] [blame] | 430 | env['PYTHONPATH'] = input_api.os_path.pathsep.join((backpath)) |
maruel@chromium.org | c0b2297 | 2009-06-25 16:19:14 +0000 | [diff] [blame] | 431 | subproc = input_api.subprocess.Popen( |
| 432 | [ |
| 433 | input_api.python_executable, |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 434 | '-m', |
| 435 | '%s' % unit_test |
maruel@chromium.org | c0b2297 | 2009-06-25 16:19:14 +0000 | [diff] [blame] | 436 | ], |
| 437 | cwd=cwd, |
| 438 | env=env, |
| 439 | stdin=input_api.subprocess.PIPE, |
| 440 | stdout=input_api.subprocess.PIPE, |
| 441 | stderr=input_api.subprocess.PIPE) |
| 442 | stdoutdata, stderrdata = subproc.communicate() |
| 443 | # Discard the output if returncode == 0 |
| 444 | if subproc.returncode: |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 445 | outputs.append('Test \'%s\' failed with code %d\n%s\n%s\n' % ( |
maruel@chromium.org | c0b2297 | 2009-06-25 16:19:14 +0000 | [diff] [blame] | 446 | unit_test_name, subproc.returncode, stdoutdata, stderrdata)) |
| 447 | if outputs: |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 448 | return [message_type('%d unit tests failed.' % len(outputs), |
maruel@chromium.org | c0b2297 | 2009-06-25 16:19:14 +0000 | [diff] [blame] | 449 | long_text='\n'.join(outputs))] |
| 450 | return [] |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 451 | |
| 452 | |
| 453 | def CheckRietveldTryJobExecution(input_api, output_api, host_url, platforms, |
| 454 | owner): |
| 455 | if not input_api.is_committing: |
| 456 | return [] |
| 457 | if not input_api.change.issue or not input_api.change.patchset: |
| 458 | return [] |
| 459 | url = '%s/%d/get_build_results/%d' % ( |
| 460 | host_url, input_api.change.issue, input_api.change.patchset) |
| 461 | try: |
| 462 | connection = input_api.urllib2.urlopen(url) |
| 463 | # platform|status|url |
| 464 | values = [item.split('|', 2) for item in connection.read().splitlines()] |
| 465 | connection.close() |
| 466 | except input_api.urllib2.HTTPError, e: |
| 467 | if e.code == 404: |
| 468 | # Fallback to no try job. |
| 469 | return [output_api.PresubmitPromptWarning( |
| 470 | 'You should try the patch first.')] |
| 471 | else: |
| 472 | # Another HTTP error happened, warn the user. |
| 473 | return [output_api.PresubmitPromptWarning( |
| 474 | 'Got %s while looking for try job status.' % str(e))] |
| 475 | |
| 476 | if not values: |
| 477 | # It returned an empty list. Probably a private review. |
| 478 | return [] |
| 479 | # Reformat as an dict of platform: [status, url] |
| 480 | values = dict([[v[0], [v[1], v[2]]] for v in values if len(v) == 3]) |
| 481 | if not values: |
| 482 | # It returned useless data. |
| 483 | return [output_api.PresubmitNotifyResult('Failed to parse try job results')] |
| 484 | |
| 485 | for platform in platforms: |
| 486 | values.setdefault(platform, ['not started', '']) |
| 487 | message = None |
maruel@chromium.org | cb2985f | 2010-11-03 14:08:31 +0000 | [diff] [blame] | 488 | non_success = [k.upper() for k, v in values.iteritems() if v[0] != 'success'] |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 489 | if 'failure' in [v[0] for v in values.itervalues()]: |
| 490 | message = 'Try job failures on %s!\n' % ', '.join(non_success) |
| 491 | elif non_success: |
| 492 | message = ('Unfinished (or not even started) try jobs on ' |
| 493 | '%s.\n') % ', '.join(non_success) |
| 494 | if message: |
| 495 | message += ( |
| 496 | 'Is try server wrong or broken? Please notify %s. ' |
| 497 | 'Thanks.\n' % owner) |
| 498 | return [output_api.PresubmitPromptWarning(message=message)] |
| 499 | return [] |
| 500 | |
| 501 | |
| 502 | def CheckBuildbotPendingBuilds(input_api, output_api, url, max_pendings, |
| 503 | ignored): |
| 504 | if not input_api.json: |
| 505 | return [output_api.PresubmitPromptWarning( |
| 506 | 'Please install simplejson or upgrade to python 2.6+')] |
| 507 | try: |
| 508 | connection = input_api.urllib2.urlopen(url) |
| 509 | raw_data = connection.read() |
| 510 | connection.close() |
| 511 | except IOError: |
| 512 | return [output_api.PresubmitNotifyResult('%s is not accessible' % url)] |
| 513 | |
| 514 | try: |
| 515 | data = input_api.json.loads(raw_data) |
| 516 | except ValueError: |
| 517 | return [output_api.PresubmitNotifyResult('Received malformed json while ' |
| 518 | 'looking up buildbot status')] |
| 519 | |
| 520 | out = [] |
| 521 | for (builder_name, builder) in data.iteritems(): |
| 522 | if builder_name in ignored: |
| 523 | continue |
maruel@chromium.org | cf1982c | 2010-10-04 15:08:28 +0000 | [diff] [blame] | 524 | if builder.get('state', '') == 'offline': |
| 525 | continue |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 526 | pending_builds_len = len(builder.get('pending_builds', [])) |
| 527 | if pending_builds_len > max_pendings: |
| 528 | out.append('%s has %d build(s) pending' % |
| 529 | (builder_name, pending_builds_len)) |
| 530 | if out: |
| 531 | return [output_api.PresubmitPromptWarning( |
| 532 | 'Build(s) pending. It is suggested to wait that no more than %d ' |
| 533 | 'builds are pending.' % max_pendings, |
| 534 | long_text='\n'.join(out))] |
| 535 | return [] |