estade@chromium.org | ae7af92 | 2012-01-27 14:51:13 +0000 | [diff] [blame] | 1 | # Copyright (c) 2012 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 | ff9a217 | 2012-04-24 16:55:32 +0000 | [diff] [blame] | 7 | import os as _os |
| 8 | _HERE = _os.path.dirname(_os.path.abspath(__file__)) |
| 9 | |
bradnelson@google.com | 56e48bc | 2011-03-24 20:51:21 +0000 | [diff] [blame] | 10 | |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 11 | ### Description checks |
| 12 | |
kuchhal@chromium.org | 00c41e4 | 2009-05-12 21:43:13 +0000 | [diff] [blame] | 13 | def CheckChangeHasTestField(input_api, output_api): |
| 14 | """Requires that the changelist have a TEST= field.""" |
maruel@chromium.org | e1a524f | 2009-05-27 14:43:46 +0000 | [diff] [blame] | 15 | if input_api.change.TEST: |
kuchhal@chromium.org | 00c41e4 | 2009-05-12 21:43:13 +0000 | [diff] [blame] | 16 | return [] |
| 17 | else: |
| 18 | return [output_api.PresubmitNotifyResult( |
jam@chromium.org | 5c76de9 | 2011-01-24 18:19:21 +0000 | [diff] [blame] | 19 | 'If this change requires manual test instructions to QA team, add ' |
| 20 | 'TEST=[instructions].')] |
kuchhal@chromium.org | 00c41e4 | 2009-05-12 21:43:13 +0000 | [diff] [blame] | 21 | |
| 22 | |
| 23 | def CheckChangeHasBugField(input_api, output_api): |
| 24 | """Requires that the changelist have a BUG= field.""" |
maruel@chromium.org | e1a524f | 2009-05-27 14:43:46 +0000 | [diff] [blame] | 25 | if input_api.change.BUG: |
kuchhal@chromium.org | 00c41e4 | 2009-05-12 21:43:13 +0000 | [diff] [blame] | 26 | return [] |
| 27 | else: |
| 28 | return [output_api.PresubmitNotifyResult( |
jam@chromium.org | 5c76de9 | 2011-01-24 18:19:21 +0000 | [diff] [blame] | 29 | 'If this change has an associated bug, add BUG=[bug number].')] |
kuchhal@chromium.org | 00c41e4 | 2009-05-12 21:43:13 +0000 | [diff] [blame] | 30 | |
| 31 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 32 | def CheckChangeHasTestedField(input_api, output_api): |
| 33 | """Requires that the changelist have a TESTED= field.""" |
maruel@chromium.org | e1a524f | 2009-05-27 14:43:46 +0000 | [diff] [blame] | 34 | if input_api.change.TESTED: |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 35 | return [] |
| 36 | else: |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 37 | return [output_api.PresubmitError('Changelist must have a TESTED= field.')] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 38 | |
| 39 | |
| 40 | def CheckChangeHasQaField(input_api, output_api): |
| 41 | """Requires that the changelist have a QA= field.""" |
| 42 | if input_api.change.QA: |
| 43 | return [] |
| 44 | else: |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 45 | return [output_api.PresubmitError('Changelist must have a QA= field.')] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 46 | |
| 47 | |
| 48 | def CheckDoNotSubmitInDescription(input_api, output_api): |
ilevy@chromium.org | c50d761 | 2012-12-05 04:25:14 +0000 | [diff] [blame] | 49 | """Checks that the user didn't add 'DO NOT ''SUBMIT' to the CL description. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 50 | """ |
ilevy@chromium.org | c50d761 | 2012-12-05 04:25:14 +0000 | [diff] [blame] | 51 | keyword = 'DO NOT ''SUBMIT' |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 52 | if keyword in input_api.change.DescriptionText(): |
| 53 | return [output_api.PresubmitError( |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 54 | keyword + ' is present in the changelist description.')] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 55 | else: |
| 56 | return [] |
| 57 | |
| 58 | |
maruel@chromium.org | bc50eb4 | 2009-06-10 18:22:47 +0000 | [diff] [blame] | 59 | def CheckChangeHasDescription(input_api, output_api): |
| 60 | """Checks the CL description is not empty.""" |
| 61 | text = input_api.change.DescriptionText() |
| 62 | if text.strip() == '': |
| 63 | if input_api.is_committing: |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 64 | return [output_api.PresubmitError('Add a description.')] |
maruel@chromium.org | bc50eb4 | 2009-06-10 18:22:47 +0000 | [diff] [blame] | 65 | else: |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 66 | return [output_api.PresubmitNotifyResult('Add a description.')] |
maruel@chromium.org | bc50eb4 | 2009-06-10 18:22:47 +0000 | [diff] [blame] | 67 | return [] |
| 68 | |
maruel@chromium.org | cc73ad6 | 2011-07-06 17:39:26 +0000 | [diff] [blame] | 69 | |
| 70 | def CheckChangeWasUploaded(input_api, output_api): |
| 71 | """Checks that the issue was uploaded before committing.""" |
maruel@chromium.org | d587f39 | 2011-07-26 00:41:18 +0000 | [diff] [blame] | 72 | if input_api.is_committing and not input_api.change.issue: |
maruel@chromium.org | cc73ad6 | 2011-07-06 17:39:26 +0000 | [diff] [blame] | 73 | return [output_api.PresubmitError( |
| 74 | 'Issue wasn\'t uploaded. Please upload first.')] |
| 75 | return [] |
| 76 | |
| 77 | |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 78 | ### Content checks |
| 79 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 80 | def CheckDoNotSubmitInFiles(input_api, output_api): |
ilevy@chromium.org | c50d761 | 2012-12-05 04:25:14 +0000 | [diff] [blame] | 81 | """Checks that the user didn't add 'DO NOT ''SUBMIT' to any files.""" |
nick@chromium.org | e06cb4e | 2011-04-23 01:20:38 +0000 | [diff] [blame] | 82 | # We want to check every text file, not just source files. |
| 83 | file_filter = lambda x : x |
ilevy@chromium.org | c50d761 | 2012-12-05 04:25:14 +0000 | [diff] [blame] | 84 | keyword = 'DO NOT ''SUBMIT' |
bulach@chromium.org | bfffd45 | 2012-02-22 01:13:29 +0000 | [diff] [blame] | 85 | errors = _FindNewViolationsOfRule(lambda _, line : keyword not in line, |
nick@chromium.org | e06cb4e | 2011-04-23 01:20:38 +0000 | [diff] [blame] | 86 | input_api, file_filter) |
| 87 | text = '\n'.join('Found %s in %s' % (keyword, loc) for loc in errors) |
| 88 | if text: |
| 89 | return [output_api.PresubmitError(text)] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 90 | return [] |
| 91 | |
| 92 | |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 93 | def CheckChangeLintsClean(input_api, output_api, source_file_filter=None): |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 94 | """Checks that all '.cc' and '.h' files pass cpplint.py.""" |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 95 | _RE_IS_TEST = input_api.re.compile(r'.*tests?.(cc|h)$') |
| 96 | result = [] |
| 97 | |
enne@chromium.org | e72c5f5 | 2013-04-16 00:36:40 +0000 | [diff] [blame] | 98 | cpplint = input_api.cpplint |
maruel@chromium.org | b17b55b | 2010-11-03 14:42:37 +0000 | [diff] [blame] | 99 | # Access to a protected member _XX of a client class |
| 100 | # pylint: disable=W0212 |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 101 | cpplint._cpplint_state.ResetErrorCounts() |
| 102 | |
| 103 | # Justifications for each filter: |
| 104 | # |
| 105 | # - build/include : Too many; fix in the future. |
| 106 | # - build/include_order : Not happening; #ifdefed includes. |
| 107 | # - build/namespace : I'm surprised by how often we violate this rule. |
| 108 | # - readability/casting : Mistakes a whole bunch of function pointer. |
| 109 | # - runtime/int : Can be fixed long term; volume of errors too high |
| 110 | # - runtime/virtual : Broken now, but can be fixed in the future? |
| 111 | # - 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] | 112 | cpplint._SetFilters('-build/include,-build/include_order,-build/namespace,' |
| 113 | '-readability/casting,-runtime/int,-runtime/virtual,' |
| 114 | '-whitespace/braces') |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 115 | |
| 116 | # We currently are more strict with normal code than unit tests; 4 and 5 are |
| 117 | # the verbosity level that would normally be passed to cpplint.py through |
| 118 | # --verbose=#. Hopefully, in the future, we can be more verbose. |
| 119 | files = [f.AbsoluteLocalPath() for f in |
| 120 | input_api.AffectedSourceFiles(source_file_filter)] |
| 121 | for file_name in files: |
| 122 | if _RE_IS_TEST.match(file_name): |
| 123 | level = 5 |
| 124 | else: |
| 125 | level = 4 |
| 126 | |
| 127 | cpplint.ProcessFile(file_name, level) |
| 128 | |
| 129 | if cpplint._cpplint_state.error_count > 0: |
| 130 | if input_api.is_committing: |
| 131 | res_type = output_api.PresubmitError |
| 132 | else: |
| 133 | res_type = output_api.PresubmitPromptWarning |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 134 | result = [res_type('Changelist failed cpplint.py check.')] |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 135 | |
| 136 | return result |
| 137 | |
| 138 | |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 139 | def CheckChangeHasNoCR(input_api, output_api, source_file_filter=None): |
maruel@chromium.org | e9b71c9 | 2009-06-10 18:10:01 +0000 | [diff] [blame] | 140 | """Checks no '\r' (CR) character is in any source files.""" |
| 141 | cr_files = [] |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 142 | for f in input_api.AffectedSourceFiles(source_file_filter): |
maruel@chromium.org | 44a17ad | 2009-06-08 14:14:35 +0000 | [diff] [blame] | 143 | if '\r' in input_api.ReadFile(f, 'rb'): |
maruel@chromium.org | e9b71c9 | 2009-06-10 18:10:01 +0000 | [diff] [blame] | 144 | cr_files.append(f.LocalPath()) |
| 145 | if cr_files: |
| 146 | return [output_api.PresubmitPromptWarning( |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 147 | 'Found a CR character in these files:', items=cr_files)] |
maruel@chromium.org | e9b71c9 | 2009-06-10 18:10:01 +0000 | [diff] [blame] | 148 | return [] |
| 149 | |
| 150 | |
thestig@chromium.org | da8cddd | 2009-08-13 00:25:55 +0000 | [diff] [blame] | 151 | def CheckSvnModifiedDirectories(input_api, output_api, source_file_filter=None): |
| 152 | """Checks for files in svn modified directories. |
| 153 | |
| 154 | They will get submitted on accident because svn commits recursively by |
| 155 | default, and that's very dangerous. |
| 156 | """ |
| 157 | if input_api.change.scm != 'svn': |
| 158 | return [] |
| 159 | |
| 160 | errors = [] |
| 161 | current_cl_files = input_api.change.GetModifiedFiles() |
| 162 | all_modified_files = input_api.change.GetAllModifiedFiles() |
| 163 | # Filter out files in the current CL. |
| 164 | modified_files = [f for f in all_modified_files if f not in current_cl_files] |
| 165 | modified_abspaths = [input_api.os_path.abspath(f) for f in modified_files] |
| 166 | |
sail@chromium.org | 5538e02 | 2011-05-12 17:53:16 +0000 | [diff] [blame] | 167 | for f in input_api.AffectedFiles(file_filter=source_file_filter): |
thestig@chromium.org | da8cddd | 2009-08-13 00:25:55 +0000 | [diff] [blame] | 168 | if f.Action() == 'M' and f.IsDirectory(): |
| 169 | curpath = f.AbsoluteLocalPath() |
| 170 | bad_files = [] |
| 171 | # Check if any of the modified files in other CLs are under curpath. |
| 172 | for i in xrange(len(modified_files)): |
| 173 | abspath = modified_abspaths[i] |
| 174 | if input_api.os_path.commonprefix([curpath, abspath]) == curpath: |
| 175 | bad_files.append(modified_files[i]) |
| 176 | if bad_files: |
| 177 | if input_api.is_committing: |
| 178 | error_type = output_api.PresubmitPromptWarning |
| 179 | else: |
| 180 | error_type = output_api.PresubmitNotifyResult |
| 181 | errors.append(error_type( |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 182 | 'Potential accidental commits in changelist %s:' % f.LocalPath(), |
thestig@chromium.org | da8cddd | 2009-08-13 00:25:55 +0000 | [diff] [blame] | 183 | items=bad_files)) |
| 184 | return errors |
| 185 | |
| 186 | |
maruel@chromium.org | e9b71c9 | 2009-06-10 18:10:01 +0000 | [diff] [blame] | 187 | def CheckChangeHasOnlyOneEol(input_api, output_api, source_file_filter=None): |
| 188 | """Checks the files ends with one and only one \n (LF).""" |
| 189 | eof_files = [] |
| 190 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 191 | contents = input_api.ReadFile(f, 'rb') |
| 192 | # 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] | 193 | 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] | 194 | eof_files.append(f.LocalPath()) |
| 195 | |
| 196 | if eof_files: |
| 197 | return [output_api.PresubmitPromptWarning( |
| 198 | 'These files should end in one (and only one) newline character:', |
| 199 | items=eof_files)] |
| 200 | return [] |
| 201 | |
| 202 | |
| 203 | def CheckChangeHasNoCrAndHasOnlyOneEol(input_api, output_api, |
| 204 | source_file_filter=None): |
| 205 | """Runs both CheckChangeHasNoCR and CheckChangeHasOnlyOneEOL in one pass. |
| 206 | |
| 207 | It is faster because it is reading the file only once. |
| 208 | """ |
| 209 | cr_files = [] |
| 210 | eof_files = [] |
| 211 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 212 | contents = input_api.ReadFile(f, 'rb') |
| 213 | if '\r' in contents: |
| 214 | cr_files.append(f.LocalPath()) |
| 215 | # 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] | 216 | 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] | 217 | eof_files.append(f.LocalPath()) |
| 218 | outputs = [] |
| 219 | if cr_files: |
| 220 | outputs.append(output_api.PresubmitPromptWarning( |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 221 | 'Found a CR character in these files:', items=cr_files)) |
maruel@chromium.org | e9b71c9 | 2009-06-10 18:10:01 +0000 | [diff] [blame] | 222 | if eof_files: |
| 223 | outputs.append(output_api.PresubmitPromptWarning( |
| 224 | 'These files should end in one (and only one) newline character:', |
| 225 | items=eof_files)) |
maruel@chromium.org | 44a17ad | 2009-06-08 14:14:35 +0000 | [diff] [blame] | 226 | return outputs |
| 227 | |
| 228 | |
chrisha@google.com | 267d659 | 2012-06-19 19:23:31 +0000 | [diff] [blame] | 229 | def _ReportErrorFileAndLine(filename, line_num, dummy_line): |
nick@chromium.org | e06cb4e | 2011-04-23 01:20:38 +0000 | [diff] [blame] | 230 | """Default error formatter for _FindNewViolationsOfRule.""" |
danakj@chromium.org | c5965ba | 2013-08-14 00:27:24 +0000 | [diff] [blame] | 231 | return '%s:%s' % (filename, line_num) |
nick@chromium.org | e06cb4e | 2011-04-23 01:20:38 +0000 | [diff] [blame] | 232 | |
| 233 | |
| 234 | def _FindNewViolationsOfRule(callable_rule, input_api, source_file_filter=None, |
| 235 | error_formatter=_ReportErrorFileAndLine): |
| 236 | """Find all newly introduced violations of a per-line rule (a callable). |
| 237 | |
| 238 | Arguments: |
bulach@chromium.org | bfffd45 | 2012-02-22 01:13:29 +0000 | [diff] [blame] | 239 | callable_rule: a callable taking a file extension and line of input and |
| 240 | returning True if the rule is satisfied and False if there was a problem. |
nick@chromium.org | e06cb4e | 2011-04-23 01:20:38 +0000 | [diff] [blame] | 241 | input_api: object to enumerate the affected files. |
| 242 | source_file_filter: a filter to be passed to the input api. |
| 243 | error_formatter: a callable taking (filename, line_number, line) and |
| 244 | returning a formatted error string. |
| 245 | |
| 246 | Returns: |
| 247 | A list of the newly-introduced violations reported by the rule. |
| 248 | """ |
| 249 | errors = [] |
sail@chromium.org | 5538e02 | 2011-05-12 17:53:16 +0000 | [diff] [blame] | 250 | for f in input_api.AffectedFiles(include_deletes=False, |
| 251 | file_filter=source_file_filter): |
nick@chromium.org | e06cb4e | 2011-04-23 01:20:38 +0000 | [diff] [blame] | 252 | # For speed, we do two passes, checking first the full file. Shelling out |
| 253 | # to the SCM to determine the changed region can be quite expensive on |
| 254 | # Win32. Assuming that most files will be kept problem-free, we can |
| 255 | # skip the SCM operations most of the time. |
bulach@chromium.org | bfffd45 | 2012-02-22 01:13:29 +0000 | [diff] [blame] | 256 | extension = str(f.LocalPath()).rsplit('.', 1)[-1] |
| 257 | if all(callable_rule(extension, line) for line in f.NewContents()): |
nick@chromium.org | e06cb4e | 2011-04-23 01:20:38 +0000 | [diff] [blame] | 258 | continue # No violation found in full text: can skip considering diff. |
| 259 | |
| 260 | for line_num, line in f.ChangedContents(): |
bulach@chromium.org | bfffd45 | 2012-02-22 01:13:29 +0000 | [diff] [blame] | 261 | if not callable_rule(extension, line): |
nick@chromium.org | e06cb4e | 2011-04-23 01:20:38 +0000 | [diff] [blame] | 262 | errors.append(error_formatter(f.LocalPath(), line_num, line)) |
| 263 | |
| 264 | return errors |
| 265 | |
| 266 | |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 267 | def CheckChangeHasNoTabs(input_api, output_api, source_file_filter=None): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 268 | """Checks that there are no tab characters in any of the text files to be |
| 269 | submitted. |
| 270 | """ |
maruel@chromium.org | 115ae6c | 2010-06-18 17:11:43 +0000 | [diff] [blame] | 271 | # In addition to the filter, make sure that makefiles are blacklisted. |
| 272 | if not source_file_filter: |
| 273 | # It's the default filter. |
| 274 | source_file_filter = input_api.FilterSourceFile |
| 275 | def filter_more(affected_file): |
cmp@chromium.org | cb5e57c | 2012-04-06 19:50:15 +0000 | [diff] [blame] | 276 | basename = input_api.os_path.basename(affected_file.LocalPath()) |
| 277 | return (not (basename in ('Makefile', 'makefile') or |
| 278 | basename.endswith('.mk')) and |
maruel@chromium.org | 115ae6c | 2010-06-18 17:11:43 +0000 | [diff] [blame] | 279 | source_file_filter(affected_file)) |
nick@chromium.org | e06cb4e | 2011-04-23 01:20:38 +0000 | [diff] [blame] | 280 | |
bulach@chromium.org | bfffd45 | 2012-02-22 01:13:29 +0000 | [diff] [blame] | 281 | tabs = _FindNewViolationsOfRule(lambda _, line : '\t' not in line, |
nick@chromium.org | e06cb4e | 2011-04-23 01:20:38 +0000 | [diff] [blame] | 282 | input_api, filter_more) |
| 283 | |
maruel@chromium.org | e9b71c9 | 2009-06-10 18:10:01 +0000 | [diff] [blame] | 284 | if tabs: |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 285 | return [output_api.PresubmitPromptWarning('Found a tab character in:', |
| 286 | long_text='\n'.join(tabs))] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 287 | return [] |
| 288 | |
| 289 | |
estade@chromium.org | fdcc9f7 | 2011-02-07 22:25:07 +0000 | [diff] [blame] | 290 | def CheckChangeTodoHasOwner(input_api, output_api, source_file_filter=None): |
| 291 | """Checks that the user didn't add TODO(name) without an owner.""" |
| 292 | |
ilevy@chromium.org | c50d761 | 2012-12-05 04:25:14 +0000 | [diff] [blame] | 293 | unowned_todo = input_api.re.compile('TO''DO[^(]') |
bulach@chromium.org | bfffd45 | 2012-02-22 01:13:29 +0000 | [diff] [blame] | 294 | errors = _FindNewViolationsOfRule(lambda _, x : not unowned_todo.search(x), |
nick@chromium.org | e06cb4e | 2011-04-23 01:20:38 +0000 | [diff] [blame] | 295 | input_api, source_file_filter) |
ilevy@chromium.org | c50d761 | 2012-12-05 04:25:14 +0000 | [diff] [blame] | 296 | errors = ['Found TO''DO with no owner in ' + x for x in errors] |
nick@chromium.org | e06cb4e | 2011-04-23 01:20:38 +0000 | [diff] [blame] | 297 | if errors: |
| 298 | return [output_api.PresubmitPromptWarning('\n'.join(errors))] |
estade@chromium.org | fdcc9f7 | 2011-02-07 22:25:07 +0000 | [diff] [blame] | 299 | return [] |
| 300 | |
| 301 | |
maruel@chromium.org | f5888bb | 2009-06-10 20:26:37 +0000 | [diff] [blame] | 302 | def CheckChangeHasNoStrayWhitespace(input_api, output_api, |
| 303 | source_file_filter=None): |
| 304 | """Checks that there is no stray whitespace at source lines end.""" |
bulach@chromium.org | bfffd45 | 2012-02-22 01:13:29 +0000 | [diff] [blame] | 305 | errors = _FindNewViolationsOfRule(lambda _, line : line.rstrip() == line, |
nick@chromium.org | e06cb4e | 2011-04-23 01:20:38 +0000 | [diff] [blame] | 306 | input_api, source_file_filter) |
maruel@chromium.org | f5888bb | 2009-06-10 20:26:37 +0000 | [diff] [blame] | 307 | if errors: |
| 308 | return [output_api.PresubmitPromptWarning( |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 309 | 'Found line ending with white spaces in:', |
| 310 | long_text='\n'.join(errors))] |
maruel@chromium.org | f5888bb | 2009-06-10 20:26:37 +0000 | [diff] [blame] | 311 | return [] |
| 312 | |
| 313 | |
jamesr@chromium.org | af27f46 | 2013-04-04 21:44:22 +0000 | [diff] [blame] | 314 | def CheckLongLines(input_api, output_api, maxlen, source_file_filter=None): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 315 | """Checks that there aren't any lines longer than maxlen characters in any of |
| 316 | the text files to be submitted. |
| 317 | """ |
dpranke@chromium.org | e3b1c3d | 2012-10-20 22:28:14 +0000 | [diff] [blame] | 318 | maxlens = { |
| 319 | 'java': 100, |
torne@chromium.org | 0ecad9c | 2013-02-15 16:35:16 +0000 | [diff] [blame] | 320 | # This is specifically for Android's handwritten makefiles (Android.mk). |
| 321 | 'mk': 200, |
bulach@chromium.org | bfffd45 | 2012-02-22 01:13:29 +0000 | [diff] [blame] | 322 | '': maxlen, |
| 323 | } |
maruel@chromium.org | eba6462 | 2011-06-20 18:26:48 +0000 | [diff] [blame] | 324 | |
erikchen@google.com | 1281608 | 2013-12-03 02:04:20 +0000 | [diff] [blame] | 325 | # Language specific exceptions to max line length. |
| 326 | # '.h' is considered an obj-c file extension, since OBJC_EXCEPTIONS are a |
| 327 | # superset of CPP_EXCEPTIONS. |
| 328 | CPP_FILE_EXTS = ('c', 'cc') |
| 329 | CPP_EXCEPTIONS = ('#define', '#endif', '#if', '#include', '#pragma') |
| 330 | JAVA_FILE_EXTS = ('java',) |
| 331 | JAVA_EXCEPTIONS = ('import ', 'package ') |
| 332 | OBJC_FILE_EXTS = ('h', 'm', 'mm') |
| 333 | OBJC_EXCEPTIONS = ('#define', '#endif', '#if', '#import', '#include', |
| 334 | '#pragma') |
| 335 | |
| 336 | LANGUAGE_EXCEPTIONS = [ |
| 337 | (CPP_FILE_EXTS, CPP_EXCEPTIONS), |
| 338 | (JAVA_FILE_EXTS, JAVA_EXCEPTIONS), |
| 339 | (OBJC_FILE_EXTS, OBJC_EXCEPTIONS), |
| 340 | ] |
sivachandra@chromium.org | 270db5c | 2012-10-09 22:38:44 +0000 | [diff] [blame] | 341 | |
bulach@chromium.org | bfffd45 | 2012-02-22 01:13:29 +0000 | [diff] [blame] | 342 | def no_long_lines(file_extension, line): |
erikchen@google.com | 1281608 | 2013-12-03 02:04:20 +0000 | [diff] [blame] | 343 | # Check for language specific exceptions. |
| 344 | if any(file_extension in exts and line.startswith(exceptions) |
| 345 | for exts, exceptions in LANGUAGE_EXCEPTIONS): |
sivachandra@chromium.org | 270db5c | 2012-10-09 22:38:44 +0000 | [diff] [blame] | 346 | return True |
| 347 | |
bulach@chromium.org | bfffd45 | 2012-02-22 01:13:29 +0000 | [diff] [blame] | 348 | file_maxlen = maxlens.get(file_extension, maxlens['']) |
| 349 | # Stupidly long symbols that needs to be worked around if takes 66% of line. |
| 350 | long_symbol = file_maxlen * 2 / 3 |
| 351 | # Hard line length limit at 50% more. |
| 352 | extra_maxlen = file_maxlen * 3 / 2 |
| 353 | |
| 354 | line_len = len(line) |
| 355 | if line_len <= file_maxlen: |
maruel@chromium.org | eba6462 | 2011-06-20 18:26:48 +0000 | [diff] [blame] | 356 | return True |
| 357 | |
bulach@chromium.org | bfffd45 | 2012-02-22 01:13:29 +0000 | [diff] [blame] | 358 | if line_len > extra_maxlen: |
maruel@chromium.org | eba6462 | 2011-06-20 18:26:48 +0000 | [diff] [blame] | 359 | return False |
| 360 | |
| 361 | return ( |
maruel@chromium.org | eba6462 | 2011-06-20 18:26:48 +0000 | [diff] [blame] | 362 | any((url in line) for url in ('http://', 'https://')) or |
| 363 | input_api.re.match( |
| 364 | r'.*[A-Za-z][A-Za-z_0-9]{%d,}.*' % long_symbol, line)) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 365 | |
nick@chromium.org | e06cb4e | 2011-04-23 01:20:38 +0000 | [diff] [blame] | 366 | def format_error(filename, line_num, line): |
| 367 | return '%s, line %s, %s chars' % (filename, line_num, len(line)) |
| 368 | |
| 369 | errors = _FindNewViolationsOfRule(no_long_lines, input_api, |
| 370 | source_file_filter, |
| 371 | error_formatter=format_error) |
| 372 | if errors: |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 373 | msg = 'Found lines longer than %s characters (first 5 shown).' % maxlen |
nick@chromium.org | e06cb4e | 2011-04-23 01:20:38 +0000 | [diff] [blame] | 374 | return [output_api.PresubmitPromptWarning(msg, items=errors[:5])] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 375 | else: |
| 376 | return [] |
| 377 | |
| 378 | |
maruel@chromium.org | cb2985f | 2010-11-03 14:08:31 +0000 | [diff] [blame] | 379 | 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] | 380 | accept_empty_files=True): |
maruel@chromium.org | b9e7ada | 2010-01-27 23:12:39 +0000 | [diff] [blame] | 381 | """Verifies the license header. |
| 382 | """ |
maruel@chromium.org | cb2985f | 2010-11-03 14:08:31 +0000 | [diff] [blame] | 383 | 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] | 384 | bad_files = [] |
| 385 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 386 | contents = input_api.ReadFile(f, 'rb') |
maruel@chromium.org | 7162685 | 2010-11-03 13:14:25 +0000 | [diff] [blame] | 387 | if accept_empty_files and not contents: |
| 388 | continue |
maruel@chromium.org | b9e7ada | 2010-01-27 23:12:39 +0000 | [diff] [blame] | 389 | if not license_re.search(contents): |
| 390 | bad_files.append(f.LocalPath()) |
| 391 | if bad_files: |
| 392 | if input_api.is_committing: |
| 393 | res_type = output_api.PresubmitPromptWarning |
| 394 | else: |
| 395 | res_type = output_api.PresubmitNotifyResult |
| 396 | return [res_type( |
bradnelson@google.com | 393460b | 2011-03-29 01:20:26 +0000 | [diff] [blame] | 397 | 'License must match:\n%s\n' % license_re.pattern + |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 398 | 'Found a bad license header in these files:', items=bad_files)] |
maruel@chromium.org | b9e7ada | 2010-01-27 23:12:39 +0000 | [diff] [blame] | 399 | return [] |
| 400 | |
| 401 | |
maruel@chromium.org | 1a0e3cb | 2009-06-10 18:03:04 +0000 | [diff] [blame] | 402 | def CheckChangeSvnEolStyle(input_api, output_api, source_file_filter=None): |
maruel@chromium.org | b7d4690 | 2009-06-10 14:12:10 +0000 | [diff] [blame] | 403 | """Checks that the source files have svn:eol-style=LF.""" |
maruel@chromium.org | 46e832a | 2009-06-18 19:58:07 +0000 | [diff] [blame] | 404 | return CheckSvnProperty(input_api, output_api, |
| 405 | 'svn:eol-style', 'LF', |
| 406 | input_api.AffectedSourceFiles(source_file_filter)) |
| 407 | |
| 408 | |
| 409 | def CheckSvnForCommonMimeTypes(input_api, output_api): |
| 410 | """Checks that common binary file types have the correct svn:mime-type.""" |
| 411 | output = [] |
| 412 | files = input_api.AffectedFiles(include_deletes=False) |
maruel@chromium.org | e49187c | 2009-06-26 22:44:53 +0000 | [diff] [blame] | 413 | def IsExts(x, exts): |
| 414 | path = x.LocalPath() |
| 415 | for extension in exts: |
| 416 | if path.endswith(extension): |
| 417 | return True |
| 418 | return False |
maruel@chromium.org | 46e832a | 2009-06-18 19:58:07 +0000 | [diff] [blame] | 419 | def FilterFiles(extension): |
maruel@chromium.org | e49187c | 2009-06-26 22:44:53 +0000 | [diff] [blame] | 420 | return filter(lambda x: IsExts(x, extension), files) |
maruel@chromium.org | 46e832a | 2009-06-18 19:58:07 +0000 | [diff] [blame] | 421 | def RunCheck(mime_type, files): |
| 422 | output.extend(CheckSvnProperty(input_api, output_api, 'svn:mime-type', |
| 423 | mime_type, files)) |
maruel@chromium.org | e49187c | 2009-06-26 22:44:53 +0000 | [diff] [blame] | 424 | RunCheck('application/pdf', FilterFiles(['.pdf'])) |
| 425 | RunCheck('image/bmp', FilterFiles(['.bmp'])) |
| 426 | RunCheck('image/gif', FilterFiles(['.gif'])) |
| 427 | RunCheck('image/png', FilterFiles(['.png'])) |
| 428 | RunCheck('image/jpeg', FilterFiles(['.jpg', '.jpeg', '.jpe'])) |
| 429 | RunCheck('image/vnd.microsoft.icon', FilterFiles(['.ico'])) |
maruel@chromium.org | 46e832a | 2009-06-18 19:58:07 +0000 | [diff] [blame] | 430 | return output |
| 431 | |
| 432 | |
| 433 | def CheckSvnProperty(input_api, output_api, prop, expected, affected_files): |
| 434 | """Checks that affected_files files have prop=expected.""" |
thestig@chromium.org | da8cddd | 2009-08-13 00:25:55 +0000 | [diff] [blame] | 435 | if input_api.change.scm != 'svn': |
| 436 | return [] |
| 437 | |
| 438 | bad = filter(lambda f: f.Property(prop) != expected, affected_files) |
maruel@chromium.org | b7d4690 | 2009-06-10 14:12:10 +0000 | [diff] [blame] | 439 | if bad: |
maruel@chromium.org | 0874d47 | 2009-06-10 19:08:33 +0000 | [diff] [blame] | 440 | if input_api.is_committing: |
maruel@chromium.org | e3608df | 2009-11-10 20:22:57 +0000 | [diff] [blame] | 441 | res_type = output_api.PresubmitError |
maruel@chromium.org | 0874d47 | 2009-06-10 19:08:33 +0000 | [diff] [blame] | 442 | else: |
maruel@chromium.org | e3608df | 2009-11-10 20:22:57 +0000 | [diff] [blame] | 443 | res_type = output_api.PresubmitNotifyResult |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 444 | message = 'Run the command: svn pset %s %s \\' % (prop, expected) |
maruel@chromium.org | e3608df | 2009-11-10 20:22:57 +0000 | [diff] [blame] | 445 | return [res_type(message, items=bad)] |
maruel@chromium.org | b7d4690 | 2009-06-10 14:12:10 +0000 | [diff] [blame] | 446 | return [] |
| 447 | |
| 448 | |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 449 | ### Other checks |
| 450 | |
| 451 | def CheckDoNotSubmit(input_api, output_api): |
| 452 | return ( |
| 453 | CheckDoNotSubmitInDescription(input_api, output_api) + |
| 454 | CheckDoNotSubmitInFiles(input_api, output_api) |
| 455 | ) |
| 456 | |
| 457 | |
bradnelson@google.com | c0b332a | 2010-08-26 00:30:37 +0000 | [diff] [blame] | 458 | def CheckTreeIsOpen(input_api, output_api, |
| 459 | url=None, closed=None, json_url=None): |
| 460 | """Check whether to allow commit without prompt. |
| 461 | |
| 462 | Supports two styles: |
| 463 | 1. Checks that an url's content doesn't match a regexp that would mean that |
| 464 | the tree is closed. (old) |
| 465 | 2. Check the json_url to decide whether to allow commit without prompt. |
| 466 | Args: |
| 467 | input_api: input related apis. |
| 468 | output_api: output related apis. |
| 469 | url: url to use for regex based tree status. |
| 470 | closed: regex to match for closed status. |
| 471 | json_url: url to download json style status. |
| 472 | """ |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 473 | if not input_api.is_committing: |
| 474 | return [] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 475 | try: |
bradnelson@google.com | c0b332a | 2010-08-26 00:30:37 +0000 | [diff] [blame] | 476 | if json_url: |
| 477 | connection = input_api.urllib2.urlopen(json_url) |
| 478 | status = input_api.json.loads(connection.read()) |
| 479 | connection.close() |
| 480 | if not status['can_commit_freely']: |
| 481 | short_text = 'Tree state is: ' + status['general_state'] |
| 482 | long_text = status['message'] + '\n' + json_url |
| 483 | return [output_api.PresubmitError(short_text, long_text=long_text)] |
| 484 | else: |
| 485 | # TODO(bradnelson): drop this once all users are gone. |
| 486 | connection = input_api.urllib2.urlopen(url) |
| 487 | status = connection.read() |
| 488 | connection.close() |
| 489 | if input_api.re.match(closed, status): |
| 490 | long_text = status + '\n' + url |
| 491 | return [output_api.PresubmitError('The tree is closed.', |
| 492 | long_text=long_text)] |
rohitrao@chromium.org | d490ee8 | 2012-02-06 19:31:33 +0000 | [diff] [blame] | 493 | except IOError as e: |
| 494 | return [output_api.PresubmitError('Error fetching tree status.', |
| 495 | long_text=str(e))] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 496 | return [] |
maruel@chromium.org | 7b305e8 | 2009-05-19 18:24:20 +0000 | [diff] [blame] | 497 | |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 498 | def GetUnitTestsInDirectory( |
maruel@chromium.org | 899e1c1 | 2011-04-07 17:03:18 +0000 | [diff] [blame] | 499 | input_api, output_api, directory, whitelist=None, blacklist=None): |
maruel@chromium.org | 2b5ce56 | 2011-03-31 16:15:44 +0000 | [diff] [blame] | 500 | """Lists all files in a directory and runs them. Doesn't recurse. |
| 501 | |
nick@chromium.org | ff52619 | 2013-06-10 19:30:26 +0000 | [diff] [blame] | 502 | It's mainly a wrapper for RunUnitTests. Use whitelist and blacklist to filter |
maruel@chromium.org | 2b5ce56 | 2011-03-31 16:15:44 +0000 | [diff] [blame] | 503 | tests accordingly. |
| 504 | """ |
| 505 | unit_tests = [] |
| 506 | test_path = input_api.os_path.abspath( |
| 507 | input_api.os_path.join(input_api.PresubmitLocalPath(), directory)) |
| 508 | |
| 509 | def check(filename, filters): |
| 510 | return any(True for i in filters if input_api.re.match(i, filename)) |
| 511 | |
maruel@chromium.org | fae707b | 2011-09-15 18:57:58 +0000 | [diff] [blame] | 512 | to_run = found = 0 |
maruel@chromium.org | 2b5ce56 | 2011-03-31 16:15:44 +0000 | [diff] [blame] | 513 | for filename in input_api.os_listdir(test_path): |
maruel@chromium.org | fae707b | 2011-09-15 18:57:58 +0000 | [diff] [blame] | 514 | found += 1 |
maruel@chromium.org | 2b5ce56 | 2011-03-31 16:15:44 +0000 | [diff] [blame] | 515 | fullpath = input_api.os_path.join(test_path, filename) |
| 516 | if not input_api.os_path.isfile(fullpath): |
| 517 | continue |
| 518 | if whitelist and not check(filename, whitelist): |
| 519 | continue |
| 520 | if blacklist and check(filename, blacklist): |
| 521 | continue |
| 522 | unit_tests.append(input_api.os_path.join(directory, filename)) |
maruel@chromium.org | fae707b | 2011-09-15 18:57:58 +0000 | [diff] [blame] | 523 | to_run += 1 |
| 524 | input_api.logging.debug('Found %d files, running %d' % (found, to_run)) |
| 525 | if not to_run: |
| 526 | return [ |
| 527 | output_api.PresubmitPromptWarning( |
| 528 | 'Out of %d files, found none that matched w=%r, b=%r in directory %s' |
| 529 | % (found, whitelist, blacklist, directory)) |
| 530 | ] |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 531 | return GetUnitTests(input_api, output_api, unit_tests) |
maruel@chromium.org | 2b5ce56 | 2011-03-31 16:15:44 +0000 | [diff] [blame] | 532 | |
| 533 | |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 534 | def GetUnitTests(input_api, output_api, unit_tests): |
maruel@chromium.org | 2b5ce56 | 2011-03-31 16:15:44 +0000 | [diff] [blame] | 535 | """Runs all unit tests in a directory. |
| 536 | |
| 537 | On Windows, sys.executable is used for unit tests ending with ".py". |
| 538 | """ |
| 539 | # We don't want to hinder users from uploading incomplete patches. |
| 540 | if input_api.is_committing: |
| 541 | message_type = output_api.PresubmitError |
| 542 | else: |
| 543 | message_type = output_api.PresubmitPromptWarning |
| 544 | |
maruel@chromium.org | 2b5ce56 | 2011-03-31 16:15:44 +0000 | [diff] [blame] | 545 | results = [] |
| 546 | for unit_test in unit_tests: |
| 547 | cmd = [] |
| 548 | if input_api.platform == 'win32' and unit_test.endswith('.py'): |
| 549 | # Windows needs some help. |
| 550 | cmd = [input_api.python_executable] |
| 551 | cmd.append(unit_test) |
maruel@chromium.org | 899e1c1 | 2011-04-07 17:03:18 +0000 | [diff] [blame] | 552 | if input_api.verbose: |
maruel@chromium.org | 6c7723e | 2011-04-12 19:04:55 +0000 | [diff] [blame] | 553 | cmd.append('--verbose') |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 554 | results.append(input_api.Command( |
| 555 | name=unit_test, |
| 556 | cmd=cmd, |
| 557 | kwargs={'cwd': input_api.PresubmitLocalPath()}, |
| 558 | message=message_type)) |
maruel@chromium.org | 2b5ce56 | 2011-03-31 16:15:44 +0000 | [diff] [blame] | 559 | return results |
| 560 | |
nick@chromium.org | ff52619 | 2013-06-10 19:30:26 +0000 | [diff] [blame] | 561 | |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 562 | def GetPythonUnitTests(input_api, output_api, unit_tests): |
maruel@chromium.org | c0b2297 | 2009-06-25 16:19:14 +0000 | [diff] [blame] | 563 | """Run the unit tests out of process, capture the output and use the result |
| 564 | code to determine success. |
maruel@chromium.org | 2b5ce56 | 2011-03-31 16:15:44 +0000 | [diff] [blame] | 565 | |
| 566 | DEPRECATED. |
maruel@chromium.org | c0b2297 | 2009-06-25 16:19:14 +0000 | [diff] [blame] | 567 | """ |
maruel@chromium.org | d7dccf5 | 2009-06-06 18:51:58 +0000 | [diff] [blame] | 568 | # We don't want to hinder users from uploading incomplete patches. |
| 569 | if input_api.is_committing: |
| 570 | message_type = output_api.PresubmitError |
| 571 | else: |
| 572 | message_type = output_api.PresubmitNotifyResult |
maruel@chromium.org | 0e76605 | 2011-04-06 13:32:51 +0000 | [diff] [blame] | 573 | results = [] |
maruel@chromium.org | 7b305e8 | 2009-05-19 18:24:20 +0000 | [diff] [blame] | 574 | for unit_test in unit_tests: |
maruel@chromium.org | c0b2297 | 2009-06-25 16:19:14 +0000 | [diff] [blame] | 575 | # Run the unit tests out of process. This is because some unit tests |
| 576 | # stub out base libraries and don't clean up their mess. It's too easy to |
| 577 | # get subtle bugs. |
| 578 | cwd = None |
| 579 | env = None |
| 580 | unit_test_name = unit_test |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 581 | # '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] | 582 | # directory instead. |
| 583 | if '.' in unit_test: |
| 584 | # Tests imported in submodules (subdirectories) assume that the current |
| 585 | # directory is in the PYTHONPATH. Manually fix that. |
| 586 | unit_test = unit_test.replace('.', '/') |
| 587 | cwd = input_api.os_path.dirname(unit_test) |
| 588 | unit_test = input_api.os_path.basename(unit_test) |
| 589 | env = input_api.environ.copy() |
kbr@google.com | ab31859 | 2009-09-04 00:54:55 +0000 | [diff] [blame] | 590 | # At least on Windows, it seems '.' must explicitly be in PYTHONPATH |
| 591 | backpath = [ |
| 592 | '.', input_api.os_path.pathsep.join(['..'] * (cwd.count('/') + 1)) |
| 593 | ] |
maruel@chromium.org | c0b2297 | 2009-06-25 16:19:14 +0000 | [diff] [blame] | 594 | if env.get('PYTHONPATH'): |
| 595 | backpath.append(env.get('PYTHONPATH')) |
ukai@chromium.org | a301f1f | 2009-08-05 10:37:33 +0000 | [diff] [blame] | 596 | env['PYTHONPATH'] = input_api.os_path.pathsep.join((backpath)) |
maruel@chromium.org | 0e76605 | 2011-04-06 13:32:51 +0000 | [diff] [blame] | 597 | cmd = [input_api.python_executable, '-m', '%s' % unit_test] |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 598 | results.append(input_api.Command( |
| 599 | name=unit_test_name, |
| 600 | cmd=cmd, |
| 601 | kwargs={'env': env, 'cwd': cwd}, |
| 602 | message=message_type)) |
maruel@chromium.org | 0e76605 | 2011-04-06 13:32:51 +0000 | [diff] [blame] | 603 | return results |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 604 | |
| 605 | |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 606 | def RunUnitTestsInDirectory(input_api, *args, **kwargs): |
| 607 | """Run tests in a directory serially. |
| 608 | |
| 609 | For better performance, use GetUnitTestsInDirectory and then |
| 610 | pass to input_api.RunTests. |
| 611 | """ |
| 612 | return input_api.RunTests( |
| 613 | GetUnitTestsInDirectory(input_api, *args, **kwargs), False) |
| 614 | |
| 615 | |
| 616 | def RunUnitTests(input_api, *args, **kwargs): |
| 617 | """Run tests serially. |
| 618 | |
| 619 | For better performance, use GetUnitTests and then pass to |
| 620 | input_api.RunTests. |
| 621 | """ |
| 622 | return input_api.RunTests(GetUnitTests(input_api, *args, **kwargs), False) |
| 623 | |
| 624 | |
| 625 | def RunPythonUnitTests(input_api, *args, **kwargs): |
| 626 | """Run python tests in a directory serially. |
| 627 | |
| 628 | DEPRECATED |
| 629 | """ |
| 630 | return input_api.RunTests( |
| 631 | GetPythonUnitTests(input_api, *args, **kwargs), False) |
| 632 | |
| 633 | |
maruel@chromium.org | 5d0dc43 | 2011-01-03 02:40:37 +0000 | [diff] [blame] | 634 | def _FetchAllFiles(input_api, white_list, black_list): |
| 635 | """Hack to fetch all files.""" |
| 636 | # We cannot use AffectedFiles here because we want to test every python |
| 637 | # file on each single python change. It's because a change in a python file |
| 638 | # can break another unmodified file. |
| 639 | # Use code similar to InputApi.FilterSourceFile() |
| 640 | def Find(filepath, filters): |
| 641 | for item in filters: |
| 642 | if input_api.re.match(item, filepath): |
| 643 | return True |
| 644 | return False |
| 645 | |
maruel@chromium.org | 5d0dc43 | 2011-01-03 02:40:37 +0000 | [diff] [blame] | 646 | files = [] |
| 647 | path_len = len(input_api.PresubmitLocalPath()) |
maruel@chromium.org | 2b5ce56 | 2011-03-31 16:15:44 +0000 | [diff] [blame] | 648 | for dirpath, dirnames, filenames in input_api.os_walk( |
| 649 | input_api.PresubmitLocalPath()): |
maruel@chromium.org | 5d0dc43 | 2011-01-03 02:40:37 +0000 | [diff] [blame] | 650 | # Passes dirnames in black list to speed up search. |
| 651 | for item in dirnames[:]: |
| 652 | filepath = input_api.os_path.join(dirpath, item)[path_len + 1:] |
| 653 | if Find(filepath, black_list): |
| 654 | dirnames.remove(item) |
| 655 | for item in filenames: |
| 656 | filepath = input_api.os_path.join(dirpath, item)[path_len + 1:] |
| 657 | if Find(filepath, white_list) and not Find(filepath, black_list): |
| 658 | files.append(filepath) |
| 659 | return files |
| 660 | |
| 661 | |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 662 | def GetPylint(input_api, output_api, white_list=None, black_list=None, |
ilevy@chromium.org | 7b677f7 | 2013-01-07 18:49:26 +0000 | [diff] [blame] | 663 | disabled_warnings=None, extra_paths_list=None): |
maruel@chromium.org | bf38a7e | 2010-12-14 18:15:54 +0000 | [diff] [blame] | 664 | """Run pylint on python files. |
| 665 | |
chrisha@google.com | 267d659 | 2012-06-19 19:23:31 +0000 | [diff] [blame] | 666 | The default white_list enforces looking only at *.py files. |
maruel@chromium.org | bf38a7e | 2010-12-14 18:15:54 +0000 | [diff] [blame] | 667 | """ |
maruel@chromium.org | 69eaecb | 2011-06-14 13:09:13 +0000 | [diff] [blame] | 668 | white_list = tuple(white_list or ('.*\.py$',)) |
| 669 | black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST) |
ilevy@chromium.org | 7b677f7 | 2013-01-07 18:49:26 +0000 | [diff] [blame] | 670 | extra_paths_list = extra_paths_list or [] |
| 671 | |
maruel@chromium.org | ade9c59 | 2011-04-07 15:59:11 +0000 | [diff] [blame] | 672 | if input_api.is_committing: |
| 673 | error_type = output_api.PresubmitError |
| 674 | else: |
| 675 | error_type = output_api.PresubmitPromptWarning |
maruel@chromium.org | bf38a7e | 2010-12-14 18:15:54 +0000 | [diff] [blame] | 676 | |
| 677 | # Only trigger if there is at least one python file affected. |
ilevy@chromium.org | 3657633 | 2013-01-08 03:16:15 +0000 | [diff] [blame] | 678 | def rel_path(regex): |
| 679 | """Modifies a regex for a subject to accept paths relative to root.""" |
chrisha@chromium.org | 40e5d3d | 2013-01-18 17:46:57 +0000 | [diff] [blame] | 680 | def samefile(a, b): |
| 681 | # Default implementation for platforms lacking os.path.samefile |
| 682 | # (like Windows). |
| 683 | return input_api.os_path.abspath(a) == input_api.os_path.abspath(b) |
| 684 | samefile = getattr(input_api.os_path, 'samefile', samefile) |
| 685 | if samefile(input_api.PresubmitLocalPath(), |
| 686 | input_api.change.RepositoryRoot()): |
ilevy@chromium.org | dd4e931 | 2013-01-15 03:22:04 +0000 | [diff] [blame] | 687 | return regex |
chrisha@chromium.org | 40e5d3d | 2013-01-18 17:46:57 +0000 | [diff] [blame] | 688 | |
ilevy@chromium.org | 3657633 | 2013-01-08 03:16:15 +0000 | [diff] [blame] | 689 | prefix = input_api.os_path.join(input_api.os_path.relpath( |
| 690 | input_api.PresubmitLocalPath(), input_api.change.RepositoryRoot()), '') |
| 691 | return input_api.re.escape(prefix) + regex |
ilevy@chromium.org | 7b677f7 | 2013-01-07 18:49:26 +0000 | [diff] [blame] | 692 | src_filter = lambda x: input_api.FilterSourceFile( |
| 693 | x, map(rel_path, white_list), map(rel_path, black_list)) |
maruel@chromium.org | bf38a7e | 2010-12-14 18:15:54 +0000 | [diff] [blame] | 694 | if not input_api.AffectedSourceFiles(src_filter): |
ilevy@chromium.org | 7b677f7 | 2013-01-07 18:49:26 +0000 | [diff] [blame] | 695 | input_api.logging.info('Skipping pylint: no matching changes.') |
maruel@chromium.org | bf38a7e | 2010-12-14 18:15:54 +0000 | [diff] [blame] | 696 | return [] |
| 697 | |
maruel@chromium.org | ff9a217 | 2012-04-24 16:55:32 +0000 | [diff] [blame] | 698 | extra_args = ['--rcfile=%s' % input_api.os_path.join(_HERE, 'pylintrc')] |
| 699 | if disabled_warnings: |
| 700 | extra_args.extend(['-d', ','.join(disabled_warnings)]) |
| 701 | |
chrisha@google.com | 267d659 | 2012-06-19 19:23:31 +0000 | [diff] [blame] | 702 | files = _FetchAllFiles(input_api, white_list, black_list) |
| 703 | if not files: |
maruel@chromium.org | fca5339 | 2010-12-21 18:42:57 +0000 | [diff] [blame] | 704 | return [] |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 705 | files.sort() |
chrisha@google.com | 267d659 | 2012-06-19 19:23:31 +0000 | [diff] [blame] | 706 | |
csharp@chromium.org | 4039534 | 2013-02-21 14:57:23 +0000 | [diff] [blame] | 707 | input_api.logging.info('Running pylint on %d files', len(files)) |
| 708 | input_api.logging.debug('Running pylint on: %s', files) |
chrisha@google.com | 267d659 | 2012-06-19 19:23:31 +0000 | [diff] [blame] | 709 | # Copy the system path to the environment so pylint can find the right |
| 710 | # imports. |
| 711 | env = input_api.environ.copy() |
| 712 | import sys |
ilevy@chromium.org | 7b677f7 | 2013-01-07 18:49:26 +0000 | [diff] [blame] | 713 | env['PYTHONPATH'] = input_api.os_path.pathsep.join( |
robertshield@chromium.org | a287393 | 2013-02-20 18:08:46 +0000 | [diff] [blame] | 714 | extra_paths_list + sys.path).encode('utf8') |
chrisha@google.com | 267d659 | 2012-06-19 19:23:31 +0000 | [diff] [blame] | 715 | |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 716 | def GetPylintCmd(files): |
| 717 | # Windows needs help running python files so we explicitly specify |
| 718 | # the interpreter to use. It also has limitations on the size of |
| 719 | # the command-line, so we pass arguments via a pipe. |
| 720 | if len(files) == 1: |
| 721 | description = files[0] |
| 722 | else: |
| 723 | description = '%s files' % len(files) |
chrisha@chromium.org | 1b129e5 | 2012-06-22 17:08:11 +0000 | [diff] [blame] | 724 | |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 725 | return input_api.Command( |
| 726 | name='Pylint (%s)' % description, |
| 727 | cmd=[input_api.python_executable, |
| 728 | input_api.os_path.join(_HERE, 'third_party', 'pylint.py'), |
| 729 | '--args-on-stdin'], |
| 730 | kwargs={'env': env, 'stdin': '\n'.join(files + extra_args)}, |
| 731 | message=error_type) |
chrisha@chromium.org | 1b129e5 | 2012-06-22 17:08:11 +0000 | [diff] [blame] | 732 | |
sbc@chromium.org | 7fc75ca | 2012-09-21 20:14:21 +0000 | [diff] [blame] | 733 | # Always run pylint and pass it all the py files at once. |
| 734 | # Passing py files one at time is slower and can produce |
| 735 | # different results. input_api.verbose used to be used |
| 736 | # to enable this behaviour but differing behaviour in |
| 737 | # verbose mode is not desirable. |
ilevy@chromium.org | 7b677f7 | 2013-01-07 18:49:26 +0000 | [diff] [blame] | 738 | # Leave this unreachable code in here so users can make |
| 739 | # a quick local edit to diagnose pylint issues more |
| 740 | # easily. |
sbc@chromium.org | 7fc75ca | 2012-09-21 20:14:21 +0000 | [diff] [blame] | 741 | if True: |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 742 | return [GetPylintCmd(files)] |
chrisha@google.com | 267d659 | 2012-06-19 19:23:31 +0000 | [diff] [blame] | 743 | else: |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 744 | return map(GetPylintCmd, files) |
| 745 | |
| 746 | |
| 747 | def RunPylint(input_api, *args, **kwargs): |
| 748 | """Legacy presubmit function. |
| 749 | |
| 750 | For better performance, get all tests and then pass to |
| 751 | input_api.RunTests. |
| 752 | """ |
| 753 | return input_api.RunTests(GetPylint(input_api, *args, **kwargs), False) |
maruel@chromium.org | e94aedc | 2010-12-13 21:11:30 +0000 | [diff] [blame] | 754 | |
maruel@chromium.org | ade9c59 | 2011-04-07 15:59:11 +0000 | [diff] [blame] | 755 | |
dpranke@chromium.org | 627ea67 | 2011-03-11 23:29:03 +0000 | [diff] [blame] | 756 | # TODO(dpranke): Get the host_url from the input_api instead |
chrisha@google.com | 267d659 | 2012-06-19 19:23:31 +0000 | [diff] [blame] | 757 | def CheckRietveldTryJobExecution(dummy_input_api, dummy_output_api, |
| 758 | dummy_host_url, dummy_platforms, |
| 759 | dummy_owner): |
maruel@chromium.org | 85da74b | 2011-10-27 17:13:30 +0000 | [diff] [blame] | 760 | # Temporarily 'fix' the check while the Rietveld API is being upgraded to |
| 761 | # something sensible. |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 762 | return [] |
| 763 | |
| 764 | |
| 765 | def CheckBuildbotPendingBuilds(input_api, output_api, url, max_pendings, |
| 766 | ignored): |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 767 | try: |
| 768 | connection = input_api.urllib2.urlopen(url) |
| 769 | raw_data = connection.read() |
| 770 | connection.close() |
| 771 | except IOError: |
| 772 | return [output_api.PresubmitNotifyResult('%s is not accessible' % url)] |
| 773 | |
| 774 | try: |
| 775 | data = input_api.json.loads(raw_data) |
| 776 | except ValueError: |
| 777 | return [output_api.PresubmitNotifyResult('Received malformed json while ' |
| 778 | 'looking up buildbot status')] |
| 779 | |
| 780 | out = [] |
| 781 | for (builder_name, builder) in data.iteritems(): |
| 782 | if builder_name in ignored: |
| 783 | continue |
maruel@chromium.org | cf1982c | 2010-10-04 15:08:28 +0000 | [diff] [blame] | 784 | if builder.get('state', '') == 'offline': |
| 785 | continue |
maruel@chromium.org | 3fbcb08 | 2010-03-19 14:03:28 +0000 | [diff] [blame] | 786 | pending_builds_len = len(builder.get('pending_builds', [])) |
| 787 | if pending_builds_len > max_pendings: |
| 788 | out.append('%s has %d build(s) pending' % |
| 789 | (builder_name, pending_builds_len)) |
| 790 | if out: |
| 791 | return [output_api.PresubmitPromptWarning( |
| 792 | 'Build(s) pending. It is suggested to wait that no more than %d ' |
| 793 | 'builds are pending.' % max_pendings, |
| 794 | long_text='\n'.join(out))] |
| 795 | return [] |
dpranke@chromium.org | 2a00962 | 2011-03-01 02:43:31 +0000 | [diff] [blame] | 796 | |
| 797 | |
dpranke@chromium.org | dbf8b4e | 2013-02-28 19:24:16 +0000 | [diff] [blame] | 798 | def CheckOwners(input_api, output_api, source_file_filter=None, |
| 799 | author_counts_as_owner=True): |
pam@chromium.org | f46aed9 | 2012-03-08 09:18:17 +0000 | [diff] [blame] | 800 | if input_api.is_committing: |
| 801 | if input_api.tbr: |
| 802 | return [output_api.PresubmitNotifyResult( |
| 803 | '--tbr was specified, skipping OWNERS check')] |
| 804 | if not input_api.change.issue: |
| 805 | return [output_api.PresubmitError("OWNERS check failed: this change has " |
| 806 | "no Rietveld issue number, so we can't check it for approvals.")] |
| 807 | needed = 'LGTM from an OWNER' |
| 808 | output = output_api.PresubmitError |
| 809 | else: |
| 810 | needed = 'OWNER reviewers' |
| 811 | output = output_api.PresubmitNotifyResult |
dpranke@chromium.org | 3e331bd | 2011-03-24 23:13:04 +0000 | [diff] [blame] | 812 | |
dpranke@chromium.org | add5df4 | 2011-03-08 23:04:01 +0000 | [diff] [blame] | 813 | affected_files = set([f.LocalPath() for f in |
sail@chromium.org | 5538e02 | 2011-05-12 17:53:16 +0000 | [diff] [blame] | 814 | input_api.change.AffectedFiles(file_filter=source_file_filter)]) |
dpranke@chromium.org | 3e331bd | 2011-03-24 23:13:04 +0000 | [diff] [blame] | 815 | |
dpranke@chromium.org | 2a00962 | 2011-03-01 02:43:31 +0000 | [diff] [blame] | 816 | owners_db = input_api.owners_db |
pam@chromium.org | f46aed9 | 2012-03-08 09:18:17 +0000 | [diff] [blame] | 817 | owner_email, reviewers = _RietveldOwnerAndReviewers( |
| 818 | input_api, |
| 819 | owners_db.email_regexp, |
| 820 | approval_needed=input_api.is_committing) |
| 821 | |
dpranke@chromium.org | f6ddfa4 | 2013-03-05 21:06:03 +0000 | [diff] [blame] | 822 | owner_email = owner_email or input_api.change.author_email |
| 823 | |
| 824 | if author_counts_as_owner and owner_email: |
dpranke@chromium.org | 4c7ce99 | 2013-03-06 17:15:40 +0000 | [diff] [blame] | 825 | reviewers_plus_owner = set([owner_email]).union(reviewers) |
dpranke@chromium.org | dbf8b4e | 2013-02-28 19:24:16 +0000 | [diff] [blame] | 826 | missing_files = owners_db.files_not_covered_by(affected_files, |
dpranke@chromium.org | f6ddfa4 | 2013-03-05 21:06:03 +0000 | [diff] [blame] | 827 | reviewers_plus_owner) |
pam@chromium.org | f46aed9 | 2012-03-08 09:18:17 +0000 | [diff] [blame] | 828 | else: |
dpranke@chromium.org | dbf8b4e | 2013-02-28 19:24:16 +0000 | [diff] [blame] | 829 | missing_files = owners_db.files_not_covered_by(affected_files, reviewers) |
maruel@chromium.org | e4067ab | 2011-06-03 01:07:35 +0000 | [diff] [blame] | 830 | |
dpranke@chromium.org | 6b1e3ee | 2013-02-23 00:06:38 +0000 | [diff] [blame] | 831 | if missing_files: |
zork@chromium.org | 046e175 | 2012-05-07 05:56:12 +0000 | [diff] [blame] | 832 | output_list = [ |
dpranke@chromium.org | f6ddfa4 | 2013-03-05 21:06:03 +0000 | [diff] [blame] | 833 | output('Missing %s for these files:\n %s' % |
bauerb@chromium.org | b3b5201 | 2013-04-18 19:28:04 +0000 | [diff] [blame] | 834 | (needed, '\n '.join(sorted(missing_files))))] |
zork@chromium.org | 046e175 | 2012-05-07 05:56:12 +0000 | [diff] [blame] | 835 | if not input_api.is_committing: |
dpranke@chromium.org | 31b42c0 | 2013-09-19 19:24:15 +0000 | [diff] [blame] | 836 | suggested_owners = owners_db.reviewers_for(missing_files, owner_email) |
scheib@chromium.org | 93276ab | 2013-10-14 23:55:32 +0000 | [diff] [blame] | 837 | output_list.append(output('Suggested OWNERS: ' + |
| 838 | '(Use "git-cl owners" to interactively select owners.)\n %s' % |
| 839 | ('\n '.join(suggested_owners or [])))) |
zork@chromium.org | 046e175 | 2012-05-07 05:56:12 +0000 | [diff] [blame] | 840 | return output_list |
dpranke@chromium.org | 2a00962 | 2011-03-01 02:43:31 +0000 | [diff] [blame] | 841 | |
pam@chromium.org | f46aed9 | 2012-03-08 09:18:17 +0000 | [diff] [blame] | 842 | if input_api.is_committing and not reviewers: |
| 843 | return [output('Missing LGTM from someone other than %s' % owner_email)] |
dpranke@chromium.org | 3e331bd | 2011-03-24 23:13:04 +0000 | [diff] [blame] | 844 | return [] |
dpranke@chromium.org | 627ea67 | 2011-03-11 23:29:03 +0000 | [diff] [blame] | 845 | |
| 846 | |
ilevy@chromium.org | 1fa5cb9 | 2012-12-05 04:04:42 +0000 | [diff] [blame] | 847 | def _GetRietveldIssueProps(input_api, messages): |
| 848 | """Gets the issue properties from rietveld.""" |
| 849 | issue = input_api.change.issue |
| 850 | if issue and input_api.rietveld: |
| 851 | return input_api.rietveld.get_issue_properties( |
| 852 | issue=int(issue), messages=messages) |
| 853 | |
| 854 | |
isherman@chromium.org | b5cded6 | 2014-03-25 17:47:57 +0000 | [diff] [blame] | 855 | def _ReviewersFromChange(change): |
| 856 | """Return the reviewers specified in the |change|, if any.""" |
| 857 | reviewers = set() |
| 858 | if change.R: |
| 859 | reviewers.update(set([r.strip() for r in change.R.split(',')])) |
| 860 | if change.TBR: |
| 861 | reviewers.update(set([r.strip() for r in change.TBR.split(',')])) |
isherman@chromium.org | 103ae03 | 2014-04-09 09:06:19 +0000 | [diff] [blame^] | 862 | |
| 863 | # Drop reviewers that aren't specified in email address format. |
| 864 | return set(reviewer for reviewer in reviewers if '@' in reviewer) |
isherman@chromium.org | b5cded6 | 2014-03-25 17:47:57 +0000 | [diff] [blame] | 865 | |
| 866 | |
pam@chromium.org | f46aed9 | 2012-03-08 09:18:17 +0000 | [diff] [blame] | 867 | def _RietveldOwnerAndReviewers(input_api, email_regexp, approval_needed=False): |
| 868 | """Return the owner and reviewers of a change, if any. |
| 869 | |
| 870 | If approval_needed is True, only reviewers who have approved the change |
| 871 | will be returned. |
| 872 | """ |
ilevy@chromium.org | 1fa5cb9 | 2012-12-05 04:04:42 +0000 | [diff] [blame] | 873 | issue_props = _GetRietveldIssueProps(input_api, True) |
| 874 | if not issue_props: |
isherman@chromium.org | b5cded6 | 2014-03-25 17:47:57 +0000 | [diff] [blame] | 875 | reviewers = set() |
| 876 | if not approval_needed: |
| 877 | reviewers = _ReviewersFromChange(input_api.change) |
| 878 | return None, reviewers |
maruel@chromium.org | e4067ab | 2011-06-03 01:07:35 +0000 | [diff] [blame] | 879 | |
pam@chromium.org | f46aed9 | 2012-03-08 09:18:17 +0000 | [diff] [blame] | 880 | if not approval_needed: |
| 881 | return issue_props['owner_email'], set(issue_props['reviewers']) |
| 882 | |
dpranke@chromium.org | 3e331bd | 2011-03-24 23:13:04 +0000 | [diff] [blame] | 883 | owner_email = issue_props['owner_email'] |
dpranke@chromium.org | 3e331bd | 2011-03-24 23:13:04 +0000 | [diff] [blame] | 884 | |
dpranke@chromium.org | 627ea67 | 2011-03-11 23:29:03 +0000 | [diff] [blame] | 885 | def match_reviewer(r): |
maruel@chromium.org | 80941c2 | 2011-05-30 20:14:18 +0000 | [diff] [blame] | 886 | return email_regexp.match(r) and r != owner_email |
dpranke@chromium.org | 627ea67 | 2011-03-11 23:29:03 +0000 | [diff] [blame] | 887 | |
maruel@chromium.org | 80941c2 | 2011-05-30 20:14:18 +0000 | [diff] [blame] | 888 | messages = issue_props.get('messages', []) |
| 889 | approvers = set( |
| 890 | m['sender'] for m in messages |
| 891 | if m.get('approval') and match_reviewer(m['sender'])) |
| 892 | |
| 893 | return owner_email, approvers |
dpranke@chromium.org | 627ea67 | 2011-03-11 23:29:03 +0000 | [diff] [blame] | 894 | |
bradnelson@google.com | 56e48bc | 2011-03-24 20:51:21 +0000 | [diff] [blame] | 895 | |
| 896 | def _CheckConstNSObject(input_api, output_api, source_file_filter): |
| 897 | """Checks to make sure no objective-c files have |const NSSomeClass*|.""" |
mark@chromium.org | edf744d | 2011-03-28 16:45:34 +0000 | [diff] [blame] | 898 | pattern = input_api.re.compile( |
| 899 | r'const\s+NS(?!(Point|Range|Rect|Size)\s*\*)\w*\s*\*') |
bradnelson@google.com | 56e48bc | 2011-03-24 20:51:21 +0000 | [diff] [blame] | 900 | |
| 901 | def objective_c_filter(f): |
| 902 | return (source_file_filter(f) and |
mark@chromium.org | edf744d | 2011-03-28 16:45:34 +0000 | [diff] [blame] | 903 | input_api.os_path.splitext(f.LocalPath())[1] in ('.h', '.m', '.mm')) |
bradnelson@google.com | 56e48bc | 2011-03-24 20:51:21 +0000 | [diff] [blame] | 904 | |
| 905 | files = [] |
| 906 | for f in input_api.AffectedSourceFiles(objective_c_filter): |
| 907 | contents = input_api.ReadFile(f) |
| 908 | if pattern.search(contents): |
| 909 | files.append(f) |
| 910 | |
| 911 | if files: |
| 912 | if input_api.is_committing: |
| 913 | res_type = output_api.PresubmitPromptWarning |
| 914 | else: |
| 915 | res_type = output_api.PresubmitNotifyResult |
| 916 | return [ res_type('|const NSClass*| is wrong, see ' + |
| 917 | 'http://dev.chromium.org/developers/clang-mac', |
| 918 | files) ] |
| 919 | return [] |
| 920 | |
| 921 | |
bauerb@chromium.org | 4cea01e | 2012-03-20 19:49:05 +0000 | [diff] [blame] | 922 | def CheckSingletonInHeaders(input_api, output_api, source_file_filter=None): |
bradnelson@google.com | 56e48bc | 2011-03-24 20:51:21 +0000 | [diff] [blame] | 923 | """Checks to make sure no header files have |Singleton<|.""" |
bauerb@chromium.org | 4cea01e | 2012-03-20 19:49:05 +0000 | [diff] [blame] | 924 | pattern = input_api.re.compile(r'(?<!class\s)Singleton\s*<') |
bradnelson@google.com | 56e48bc | 2011-03-24 20:51:21 +0000 | [diff] [blame] | 925 | files = [] |
| 926 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 927 | if (f.LocalPath().endswith('.h') or f.LocalPath().endswith('.hxx') or |
| 928 | f.LocalPath().endswith('.hpp') or f.LocalPath().endswith('.inl')): |
| 929 | contents = input_api.ReadFile(f) |
dilmah@chromium.org | d22b970 | 2011-08-26 13:53:49 +0000 | [diff] [blame] | 930 | for line in contents.splitlines(False): |
bauerb@chromium.org | 4cea01e | 2012-03-20 19:49:05 +0000 | [diff] [blame] | 931 | if (not input_api.re.match(r'//', line) and # Strip C++ comment. |
| 932 | pattern.search(line)): |
dilmah@chromium.org | d22b970 | 2011-08-26 13:53:49 +0000 | [diff] [blame] | 933 | files.append(f) |
| 934 | break |
bradnelson@google.com | 56e48bc | 2011-03-24 20:51:21 +0000 | [diff] [blame] | 935 | |
| 936 | if files: |
| 937 | return [ output_api.PresubmitError( |
| 938 | 'Found Singleton<T> in the following header files.\n' + |
| 939 | 'Please move them to an appropriate source file so that the ' + |
| 940 | 'template gets instantiated in a single compilation unit.', |
| 941 | files) ] |
| 942 | return [] |
| 943 | |
| 944 | |
| 945 | def PanProjectChecks(input_api, output_api, |
| 946 | excluded_paths=None, text_files=None, |
bradnelson@google.com | d57771d | 2011-03-31 19:18:32 +0000 | [diff] [blame] | 947 | license_header=None, project_name=None, |
jamesr@chromium.org | af27f46 | 2013-04-04 21:44:22 +0000 | [diff] [blame] | 948 | owners_check=True, maxlen=80): |
bradnelson@google.com | 56e48bc | 2011-03-24 20:51:21 +0000 | [diff] [blame] | 949 | """Checks that ALL chromium orbit projects should use. |
| 950 | |
| 951 | These are checks to be run on all Chromium orbit project, including: |
| 952 | Chromium |
| 953 | Native Client |
| 954 | V8 |
| 955 | When you update this function, please take this broad scope into account. |
| 956 | Args: |
| 957 | input_api: Bag of input related interfaces. |
| 958 | output_api: Bag of output related interfaces. |
| 959 | excluded_paths: Don't include these paths in common checks. |
| 960 | text_files: Which file are to be treated as documentation text files. |
| 961 | license_header: What license header should be on files. |
| 962 | project_name: What is the name of the project as it appears in the license. |
| 963 | Returns: |
| 964 | A list of warning or error objects. |
| 965 | """ |
maruel@chromium.org | 69eaecb | 2011-06-14 13:09:13 +0000 | [diff] [blame] | 966 | excluded_paths = tuple(excluded_paths or []) |
| 967 | text_files = tuple(text_files or ( |
maruel@chromium.org | fe1211a | 2011-05-28 18:54:17 +0000 | [diff] [blame] | 968 | r'.+\.txt$', |
| 969 | r'.+\.json$', |
maruel@chromium.org | 69eaecb | 2011-06-14 13:09:13 +0000 | [diff] [blame] | 970 | )) |
bradnelson@google.com | 56e48bc | 2011-03-24 20:51:21 +0000 | [diff] [blame] | 971 | project_name = project_name or 'Chromium' |
mark@chromium.org | 2cc6642 | 2012-08-07 21:22:32 +0000 | [diff] [blame] | 972 | |
| 973 | # Accept any year number from 2006 to the current year, or the special |
rvargas@chromium.org | 0265260 | 2014-03-14 19:43:20 +0000 | [diff] [blame] | 974 | # 2006-20xx string used on the oldest files. 2006-20xx is deprecated, but |
| 975 | # tolerated on old files. |
mark@chromium.org | 2cc6642 | 2012-08-07 21:22:32 +0000 | [diff] [blame] | 976 | current_year = int(input_api.time.strftime('%Y')) |
| 977 | allowed_years = (str(s) for s in reversed(xrange(2006, current_year + 1))) |
rvargas@chromium.org | 0265260 | 2014-03-14 19:43:20 +0000 | [diff] [blame] | 978 | years_re = '(' + '|'.join(allowed_years) + '|2006-2008|2006-2009|2006-2010)' |
mark@chromium.org | 2cc6642 | 2012-08-07 21:22:32 +0000 | [diff] [blame] | 979 | |
| 980 | # The (c) is deprecated, but tolerate it until it's removed from all files. |
bradnelson@google.com | 56e48bc | 2011-03-24 20:51:21 +0000 | [diff] [blame] | 981 | license_header = license_header or ( |
mark@chromium.org | 2cc6642 | 2012-08-07 21:22:32 +0000 | [diff] [blame] | 982 | r'.*? Copyright (\(c\) )?%(year)s The %(project)s Authors\. ' |
bradnelson@google.com | 56e48bc | 2011-03-24 20:51:21 +0000 | [diff] [blame] | 983 | r'All rights reserved\.\n' |
| 984 | r'.*? Use of this source code is governed by a BSD-style license that ' |
| 985 | r'can be\n' |
dbeam@chromium.org | b231210 | 2012-02-15 02:01:55 +0000 | [diff] [blame] | 986 | r'.*? found in the LICENSE file\.(?: \*/)?\n' |
bradnelson@google.com | 56e48bc | 2011-03-24 20:51:21 +0000 | [diff] [blame] | 987 | ) % { |
mark@chromium.org | 2cc6642 | 2012-08-07 21:22:32 +0000 | [diff] [blame] | 988 | 'year': years_re, |
bradnelson@google.com | 56e48bc | 2011-03-24 20:51:21 +0000 | [diff] [blame] | 989 | 'project': project_name, |
| 990 | } |
| 991 | |
| 992 | results = [] |
| 993 | # This code loads the default black list (e.g. third_party, experimental, etc) |
| 994 | # and add our black list (breakpad, skia and v8 are still not following |
| 995 | # google style and are not really living this repository). |
| 996 | # See presubmit_support.py InputApi.FilterSourceFile for the (simple) usage. |
| 997 | black_list = input_api.DEFAULT_BLACK_LIST + excluded_paths |
| 998 | white_list = input_api.DEFAULT_WHITE_LIST + text_files |
| 999 | sources = lambda x: input_api.FilterSourceFile(x, black_list=black_list) |
maruel@chromium.org | fe1211a | 2011-05-28 18:54:17 +0000 | [diff] [blame] | 1000 | text_files = lambda x: input_api.FilterSourceFile( |
| 1001 | x, black_list=black_list, white_list=white_list) |
nick@chromium.org | e06cb4e | 2011-04-23 01:20:38 +0000 | [diff] [blame] | 1002 | |
| 1003 | snapshot_memory = [] |
| 1004 | def snapshot(msg): |
| 1005 | """Measures & prints performance warning if a rule is running slow.""" |
| 1006 | dt2 = input_api.time.clock() |
| 1007 | if snapshot_memory: |
| 1008 | delta_ms = int(1000*(dt2 - snapshot_memory[0])) |
| 1009 | if delta_ms > 500: |
| 1010 | print " %s took a long time: %dms" % (snapshot_memory[1], delta_ms) |
| 1011 | snapshot_memory[:] = (dt2, msg) |
| 1012 | |
bradnelson@google.com | d57771d | 2011-03-31 19:18:32 +0000 | [diff] [blame] | 1013 | if owners_check: |
nick@chromium.org | e06cb4e | 2011-04-23 01:20:38 +0000 | [diff] [blame] | 1014 | snapshot("checking owners") |
bradnelson@google.com | d57771d | 2011-03-31 19:18:32 +0000 | [diff] [blame] | 1015 | results.extend(input_api.canned_checks.CheckOwners( |
dpranke@chromium.org | 751797a | 2011-06-07 18:46:27 +0000 | [diff] [blame] | 1016 | input_api, output_api, source_file_filter=None)) |
bradnelson@google.com | 56e48bc | 2011-03-24 20:51:21 +0000 | [diff] [blame] | 1017 | |
nick@chromium.org | e06cb4e | 2011-04-23 01:20:38 +0000 | [diff] [blame] | 1018 | snapshot("checking long lines") |
bradnelson@google.com | 56e48bc | 2011-03-24 20:51:21 +0000 | [diff] [blame] | 1019 | results.extend(input_api.canned_checks.CheckLongLines( |
jamesr@chromium.org | af27f46 | 2013-04-04 21:44:22 +0000 | [diff] [blame] | 1020 | input_api, output_api, maxlen, source_file_filter=sources)) |
nick@chromium.org | e06cb4e | 2011-04-23 01:20:38 +0000 | [diff] [blame] | 1021 | snapshot( "checking tabs") |
bradnelson@google.com | 56e48bc | 2011-03-24 20:51:21 +0000 | [diff] [blame] | 1022 | results.extend(input_api.canned_checks.CheckChangeHasNoTabs( |
| 1023 | input_api, output_api, source_file_filter=sources)) |
nick@chromium.org | e06cb4e | 2011-04-23 01:20:38 +0000 | [diff] [blame] | 1024 | snapshot( "checking stray whitespace") |
bradnelson@google.com | 56e48bc | 2011-03-24 20:51:21 +0000 | [diff] [blame] | 1025 | results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace( |
| 1026 | input_api, output_api, source_file_filter=sources)) |
nick@chromium.org | e06cb4e | 2011-04-23 01:20:38 +0000 | [diff] [blame] | 1027 | snapshot("checking nsobjects") |
bradnelson@google.com | 56e48bc | 2011-03-24 20:51:21 +0000 | [diff] [blame] | 1028 | results.extend(_CheckConstNSObject( |
| 1029 | input_api, output_api, source_file_filter=sources)) |
nick@chromium.org | e06cb4e | 2011-04-23 01:20:38 +0000 | [diff] [blame] | 1030 | snapshot("checking singletons") |
bauerb@chromium.org | 4cea01e | 2012-03-20 19:49:05 +0000 | [diff] [blame] | 1031 | results.extend(CheckSingletonInHeaders( |
bradnelson@google.com | 56e48bc | 2011-03-24 20:51:21 +0000 | [diff] [blame] | 1032 | input_api, output_api, source_file_filter=sources)) |
maruel@chromium.org | b1ce775 | 2011-05-08 13:50:16 +0000 | [diff] [blame] | 1033 | |
| 1034 | # The following checks are only done on commit, since the commit bot will |
| 1035 | # auto-fix most of these. |
| 1036 | if input_api.is_committing: |
maruel@chromium.org | 8571dac | 2011-05-10 18:10:13 +0000 | [diff] [blame] | 1037 | snapshot("checking eol style") |
| 1038 | results.extend(input_api.canned_checks.CheckChangeSvnEolStyle( |
| 1039 | input_api, output_api, source_file_filter=text_files)) |
maruel@chromium.org | b1ce775 | 2011-05-08 13:50:16 +0000 | [diff] [blame] | 1040 | snapshot("checking svn mime types") |
| 1041 | results.extend(input_api.canned_checks.CheckSvnForCommonMimeTypes( |
| 1042 | input_api, output_api)) |
| 1043 | snapshot("checking license") |
| 1044 | results.extend(input_api.canned_checks.CheckLicense( |
| 1045 | input_api, output_api, license_header, source_file_filter=sources)) |
maruel@chromium.org | cc73ad6 | 2011-07-06 17:39:26 +0000 | [diff] [blame] | 1046 | snapshot("checking was uploaded") |
| 1047 | results.extend(input_api.canned_checks.CheckChangeWasUploaded( |
| 1048 | input_api, output_api)) |
ilevy@chromium.org | c50d761 | 2012-12-05 04:25:14 +0000 | [diff] [blame] | 1049 | snapshot("checking description") |
| 1050 | results.extend(input_api.canned_checks.CheckChangeHasDescription( |
| 1051 | input_api, output_api)) |
| 1052 | results.extend(input_api.canned_checks.CheckDoNotSubmitInDescription( |
| 1053 | input_api, output_api)) |
ilevy@chromium.org | c50d761 | 2012-12-05 04:25:14 +0000 | [diff] [blame] | 1054 | snapshot("checking do not submit in files") |
| 1055 | results.extend(input_api.canned_checks.CheckDoNotSubmitInFiles( |
| 1056 | input_api, output_api)) |
nick@chromium.org | e06cb4e | 2011-04-23 01:20:38 +0000 | [diff] [blame] | 1057 | snapshot("done") |
bradnelson@google.com | 56e48bc | 2011-03-24 20:51:21 +0000 | [diff] [blame] | 1058 | return results |
enne@chromium.org | 3b7e15c | 2014-01-21 17:44:47 +0000 | [diff] [blame] | 1059 | |
| 1060 | |
| 1061 | def CheckPatchFormatted(input_api, output_api): |
| 1062 | import git_cl |
enne@chromium.org | 555cfe4 | 2014-01-29 18:21:39 +0000 | [diff] [blame] | 1063 | cmd = ['cl', 'format', '--dry-run', input_api.PresubmitLocalPath()] |
| 1064 | code, _ = git_cl.RunGitWithCode(cmd, suppress_stderr=True) |
enne@chromium.org | 3b7e15c | 2014-01-21 17:44:47 +0000 | [diff] [blame] | 1065 | if code == 2: |
| 1066 | return [output_api.PresubmitPromptWarning( |
| 1067 | 'Your patch is not formatted, please run git cl format.')] |
| 1068 | # As this is just a warning, ignore all other errors if the user |
| 1069 | # happens to have a broken clang-format, doesn't use git, etc etc. |
| 1070 | return [] |