presubmit_canned_checks.py 3.2 KB

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