fork/rust-sdks
git clone https://git.julia.blue/fork/rust-sdks
a0a961b
livekit/livekit-ffi@0.13.0
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 21import argparse 22import os 23import platform 24import re 25import sys 26import tempfile 27from zipfile import ZipFile 28 29import requests 30 31 32def target_os (): 33if sys .platform .startswith ("win" ): 34return "windows" 35elif sys .platform .startswith ("darwin" ): 36return "macos" 37elif sys .platform .startswith ("linux" ): 38return "linux" 39 40return None 41 42 43def target_arch (): 44arch = platform .machine ().lower () 45arch_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# make sure cibuildwheel download the right binary when cross-compiling to arm64 55# TODO: Make this more generic 56"initial arch: %s" % arch ) 57if os .environ .get ("CIBUILDWHEEL" )and target_os ()== "macos" : 58archflags = os .environ .get ("ARCHFLAGS" ,"" ) 59"cibuildwheel is being used, archflags: %s" % archflags ) 60if "arm64" in archflags : 61return "arm64" 62else : 63return "x86_64" 64 65return arch_mapping .get (arch ) 66 67 68# extract the version from livekit-ffi/Cargo.toml 69def ffi_version (): 70file = os .path .join (os .path .dirname (__file__ ),"livekit-ffi" ,"Cargo.toml" ) 71with open (file ,"r" )as f : 72return re .search (r'version\s*=\s*"(\d+\.\d+\.\d+)"' ,f .read ()).group (1 ) 73 74 75def download_ffi (platform ,arch ,version ,output ): 76filename = "ffi-%s-%s.zip" % (platform ,arch ) 77url = "https://github.com/livekit/client-sdk-rust/releases/download/rust-sdks/livekit-ffi@%s/%s" 78url = url % (version ,filename ) 79 80tmp = os .path .join (tempfile .gettempdir (),filename ) 81 82# download the binary 83resp = requests .get (url ,stream = True ) 84if resp .status_code != 200 : 85raise Exception ("failed to download, status: %d" % (resp .status_code )) 86 87with open (tmp ,mode = "wb" )as f : 88for chunk in resp .iter_content (chunk_size = 1024 * 8 ): 89f .write (chunk ) 90 91# unzip to output 92zip = ZipFile (tmp ) 93os .makedirs (output ,exist_ok = True ) 94zip .extractall (output ) 95 96 97if __name__ == "__main__" : 98parser = argparse .ArgumentParser () 99parser .add_argument ( 100"--platform" , 101help = "target platform" , 102default = target_os (), 103choices = ["macos" ,"linux" ,"windows" ,"ios" ,"android" ], 104 ) 105parser .add_argument ( 106"--arch" , 107help = "target architecture" , 108default = target_arch (), 109choices = ["x86_64" ,"arm64" ,"armv7" ], 110 ) 111parser .add_argument ( 112"--version" , 113help = "version to download" , 114default = ffi_version (), 115 ) 116parser .add_argument ("--output" ,help = "output path" ,required = True ) 117args = parser .parse_args () 118 119"downloading livekit-ffi v%s for %s-%s" 121% (args .version ,args .platform ,args .arch ) 122 ) 123download_ffi (args .platform ,args .arch ,args .version ,args .output ) 124"downloaded to %s" % os .path .abspath (args .output ))