|
| 1 | +# Copyright 2023 LiveKit, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +# This file is used to download prebuilt binaries of livekit-ffi from our GH releases. |
| 17 | +# It is mostly used by our bindings (e.g https://github.com/livekit/client-sdk-python) |
| 18 | +# By default, the script will try to autodetect the platform, useful to simplify the CI. |
| 19 | + |
| 20 | + |
| 21 | +import argparse |
| 22 | +import os |
| 23 | +import platform |
| 24 | +import re |
| 25 | +import sys |
| 26 | +import tempfile |
| 27 | +from zipfile import ZipFile |
| 28 | + |
| 29 | +import requests |
| 30 | + |
| 31 | + |
| 32 | +def target_os(): |
| 33 | + if sys.platform.startswith("win"): |
| 34 | + return "windows" |
| 35 | + elif sys.platform.startswith("darwin"): |
| 36 | + return "macos" |
| 37 | + elif sys.platform.startswith("linux"): |
| 38 | + return "linux" |
| 39 | + |
| 40 | + return None |
| 41 | + |
| 42 | + |
| 43 | +def target_arch(): |
| 44 | + arch = platform.machine().lower() |
| 45 | + arch_mapping = { |
| 46 | + 'amd64': 'x86_64', |
| 47 | + 'x86_64': 'x86_64', |
| 48 | + 'arm64': 'arm64', |
| 49 | + 'aarch64': 'arm64', |
| 50 | + 'armv7': 'armv7', |
| 51 | + 'armv7l': 'armv7' |
| 52 | + } |
| 53 | + |
| 54 | + return arch_mapping.get(arch) |
| 55 | + |
| 56 | + |
| 57 | +# extract the version from livekit-ffi/Cargo.toml |
| 58 | +def ffi_version(): |
| 59 | + file = os.path.join(os.path.dirname(__file__), "livekit-ffi", "Cargo.toml") |
| 60 | + with open(file, 'r') as f: |
| 61 | + return re.search(r'version\s*=\s*"(\d+\.\d+\.\d+)"', f.read()).group(1) |
| 62 | + |
| 63 | + |
| 64 | +def download_ffi(platform, arch, version, output): |
| 65 | + filename = "ffi-%s-%s.zip" % (platform, arch) |
| 66 | + url = "https://github.com/livekit/client-sdk-rust/releases/download/ffi-v%s/%s" |
| 67 | + url = url % (version, filename) |
| 68 | + |
| 69 | + tmp = os.path.join(tempfile.gettempdir(), filename) |
| 70 | + |
| 71 | + # download the binary |
| 72 | + resp = requests.get(url, stream=True) |
| 73 | + if resp.status_code != 200: |
| 74 | + raise Exception("failed to download, status: %d" % (resp.status_code)) |
| 75 | + |
| 76 | + with open(tmp, mode="wb") as f: |
| 77 | + for chunk in resp.iter_content(chunk_size=1024 * 8): |
| 78 | + f.write(chunk) |
| 79 | + |
| 80 | + # unzip to output |
| 81 | + zip = ZipFile(tmp) |
| 82 | + os.makedirs(output, exist_ok=True) |
| 83 | + zip.extractall(output) |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + target_os = target_os() |
| 88 | + target_arch = target_arch() |
| 89 | + ffi_version = ffi_version() |
| 90 | + |
| 91 | + parser = argparse.ArgumentParser() |
| 92 | + parser.add_argument( |
| 93 | + "--platform", |
| 94 | + help="target platform", |
| 95 | + default=target_os, |
| 96 | + choices=["macos", "linux", "windows", "ios", "android"], |
| 97 | + ) |
| 98 | + parser.add_argument( |
| 99 | + "--arch", |
| 100 | + help="target architecture", |
| 101 | + default=target_arch, |
| 102 | + choices=["x86_64", "arm64", "armv7"], |
| 103 | + ) |
| 104 | + parser.add_argument( |
| 105 | + "--version", |
| 106 | + help="version to download", |
| 107 | + default=ffi_version, |
| 108 | + ) |
| 109 | + parser.add_argument("--output", help="output path", required=True) |
| 110 | + args = parser.parse_args() |
| 111 | + |
| 112 | + print("downloading livekit-ffi v%s for %s-%s" % |
| 113 | + (args.version, args.platform, args.arch)) |
| 114 | + download_ffi(args.platform, args.arch, args.version, args.output) |
| 115 | + print("downloaded to %s" % os.path.abspath(args.output)) |
0 commit comments