sortChanges.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/python
  2. # Copyright (c) 2014-2015, The Tor Project, Inc.
  3. # See LICENSE for licensing information
  4. """This script sorts a bunch of changes files listed on its command
  5. line into roughly the order in which they should appear in the
  6. changelog.
  7. """
  8. import re
  9. import sys
  10. def fetch(fn):
  11. with open(fn) as f:
  12. s = f.read()
  13. s = "%s\n" % s.rstrip()
  14. return s
  15. def score(s,fname=None):
  16. m = re.match(r'^ +o ([^\n]*)\n(.*)', s, re.M|re.S)
  17. if not m:
  18. print >>sys.stderr, "Can't score %r from %s"%(s,fname)
  19. lw = m.group(1).lower()
  20. if lw.startswith("major feature"):
  21. score = 0
  22. elif lw.startswith("major bug"):
  23. score = 1
  24. elif lw.startswith("major"):
  25. score = 2
  26. elif lw.startswith("minor feature"):
  27. score = 10
  28. elif lw.startswith("minor bug"):
  29. score = 11
  30. elif lw.startswith("minor"):
  31. score = 12
  32. else:
  33. score = 100
  34. return (score, lw, m.group(1), m.group(2))
  35. def splitChanges(s):
  36. this_entry = []
  37. for line in s.split("\n"):
  38. if line.strip() == "":
  39. continue
  40. if re.match(r" +o ", line):
  41. if len(this_entry) > 2:
  42. yield "".join(this_entry)
  43. curHeader = line
  44. this_entry = [ curHeader, "\n" ]
  45. continue
  46. elif re.match(r" +- ", line):
  47. if len(this_entry) > 2:
  48. yield "".join(this_entry)
  49. this_entry = [ curHeader, "\n" ]
  50. this_entry.append(line)
  51. this_entry.append("\n")
  52. if len(this_entry) > 2:
  53. yield "".join(this_entry)
  54. changes = []
  55. for fn in sys.argv[1:]:
  56. if fn.endswith('~'):
  57. continue
  58. for change in splitChanges(fetch(fn)):
  59. changes.append(score(change,fn))
  60. changes.sort()
  61. last_lw = "this is not a header"
  62. for _, lw, header, rest in changes:
  63. if lw == last_lw:
  64. print rest,
  65. else:
  66. print
  67. print " o",header
  68. print rest,
  69. last_lw = lw