Skip to content

Commit b78d38f

Browse files
Only download binaries if needed for Python (#240)
1 parent ae7830d commit b78d38f

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

python/okapi/okapi_utils.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import io
2+
import os.path
23
import zipfile
34

45
import requests
@@ -8,7 +9,7 @@
89
from typing import Any, Optional, List, Dict, Union, Type, TypeVar
910
import platform
1011
import ctypes
11-
from os.path import join, abspath, dirname, isdir
12+
from os.path import join, abspath, dirname, isdir, basename
1213

1314
import betterproto
1415
from betterproto.lib.google.protobuf import Struct, Value, ListValue
@@ -146,20 +147,24 @@ def load_library() -> ctypes.CDLL:
146147
return OKAPI_DLL['library']
147148

148149

149-
def download_binaries():
150+
def download_binaries(force_download=True):
150151
"""
151152
Download the latest released binaries from github
152153
"""
154+
extract_dir = abspath(join(dirname(abspath(__file__)), 'okapi'))
155+
# Remove the binaries for other environments.
156+
copy_from, copy_to = get_os_arch_binary(extract_dir)
157+
158+
if not force_download and os.path.exists(abspath(join(copy_to, basename(copy_from)))):
159+
return
160+
153161
latest_release = requests.get('https://api.github.com/repos/trinsic-id/okapi/releases/latest').json()
154162
latest_assets = requests.get(latest_release['assets_url']).json()
155163
libs_asset = [asset for asset in latest_assets if asset['name'] == 'libs.zip'][0]
156164
# Download zip
157165
zip_download = requests.get(libs_asset['browser_download_url'], stream=True)
158166
z = zipfile.ZipFile(io.BytesIO(zip_download.content))
159-
extract_dir = abspath(join(dirname(abspath(__file__)), 'okapi'))
160167
z.extractall(extract_dir)
161-
# Remove the binaries for other environments.
162-
copy_from, copy_to = get_os_arch_binary(extract_dir)
163168
shutil.copy2(copy_from, copy_to)
164169
cleanup_zip_download(copy_to)
165170

python/tests/test_utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import unittest
2+
3+
4+
class UtilitiesTests(unittest.TestCase):
5+
def test_download_binaries(self):
6+
# Only run this locally, it should take 20-30 seconds the first run, and instantaneous on the second.
7+
# okapi.okapi_utils.download_binaries(False)
8+
pass
9+
10+
11+
if __name__ == '__main__':
12+
unittest.main()

0 commit comments

Comments
 (0)