1- """Command line executable allowing to update CMakeUrls.cmake
2- given a CMake version.
1+ #!/usr/bin/env python3
2+ """
3+ Command line executable allowing to update CMakeUrls.cmake given a CMake
4+ version.
35"""
46
57import argparse
@@ -34,48 +36,56 @@ def _major_minor(version):
3436
3537
3638def get_cmake_archive_urls_and_sha256s (version , verbose = False ):
37- files_base_url = "https://api.github.com/repos/Kitware/CMake/releases/tags/v%s" % version
39+ files_base_url = (
40+ "https://api.github.com/repos/Kitware/CMake/releases/tags/v%s" % version
41+ )
3842
3943 with _log ("Collecting URLs and SHA256s from '%s'" % files_base_url ):
4044
41- assets = requests .get (files_base_url ).json ()[' assets' ]
45+ assets = requests .get (files_base_url ).json ()[" assets" ]
4246
4347 sha_256_file = "cmake-%s-SHA-256.txt" % version
4448
4549 expected_files = {
46- "cmake-%s.tar.gz" % version : "unix_source" ,
47- "cmake-%s.zip" % version : "win_source" ,
48- "cmake-%s-linux-x86_64.tar.gz" % version : "linux64_binary" ,
50+ "cmake-%s.tar.gz" % version : "unix_source" ,
51+ "cmake-%s.zip" % version : "win_source" ,
52+ "cmake-%s-linux-x86_64.tar.gz" % version : "linux64_binary" ,
4953 "cmake-%s-macos10.10-universal.tar.gz" % version : "macos10_10_binary" ,
50- "cmake-%s-windows-i386.zip" % version : "win32_binary" ,
51- "cmake-%s-windows-x86_64.zip" % version : "win64_binary" ,
54+ "cmake-%s-windows-i386.zip" % version : "win32_binary" ,
55+ "cmake-%s-windows-x86_64.zip" % version : "win64_binary" ,
5256 }
5357
5458 # Get SHA256s for each asset
5559 shas = {}
5660 for asset in assets :
57- if asset [' name' ] == sha_256_file :
58- sha_256_url = asset [' browser_download_url' ]
61+ if asset [" name" ] == sha_256_file :
62+ sha_256_url = asset [" browser_download_url" ]
5963 for line in requests .get (sha_256_url ).text .splitlines ():
6064 file = line .split ()[1 ].strip ()
6165 if file in expected_files :
6266 sha256 = line .split ()[0 ].strip ()
6367 identifier = expected_files [file ]
6468 shas [identifier ] = sha256
65- assert len (shas ) == len (expected_files ), "{} != {}" .format (len (shas ), len (expected_files ))
69+ assert len (shas ) == len (expected_files ), "{} != {}" .format (
70+ len (shas ), len (expected_files )
71+ )
6672
6773 # Get download URLs for each asset
6874 urls = {}
6975 for asset in assets :
70- if asset [' name' ] in expected_files :
71- identifier = expected_files [asset [' name' ]]
72- urls [identifier ] = asset [' browser_download_url' ]
76+ if asset [" name" ] in expected_files :
77+ identifier = expected_files [asset [" name" ]]
78+ urls [identifier ] = asset [" browser_download_url" ]
7379 if len (urls ) != len (expected_files ):
74- expected_files_by_identifier = {value : key for key , value in expected_files .items ()}
80+ expected_files_by_identifier = {
81+ value : key for key , value in expected_files .items ()
82+ }
7583 missing_files = []
7684 for identifier in set (expected_files .values ()) - set (urls .keys ()):
7785 missing_files .append (expected_files_by_identifier [identifier ])
78- raise RuntimeError ("Couldn't find %s at %s" % (missing_files , files_base_url ))
86+ raise RuntimeError (
87+ "Couldn't find %s at %s" % (missing_files , files_base_url )
88+ )
7989
8090 # combine the URLs and SHA256s into a single dictionary
8191 zipped = {}
@@ -99,7 +109,8 @@ def generate_cmake_variables(urls_and_sha256s):
99109 template_inputs ["%s_url" % var_prefix ] = urls_and_sha256s [0 ]
100110 template_inputs ["%s_sha256" % var_prefix ] = urls_and_sha256s [1 ]
101111
102- cmake_variables = textwrap .dedent ("""
112+ cmake_variables = textwrap .dedent (
113+ """
103114 #-----------------------------------------------------------------------------
104115 # CMake sources
105116 set(unix_source_url "{unix_source_url}")
@@ -125,14 +136,14 @@ def generate_cmake_variables(urls_and_sha256s):
125136
126137 set(win64_binary_url "{win64_binary_url}")
127138 set(win64_binary_sha256 "{win64_binary_sha256}")
128- """ ).format (** template_inputs )
139+ """
140+ ).format (** template_inputs )
129141
130142 return cmake_variables
131143
132144
133145def update_cmake_urls_script (version ):
134- content = generate_cmake_variables (
135- get_cmake_archive_urls_and_sha256s (version ))
146+ content = generate_cmake_variables (get_cmake_archive_urls_and_sha256s (version ))
136147 cmake_urls_filename = "CMakeUrls.cmake"
137148 cmake_urls_filepath = os .path .join (ROOT_DIR , cmake_urls_filename )
138149
@@ -145,42 +156,55 @@ def _update_file(filepath, regex, replacement):
145156 msg = "Updating %s" % os .path .relpath (filepath , ROOT_DIR )
146157 with _log (msg ):
147158 pattern = re .compile (regex )
148- with open (filepath , 'r' ) as doc_file :
159+ with open (filepath , "r" ) as doc_file :
149160 lines = doc_file .readlines ()
150161 updated_content = []
151162 for line in lines :
152- updated_content .append (
153- re .sub (pattern , replacement , line ))
163+ updated_content .append (re .sub (pattern , replacement , line ))
154164 with open (filepath , "w" ) as doc_file :
155165 doc_file .writelines (updated_content )
156166
157167
158168def update_docs (version ):
159169 pattern = re .compile (
160- r"CMake \d.(\d)+.\d <https://cmake.org/cmake/help/v\d.(\d)+/index.html>" )
161- replacement = (
162- "CMake %s <https://cmake.org/cmake/help/v%s/index.html>" % (
163- version , _major_minor (version )))
164- for filename in ["docs/index.rst" , "README.rst" ]:
170+ r"CMake \d.(\d)+.\d <https://cmake.org/cmake/help/v\d.(\d)+/index.html>"
171+ )
172+ replacement = "CMake %s <https://cmake.org/cmake/help/v%s/index.html>" % (
173+ version ,
174+ _major_minor (version ),
175+ )
176+ for filename in {"docs/index.rst" , "README.rst" }:
165177 _update_file (os .path .join (ROOT_DIR , filename ), pattern , replacement )
166178
167179
168180def update_tests (version ):
169181 pattern = re .compile (r'expected_version = "\d.(\d)+.\d"' )
170182 replacement = 'expected_version = "%s"' % version
171- _update_file (os .path .join (
172- ROOT_DIR , "tests/test_distribution.py" ), pattern , replacement )
183+ _update_file (
184+ os .path .join (ROOT_DIR , "tests/test_distribution.py" ), pattern , replacement
185+ )
186+
187+
188+ def update_raw_versions (version ):
189+ pattern = re .compile (r"\d\.(\d)+\.\d" )
190+ replacement = version
191+ _update_file (
192+ os .path .join (ROOT_DIR , "docs/update_cmake_version.rst" ), pattern , replacement
193+ )
173194
174195
175196def main ():
176197 parser = argparse .ArgumentParser (description = __doc__ )
177198 parser .add_argument (
178- 'cmake_version' , metavar = 'CMAKE_VERSION' , type = str ,
179- help = 'CMake version of the form X.Y.Z'
199+ "cmake_version" ,
200+ metavar = "CMAKE_VERSION" ,
201+ type = str ,
202+ help = "CMake version of the form X.Y.Z" ,
180203 )
181204 parser .add_argument (
182- '--collect-only' , action = 'store_true' ,
183- help = 'If specified, only display the archive URLs and associated hashsums'
205+ "--collect-only" ,
206+ action = "store_true" ,
207+ help = "If specified, only display the archive URLs and associated hashsums" ,
184208 )
185209 args = parser .parse_args ()
186210 if args .collect_only :
@@ -189,6 +213,19 @@ def main():
189213 update_cmake_urls_script (args .cmake_version )
190214 update_docs (args .cmake_version )
191215 update_tests (args .cmake_version )
216+ update_raw_versions (args .cmake_version )
217+
218+ print (
219+ """Complete! Now run:
220+
221+ git switch -c update-to-cmake-{release}
222+ git add CMakeUrls.cmake docs/index.rst README.rst tests/test_distribution.py docs/update_cmake_version.rst
223+ git commit -m "Update to CMake {release}"
224+ gh pr create --fill --body "Created by update_cmake_version.py"
225+ """ .format (
226+ release = args .cmake_version
227+ )
228+ )
192229
193230
194231if __name__ == "__main__" :
0 commit comments