|
| 1 | +import io |
| 2 | +import zipfile |
| 3 | + |
| 4 | +import requests |
| 5 | +import shutil |
1 | 6 | import threading |
| 7 | +from os import listdir |
2 | 8 | from typing import Any, Optional, List, Dict, Union, Type, TypeVar |
3 | 9 | import platform |
4 | 10 | import ctypes |
5 | | -from os.path import join, abspath, dirname |
| 11 | +from os.path import join, abspath, dirname, isdir |
6 | 12 |
|
7 | 13 | import betterproto |
8 | 14 | from betterproto.lib.google.protobuf import Struct, Value, ListValue |
@@ -140,6 +146,51 @@ def load_library() -> ctypes.CDLL: |
140 | 146 | return OKAPI_DLL['library'] |
141 | 147 |
|
142 | 148 |
|
| 149 | +def download_binaries(): |
| 150 | + """ |
| 151 | + Download the latest released binaries from github |
| 152 | + """ |
| 153 | + latest_release = requests.get('https://api.github.com/repos/trinsic-id/okapi/releases/latest').json() |
| 154 | + latest_assets = requests.get(latest_release['assets_url']).json() |
| 155 | + libs_asset = [asset for asset in latest_assets if asset['name'] == 'libs.zip'][0] |
| 156 | + # Download zip |
| 157 | + zip_download = requests.get(libs_asset['browser_download_url'], stream=True) |
| 158 | + z = zipfile.ZipFile(io.BytesIO(zip_download.content)) |
| 159 | + extract_dir = abspath(join(dirname(abspath(__file__)), 'okapi')) |
| 160 | + z.extractall(extract_dir) |
| 161 | + # Remove the binaries for other environments. |
| 162 | + copy_from, copy_to = get_os_arch_binary(extract_dir) |
| 163 | + shutil.copy2(copy_from, copy_to) |
| 164 | + cleanup_zip_download(copy_to) |
| 165 | + |
| 166 | + |
| 167 | +def cleanup_zip_download(copy_to): |
| 168 | + # Delete folders |
| 169 | + for folder_name in listdir(copy_to): |
| 170 | + full_path = join(copy_to, folder_name) |
| 171 | + if isdir(full_path): |
| 172 | + shutil.rmtree(full_path) |
| 173 | + |
| 174 | + |
| 175 | +def get_os_arch_binary(extract_dir): |
| 176 | + copy_from = '' |
| 177 | + copy_to = join(extract_dir, 'libs') |
| 178 | + os_name = platform.system().lower() |
| 179 | + processor_name = platform.machine().lower() |
| 180 | + if os_name == 'windows': |
| 181 | + copy_from = join(copy_to, 'windows', 'okapi.dll') |
| 182 | + elif os_name == 'linux': |
| 183 | + if processor_name == 'amd64': |
| 184 | + copy_from = join(copy_to, 'linux', 'libokapi.so') |
| 185 | + elif processor_name == 'armv7': |
| 186 | + copy_from = join(copy_to, 'linux-armv7', 'libokapi.so') |
| 187 | + elif processor_name == 'aarch64': |
| 188 | + copy_from = join(copy_to, 'linux-aarch64', 'libokapi.so') |
| 189 | + elif os_name == 'darwin': |
| 190 | + copy_from = join(copy_to, 'macos', 'libokapi.dylib') |
| 191 | + return copy_from, copy_to |
| 192 | + |
| 193 | + |
143 | 194 | def wrap_native_function(function_name: str, *, arg_types: Optional[List[Any]] = None, |
144 | 195 | return_type: Optional[Any] = None): |
145 | 196 | library_function = getattr(load_library(), function_name) |
|
0 commit comments