-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathsetup_devbranch.py
More file actions
122 lines (87 loc) · 3.18 KB
/
Copy pathsetup_devbranch.py
File metadata and controls
122 lines (87 loc) · 3.18 KB
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
#!/usr/bin/env python3
"""This script is part of the GitHub CI postrelease-setup-devbranch pipeline
The following preparation steps are executed:
- update version numbers in _version.py and pyproject.toml: append a -dev suffix
- insert a vanilla "unreleased" section on top of CHANGELOG.md
The changes are not commited to the repository. This is dealt with in the bash script
`setup_devbranch.sh` (which is also the caller of this script).
"""
import glob
import json
import re
import subprocess
def get_last_version() -> str:
"""Return the version number of the last release."""
json_string = (
subprocess.run(
["gh", "release", "view", "--json", "tagName"],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
.stdout.decode("utf8")
.strip()
)
return json.loads(json_string)["tagName"]
def update_changelog():
"""Insert a vanilla "Unreleased" section on top."""
with open("CHANGELOG.md", "r", encoding="UTF-8") as changelog:
lines = changelog.readlines()
if "## Unreleased" in lines:
return
with open("CHANGELOG.md", "w", encoding="UTF-8") as changelog:
changelog.write(
"""# Changelog
## Unreleased
Release date: YYYY-MM-DD
Code freeze date: YYYY-MM-DD
### Description
### Dependency Changes
### Added
### Changed
### Fixed
### Deprecated
### Removed
"""
)
changelog.writelines(lines[2:])
def update_version(nvn):
"""Update the _version.py file"""
[file_with_version] = glob.glob("climada*/_version.py")
regex = r"(^__version__\s*=\s*[\'\"]).*([\'\"]\s*$)"
return update_file(file_with_version, regex, nvn)
def update_pyproject(new_version_number):
"""Update the pyproject.toml file"""
file_with_version = "pyproject.toml"
regex = r"(^version\s*=\s*[\'\"]).*([\'\"]\s*$)"
return update_file(file_with_version, regex, new_version_number)
def update_file(file_with_version, regex, new_version_number):
"""Replace the version number(s) in a file, based on a rgular expression."""
with open(file_with_version, "r", encoding="UTF-8") as curf:
lines = curf.readlines()
successfully_updated = False
for i, line in enumerate(lines):
mtch = re.match(regex, line)
if mtch:
lines[i] = f"{mtch.group(1)}{new_version_number}{mtch.group(2)}"
successfully_updated = True
if not successfully_updated:
raise RuntimeError(f"cannot determine version of {file_with_version}")
with open(file_with_version, "w", encoding="UTF-8") as newf:
for line in lines:
newf.write(line)
def setup_devbranch():
"""Adjust files after a release was published, i.e.,
apply the canonical deviations from main in develop.
Just changes files, all `git` commands are in the setup_devbranch.sh file.
"""
main_version = get_last_version().strip("v")
semver = main_version.split(".")
semver[-1] = f"{int(semver[-1]) + 1}-dev"
dev_version = ".".join(semver)
update_pyproject(dev_version)
update_version(dev_version)
update_changelog()
print(f"v{dev_version}")
if __name__ == "__main__":
setup_devbranch()