Skip to content

Commit 78c0764

Browse files
add file download utils & c lib download tool
1 parent 169cb99 commit 78c0764

3 files changed

Lines changed: 127 additions & 2 deletions

File tree

bcos3sdklib/download_c_libs.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import sys
2+
sys.path.append("..")
3+
import os
4+
import time
5+
import platform
6+
7+
from bcos3sdk.bcos3client import Bcos3Client
8+
9+
10+
from client import filedownloader
11+
#基础下载站,有可能可以换成gitee
12+
base_url = "https://github.com/FISCO-BCOS/bcos-c-sdk/releases/download"
13+
#版本号,可以动态更新,实际上加到url时,前面会加个v,如v3.4.0
14+
version = "3.4.0"
15+
#网络代理,下载时可能需要使用
16+
net_proxy=[]
17+
18+
# sample: 设置代理
19+
net_proxy = {
20+
'http': 'http://127.0.0.1:7890',
21+
'https': 'http://127.0.0.1:7890',
22+
}
23+
24+
win_dll_name = "bcos-c-sdk.dll"
25+
win_lib_name = "bcos-c-sdk.lib"
26+
linux_aarch64_name = "libbcos-c-sdk-aarch64.so"
27+
linux_lib_name = "libbcos-c-sdk.so"
28+
mac_m1_aarch64_dylib_name = "libbcos-c-sdk-aarch64.dylib"
29+
mac_x64_lib_name = "libbcos-c-sdk.dylib"
30+
31+
file_list= []
32+
platsys = platform.platform().lower()
33+
print("Platform is ",platsys)
34+
if platsys.startswith("win"): # Windows
35+
file_list.append(win_lib_name)
36+
file_list.append(win_dll_name)
37+
elif "mac" in platsys: # Mac
38+
if "arm64" in platsys: # mac m1
39+
file_list.append(mac_m1_aarch64_dylib_name)
40+
else: # mac x86
41+
file_list.append(mac_x64_lib_name)
42+
elif "linux" in platsys: # Linux
43+
if "arm64" in platsys or "aarch64" in platsys:
44+
file_list.append(linux_aarch64_name)
45+
elif "x86_64" in platsys: # linux x86
46+
file_list.append(linux_lib_name)
47+
else: # unknown arch
48+
raise Exception('''Unsupported linux arch {}'''.format(platsys))
49+
else: # unknown os
50+
raise Exception('''Unsupported os {}'''.format(platsys))
51+
52+
#开始下载一到多个文件到当前目录。如果当前目录有文件,那么要注意备份
53+
for fname in file_list:
54+
url = f"{base_url}/v{version}/{fname}"
55+
print("download: ",url)
56+
filedownloader.download_file_with_progress(url,fname,proxies=net_proxy)
57+
if not os.path.exists(fname):
58+
print(f"! Download Fail ,File NOT Exist {fname},from {url}")
59+
else:
60+
stats = os.stat(fname)
61+
#mtime_local = time.localtime(stats.st_mtime)
62+
#mtime_str = time.strftime('%Y-%m-%d %H:%M:%S',mtime_local)
63+
print(f"file {fname} ,size {int(stats.st_size/1024)}k bytes")
64+
65+
#下载完lib之后顺便测试一下sdk,如果上一层目录的client_config和本目录的bcos3_sdk_config.ini里的信息配置是对的,
66+
# 那么应该可以打印出版本信息,否则会抛异常
67+
print(" --- download DONE. make sure config file (etc: client_config.py and bcos3_sdk_config.ini) has setup right.")
68+
print(" --- checking sdk :")
69+
os.chdir("../")
70+
client = Bcos3Client()
71+
print(client.getinfo())

client/filedownloader.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import requests
2+
from tqdm import tqdm
3+
4+
5+
6+
7+
def download_file(url, local_path,proxies=None):
8+
response = requests.get(url,proxies=proxies)
9+
# 检查请求是否成功
10+
if response.status_code == 200:
11+
with open(local_path, 'wb') as file:
12+
file.write(response.content)
13+
print(f"File download to :{local_path}")
14+
else:
15+
print(f"File download FAIL,status :{response.status_code}")
16+
17+
18+
def download_file_with_progress(url, local_path,proxies=None):
19+
response = requests.get(url, stream=True,proxies=proxies)
20+
21+
# 获取文件大小(以字节为单位)
22+
file_size = int(response.headers.get('Content-Length', 0))
23+
24+
# 设置进度条
25+
progress_bar = tqdm(total=file_size, unit='B', unit_scale=True)
26+
27+
# 写入文件
28+
with open(local_path, 'wb') as file:
29+
for data in response.iter_content(chunk_size=1024):
30+
progress_bar.update(len(data))
31+
file.write(data)
32+
progress_bar.close()
33+
print(f"File download to :{local_path}")
34+
35+
36+
37+
38+
'''
39+
# 例子:下载图片
40+
url = 'https://example.com/image.jpg'
41+
local_path = 'path/to/save/image.jpg'
42+
43+
download_file(url, local_path)
44+
45+
46+
47+
# 例子:下载图片
48+
url = 'https://example.com/image.jpg'
49+
local_path = 'path/to/save/image.jpg'
50+
51+
download_file_with_progress(url, local_path)
52+
'''

client/local_lib_helper.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@ def __init__(self, base_name, dir_path) -> None:
3535
elif "mac" in platsys: # Mac
3636
if "arm64" in platsys: # mac m1
3737
lib_prefix = 'lib'
38-
lib_suffix = '-arch64.dylib'
38+
lib_suffix = '-aarch64.dylib'
3939
else: # mac x86
4040
lib_prefix = 'lib'
4141
lib_suffix = '.dylib'
4242
elif "linux" in platsys: # Linux
4343
if "arm64" in platsys or "aarch64" in platsys: # linux arm, unsupported
44-
raise Exception('''Unsupported linux arch {}'''.format(platsys))
44+
lib_prefix = 'lib'
45+
lib_suffix = '-aarch64.lib'
46+
#'https://github.com/FISCO-BCOS/bcos-c-sdk/releases/download/v3.4.0/libbcos-c-sdk-aarch64.so'
4547
elif "x86_64" in platsys: # linux x86
4648
lib_prefix = 'lib'
4749
lib_suffix = '.so'

0 commit comments

Comments
 (0)