[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Generic presubmit checks that can be reused by other presubmit checks.""" |
| 7 | |
| 8 | |
[email protected] | 3410d91 | 2009-06-09 20:56:16 | [diff] [blame] | 9 | ### Description checks |
| 10 | |
[email protected] | 00c41e4 | 2009-05-12 21:43:13 | [diff] [blame] | 11 | def CheckChangeHasTestField(input_api, output_api): |
| 12 | """Requires that the changelist have a TEST= field.""" |
[email protected] | e1a524f | 2009-05-27 14:43:46 | [diff] [blame] | 13 | if input_api.change.TEST: |
[email protected] | 00c41e4 | 2009-05-12 21:43:13 | [diff] [blame] | 14 | return [] |
| 15 | else: |
| 16 | return [output_api.PresubmitNotifyResult( |
| 17 | "Changelist should have a TEST= field. TEST=none is allowed.")] |
| 18 | |
| 19 | |
| 20 | def CheckChangeHasBugField(input_api, output_api): |
| 21 | """Requires that the changelist have a BUG= field.""" |
[email protected] | e1a524f | 2009-05-27 14:43:46 | [diff] [blame] | 22 | if input_api.change.BUG: |
[email protected] | 00c41e4 | 2009-05-12 21:43:13 | [diff] [blame] | 23 | return [] |
| 24 | else: |
| 25 | return [output_api.PresubmitNotifyResult( |
| 26 | "Changelist should have a BUG= field. BUG=none is allowed.")] |
| 27 | |
| 28 | |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 29 | def CheckChangeHasTestedField(input_api, output_api): |
| 30 | """Requires that the changelist have a TESTED= field.""" |
[email protected] | e1a524f | 2009-05-27 14:43:46 | [diff] [blame] | 31 | if input_api.change.TESTED: |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 32 | return [] |
| 33 | else: |
| 34 | return [output_api.PresubmitError("Changelist must have a TESTED= field.")] |
| 35 | |
| 36 | |
| 37 | def CheckChangeHasQaField(input_api, output_api): |
| 38 | """Requires that the changelist have a QA= field.""" |
| 39 | if input_api.change.QA: |
| 40 | return [] |
| 41 | else: |
| 42 | return [output_api.PresubmitError("Changelist must have a QA= field.")] |
| 43 | |
| 44 | |
| 45 | def CheckDoNotSubmitInDescription(input_api, output_api): |
| 46 | """Checks that the user didn't add 'DO NOT ' + 'SUBMIT' to the CL description. |
| 47 | """ |
| 48 | keyword = 'DO NOT ' + 'SUBMIT' |
| 49 | if keyword in input_api.change.DescriptionText(): |
| 50 | return [output_api.PresubmitError( |
| 51 | keyword + " is present in the changelist description.")] |
| 52 | else: |
| 53 | return [] |
| 54 | |
| 55 | |
[email protected] | 3410d91 | 2009-06-09 20:56:16 | [diff] [blame] | 56 | ### Content checks |
| 57 | |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 58 | def CheckDoNotSubmitInFiles(input_api, output_api): |
| 59 | """Checks that the user didn't add 'DO NOT ' + 'SUBMIT' to any files.""" |
| 60 | keyword = 'DO NOT ' + 'SUBMIT' |
[email protected] | 3410d91 | 2009-06-09 20:56:16 | [diff] [blame] | 61 | # We want to check every text files, not just source files. |
| 62 | for f, line_num, line in input_api.RightHandSideLines(lambda x: x): |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 63 | if keyword in line: |
| 64 | text = 'Found ' + keyword + ' in %s, line %s' % (f.LocalPath(), line_num) |
| 65 | return [output_api.PresubmitError(text)] |
| 66 | return [] |
| 67 | |
| 68 | |
[email protected] | 3410d91 | 2009-06-09 20:56:16 | [diff] [blame] | 69 | def CheckChangeHasNoCR(input_api, output_api, source_file_filter=None): |
[email protected] | 44a17ad | 2009-06-08 14:14:35 | [diff] [blame] | 70 | """Checks that there are no \r, \r\n (CR or CRLF) characters in any of the |
[email protected] | 3410d91 | 2009-06-09 20:56:16 | [diff] [blame] | 71 | source files to be submitted. |
[email protected] | 44a17ad | 2009-06-08 14:14:35 | [diff] [blame] | 72 | """ |
| 73 | outputs = [] |
[email protected] | 3410d91 | 2009-06-09 20:56:16 | [diff] [blame] | 74 | for f in input_api.AffectedSourceFiles(source_file_filter): |
[email protected] | 44a17ad | 2009-06-08 14:14:35 | [diff] [blame] | 75 | if '\r' in input_api.ReadFile(f, 'rb'): |
| 76 | outputs.append(output_api.PresubmitPromptWarning( |
| 77 | "Found a CR character in %s" % f.LocalPath())) |
| 78 | return outputs |
| 79 | |
| 80 | |
[email protected] | 3410d91 | 2009-06-09 20:56:16 | [diff] [blame] | 81 | def CheckChangeHasNoTabs(input_api, output_api, source_file_filter=None): |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 82 | """Checks that there are no tab characters in any of the text files to be |
| 83 | submitted. |
| 84 | """ |
[email protected] | 3410d91 | 2009-06-09 20:56:16 | [diff] [blame] | 85 | for f, line_num, line in input_api.RightHandSideLines(source_file_filter): |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 86 | if '\t' in line: |
[email protected] | 8949138 | 2009-06-06 18:58:39 | [diff] [blame] | 87 | return [output_api.PresubmitPromptWarning( |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 88 | "Found a tab character in %s, line %s" % |
| 89 | (f.LocalPath(), line_num))] |
| 90 | return [] |
| 91 | |
| 92 | |
[email protected] | 3410d91 | 2009-06-09 20:56:16 | [diff] [blame] | 93 | def CheckLongLines(input_api, output_api, maxlen=80, source_file_filter=None): |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 94 | """Checks that there aren't any lines longer than maxlen characters in any of |
| 95 | the text files to be submitted. |
| 96 | """ |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 97 | bad = [] |
[email protected] | 3410d91 | 2009-06-09 20:56:16 | [diff] [blame] | 98 | for f, line_num, line in input_api.RightHandSideLines(source_file_filter): |
[email protected] | 5c2720e | 2009-06-09 14:04:08 | [diff] [blame] | 99 | # Allow lines with http://, https:// and #define/#pragma/#include/#if/#endif |
| 100 | # to exceed the maxlen rule. |
| 101 | if (len(line) > maxlen and |
| 102 | not 'http://' in line and |
| 103 | not 'https://' in line and |
| 104 | not line.startswith('#define') and |
| 105 | not line.startswith('#include') and |
| 106 | not line.startswith('#pragma') and |
| 107 | not line.startswith('#if') and |
| 108 | not line.startswith('#endif')): |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 109 | bad.append( |
| 110 | '%s, line %s, %s chars' % |
[email protected] | 1487d53 | 2009-06-06 00:22:57 | [diff] [blame] | 111 | (f.LocalPath(), line_num, len(line))) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 112 | if len(bad) == 5: # Just show the first 5 errors. |
| 113 | break |
| 114 | |
| 115 | if bad: |
| 116 | msg = "Found lines longer than %s characters (first 5 shown)." % maxlen |
| 117 | return [output_api.PresubmitPromptWarning(msg, items=bad)] |
| 118 | else: |
| 119 | return [] |
| 120 | |
| 121 | |
[email protected] | 1a0e3cb | 2009-06-10 18:03:04 | [diff] [blame^] | 122 | def CheckChangeSvnEolStyle(input_api, output_api, source_file_filter=None): |
[email protected] | b7d4690 | 2009-06-10 14:12:10 | [diff] [blame] | 123 | """Checks that the source files have svn:eol-style=LF.""" |
| 124 | bad = filter(lambda f: f.scm == 'svn' and f.Property('svn:eol-style') != 'LF', |
| 125 | input_api.AffectedSourceFiles(source_file_filter)) |
| 126 | if bad: |
| 127 | return [output_api.PresubmitError( |
| 128 | "Fix these files with svn svn:eol-style=LF", items=bad)] |
| 129 | return [] |
| 130 | |
| 131 | |
[email protected] | 3410d91 | 2009-06-09 20:56:16 | [diff] [blame] | 132 | ### Other checks |
| 133 | |
| 134 | def CheckDoNotSubmit(input_api, output_api): |
| 135 | return ( |
| 136 | CheckDoNotSubmitInDescription(input_api, output_api) + |
| 137 | CheckDoNotSubmitInFiles(input_api, output_api) |
| 138 | ) |
| 139 | |
| 140 | |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 141 | def CheckTreeIsOpen(input_api, output_api, url, closed): |
| 142 | """Checks that an url's content doesn't match a regexp that would mean that |
| 143 | the tree is closed.""" |
[email protected] | 8949138 | 2009-06-06 18:58:39 | [diff] [blame] | 144 | assert(input_api.is_committing) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 145 | try: |
| 146 | connection = input_api.urllib2.urlopen(url) |
| 147 | status = connection.read() |
| 148 | connection.close() |
| 149 | if input_api.re.match(closed, status): |
| 150 | long_text = status + '\n' + url |
[email protected] | 8949138 | 2009-06-06 18:58:39 | [diff] [blame] | 151 | return [output_api.PresubmitPromptWarning("The tree is closed.", |
| 152 | long_text=long_text)] |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 153 | except IOError: |
| 154 | pass |
| 155 | return [] |
[email protected] | 7b305e8 | 2009-05-19 18:24:20 | [diff] [blame] | 156 | |
| 157 | |
[email protected] | 1487d53 | 2009-06-06 00:22:57 | [diff] [blame] | 158 | def _RunPythonUnitTests_LoadTests(input_api, module_name): |
| 159 | """Meant to be stubbed out during unit testing.""" |
| 160 | module = __import__(module_name) |
| 161 | for part in module_name.split('.')[1:]: |
| 162 | module = getattr(module, part) |
| 163 | return input_api.unittest.TestLoader().loadTestsFromModule(module)._tests |
| 164 | |
[email protected] | 3410d91 | 2009-06-09 20:56:16 | [diff] [blame] | 165 | |
[email protected] | 7b305e8 | 2009-05-19 18:24:20 | [diff] [blame] | 166 | def RunPythonUnitTests(input_api, output_api, unit_tests): |
| 167 | """Imports the unit_tests modules and run them.""" |
[email protected] | d7dccf5 | 2009-06-06 18:51:58 | [diff] [blame] | 168 | # We don't want to hinder users from uploading incomplete patches. |
| 169 | if input_api.is_committing: |
| 170 | message_type = output_api.PresubmitError |
| 171 | else: |
| 172 | message_type = output_api.PresubmitNotifyResult |
[email protected] | 7b305e8 | 2009-05-19 18:24:20 | [diff] [blame] | 173 | tests_suite = [] |
[email protected] | 7b305e8 | 2009-05-19 18:24:20 | [diff] [blame] | 174 | outputs = [] |
| 175 | for unit_test in unit_tests: |
| 176 | try: |
[email protected] | de0ba29 | 2009-06-06 19:43:27 | [diff] [blame] | 177 | tests_suite.extend(_RunPythonUnitTests_LoadTests(input_api, unit_test)) |
[email protected] | 7b305e8 | 2009-05-19 18:24:20 | [diff] [blame] | 178 | except ImportError: |
[email protected] | d7dccf5 | 2009-06-06 18:51:58 | [diff] [blame] | 179 | outputs.append(message_type("Failed to load %s" % unit_test, |
| 180 | long_text=input_api.traceback.format_exc())) |
[email protected] | 7b305e8 | 2009-05-19 18:24:20 | [diff] [blame] | 181 | |
[email protected] | c809ec2 | 2009-06-10 14:25:42 | [diff] [blame] | 182 | buffer = input_api.cStringIO.StringIO() |
| 183 | results = input_api.unittest.TextTestRunner(stream=buffer, verbosity=0).run( |
[email protected] | 1487d53 | 2009-06-06 00:22:57 | [diff] [blame] | 184 | input_api.unittest.TestSuite(tests_suite)) |
[email protected] | 7b305e8 | 2009-05-19 18:24:20 | [diff] [blame] | 185 | if not results.wasSuccessful(): |
[email protected] | de0ba29 | 2009-06-06 19:43:27 | [diff] [blame] | 186 | outputs.append(message_type("%d unit tests failed." % |
[email protected] | c809ec2 | 2009-06-10 14:25:42 | [diff] [blame] | 187 | (len(results.failures) + len(results.errors)), |
| 188 | long_text=buffer.getvalue())) |
[email protected] | 7b305e8 | 2009-05-19 18:24:20 | [diff] [blame] | 189 | return outputs |