lintChanges.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "Deprecated features"])
  22. NEEDS_SUBCATEGORIES = set([
  23. "Minor bugfix",
  24. "Minor bugfixes",
  25. "Major bugfix",
  26. "Major bugfixes",
  27. "Minor feature",
  28. "Minor features",
  29. "Major feature",
  30. "Major features",
  31. ])
  32. def lintfile(fname):
  33. have_warned = []
  34. def warn(s):
  35. if not have_warned:
  36. have_warned.append(1)
  37. print("{}:".format(fname))
  38. print("\t{}".format(s))
  39. m = re.search(r'(\d{3,})', os.path.basename(fname))
  40. if m:
  41. bugnum = m.group(1)
  42. else:
  43. bugnum = None
  44. with open(fname) as f:
  45. contents = f.read()
  46. if bugnum and bugnum not in contents:
  47. warn("bug number {} does not appear".format(bugnum))
  48. m = re.match(r'^[ ]{2}o ([^\(:]*)([^:]*):', contents)
  49. if not m:
  50. warn("Header not in format expected. (' o Foo:' or ' o Foo (Bar):')")
  51. elif m.group(1).strip() not in KNOWN_GROUPS:
  52. warn("Unrecognized header: %r" % m.group(1))
  53. elif (m.group(1) in NEEDS_SUBCATEGORIES and '(' not in m.group(2)):
  54. warn("Missing subcategory on %r" % m.group(1))
  55. if m:
  56. isBug = ("bug" in m.group(1).lower() or "fix" in m.group(1).lower())
  57. else:
  58. isBug = False
  59. contents = " ".join(contents.split())
  60. if re.search(r'\#\d{2,}', contents):
  61. warn("Don't use a # before ticket numbers. ('bug 1234' not '#1234')")
  62. if isBug and not re.search(r'(\d+)', contents):
  63. warn("Ticket marked as bugfix, but does not mention a number.")
  64. elif isBug and not re.search(r'Fixes ([a-z ]*)bug (\d+)', contents):
  65. warn("Ticket marked as bugfix, but does not say 'Fixes bug XXX'")
  66. if re.search(r'[bB]ug (\d+)', contents):
  67. if not re.search(r'[Bb]ugfix on ', contents):
  68. warn("Bugfix does not say 'bugfix on X.Y.Z'")
  69. elif not re.search('[fF]ixes ([a-z ]*)bug (\d+); bugfix on ',
  70. contents):
  71. warn("Bugfix does not say 'Fixes bug X; bugfix on Y'")
  72. elif re.search('tor-([0-9]+)', contents):
  73. warn("Do not prefix versions with 'tor-'. ('0.1.2', not 'tor-0.1.2'.)")
  74. return have_warned != []
  75. def files(args):
  76. """Walk through the arguments: for directories, yield their contents;
  77. for files, just yield the files. Only search one level deep, because
  78. that's how the changes directory is laid out."""
  79. for f in args:
  80. if os.path.isdir(f):
  81. for item in os.listdir(f):
  82. if item.startswith("."): #ignore dotfiles
  83. continue
  84. yield os.path.join(f, item)
  85. else:
  86. yield f
  87. if __name__ == '__main__':
  88. problems = 0
  89. for fname in files(sys.argv[1:]):
  90. if fname.endswith("~"):
  91. continue
  92. if lintfile(fname):
  93. problems += 1
  94. if problems:
  95. sys.exit(1)
  96. else:
  97. sys.exit(0)