|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# This source file is part of the Swift.org open source project |
| 4 | +# |
| 5 | +# Copyright (c) 2024 Apple Inc. and the Swift project authors |
| 6 | +# Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +# |
| 8 | +# See https://swift.org/LICENSE.txt for license information |
| 9 | +# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | + |
| 11 | + |
| 12 | +""" |
| 13 | +Utility to download unpublished toolchains |
| 14 | +""" |
| 15 | + |
| 16 | +import argparse |
| 17 | +import os |
| 18 | +import sys |
| 19 | +import tarfile |
| 20 | +import urllib.request |
| 21 | + |
| 22 | + |
| 23 | +_CI_URL_ = "https://ci.swift.org/job/oss-swift-package-" |
| 24 | + |
| 25 | + |
| 26 | +def get_build_url(platform, arch): |
| 27 | + if arch == "x86_64" or arch == "Universal": |
| 28 | + arch = "" |
| 29 | + else: |
| 30 | + arch = f"-{arch}" |
| 31 | + return f"{_CI_URL_}{platform}{arch}/lastSuccessfulBuild/consoleText" |
| 32 | + |
| 33 | + |
| 34 | +def get_latest_toolchain_url(build_url): |
| 35 | + toolchain_url = None |
| 36 | + with urllib.request.urlopen(build_url) as response: |
| 37 | + body = response.read().decode('utf-8') |
| 38 | + lines = body.split('\n') |
| 39 | + toolchain = [x for x in lines if 'Toolchain:' in x] |
| 40 | + if len(toolchain) > 0: |
| 41 | + toolchain_url = toolchain[0].removeprefix('Toolchain: ') |
| 42 | + return toolchain_url |
| 43 | + |
| 44 | + |
| 45 | +def download_toolchain(toolchain_url, output_dir): |
| 46 | + file_name = toolchain_url.rsplit('/', 1)[-1] |
| 47 | + output_path = os.path.join(output_dir, file_name) |
| 48 | + urllib.request.urlretrieve(toolchain_url, output_path) |
| 49 | + return output_path |
| 50 | + |
| 51 | + |
| 52 | +def untar_toolchain(output_dir, tar_path): |
| 53 | + with tarfile.open(tar_path, "r") as tf: |
| 54 | + tf.extractall(path=output_dir) |
| 55 | + |
| 56 | + |
| 57 | +def parse_args(): |
| 58 | + parser = argparse.ArgumentParser() |
| 59 | + |
| 60 | + parser.add_argument( |
| 61 | + "--output-dir", |
| 62 | + required=True, |
| 63 | + help="Output dir to download the toolchain", |
| 64 | + ) |
| 65 | + |
| 66 | + parser.add_argument( |
| 67 | + "--platform", |
| 68 | + required=True, |
| 69 | + choices=["amazon-linux-2", |
| 70 | + "debian-12", |
| 71 | + "fedora-39", |
| 72 | + "macos", |
| 73 | + "ubi-9", |
| 74 | + "ubuntu-20_04", |
| 75 | + "ubuntu-22_04", |
| 76 | + "ubuntu-24_04"], |
| 77 | + help="Platform", |
| 78 | + ) |
| 79 | + |
| 80 | + parser.add_argument( |
| 81 | + "--arch", |
| 82 | + required=True, |
| 83 | + choices=["x86_64", "aarch64", "Universal"], |
| 84 | + help="architectures", |
| 85 | + ) |
| 86 | + |
| 87 | + parser.add_argument( |
| 88 | + "--untar", |
| 89 | + action=argparse.BooleanOptionalAction, |
| 90 | + help="Untar the toolchain in the output directory", |
| 91 | + ) |
| 92 | + |
| 93 | + return parser.parse_args() |
| 94 | + |
| 95 | + |
| 96 | +def main(): |
| 97 | + args = parse_args() |
| 98 | + |
| 99 | + if not os.path.isdir(args.output_dir): |
| 100 | + print(f"Error: Directory does not exist at {args.output_dir}.") |
| 101 | + return -1 |
| 102 | + |
| 103 | + build_url = get_build_url(args.platform, args.arch) |
| 104 | + toolchain_url = get_latest_toolchain_url(build_url) |
| 105 | + if not toolchain_url: |
| 106 | + print(f"Error: Unable to find toolchain for {args.platform}.") |
| 107 | + return -1 |
| 108 | + print(f"[Downloading] {toolchain_url}") |
| 109 | + output_path = download_toolchain(toolchain_url, args.output_dir) |
| 110 | + print(f"[Toolchain] {output_path}") |
| 111 | + |
| 112 | + if args.untar: |
| 113 | + untar_toolchain(args.output_dir, output_path) |
| 114 | + print(f"[Extracted] {args.output_dir}/usr") |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + sys.exit(main()) |
0 commit comments