11import ctypes
2+ import glob
23import os
34from platform import machine
45from sys import platform
89from httpx import get , stream
910from orjson import loads
1011
11-
1212root_dir = os .path .abspath (os .path .dirname (__file__ ))
1313
1414# map machine architecture to hrequests-cgo binary name
@@ -36,6 +36,7 @@ class LibraryManager:
3636 def __init__ (self ):
3737 self .parent_path = os .path .join (root_dir , 'bin' )
3838 self .file_cont , self .file_ext = self .get_name ()
39+ self .file_pref = f'hrequests-cgo-{ self .BRIDGE_VERSION } '
3940 filename = self .check_library ()
4041 self .full_path = os .path .join (self .parent_path , filename )
4142
@@ -52,25 +53,38 @@ def get_name() -> Tuple[str, str]:
5253 return f'linux-{ arch } ' , '.so'
5354
5455 def check_library (self ):
55- for file in os .listdir (self .parent_path ):
56+ files = sorted (glob .glob ('hrequests-cgo-*' , root_dir = self .parent_path ), reverse = True )
57+ for file in files :
5658 if not file .endswith (self .file_ext ):
5759 continue
58- if file .startswith (f'hrequests-cgo- { self .BRIDGE_VERSION } ' ):
60+ if file .startswith (self .file_pref ):
5961 return file
6062 # delete residual files from previous versions
6163 os .remove (os .path .join (self .parent_path , file ))
6264 self .download_library ()
6365 return self .check_library ()
6466
67+ def check_assets (self , assets ):
68+ for asset in assets :
69+ if (
70+ # filter via version
71+ asset ['name' ].startswith (self .file_pref )
72+ # filter via os
73+ and self .file_cont in asset ['name' ]
74+ # filter via file extension
75+ and asset ['name' ].endswith (self .file_ext )
76+ ):
77+ return asset ['browser_download_url' ], asset ['name' ]
78+
6579 def download_library (self ):
6680 print ('Downloading hrequests-cgo library from daijro/hrequests...' )
6781 # pull release assets from github daijro/hrequests
68- resp = get ('https://api.github.com/repos/daijro/hrequests/releases/latest ' )
69- assets = loads (resp .content )[ 'assets' ]
70- for asset in assets :
71- if self .file_cont in asset [ 'name' ] and asset [ 'name' ]. endswith ( self . file_ext ):
72- url : str = asset [ 'browser_download_url' ]
73- name : str = asset [ 'name' ]
82+ resp = get ('https://api.github.com/repos/daijro/hrequests/releases' )
83+ releases = loads (resp .content )
84+ for release in releases :
85+ asset = self .check_assets ( release [ 'assets' ])
86+ if asset :
87+ url , name = asset
7488 break
7589 else :
7690 raise IOError ('Could not find a matching binary for your system.' )
0 commit comments