OLD | NEW |
(Empty) | |
| 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 |
| 9 def CheckChangeHasTestedField(input_api, output_api): |
| 10 """Requires that the changelist have a TESTED= field.""" |
| 11 if input_api.change.Tested: |
| 12 return [] |
| 13 else: |
| 14 return [output_api.PresubmitError("Changelist must have a TESTED= field.")] |
| 15 |
| 16 |
| 17 def CheckChangeHasQaField(input_api, output_api): |
| 18 """Requires that the changelist have a QA= field.""" |
| 19 if input_api.change.QA: |
| 20 return [] |
| 21 else: |
| 22 return [output_api.PresubmitError("Changelist must have a QA= field.")] |
| 23 |
| 24 |
| 25 def CheckDoNotSubmitInDescription(input_api, output_api): |
| 26 """Checks that the user didn't add 'DO NOT ' + 'SUBMIT' to the CL description. |
| 27 """ |
| 28 keyword = 'DO NOT ' + 'SUBMIT' |
| 29 if keyword in input_api.change.DescriptionText(): |
| 30 return [output_api.PresubmitError( |
| 31 keyword + " is present in the changelist description.")] |
| 32 else: |
| 33 return [] |
| 34 |
| 35 |
| 36 def CheckDoNotSubmitInFiles(input_api, output_api): |
| 37 """Checks that the user didn't add 'DO NOT ' + 'SUBMIT' to any files.""" |
| 38 keyword = 'DO NOT ' + 'SUBMIT' |
| 39 for f, line_num, line in input_api.RightHandSideLines(): |
| 40 if keyword in line: |
| 41 text = 'Found ' + keyword + ' in %s, line %s' % (f.LocalPath(), line_num) |
| 42 return [output_api.PresubmitError(text)] |
| 43 return [] |
| 44 |
| 45 |
| 46 def CheckDoNotSubmit(input_api, output_api): |
| 47 return ( |
| 48 CheckDoNotSubmitInDescription(input_api, output_api) + |
| 49 CheckDoNotSubmitInFiles(input_api, output_api) |
| 50 ) |
| 51 |
| 52 |
| 53 def CheckChangeHasNoTabs(input_api, output_api): |
| 54 """Checks that there are no tab characters in any of the text files to be |
| 55 submitted. |
| 56 """ |
| 57 for f, line_num, line in input_api.RightHandSideLines(): |
| 58 if '\t' in line: |
| 59 return [output_api.PresubmitError( |
| 60 "Found a tab character in %s, line %s" % |
| 61 (f.LocalPath(), line_num))] |
| 62 return [] |
| 63 |
| 64 |
| 65 def CheckLongLines(input_api, output_api, maxlen=80): |
| 66 """Checks that there aren't any lines longer than maxlen characters in any of |
| 67 the text files to be submitted. |
| 68 """ |
| 69 basename = input_api.basename |
| 70 |
| 71 bad = [] |
| 72 for f, line_num, line in input_api.RightHandSideLines(): |
| 73 if line.endswith('\n'): |
| 74 line = line[:-1] |
| 75 if len(line) > maxlen: |
| 76 bad.append( |
| 77 '%s, line %s, %s chars' % |
| 78 (basename(f.LocalPath()), line_num, len(line))) |
| 79 if len(bad) == 5: # Just show the first 5 errors. |
| 80 break |
| 81 |
| 82 if bad: |
| 83 msg = "Found lines longer than %s characters (first 5 shown)." % maxlen |
| 84 return [output_api.PresubmitPromptWarning(msg, items=bad)] |
| 85 else: |
| 86 return [] |
| 87 |
| 88 |
| 89 def CheckTreeIsOpen(input_api, output_api, url, closed): |
| 90 """Checks that an url's content doesn't match a regexp that would mean that |
| 91 the tree is closed.""" |
| 92 try: |
| 93 connection = input_api.urllib2.urlopen(url) |
| 94 status = connection.read() |
| 95 connection.close() |
| 96 if input_api.re.match(closed, status): |
| 97 long_text = status + '\n' + url |
| 98 return [output_api.PresubmitError("The tree is closed.", |
| 99 long_text=long_text)] |
| 100 except IOError: |
| 101 pass |
| 102 return [] |
OLD | NEW |