lintChanges.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. def lintfile(fname):
  8. have_warned = []
  9. def warn(s):
  10. if not have_warned:
  11. have_warned.append(1)
  12. print("{}:".format(fname))
  13. print("\t{}".format(s))
  14. m = re.search(r'(\d{3,})', os.path.basename(fname))
  15. if m:
  16. bugnum = m.group(1)
  17. else:
  18. bugnum = None
  19. with open(fname) as f:
  20. contents = f.read()
  21. if bugnum and bugnum not in contents:
  22. warn("bug number {} does not appear".format(bugnum))
  23. lines = contents.split("\n")
  24. isBug = ("bug" in lines[0] or "fix" in lines[0])
  25. if not re.match(r'^[ ]{2}o (.*)', contents):
  26. warn("header not in format expected")
  27. contents = " ".join(contents.split())
  28. if re.search(r'\#\d{2,}', contents):
  29. warn("don't use a # before ticket numbers")
  30. if isBug and not re.search(r'(\d+)', contents):
  31. warn("bugfix does not mention a number")
  32. elif isBug and not re.search(r'Fixes ([a-z ]*)bug (\d+)', contents):
  33. warn("bugfix does not say 'Fixes bug XXX'")
  34. if re.search(r'[bB]ug (\d+)', contents):
  35. if not re.search(r'[Bb]ugfix on ', contents):
  36. warn("bugfix does not say 'bugfix on X.Y.Z'")
  37. elif not re.search('[fF]ixes ([a-z ]*)bug (\d+); bugfix on ',
  38. contents):
  39. warn("bugfix incant is not semicoloned")
  40. if __name__ == '__main__':
  41. for fname in sys.argv[1:]:
  42. if fname.endswith("~"):
  43. continue
  44. lintfile(fname)