| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import argparse |
| 6 |
import dataclasses |
| 7 |
import re |
| 8 |
import sys |
| 9 |
import time |
| 10 |
from contextlib import contextmanager |
| 11 |
from pathlib import Path |
| 12 |
from typing import TYPE_CHECKING |
| 13 |
|
| 14 |
if TYPE_CHECKING: |
| 15 |
from collections.abc import Iterator, Sequence |
| 16 |
from typing import Literal |
| 17 |
|
| 18 |
|
| 19 |
script_dir = Path(__file__).resolve().parent |
| 20 |
package_dir = script_dir.parent |
| 21 |
|
| 22 |
|
| 23 |
@dataclasses.dataclass(frozen=True, slots=True) |
| 24 |
class VersionInfo: |
| 25 |
major: int |
| 26 |
minor: int |
| 27 |
micro: int |
| 28 |
level: Literal['a', 'b', 'rc', 'final'] |
| 29 |
serial: int |
| 30 |
|
| 31 |
@property |
| 32 |
def releaselevel(self) -> Literal['alpha', 'beta', 'candidate', 'final']: |
| 33 |
if self.level == 'final': |
| 34 |
return 'final' |
| 35 |
if self.level == 'a': |
| 36 |
return 'alpha' |
| 37 |
if self.level == 'b': |
| 38 |
return 'beta' |
| 39 |
if self.level == 'rc': |
| 40 |
return 'candidate' |
| 41 |
msg = f'Unknown release level: {self.level}' |
| 42 |
raise RuntimeError(msg) |
| 43 |
|
| 44 |
@property |
| 45 |
def is_final(self) -> bool: |
| 46 |
return self.level == 'final' |
| 47 |
|
| 48 |
@property |
| 49 |
def version(self) -> str: |
| 50 |
return f'{self.major}.{self.minor}.{self.micro}' |
| 51 |
|
| 52 |
@property |
| 53 |
def release(self) -> str: |
| 54 |
return f'{self.major}.{self.minor}.{self.micro}{self.level}{self.serial}' |
| 55 |
|
| 56 |
@property |
| 57 |
def version_tuple(self) -> tuple[int, int, int]: |
| 58 |
return self.major, self.minor, self.micro |
| 59 |
|
| 60 |
@property |
| 61 |
def release_tuple(self) -> tuple[int, int, int, str, int]: |
| 62 |
return self.major, self.minor, self.micro, self.releaselevel, self.serial |
| 63 |
|
| 64 |
|
| 65 |
def parse_version(version: str) -> VersionInfo: |
| 66 |
# Final version: |
| 67 |
# - "X.Y.Z" -> (X, Y, Z, 'final', 0) |
| 68 |
# - "X.Y" -> (X, Y, 0, 'final', 0) [shortcut] |
| 69 |
if matched := re.fullmatch(r'(\d+)\.(\d+)(?:\.(\d+))?', version): |
| 70 |
major, minor, micro = matched.groups(default='0') |
| 71 |
return VersionInfo(int(major), int(minor), int(micro), 'final', 0) |
| 72 |
|
| 73 |
# Pre-release versions: |
| 74 |
# - "X.Y.ZaN" -> (X, Y, Z, 'alpha', N) |
| 75 |
# - "X.Y.ZbN" -> (X, Y, Z, 'beta', N) |
| 76 |
# - "X.Y.ZrcN" -> (X, Y, Z, 'candidate', N) |
| 77 |
if matched := re.fullmatch(r'(\d+)\.(\d+)\.(\d+)(a|b|rc)(\d+)', version): |
| 78 |
major, minor, micro, level, serial = matched.groups() |
| 79 |
return VersionInfo(int(major), int(minor), int(micro), level, int(serial)) # type: ignore[arg-type] |
| 80 |
|
| 81 |
msg = f'Unknown version: {version}' |
| 82 |
raise RuntimeError(msg) |
| 83 |
|
| 84 |
|
| 85 |
def bump_version( |
| 86 |
path: Path, version_info: VersionInfo, in_develop: bool = True |
| 87 |
) -> None: |
| 88 |
if in_develop or version_info.is_final: |
| 89 |
version = version_info.version |
| 90 |
else: |
| 91 |
version = version_info.release |
| 92 |
|
| 93 |
with open(path, encoding='utf-8') as f: |
| 94 |
lines = f.read().splitlines(keepends=True) |
| 95 |
|
| 96 |
for i, line in enumerate(lines): |
| 97 |
if line.startswith('__version__: Final = '): |
| 98 |
lines[i] = f"__version__: Final = '{version}'\n" |
| 99 |
continue |
| 100 |
if line.startswith('version_info: Final = '): |
| 101 |
lines[i] = f'version_info: Final = {version_info.release_tuple}\n' |
| 102 |
continue |
| 103 |
if line.startswith('_in_development = '): |
| 104 |
lines[i] = f'_in_development = {in_develop}\n' |
| 105 |
continue |
| 106 |
|
| 107 |
with open(path, 'w', encoding='utf-8') as f: |
| 108 |
f.writelines(lines) |
| 109 |
|
| 110 |
|
| 111 |
class Changes: |
| 112 |
def __init__(self, path: Path) -> None: |
| 113 |
self.path = path |
| 114 |
|
| 115 |
with open(self.path, encoding='utf-8') as f: |
| 116 |
version = f.readline().strip() |
| 117 |
matched = re.fullmatch(r'Release (.*) \((.*)\)', version) |
| 118 |
if matched is None: |
| 119 |
msg = f'Unknown CHANGES format: {version}' |
| 120 |
raise RuntimeError(msg) |
| 121 |
|
| 122 |
self.version, release_date = matched.groups() |
| 123 |
self.in_development = release_date == 'in development' |
| 124 |
self.version_tuple = parse_version(self.version).version_tuple |
| 125 |
|
| 126 |
def finalise_release_date(self) -> None: |
| 127 |
release_date = time.strftime('%b %d, %Y') |
| 128 |
heading = f'Release {self.version} (released {release_date})' |
| 129 |
with open(self.path, 'r+', encoding='utf-8') as f: |
| 130 |
f.readline() # skip first two lines |
| 131 |
f.readline() |
| 132 |
body = f.read() |
| 133 |
|
| 134 |
f.seek(0) |
| 135 |
f.truncate(0) |
| 136 |
f.write(heading + '\n') |
| 137 |
f.write('=' * len(heading) + '\n') |
| 138 |
f.write(self.filter_empty_sections(body)) |
| 139 |
|
| 140 |
def add_release(self, version_info: VersionInfo) -> None: |
| 141 |
heading = f'Release {version_info.version} (in development)' |
| 142 |
tmpl = (script_dir / 'CHANGES_template.rst').read_text(encoding='utf-8') |
| 143 |
with open(self.path, 'r+', encoding='utf-8') as f: |
| 144 |
body = f.read() |
| 145 |
|
| 146 |
f.seek(0) |
| 147 |
f.truncate(0) |
| 148 |
f.write(heading + '\n') |
| 149 |
f.write('=' * len(heading) + '\n') |
| 150 |
f.write('\n') |
| 151 |
f.write(tmpl) |
| 152 |
f.write('\n') |
| 153 |
f.write(body) |
| 154 |
|
| 155 |
@staticmethod |
| 156 |
def filter_empty_sections(body: str) -> str: |
| 157 |
return re.sub( |
| 158 |
'^\n.+\n-{3,}\n+(?=\n.+\n[-=]{3,}\n)', '', body, flags=re.MULTILINE |
| 159 |
) |
| 160 |
|
| 161 |
|
| 162 |
class Skip(Exception): |
| 163 |
pass |
| 164 |
|
| 165 |
|
| 166 |
@contextmanager |
| 167 |
def processing(message: str) -> Iterator[None]: |
| 168 |
try: |
| 169 |
print(message + ' ... ', end='') |
| 170 |
yield |
| 171 |
except Skip as exc: |
| 172 |
print(f'skip: {exc}') |
| 173 |
except Exception: |
| 174 |
print('error') |
| 175 |
raise |
| 176 |
else: |
| 177 |
print('done') |
| 178 |
|
| 179 |
|
| 180 |
def parse_options(argv: Sequence[str]) -> tuple[VersionInfo, bool]: |
| 181 |
parser = argparse.ArgumentParser() |
| 182 |
parser.add_argument('version', help='A version number (cf. 1.6.0b0)') |
| 183 |
parser.add_argument('--in-develop', action='store_true') |
| 184 |
options = parser.parse_args(argv) |
| 185 |
return parse_version(options.version), options.in_develop |
| 186 |
|
| 187 |
|
| 188 |
def main() -> None: |
| 189 |
version, in_develop = parse_options(sys.argv[1:]) |
| 190 |
|
| 191 |
with processing('Rewriting sphinx/__init__.py'): |
| 192 |
bump_version(package_dir / 'sphinx' / '__init__.py', version, in_develop) |
| 193 |
|
| 194 |
with processing('Rewriting CHANGES'): |
| 195 |
changes = Changes(package_dir / 'CHANGES.rst') |
| 196 |
if changes.version_tuple == version.version_tuple: |
| 197 |
if changes.in_development and version.is_final and not in_develop: |
| 198 |
changes.finalise_release_date() |
| 199 |
else: |
| 200 |
reason = 'version not changed' |
| 201 |
raise Skip(reason) |
| 202 |
else: |
| 203 |
if changes.in_development: |
| 204 |
print(f'WARNING: last version is not released yet: {changes.version}') |
| 205 |
changes.add_release(version) |
| 206 |
|
| 207 |
|
| 208 |
if __name__ == '__main__': |
| 209 |
main() |