Debian Codegraph @

script/bump_changelog.py

raw
1 # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
2 # For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
3 # Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt
4
5 """
6 This script permits to upgrade the changelog in astroid or pylint when releasing a version.
7 """
8
9 # pylint: disable=logging-fstring-interpolation
10
11 from __future__ import annotations
12
13 import argparse
14 import enum
15 import logging
16 from datetime import datetime
17 from pathlib import Path
18
19 DEFAULT_CHANGELOG_PATH = Path("ChangeLog")
20
21 RELEASE_DATE_TEXT = "Release date: TBA"
22 WHATS_NEW_TEXT = "What's New in astroid"
23 TODAY = datetime.now()
24 FULL_WHATS_NEW_TEXT = WHATS_NEW_TEXT + " {version}?"
25 NEW_RELEASE_DATE_MESSAGE = f"Release date: {TODAY.strftime('%Y-%m-%d')}"
26
27
28 def main() -> None:
29 parser = argparse.ArgumentParser(__doc__)
30 parser.add_argument("version", help="The version we want to release")
31 parser.add_argument(
32 "-v", "--verbose", action="store_true", default=False, help="Logging or not"
33 )
34 args = parser.parse_args()
35 if args.verbose:
36 logging.basicConfig(level=logging.DEBUG)
37 logging.debug(f"Launching bump_changelog with args: {args}")
38 if any(s in args.version for s in ("dev", "a", "b", "rc")):
39 return
40 with open(DEFAULT_CHANGELOG_PATH, encoding="utf-8") as f:
41 content = f.read()
42 content = transform_content(content, args.version)
43 with open(DEFAULT_CHANGELOG_PATH, "w", encoding="utf8") as f:
44 f.write(content)
45
46
47 class VersionType(enum.Enum):
48 MAJOR = 0
49 MINOR = 1
50 PATCH = 2
51
52
53 def get_next_version(version: str, version_type: VersionType) -> str:
54 new_version = version.split(".")
55 part_to_increase = new_version[version_type.value]
56 if "-" in part_to_increase:
57 part_to_increase = part_to_increase.split("-")[0]
58 for i in range(version_type.value, 3):
59 new_version[i] = "0"
60 new_version[version_type.value] = str(int(part_to_increase) + 1)
61 return ".".join(new_version)
62
63
64 def get_next_versions(version: str, version_type: VersionType) -> list[str]:
65 if version_type == VersionType.PATCH:
66 # "2.6.1" => ["2.6.2"]
67 return [get_next_version(version, VersionType.PATCH)]
68 if version_type == VersionType.MINOR:
69 # "2.6.0" => ["2.7.0", "2.6.1"]
70 assert version.endswith(".0"), f"{version} does not look like a minor version"
71 else:
72 # "3.0.0" => ["3.1.0", "3.0.1"]
73 assert version.endswith(".0.0"), f"{version} does not look like a major version"
74 next_minor_version = get_next_version(version, VersionType.MINOR)
75 next_patch_version = get_next_version(version, VersionType.PATCH)
76 logging.debug(f"Getting the new version for {version} - {version_type.name}")
77 return [next_minor_version, next_patch_version]
78
79
80 def get_version_type(version: str) -> VersionType:
81 if version.endswith(".0.0"):
82 version_type = VersionType.MAJOR
83 elif version.endswith(".0"):
84 version_type = VersionType.MINOR
85 else:
86 version_type = VersionType.PATCH
87 return version_type
88
89
90 def get_whats_new(
91 version: str, add_date: bool = False, change_date: bool = False
92 ) -> str:
93 whats_new_text = FULL_WHATS_NEW_TEXT.format(version=version)
94 result = [whats_new_text, "=" * len(whats_new_text)]
95 if add_date and change_date:
96 result += [NEW_RELEASE_DATE_MESSAGE]
97 elif add_date:
98 result += [RELEASE_DATE_TEXT]
99 elif change_date:
100 raise ValueError("Can't use change_date=True with add_date=False")
101 logging.debug(
102 f"version='{version}', add_date='{add_date}', change_date='{change_date}': {result}"
103 )
104 return "\n".join(result)
105
106
107 def get_all_whats_new(version: str, version_type: VersionType) -> str:
108 result = ""
109 for version_ in get_next_versions(version, version_type=version_type):
110 result += get_whats_new(version_, add_date=True) + "\n" * 4
111 return result
112
113
114 def transform_content(content: str, version: str) -> str:
115 version_type = get_version_type(version)
116 next_version = get_next_version(version, version_type)
117 old_date = get_whats_new(version, add_date=True)
118 new_date = get_whats_new(version, add_date=True, change_date=True)
119 next_version_with_date = get_all_whats_new(version, version_type)
120 do_checks(content, next_version, version, version_type)
121 index = content.find(old_date)
122 logging.debug(f"Replacing\n'{old_date}'\nby\n'{new_date}'\n")
123 content = content.replace(old_date, new_date)
124 end_content = content[index:]
125 content = content[:index]
126 logging.debug(f"Adding:\n'{next_version_with_date}'\n")
127 content += next_version_with_date + end_content
128 return content
129
130
131 def do_checks(content, next_version, version, version_type):
132 err = "in the changelog, fix that first!"
133 NEW_VERSION_ERROR_MSG = (
134 f"The text for this version '{{version}}' did not exists {err}"
135 )
136 NEXT_VERSION_ERROR_MSG = (
137 f"The text for the next version '{{version}}' already exists {err}"
138 )
139 wn_next_version = get_whats_new(next_version)
140 wn_this_version = get_whats_new(version)
141 # There is only one field where the release date is TBA
142 if version_type in [VersionType.MAJOR, VersionType.MINOR]:
143 assert (
144 content.count(RELEASE_DATE_TEXT) <= 1
145 ), f"There should be only one release date 'TBA' ({version}) {err}"
146 else:
147 next_minor_version = get_next_version(version, VersionType.MINOR)
148 assert (
149 content.count(RELEASE_DATE_TEXT) <= 2
150 ), f"There should be only two release dates 'TBA' ({version} and {next_minor_version}) {err}"
151 # There is already a release note for the version we want to release
152 assert content.count(wn_this_version) == 1, NEW_VERSION_ERROR_MSG.format(
153 version=version
154 )
155 # There is no release notes for the next version
156 assert content.count(wn_next_version) == 0, NEXT_VERSION_ERROR_MSG.format(
157 version=next_version
158 )
159
160
161 if __name__ == "__main__":
162 main()