|
| 1 | +"""Command line executable allowing to update CastXMLUrls.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 girder_client |
| 13 | +except ImportError: |
| 14 | + raise SystemExit( |
| 15 | + "girder_client not available: " |
| 16 | + "consider installing it running 'pip install girder-client'" |
| 17 | + ) |
| 18 | + |
| 19 | +ROOT_DIR = os.path.join(os.path.dirname(__file__), "..") |
| 20 | + |
| 21 | + |
| 22 | +@contextlib.contextmanager |
| 23 | +def _log(txt, verbose=True): |
| 24 | + if verbose: |
| 25 | + print(txt) |
| 26 | + yield |
| 27 | + if verbose: |
| 28 | + print("%s - done" % txt) |
| 29 | + |
| 30 | + |
| 31 | +def get_archive_urls_and_shas(version, verbose=False): |
| 32 | + |
| 33 | + host_url = 'https://data.kitware.com' |
| 34 | + folder_id = '57b5de948d777f10f2696370' |
| 35 | + |
| 36 | + with _log("Collecting URLs and SHAs from '%s/#folder/%s'" % (host_url, folder_id)): |
| 37 | + |
| 38 | + api_url = '%s/api/v1' % host_url |
| 39 | + gc = girder_client.GirderClient(apiUrl=api_url) |
| 40 | + |
| 41 | + folders = gc.listFolder('57b5de948d777f10f2696370') |
| 42 | + archive_folder_ids = [folder['_id'] for folder in folders if folder['name'] == "v%s" % version] |
| 43 | + assert len(archive_folder_ids) == 1 |
| 44 | + |
| 45 | + items = gc.listItem(archive_folder_ids[0]) |
| 46 | + |
| 47 | + expected_files = { |
| 48 | + "castxml-linux.tar.gz": "linux64_binary", |
| 49 | + "castxml-macosx.tar.gz": "macosx_binary", |
| 50 | + "castxml-windows.zip": "win64_binary", |
| 51 | + } |
| 52 | + |
| 53 | + # Get download URL and SHA512 for each file |
| 54 | + urls_and_shas = {} |
| 55 | + for item in items: |
| 56 | + files = list(gc.listFile(item['_id'])) |
| 57 | + assert len(files) == 1 |
| 58 | + file_id = files[0]['_id'] |
| 59 | + file = files[0]['name'] |
| 60 | + sha = files[0]['sha512'] |
| 61 | + url = '%s/file/%s/download' % (api_url, file_id) |
| 62 | + if file in expected_files: |
| 63 | + identifier = expected_files[file] |
| 64 | + urls_and_shas[identifier] = (url, sha) |
| 65 | + if verbose: |
| 66 | + print("[%s]\n%s\n%s\n" % (identifier, url, sha)) |
| 67 | + |
| 68 | + assert len(urls_and_shas) == len(expected_files) |
| 69 | + |
| 70 | + return urls_and_shas |
| 71 | + |
| 72 | + |
| 73 | +def generate_cmake_variables(urls_and_shas): |
| 74 | + template_inputs = {} |
| 75 | + |
| 76 | + for var_prefix, urls_and_shas in urls_and_shas.items(): |
| 77 | + template_inputs["%s_url" % var_prefix] = urls_and_shas[0] |
| 78 | + template_inputs["%s_sha512" % var_prefix] = urls_and_shas[1] |
| 79 | + |
| 80 | + cmake_variables = textwrap.dedent(""" |
| 81 | + #----------------------------------------------------------------------------- |
| 82 | + # CastXML binaries |
| 83 | +
|
| 84 | + set(linux32_binary_url "NA") # Linux 32-bit binaries not available |
| 85 | + set(linux32_binary_sha512 "NA") |
| 86 | +
|
| 87 | + set(linux64_binary_url "{linux64_binary_url}") |
| 88 | + set(linux64_binary_sha512 "{linux64_binary_sha512}") |
| 89 | +
|
| 90 | + set(macosx_binary_url "{macosx_binary_url}") |
| 91 | + set(macosx_binary_sha512 "{macosx_binary_sha512}") |
| 92 | +
|
| 93 | + set(win32_binary_url "NA") # Windows 32-bit binaries not available |
| 94 | + set(win32_binary_sha512 "NA") |
| 95 | +
|
| 96 | + set(win64_binary_url "{win64_binary_url}") |
| 97 | + set(win64_binary_sha512 "{win64_binary_sha512}") |
| 98 | + """).format(**template_inputs) |
| 99 | + |
| 100 | + return cmake_variables |
| 101 | + |
| 102 | + |
| 103 | +def update_urls_script(version): |
| 104 | + content = generate_cmake_variables( |
| 105 | + get_archive_urls_and_shas(version)) |
| 106 | + urls_filename = "CastXMLUrls.cmake" |
| 107 | + urls_filepath = os.path.join(ROOT_DIR, urls_filename) |
| 108 | + |
| 109 | + msg = "Updating '%s' with CastXML version %s" % (urls_filename, version) |
| 110 | + with _log(msg), open(urls_filepath, "w") as urls_file: |
| 111 | + urls_file.write(content) |
| 112 | + |
| 113 | + |
| 114 | +def _update_file(filepath, regex, replacement, verbose=True): |
| 115 | + msg = "Updating %s" % os.path.relpath(filepath, ROOT_DIR) |
| 116 | + with _log(msg, verbose=verbose): |
| 117 | + pattern = re.compile(regex) |
| 118 | + with open(filepath, 'r') as doc_file: |
| 119 | + lines = doc_file.readlines() |
| 120 | + updated_content = [] |
| 121 | + for line in lines: |
| 122 | + updated_content.append( |
| 123 | + re.sub(pattern, replacement, line)) |
| 124 | + with open(filepath, "w") as doc_file: |
| 125 | + doc_file.writelines(updated_content) |
| 126 | + |
| 127 | + |
| 128 | +def update_docs(version): |
| 129 | + pattern = re.compile(r"CastXML \d\.\d\.\d") |
| 130 | + replacement = "CastXML %s" % version |
| 131 | + _update_file( |
| 132 | + os.path.join(ROOT_DIR, "README.md"), |
| 133 | + pattern, replacement) |
| 134 | + |
| 135 | + pattern = re.compile(r"\d\.\d\.\d") |
| 136 | + replacement = version |
| 137 | + _update_file( |
| 138 | + os.path.join(ROOT_DIR, "docs/update_castxml_version.rst"), |
| 139 | + pattern, replacement) |
| 140 | + |
| 141 | + |
| 142 | +def update_tests(version): |
| 143 | + pattern = re.compile(r'expected_version = "\d.(\d)+.\d"') |
| 144 | + replacement = 'expected_version = "%s"' % version |
| 145 | + _update_file(os.path.join( |
| 146 | + ROOT_DIR, "tests/test_distribution.py"), pattern, replacement) |
| 147 | + |
| 148 | + |
| 149 | +def main(): |
| 150 | + parser = argparse.ArgumentParser(description=__doc__) |
| 151 | + parser.add_argument( |
| 152 | + 'version', metavar='CASTXML_VERSION', type=str, |
| 153 | + help='CastXML version of the form X.Y.Z' |
| 154 | + ) |
| 155 | + parser.add_argument( |
| 156 | + '--collect-only', action='store_true', |
| 157 | + help='If specified, only display the archive URLs and associated hashsums' |
| 158 | + ) |
| 159 | + args = parser.parse_args() |
| 160 | + if args.collect_only: |
| 161 | + get_archive_urls_and_shas(args.version, verbose=True) |
| 162 | + else: |
| 163 | + update_urls_script(args.version) |
| 164 | + update_docs(args.version) |
| 165 | + update_tests(args.version) |
| 166 | + |
| 167 | + |
| 168 | +if __name__ == "__main__": |
| 169 | + main() |
0 commit comments