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