4
0

release_upgrade_notes.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/env python3
  2. """Move upgrade notes that shipped in a release under that release's heading.
  3. Upgrade notes for changes that are merged but not yet released live under the
  4. "Next release (unreleased)" section of upgrade_notes.adoc. When a release is
  5. tagged, every note that was in the unreleased section *at the tagged commit*
  6. moves into a new "== <version>" section. Notes added to the unreleased section
  7. after the release was cut stay where they are.
  8. Usage:
  9. release_upgrade_notes.py <version> <released-file> <target-file>
  10. <released-file> is upgrade_notes.adoc as it was at the release tag, and
  11. <target-file> is the copy to update in place (normally on the next branch).
  12. """
  13. from __future__ import annotations
  14. import sys
  15. from pathlib import Path
  16. UNRELEASED_HEADING = "== Next release (unreleased)"
  17. def split_sections(lines: list[str], prefix: str) -> tuple[list[str], list[list[str]]]:
  18. """Split lines into a preamble and blocks that each start at a heading.
  19. An anchor line such as [#some-id] directly above a heading belongs to that
  20. heading's block, so that anchors move with their notes.
  21. """
  22. preamble: list[str] = []
  23. blocks: list[list[str]] = []
  24. for line in lines:
  25. if line.startswith(prefix) and not line.startswith(prefix + "="):
  26. anchor = _take_anchor(preamble if not blocks else blocks[-1])
  27. blocks.append(anchor + [line])
  28. elif blocks:
  29. blocks[-1].append(line)
  30. else:
  31. preamble.append(line)
  32. return preamble, blocks
  33. def _take_anchor(block: list[str]) -> list[str]:
  34. if block and block[-1].startswith("[#"):
  35. return [block.pop()]
  36. return []
  37. def note_key(note: list[str]) -> str:
  38. """Identify a note by its anchor, falling back to its heading."""
  39. for line in note:
  40. if line.startswith("[#") or line.startswith("=== "):
  41. return line.strip()
  42. return ""
  43. def section_heading(section: list[str]) -> str:
  44. for line in section:
  45. if not line.startswith("[#"):
  46. return line.rstrip()
  47. return ""
  48. def unreleased_notes(lines: list[str]) -> tuple[list[str], list[list[str]]] | None:
  49. _, sections = split_sections(lines, "== ")
  50. for section in sections:
  51. if section_heading(section) == UNRELEASED_HEADING:
  52. return split_sections(section, "=== ")
  53. return None
  54. def released_keys(released_lines: list[str]) -> set[str]:
  55. found = unreleased_notes(released_lines)
  56. if found is None:
  57. return set()
  58. return {note_key(note) for note in found[1]}
  59. def build_version_section(version: str, notes: list[list[str]]) -> list[str]:
  60. section = [f"== {version}\n", "\n"]
  61. for note in notes:
  62. section.extend(note)
  63. return section
  64. def build_unreleased_section(heading: list[str], notes: list[list[str]]) -> list[str]:
  65. if not notes:
  66. return []
  67. section = list(heading)
  68. for note in notes:
  69. section.extend(note)
  70. return section
  71. def move_released_notes(version: str, released_lines: list[str], target_lines: list[str]) -> list[str] | None:
  72. """Return the updated target lines, or None when there is nothing to move."""
  73. keys = released_keys(released_lines)
  74. if not keys:
  75. return None
  76. preamble, sections = split_sections(target_lines, "== ")
  77. if any(section_heading(section) == f"== {version}" for section in sections):
  78. return None
  79. result = list(preamble)
  80. moved = False
  81. for section in sections:
  82. if section_heading(section) != UNRELEASED_HEADING:
  83. result.extend(section)
  84. continue
  85. heading, notes = split_sections(section, "=== ")
  86. shipped = [note for note in notes if note_key(note) in keys]
  87. if not shipped:
  88. result.extend(section)
  89. continue
  90. remaining = [note for note in notes if note_key(note) not in keys]
  91. result.extend(build_unreleased_section(heading, remaining))
  92. result.extend(build_version_section(version, shipped))
  93. moved = True
  94. return result if moved else None
  95. def main(argv: list[str]) -> int:
  96. if len(argv) != 4:
  97. print(__doc__, file=sys.stderr)
  98. return 2
  99. version, released_path, target_path = argv[1], Path(argv[2]), Path(argv[3])
  100. released_lines = released_path.read_text().splitlines(keepends=True)
  101. target_lines = target_path.read_text().splitlines(keepends=True)
  102. updated = move_released_notes(version, released_lines, target_lines)
  103. if updated is None:
  104. print(f"Nothing to move for {version}: no unreleased upgrade notes shipped, or the section already exists.")
  105. return 0
  106. target_path.write_text("".join(updated))
  107. print(f"Moved released upgrade notes under '== {version}' in {target_path}.")
  108. return 0
  109. if __name__ == "__main__":
  110. sys.exit(main(sys.argv))