lintChanges.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/python
  2. from __future__ import print_function
  3. from __future__ import with_statement
  4. import sys
  5. import re
  6. import os
  7. KNOWN_GROUPS=set([
  8. "Minor bugfix",
  9. "Minor bugfixes",
  10. "Major bugfix",
  11. "Major bugfixes",
  12. "Minor feature",
  13. "Minor features",
  14. "Major feature",
  15. "Major features",
  16. "New system requirements",
  17. "Testing",
  18. "Documentation",
  19. "Code simplification and refactoring",
  20. "Removed features"])
  21. def lintfile(fname):
  22. have_warned = []
  23. def warn(s):
  24. if not have_warned:
  25. have_warned.append(1)
  26. print("{}:".format(fname))
  27. print("\t{}".format(s))
  28. m = re.search(r'(\d{3,})', os.path.basename(fname))
  29. if m:
  30. bugnum = m.group(1)
  31. else:
  32. bugnum = None
  33. with open(fname) as f:
  34. contents = f.read()
  35. if bugnum and bugnum not in contents:
  36. warn("bug number {} does not appear".format(bugnum))
  37. lines = contents.split("\n")
  38. m = re.match(r'^[ ]{2}o ([^\(:]*)([^:]*):', contents)
  39. if not m:
  40. warn("header not in format expected")
  41. elif m.group(1).strip() not in KNOWN_GROUPS:
  42. warn("Weird header: %r"%m.group(1))
  43. elif ( ("bugfix" in m.group(1) or "feature" in m.group(1)) and
  44. ("Removed" not in m.group(1)) and
  45. '(' not in m.group(2)):
  46. warn("Missing subcategory on %s"%m.group(1))
  47. if m:
  48. isBug = ("bug" in m.group(1).lower() or "fix" in m.group(1).lower())
  49. else:
  50. isBug = False
  51. contents = " ".join(contents.split())
  52. if re.search(r'\#\d{2,}', contents):
  53. warn("don't use a # before ticket numbers")
  54. if isBug and not re.search(r'(\d+)', contents):
  55. warn("bugfix does not mention a number")
  56. elif isBug and not re.search(r'Fixes ([a-z ]*)bug (\d+)', contents):
  57. warn("bugfix does not say 'Fixes bug XXX'")
  58. if re.search(r'[bB]ug (\d+)', contents):
  59. if not re.search(r'[Bb]ugfix on ', contents):
  60. warn("bugfix does not say 'bugfix on X.Y.Z'")
  61. elif not re.search('[fF]ixes ([a-z ]*)bug (\d+); bugfix on ',
  62. contents):
  63. warn("bugfix incant is not semicoloned")
  64. if __name__ == '__main__':
  65. for fname in sys.argv[1:]:
  66. if fname.endswith("~"):
  67. continue
  68. lintfile(fname)