lintChanges.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!/usr/bin/env python
  2. # Future imports for Python 2.7, mandatory in 3.0
  3. from __future__ import division
  4. from __future__ import print_function
  5. from __future__ import unicode_literals
  6. import sys
  7. import re
  8. import os
  9. KNOWN_GROUPS = set([
  10. "Minor bugfix",
  11. "Minor bugfixes",
  12. "Major bugfix",
  13. "Major bugfixes",
  14. "Minor feature",
  15. "Minor features",
  16. "Major feature",
  17. "Major features",
  18. "New system requirements",
  19. "Testing",
  20. "Documentation",
  21. "Code simplification and refactoring",
  22. "Removed features",
  23. "Deprecated features",
  24. "Directory authority changes",
  25. # These aren't preferred, but sortChanges knows how to clean them up.
  26. "Code simplifications and refactoring",
  27. "Code simplification and refactorings",
  28. "Code simplifications and refactorings"])
  29. NEEDS_SUBCATEGORIES = set([
  30. "Minor bugfix",
  31. "Minor bugfixes",
  32. "Major bugfix",
  33. "Major bugfixes",
  34. "Minor feature",
  35. "Minor features",
  36. "Major feature",
  37. "Major features",
  38. ])
  39. def split_tor_version(version):
  40. '''
  41. Return the initial numeric components of the Tor version as a list of ints.
  42. For versions earlier than 0.1.0, returns MAJOR, MINOR, and MICRO.
  43. For versions 0.1.0 and later, returns MAJOR, MINOR, MICRO, and PATCHLEVEL if present.
  44. If the version is malformed, returns None.
  45. '''
  46. version_match = re.match('([0-9]+)\.([0-9]+)\.([0-9]+)(\.([0-9]+))?', version)
  47. if version_match is None:
  48. return None
  49. version_groups = version_match.groups()
  50. if version_groups is None:
  51. return None
  52. if len(version_groups) < 3:
  53. return None
  54. if len(version_groups) != 5:
  55. return None
  56. version_components = version_groups[0:3]
  57. version_components += version_groups[4:5]
  58. try:
  59. version_list = [int(v) for v in version_components if v is not None]
  60. except ValueError:
  61. return None
  62. return version_list
  63. def lintfile(fname):
  64. have_warned = []
  65. def warn(s):
  66. if not have_warned:
  67. have_warned.append(1)
  68. print("{}:".format(fname))
  69. print("\t{}".format(s))
  70. m = re.search(r'(\d{3,})', os.path.basename(fname))
  71. if m:
  72. bugnum = m.group(1)
  73. else:
  74. bugnum = None
  75. with open(fname) as f:
  76. contents = f.read()
  77. if bugnum and bugnum not in contents:
  78. warn("bug number {} does not appear".format(bugnum))
  79. m = re.match(r'^[ ]{2}o ([^\(:]*)([^:]*):', contents)
  80. if not m:
  81. warn("Header not in format expected. (' o Foo:' or ' o Foo (Bar):')")
  82. elif m.group(1).strip() not in KNOWN_GROUPS:
  83. warn("Unrecognized header: %r" % m.group(1))
  84. elif (m.group(1) in NEEDS_SUBCATEGORIES and '(' not in m.group(2)):
  85. warn("Missing subcategory on %r" % m.group(1))
  86. if m:
  87. isBug = ("bug" in m.group(1).lower() or "fix" in m.group(1).lower())
  88. else:
  89. isBug = False
  90. contents = " ".join(contents.split())
  91. if re.search(r'\#\d{2,}', contents):
  92. warn("Don't use a # before ticket numbers. ('bug 1234' not '#1234')")
  93. if isBug and not re.search(r'(\d+)', contents):
  94. warn("Ticket marked as bugfix, but does not mention a number.")
  95. elif isBug and not re.search(r'Fixes ([a-z ]*)bugs? (\d+)', contents):
  96. warn("Ticket marked as bugfix, but does not say 'Fixes bug XXX'")
  97. if re.search(r'[bB]ug (\d+)', contents):
  98. if not re.search(r'[Bb]ugfix on ', contents):
  99. warn("Bugfix does not say 'bugfix on X.Y.Z'")
  100. elif not re.search('[fF]ixes ([a-z ]*)bugs? (\d+)((, \d+)* and \d+)?; bugfix on ',
  101. contents):
  102. warn("Bugfix does not say 'Fixes bug X; bugfix on Y'")
  103. elif re.search('tor-([0-9]+)', contents):
  104. warn("Do not prefix versions with 'tor-'. ('0.1.2', not 'tor-0.1.2'.)")
  105. else:
  106. bugfix_match = re.search('bugfix on ([0-9]+\.[0-9]+\.[0-9]+)', contents)
  107. if bugfix_match is None:
  108. warn("Versions must have at least 3 digits. ('0.1.2', '0.3.4.8', or '0.3.5.1-alpha'.)")
  109. elif bugfix_match.group(0) is None:
  110. warn("Versions must have at least 3 digits. ('0.1.2', '0.3.4.8', or '0.3.5.1-alpha'.)")
  111. else:
  112. bugfix_match = re.search('bugfix on ([0-9a-z][-.0-9a-z]+[0-9a-z])', contents)
  113. bugfix_group = bugfix_match.groups() if bugfix_match is not None else None
  114. bugfix_version = bugfix_group[0] if bugfix_group is not None else None
  115. package_version = os.environ.get('PACKAGE_VERSION', None)
  116. if bugfix_version is None:
  117. # This should be unreachable, unless the patterns are out of sync
  118. warn("Malformed bugfix version.")
  119. elif package_version is not None:
  120. # If $PACKAGE_VERSION isn't set, skip this check
  121. bugfix_split = split_tor_version(bugfix_version)
  122. package_split = split_tor_version(package_version)
  123. if bugfix_split is None:
  124. # This should be unreachable, unless the patterns are out of sync
  125. warn("Malformed bugfix version: '{}'.".format(bugfix_version))
  126. elif package_split is None:
  127. # This should be unreachable, unless the patterns are out of sync, or the package versioning scheme has changed
  128. warn("Malformed $PACKAGE_VERSION: '{}'.".format(package_version))
  129. elif bugfix_split > package_split:
  130. warn("Bugfixes must be made on earlier versions (or this version). (Bugfix on version: '{}', current tor package version: '{}'.)".format(bugfix_version, package_version))
  131. return have_warned != []
  132. def files(args):
  133. """Walk through the arguments: for directories, yield their contents;
  134. for files, just yield the files. Only search one level deep, because
  135. that's how the changes directory is laid out."""
  136. for f in args:
  137. if os.path.isdir(f):
  138. for item in os.listdir(f):
  139. if item.startswith("."): #ignore dotfiles
  140. continue
  141. yield os.path.join(f, item)
  142. else:
  143. yield f
  144. if __name__ == '__main__':
  145. problems = 0
  146. for fname in files(sys.argv[1:]):
  147. if fname.endswith("~"):
  148. continue
  149. if lintfile(fname):
  150. problems += 1
  151. if problems:
  152. sys.exit(1)
  153. else:
  154. sys.exit(0)