|
5 | 5 | from os import getenv, path, remove |
6 | 6 |
|
7 | 7 |
|
8 | | -def download_native_libraries(name: str, version: str, destination: str) -> bool: |
9 | | - mac = f'{destination}/darwin-x86-64/libcosmian_{name}.dylib' |
10 | | - linux = f'{destination}/linux-x86-64/libcosmian_{name}.so' |
11 | | - windows = f'{destination}/win32-x86-64/cosmian_{name}.dll' |
| 8 | +def files_to_be_copied(name: str): |
| 9 | + """ |
| 10 | + Returns the list of files to be copied |
| 11 | + """ |
| 12 | + destination = 'src/main/resources' |
| 13 | + return { |
| 14 | + f'tmp/x86_64-apple-darwin/x86_64-apple-darwin/release/libcloudproof_{name}.dylib': f'{destination}/darwin-x86-64/libcloudproof_{name}.dylib', |
| 15 | + f'tmp/x86_64-unknown-linux-gnu/x86_64-unknown-linux-gnu/release/libcloudproof_{name}.so': f'{destination}/linux-x86-64/libcloudproof_{name}.so', |
| 16 | + f'tmp/x86_64-pc-windows-gnu/x86_64-pc-windows-gnu/release/cloudproof_{name}.dll': f'{destination}/win32-x86-64/cloudproof_{name}.dll', |
| 17 | + } |
12 | 18 |
|
13 | | - if not path.exists(mac) or not path.exists(linux) or not path.exists(windows): |
14 | | - print( |
15 | | - f'Missing {name} native library. Copy {name} {version} to {destination}...' |
16 | | - ) |
17 | 19 |
|
18 | | - url = f'https://package.cosmian.com/{name}/{version}/all.zip' |
19 | | - try: |
20 | | - r = urllib.request.urlopen(url) |
21 | | - if r.getcode() != 200: |
22 | | - print(f'Cannot get {name} {version} (status code: {r.getcode()})') |
23 | | - else: |
24 | | - if path.exists('tmp'): |
25 | | - shutil.rmtree('tmp') |
26 | | - if path.exists('all.zip'): |
27 | | - remove('all.zip') |
| 20 | +def download_native_libraries(version: str) -> bool: |
| 21 | + """Download and extract native libraries""" |
| 22 | + to_be_copied = files_to_be_copied('findex') |
| 23 | + cover_crypt_files = files_to_be_copied('cover_crypt') |
| 24 | + to_be_copied.update(cover_crypt_files) |
28 | 25 |
|
29 | | - open('all.zip', 'wb').write(r.read()) |
30 | | - with zipfile.ZipFile('all.zip', 'r') as zip_ref: |
31 | | - zip_ref.extractall('tmp') |
32 | | - shutil.copyfile( |
33 | | - f'tmp/x86_64-apple-darwin/x86_64-apple-darwin/release/libcosmian_{name}.dylib', |
34 | | - f'{mac}', |
35 | | - ) |
36 | | - shutil.copyfile( |
37 | | - f'tmp/x86_64-unknown-linux-gnu/x86_64-unknown-linux-gnu/release/libcosmian_{name}.so', |
38 | | - f'{linux}', |
39 | | - ) |
40 | | - shutil.copyfile( |
41 | | - f'tmp/x86_64-pc-windows-gnu/x86_64-pc-windows-gnu/release/cosmian_{name}.dll', |
42 | | - f'{windows}', |
| 26 | + missing_files = False |
| 27 | + for key, value in to_be_copied.items(): |
| 28 | + if not path.exists(value): |
| 29 | + missing_files = True |
| 30 | + break |
| 31 | + |
| 32 | + if missing_files: |
| 33 | + url = f'https://package.cosmian.com/cloudproof_rust/{version}/all.zip' |
| 34 | + try: |
| 35 | + with urllib.request.urlopen(url) as request: |
| 36 | + if request.getcode() != 200: |
| 37 | + print( |
| 38 | + f'Cannot get cloudproof_rust {version} \ |
| 39 | + (status code: {request.getcode()})' |
43 | 40 | ) |
44 | | - shutil.rmtree('tmp') |
45 | | - remove('all.zip') |
46 | | - except Exception as e: |
47 | | - print(f'Cannot get {name} {version} ({e})') |
| 41 | + else: |
| 42 | + if path.exists('tmp'): |
| 43 | + shutil.rmtree('tmp') |
| 44 | + if path.exists('all.zip'): |
| 45 | + remove('all.zip') |
| 46 | + |
| 47 | + # pylint: disable=consider-using-with |
| 48 | + open('all.zip', 'wb').write(request.read()) |
| 49 | + |
| 50 | + with zipfile.ZipFile('all.zip', 'r') as zip_ref: |
| 51 | + zip_ref.extractall('tmp') |
| 52 | + for key, value in to_be_copied.items(): |
| 53 | + shutil.copyfile(key, value) |
| 54 | + print(f'Copied OK: {value}...') |
| 55 | + |
| 56 | + shutil.rmtree('tmp') |
| 57 | + remove('all.zip') |
| 58 | + # pylint: disable=broad-except |
| 59 | + except Exception as exception: |
| 60 | + print(f'Cannot get cloudproof_rust {version} ({exception})') |
48 | 61 | return False |
49 | 62 | return True |
50 | 63 |
|
51 | 64 |
|
52 | 65 | if __name__ == '__main__': |
53 | | - ret = download_native_libraries('findex', 'v2.1.0', 'src/main/resources') |
54 | | - if ret is False and getenv('GITHUB_ACTIONS'): |
55 | | - download_native_libraries('findex', 'last_build', 'src/main/resources') |
56 | | - |
57 | | - ret = download_native_libraries('cover_crypt', 'v10.0.0', 'src/main/resources') |
| 66 | + ret = download_native_libraries('v1.0.0') |
58 | 67 | if ret is False and getenv('GITHUB_ACTIONS'): |
59 | | - download_native_libraries('cover_crypt', 'last_build', 'src/main/resources') |
| 68 | + download_native_libraries('last_build/feature/add_findex') |
0 commit comments