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+
214import glob
315import json
416import 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
5062def 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
121133def 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
155166class 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
195206def prepare_new_release (level ):
@@ -215,8 +226,7 @@ def prepare_new_release(level):
215226if __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 )
0 commit comments