-
Notifications
You must be signed in to change notification settings - Fork 14
/
update-version.py
225 lines (186 loc) · 6.25 KB
/
update-version.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python3
"""
update-version.py: Execute the first steps of Transformer's release process.
See https://transformer.readthedocs.io/en/latest/Versioning.html#release-process
for details.
Usage:
update-version.py INCREMENT
update-version.py --help
"""
import enum
import logging
import os
import subprocess
import tempfile
from datetime import datetime
from pathlib import Path
from typing import Sequence
import dataclasses
import packaging.version
import pygments
import pytest
import tomlkit
from dataclasses import dataclass
from docopt import docopt
from pygments.formatters.terminal import TerminalFormatter
from pygments.lexers.diff import DiffLexer
@dataclass
class Version:
major: int
minor: int
patch: int
@classmethod
def parse(cls, s: str) -> "Version":
v = packaging.version.parse(s)
return Version(*v.release)
def __str__(self) -> str:
return ".".join((str(self.major), str(self.minor), str(self.patch)))
@pytest.mark.parametrize(
"argument,expected",
(
("1.0.10", Version(1, 0, 10)),
("12.345.0", Version(12, 345, 0)),
("1.2.3.rc2", Version(1, 2, 3)),
),
)
def test_version_parse(argument: str, expected: Version):
assert Version.parse(argument) == expected
@pytest.mark.parametrize(
"v,expected", ((Version(1, 0, 9), "1.0.9"), (Version(12, 345, 0), "12.345.0"))
)
def test_version_str(v: Version, expected: str):
assert str(v) == expected
@dataclass
class Patch:
target: os.PathLike
lines: Sequence[str]
def apply(self) -> bool:
target = os.fspath(self.target)
with tempfile.NamedTemporaryFile("a+") as f:
f.write("\n".join(self.lines))
f.write("\n")
f.seek(0)
result = subprocess.run(["patch", "-u", target], stdin=f)
if result.returncode == 0:
return True
logging.error(f"failed to apply patch")
logging.info(f"patch was {self!r}")
return False
def pretty(self) -> str:
lines = list(self.lines)
lines[0] += f" {self.target}" # include target name in comments
return pygments.highlight("\n".join(lines), DiffLexer(), TerminalFormatter())
def pyproject_patch(old_v: Version, new_v: Version) -> Patch:
return Patch(
target=Path("pyproject.toml"),
lines=["@@ -3 +3 @@", f'-version = "{old_v}"', f'+version = "{new_v}"'],
)
def sphinx_patch(old_v: Version, new_v: Version) -> Patch:
old_short = f"{old_v.major}.{old_v.minor}"
new_short = f"{new_v.major}.{new_v.minor}"
return Patch(
target=Path("docs", "conf.py"),
lines=[
"@@ -25,5 +25,5 @@",
" # The short X.Y version",
f'-version = "{old_short}"',
f'+version = "{new_short}"',
" # The full version, including alpha/beta/rc tags",
f'-release = "{old_v}"',
f'+release = "{new_v}"',
" ",
],
)
def changelog_patch(old_v: Version, new_v: Version) -> Patch:
new_diff_url = (
"https://github.com/zalando-incubator/transformer/compare/"
f"v{old_v}...v{new_v}"
)
release_date = datetime.now().strftime("%Y-%m-%d %H:%M")
return Patch(
target=Path("docs", "Changelog.rst"),
lines=[
"@@ -17,2 +17,13 @@",
" ",
f"+.. _v{new_v}:",
"+",
f"+v{new_v}",
"+" + ("=" * (1 + len(str(new_v)))),
"+",
f"+- Release date: {release_date}",
"+",
"+- Diff__.",
"+",
f"+__ {new_diff_url}",
"+",
f" .. _v{old_v}:",
],
)
class Increment(enum.IntEnum):
MAJOR = 0
MINOR = 1
PATCH = 2
def increment_version(v: Version, incr: Increment) -> Version:
values = list(dataclasses.astuple(v))
values[incr.value] += 1
for i in range(incr.value + 1, max(Increment).value + 1):
values[i] = 0
return Version(*values)
@pytest.mark.parametrize(
"incr,expected",
(
(Increment.PATCH, Version(1, 0, 10)),
(Increment.MINOR, Version(1, 1, 0)),
(Increment.MAJOR, Version(2, 0, 0)),
),
)
def test_increment_version(incr: Increment, expected: Version):
current_version = Version(1, 0, 9)
assert increment_version(current_version, incr) == expected
assert current_version == Version(1, 0, 9), "argument should not change"
def pyproject_version(pyproject_path: os.PathLike) -> Version:
pyproject_data = tomlkit.parse(Path(pyproject_path).read_text())
return Version.parse(str(pyproject_data["tool"]["poetry"]["version"]))
def run():
opts = docopt(__doc__)
incr_str = opts["INCREMENT"].upper()
try:
incr: Increment = Increment[incr_str]
except KeyError:
logging.fatal(f"Invalid increment value {incr_str!r}")
logging.info(f"Expecting one of {[i.name for i in Increment]}")
raise # PyCharm doesn't get that exit() prevents increments from not existing
# Find the current version
pyproject_path = Path("pyproject.toml")
logging.info(f"Reading {pyproject_path} ...")
old_version = pyproject_version(pyproject_path)
logging.info(f"Current version: {old_version}")
# Compute the new version, according to *incr*.
new_version = increment_version(old_version, incr)
logging.info(f"New version: {new_version} ({old_version} + {incr.name})")
# Compute the corresponding patches.
patches = [
pyproject_patch(old_version, new_version),
changelog_patch(old_version, new_version),
sphinx_patch(old_version, new_version),
]
# Show the patches, in case users want to reverse them later, and apply them.
logging.info("Applying these patches:")
expected_successes = len(patches)
actual_successes = 0
for patch in patches:
print(patch.pretty())
if patch.apply():
actual_successes += 1
print()
logging.info(f"Patched {actual_successes}/{expected_successes} of expected files.")
if actual_successes < expected_successes:
raise RuntimeError()
if __name__ == "__main__":
logging.basicConfig(
level=logging.INFO, format="%(asctime)s\t%(levelname)s\t%(message)s"
)
try:
run()
except Exception:
exit(1)