|
| 1 | +"""Command line executable allowing to update CMakeUrls.cmake |
| 2 | + given a CMake version. |
| 3 | +""" |
| 4 | + |
| 5 | +import argparse |
| 6 | +import contextlib |
| 7 | +import os |
| 8 | +import re |
| 9 | +import textwrap |
| 10 | + |
| 11 | +try: |
| 12 | + import requests |
| 13 | +except ImportError: |
| 14 | + raise SystemExit( |
| 15 | + "requests not available: " |
| 16 | + "consider installing it running 'pip install requests'" |
| 17 | + ) |
| 18 | + |
| 19 | +try: |
| 20 | + from bs4 import BeautifulSoup |
| 21 | +except ImportError: |
| 22 | + raise SystemExit( |
| 23 | + "BeautifulSoup not available: " |
| 24 | + "consider installing it running 'pip install beautifulsoup4'" |
| 25 | + ) |
| 26 | + |
| 27 | +ROOT_DIR = os.path.join(os.path.dirname(__file__), "..") |
| 28 | + |
| 29 | + |
| 30 | +@contextlib.contextmanager |
| 31 | +def _log(txt, verbose=True): |
| 32 | + if verbose: |
| 33 | + print(txt) |
| 34 | + yield |
| 35 | + if verbose: |
| 36 | + print("%s - done" % txt) |
| 37 | + |
| 38 | + |
| 39 | +def _major_minor(version): |
| 40 | + """Given a string of the form ``X.Y.Z``, returns ``X.Y``.""" |
| 41 | + return ".".join(version.split(".")[:2]) |
| 42 | + |
| 43 | + |
| 44 | +def get_cmake_archive_urls_and_sha256s(version): |
| 45 | + files_base_url = "https://cmake.org/files/v%s" % _major_minor(version) |
| 46 | + |
| 47 | + with _log("Collecting URLs and SHA256s from '%s'" % files_base_url): |
| 48 | + |
| 49 | + soup = BeautifulSoup(requests.get(files_base_url).text, 'html.parser') |
| 50 | + |
| 51 | + sha_256_file = "cmake-%s-SHA-256.txt" % version |
| 52 | + |
| 53 | + expected = { |
| 54 | + "cmake-%s.tar.gz" % version: "unix_source", |
| 55 | + "cmake-%s.zip" % version: "win_source", |
| 56 | + "cmake-%s-Linux-x86_64.tar.gz" % version: "linux64_binary", |
| 57 | + "cmake-%s-Darwin-x86_64.tar.gz" % version: "macosx_binary", |
| 58 | + "cmake-%s-win32-x86.zip" % version: "win32_binary", |
| 59 | + "cmake-%s-win64-x64.zip" % version: "win64_binary", |
| 60 | + } |
| 61 | + |
| 62 | + # Check that (1) "a" text matches "href" value and (2) that all expected |
| 63 | + # files are listed on the page. |
| 64 | + found = 0 |
| 65 | + for a in soup.find_all('a'): |
| 66 | + if a.text in expected or a.text == sha_256_file: |
| 67 | + found += 1 |
| 68 | + assert a.text == a.get("href") |
| 69 | + assert len(expected) + 1 == found |
| 70 | + |
| 71 | + # Get SHA256s and URLs |
| 72 | + urls = {} |
| 73 | + sha_256_url = files_base_url + "/" + sha_256_file |
| 74 | + for line in requests.get(sha_256_url).text.splitlines(): |
| 75 | + file = line.split()[1].strip() |
| 76 | + if file in expected: |
| 77 | + sha256 = line.split()[0].strip() |
| 78 | + identifier = expected[file] |
| 79 | + urls[identifier] = (files_base_url + "/" + file, sha256) |
| 80 | + assert len(urls) == len(expected) |
| 81 | + |
| 82 | + return urls |
| 83 | + |
| 84 | + |
| 85 | +def generate_cmake_variables(urls_and_sha256s): |
| 86 | + template_inputs = {} |
| 87 | + |
| 88 | + # Get SHA256s and URLs |
| 89 | + for var_prefix, urls_and_sha256s in urls_and_sha256s.items(): |
| 90 | + template_inputs["%s_url" % var_prefix] = urls_and_sha256s[0] |
| 91 | + template_inputs["%s_sha256" % var_prefix] = urls_and_sha256s[1] |
| 92 | + |
| 93 | + cmake_variables = textwrap.dedent(""" |
| 94 | + #----------------------------------------------------------------------------- |
| 95 | + # CMake sources |
| 96 | + set(unix_source_url "{unix_source_url}") |
| 97 | + set(unix_source_sha256 "{unix_source_sha256}") |
| 98 | +
|
| 99 | + set(windows_source_url "{win_source_url}") |
| 100 | + set(windows_source_sha256 "{win_source_sha256}") |
| 101 | +
|
| 102 | + #----------------------------------------------------------------------------- |
| 103 | + # CMake binaries |
| 104 | +
|
| 105 | + set(linux32_binary_url "NA") # Linux 32-bit binaries not available |
| 106 | + set(linux32_binary_sha256 "NA") |
| 107 | +
|
| 108 | + set(linux64_binary_url "{linux64_binary_url}") |
| 109 | + set(linux64_binary_sha256 "{linux64_binary_sha256}") |
| 110 | +
|
| 111 | + set(macosx_binary_url "{macosx_binary_url}") |
| 112 | + set(macosx_binary_sha256 "{macosx_binary_sha256}") |
| 113 | +
|
| 114 | + set(win32_binary_url "{win32_binary_url}") |
| 115 | + set(win32_binary_sha256 "{win32_binary_sha256}") |
| 116 | +
|
| 117 | + set(win64_binary_url "{win64_binary_url}") |
| 118 | + set(win64_binary_sha256 "{win64_binary_sha256}") |
| 119 | + """).format(**template_inputs) |
| 120 | + |
| 121 | + return cmake_variables |
| 122 | + |
| 123 | + |
| 124 | +def update_cmake_urls_script(version): |
| 125 | + content = generate_cmake_variables( |
| 126 | + get_cmake_archive_urls_and_sha256s(version)) |
| 127 | + cmake_urls_filename = "CMakeUrls.cmake" |
| 128 | + cmake_urls_filepath = os.path.join(ROOT_DIR, cmake_urls_filename) |
| 129 | + |
| 130 | + msg = "Updating '%s' with CMake version %s" % (cmake_urls_filename, version) |
| 131 | + with _log(msg), open(cmake_urls_filepath, "w") as cmake_file: |
| 132 | + cmake_file.write(content) |
| 133 | + |
| 134 | + |
| 135 | +def _update_file(filepath, regex, replacement): |
| 136 | + msg = "Updating %s" % os.path.relpath(filepath, ROOT_DIR) |
| 137 | + with _log(msg): |
| 138 | + pattern = re.compile(regex) |
| 139 | + with open(filepath, 'r') as doc_file: |
| 140 | + lines = doc_file.readlines() |
| 141 | + updated_content = [] |
| 142 | + for line in lines: |
| 143 | + updated_content.append( |
| 144 | + re.sub(pattern, replacement, line)) |
| 145 | + with open(filepath, "w") as doc_file: |
| 146 | + doc_file.writelines(updated_content) |
| 147 | + |
| 148 | + |
| 149 | +def update_docs(version): |
| 150 | + pattern = re.compile( |
| 151 | + r"CMake \d.\d.\d <https://cmake.org/cmake/help/v\d.\d/index.html>") |
| 152 | + replacement = ( |
| 153 | + "CMake %s <https://cmake.org/cmake/help/v%s/index.html>" % ( |
| 154 | + version, _major_minor(version))) |
| 155 | + for filename in ["docs/index.rst", "README.rst"]: |
| 156 | + _update_file(os.path.join(ROOT_DIR, filename), pattern, replacement) |
| 157 | + |
| 158 | + |
| 159 | +def update_tests(version): |
| 160 | + pattern = re.compile(r'expected_version = "\d.\d.\d"') |
| 161 | + replacement = 'expected_version = "%s"' % version |
| 162 | + _update_file(os.path.join( |
| 163 | + ROOT_DIR, "tests/test_wheel.py"), pattern, replacement) |
| 164 | + |
| 165 | + |
| 166 | +def main(): |
| 167 | + parser = argparse.ArgumentParser(description=__doc__) |
| 168 | + parser.add_argument( |
| 169 | + 'cmake_version', metavar='CMAKE_VERSION', type=str, |
| 170 | + help='CMake version of the form X.Y.Z' |
| 171 | + ) |
| 172 | + args = parser.parse_args() |
| 173 | + update_cmake_urls_script(args.cmake_version) |
| 174 | + update_docs(args.cmake_version) |
| 175 | + update_tests(args.cmake_version) |
| 176 | + |
| 177 | + |
| 178 | +if __name__ == "__main__": |
| 179 | + main() |
0 commit comments