Skip to content

Commit 47b80ad

Browse files
cleaning up
1 parent 6754ba7 commit 47b80ad

File tree

4 files changed

+100
-101
lines changed

4 files changed

+100
-101
lines changed

.github/scripts/make_release.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
#!/usr/bin/env python3
2+
"""This script is part of the GitHub CI make-release pipeline
3+
4+
It reads the version number from climada/_version.py and then uses the `gh` cli
5+
to create the new release.
6+
7+
"""
28
import glob
39
import re
410
import subprocess
@@ -7,8 +13,8 @@
713
def get_version() -> str:
814
"""Return the current version number, based on the _version.py file."""
915
[version_file] = glob.glob("climada*/_version.py")
10-
with open(version_file) as vf:
11-
content = vf.read()
16+
with open(version_file, 'r', encoding="UTF-8") as vfp:
17+
content = vfp.read()
1218
regex = r'^__version__\s*=\s*[\'\"](.*)[\'\"]\s*$'
1319
mtch = re.match(regex, content)
1420
return mtch.group(1)

.github/scripts/prepare_release.py

Lines changed: 70 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
#!/usr/bin/env python3
2+
"""This script is part of the GitHub CI make-release pipeline
3+
4+
The following preparation steps are executed:
5+
6+
- update version numbers in _version.py and setup.py
7+
- purge the "Unreleased" section of CHANGELOG.md and rename it to the new version number
8+
- copy the README.md file to doc/misc/README.md,
9+
but without the badges as they interfere with the sphinx doc builder
10+
11+
All changes are immediately commited to the repository.
12+
"""
13+
214
import glob
315
import json
416
import re
@@ -36,71 +48,71 @@ def bump_version_number(version_number: str, level: str) -> str:
3648
return ".".join([major, minor, patch])
3749

3850

39-
def update_readme(nvn):
51+
def update_readme(_nvn):
4052
"""align doc/misc/README.md with ./README.md but remove the non-markdown header lines from """
41-
with open("README.md", 'r') as rmin:
53+
with open("README.md", 'r', encoding="UTF-8") as rmin:
4254
lines = [line for line in rmin.readlines() if not line.startswith('[![')]
4355
while not lines[0].strip():
4456
lines = lines[1:]
45-
with open("doc/misc/README.md", 'w') as rmout:
57+
with open("doc/misc/README.md", 'w', encoding="UTF-8") as rmout:
4658
rmout.writelines(lines)
4759
return GitFile('doc/misc/README.md')
4860

4961

5062
def update_changelog(nvn):
5163
"""Rename the "Unreleased" section, remove unused subsections and the code-freeze date,
5264
set the release date to today"""
53-
r = []
54-
crn = None
55-
cr = []
56-
ctn = None
57-
ct = []
58-
with open("CHANGELOG.md", 'r') as cl:
59-
for line in cl.readlines():
65+
releases = []
66+
release_name = None
67+
release = []
68+
section_name = None
69+
section = []
70+
with open("CHANGELOG.md", 'r', encoding="UTF-8") as changelog:
71+
for line in changelog.readlines():
6072
if line.startswith('#'):
6173
if line.startswith('### '):
62-
if ct:
63-
cr.append((ctn, ct))
64-
ctn = line[4:].strip()
65-
ct = []
66-
#print("tag:", ctn)
74+
if section:
75+
release.append((section_name, section))
76+
section_name = line[4:].strip()
77+
section = []
78+
#print("tag:", section_name)
6779
elif line.startswith('## '):
68-
if ct:
69-
cr.append((ctn, ct))
70-
if cr:
71-
r.append((crn, cr))
72-
crn = line[3:].strip()
73-
cr = []
74-
ctn = None
75-
ct = []
76-
#print("release:", crn)
80+
if section:
81+
release.append((section_name, section))
82+
if release:
83+
releases.append((release_name, release))
84+
release_name = line[3:].strip()
85+
release = []
86+
section_name = None
87+
section = []
88+
#print("release:", release_name)
7789
else:
78-
ct.append(line)
79-
if ct:
80-
cr.append((ctn, ct))
81-
if cr:
82-
r.append((crn, cr))
83-
84-
with open("CHANGELOG.md", 'w') as cl:
85-
cl.write("# Changelog\n\n")
86-
for crn, cr in r:
87-
if crn:
88-
if crn.lower() == "unreleased":
89-
crn = nvn
90-
cl.write(f"## {crn}\n")
91-
for ctn, ct in cr:
92-
if any(ln.strip() for ln in ct):
93-
if ctn:
94-
cl.write(f"### {ctn}\n")
95-
lines = [ln.strip() for ln in ct if "code freeze date: " not in ln.lower()]
96-
if not ctn and crn.lower() == nvn:
90+
section.append(line)
91+
if section:
92+
release.append((section_name, section))
93+
if release:
94+
releases.append((release_name, release))
95+
96+
with open("CHANGELOG.md", 'w', encoding="UTF-8") as changelog:
97+
changelog.write("# Changelog\n\n")
98+
for release_name, release in releases:
99+
if release_name:
100+
if release_name.lower() == "unreleased":
101+
release_name = nvn
102+
changelog.write(f"## {release_name}\n")
103+
for section_name, section in release:
104+
if any(ln.strip() for ln in section):
105+
if section_name:
106+
changelog.write(f"### {section_name}\n")
107+
lines = [ln.strip() for ln in section if "code freeze date: " not in ln.lower()]
108+
if not section_name and release_name.lower() == nvn:
97109
print("setting date")
98110
for i, line in enumerate(lines):
99111
if "release date: " in line.lower():
100112
today = time.strftime("%Y-%m-%d")
101113
lines[i] = f"Release date: {today}"
102-
cl.write("\n".join(lines).replace("\n\n", "\n"))
103-
cl.write("\n")
114+
changelog.write("\n".join(lines).replace("\n\n", "\n"))
115+
changelog.write("\n")
104116
return GitFile('CHANGELOG.md')
105117

106118

@@ -120,17 +132,17 @@ def update_setup(new_version_number):
120132

121133
def update_file(file_with_version, regex, new_version_number):
122134
"""Replace the version number(s) in a file, based on a rgular expression."""
123-
with open(file_with_version, 'r') as curf:
135+
with open(file_with_version, 'r', encoding="UTF-8") as curf:
124136
lines = curf.readlines()
125137
successfully_updated = False
126138
for i, line in enumerate(lines):
127-
m = re.match(regex, line)
128-
if m:
129-
lines[i] = f"{m.group(1)}{new_version_number}{m.group(2)}"
139+
mtch = re.match(regex, line)
140+
if mtch:
141+
lines[i] = f"{mtch.group(1)}{new_version_number}{mtch.group(2)}"
130142
successfully_updated = True
131143
if not successfully_updated:
132-
raise Exception(f"cannot determine version of {file_with_version}")
133-
with open(file_with_version, 'w') as newf:
144+
raise RuntimeError(f"cannot determine version of {file_with_version}")
145+
with open(file_with_version, 'w', encoding="UTF-8") as newf:
134146
for line in lines:
135147
newf.write(line)
136148
return GitFile(file_with_version)
@@ -140,16 +152,15 @@ class GitFile():
140152
"""Helper class for `git add`."""
141153
def __init__(self, path):
142154
self.path = path
143-
155+
144156
def gitadd(self):
145157
"""run `git add`"""
146-
answer = subprocess.run(
158+
_gitadd = subprocess.run(
147159
["git", "add", self.path],
148160
check=True,
149161
stdout=subprocess.PIPE,
150162
stderr=subprocess.PIPE,
151163
).stdout.decode("utf8")
152-
#print(answer)
153164

154165

155166
class Git():
@@ -186,10 +197,10 @@ def commit(self, new_version):
186197
except subprocess.CalledProcessError as err:
187198
message = err.stdout.decode("utf8")
188199
print("message:", message)
189-
if ("nothing to commit" in message):
200+
if "nothing to commit" in message:
190201
print("repo already up to date with new version number")
191202
else:
192-
raise Exception(f"failed to run: {message}") from err
203+
raise RuntimeError(f"failed to run: {message}") from err
193204

194205

195206
def prepare_new_release(level):
@@ -215,8 +226,7 @@ def prepare_new_release(level):
215226
if __name__ == "__main__":
216227
from sys import argv
217228
try:
218-
level = argv[1]
229+
LEVEL = argv[1]
219230
except IndexError:
220-
level = "patch"
221-
prepare_new_release(level)
222-
231+
LEVEL = "patch"
232+
prepare_new_release(LEVEL)

.github/scripts/setup_devbranch.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
#!/usr/bin/env python3
2+
"""This script is part of the GitHub CI postrelease-setup-devbranch pipeline
3+
4+
The following preparation steps are executed:
5+
6+
- update version numbers in _version.py and setup.py: append a -dev suffix
7+
- insert a vanilla "unreleased" section on top of CHANGELOG.md
8+
9+
The changes are not commited to the repository. This is dealt with in the bash script
10+
`setup_devbranch.sh` (which is also the caller of this script).
11+
"""
212
import glob
313
import json
414
import re
@@ -23,14 +33,14 @@ def get_last_version() -> str:
2333

2434
def update_changelog():
2535
"""Insert a vanilla "Unreleased" section on top."""
26-
with open("CHANGELOG.md", 'r') as cl:
27-
lines = cl.readlines()
36+
with open("CHANGELOG.md", 'r', encoding="UTF-8") as changelog:
37+
lines = changelog.readlines()
2838

2939
if "## Unreleased" in lines:
3040
return
3141

32-
with open("CHANGELOG.md", 'w') as cl:
33-
cl.write("""# Changelog
42+
with open("CHANGELOG.md", 'w', encoding="UTF-8") as changelog:
43+
changelog.write("""# Changelog
3444
3545
## Unreleased
3646
@@ -53,7 +63,7 @@ def update_changelog():
5363
### Removed
5464
5565
""")
56-
cl.writelines(lines[2:])
66+
changelog.writelines(lines[2:])
5767

5868

5969
def update_version(nvn):
@@ -72,17 +82,17 @@ def update_setup(new_version_number):
7282

7383
def update_file(file_with_version, regex, new_version_number):
7484
"""Replace the version number(s) in a file, based on a rgular expression."""
75-
with open(file_with_version, 'r') as curf:
85+
with open(file_with_version, 'r', encoding="UTF-8") as curf:
7686
lines = curf.readlines()
7787
successfully_updated = False
7888
for i, line in enumerate(lines):
79-
m = re.match(regex, line)
80-
if m:
81-
lines[i] = f"{m.group(1)}{new_version_number}{m.group(2)}"
89+
mtch = re.match(regex, line)
90+
if mtch:
91+
lines[i] = f"{mtch.group(1)}{new_version_number}{mtch.group(2)}"
8292
successfully_updated = True
8393
if not successfully_updated:
84-
raise Exception(f"cannot determine version of {file_with_version}")
85-
with open(file_with_version, 'w') as newf:
94+
raise RuntimeError(f"cannot determine version of {file_with_version}")
95+
with open(file_with_version, 'w', encoding="UTF-8") as newf:
8696
for line in lines:
8797
newf.write(line)
8898

@@ -94,7 +104,7 @@ def setup_devbranch():
94104
Just changes files, all `git` commands are in the setup_devbranch.sh file.
95105
"""
96106
main_version = get_last_version().strip('v')
97-
107+
98108
dev_version = f"{main_version}-dev"
99109

100110
update_setup(dev_version)
@@ -106,4 +116,3 @@ def setup_devbranch():
106116

107117
if __name__ == "__main__":
108118
setup_devbranch()
109-

.github/workflows/minor-release.yml

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)