|
1 | 1 | import os |
2 | | -import shutil |
| 2 | +import platform |
3 | 3 | import struct |
4 | | - |
5 | | -from setuptools import Extension, find_packages |
6 | | -from setuptools.dist import Distribution |
7 | | -from pathlib import Path |
8 | 4 | from io import BytesIO |
9 | | -from zipfile import ZipFile |
| 5 | +from pathlib import Path |
10 | 6 | from urllib.request import urlopen |
11 | | -import platform |
| 7 | +from zipfile import ZipFile |
| 8 | +import shutil |
| 9 | +from setuptools import Extension, find_packages |
| 10 | +from setuptools.dist import Distribution |
| 11 | +import toml |
| 12 | + |
| 13 | + |
| 14 | +class Downloader(object): |
| 15 | + def __init__(self, version: str, path_to_extract: Path, |
| 16 | + path_to_copy_includes: Path, path_to_copy_libs: Path): |
| 17 | + self.__version = version |
| 18 | + self.__path_to_extract = path_to_extract |
| 19 | + self.__path_to_copy_includes = path_to_copy_includes |
| 20 | + self.__path_to_copy_libs = path_to_copy_libs |
| 21 | + |
| 22 | + def download(self) -> str: |
| 23 | + __c_api_extracted_root_dir = self.__path_to_extract / f'dxfeed-c-api-{self.__version}-no-tls' |
| 24 | + is_x64 = 8 * struct.calcsize("P") == 64 |
| 25 | + |
| 26 | + if platform.system() == 'Windows': |
| 27 | + current_os = 'windows' |
| 28 | + elif platform.system() == 'Darwin': |
| 29 | + current_os = 'macosx' |
| 30 | + else: |
| 31 | + current_os = 'centos' # Since centos uses an earlier version of libc |
| 32 | + |
| 33 | + if (not os.path.exists(self.__path_to_extract)) or (not os.path.exists(__c_api_extracted_root_dir)): |
| 34 | + url = f'https://github.com/dxFeed/dxfeed-c-api/releases/download/{self.__version}/dxfeed-c-api-' \ |
| 35 | + f'{self.__version}-{current_os}-no-tls.zip ' |
| 36 | + print(f'Downloading the "{url}"') |
| 37 | + resp = urlopen(url) |
| 38 | + zipfile = ZipFile(BytesIO(resp.read())) |
| 39 | + print(f'Extracting to "{self.__path_to_extract}"') |
| 40 | + zipfile.extractall(self.__path_to_extract) |
| 41 | + |
| 42 | + if __debug__: |
| 43 | + # noinspection PyUnreachableCode |
| 44 | + c_api_debug_suffix = 'd' |
| 45 | + else: |
| 46 | + c_api_debug_suffix = '' |
| 47 | + |
| 48 | + if is_x64: |
| 49 | + c_api_x64_suffix = '_64' |
| 50 | + else: |
| 51 | + c_api_x64_suffix = '' |
| 52 | + |
| 53 | + __c_api_library_name = f'DXFeed{c_api_debug_suffix}{c_api_x64_suffix}' |
| 54 | + |
| 55 | + if platform.system() == 'Windows': |
| 56 | + if is_x64: |
| 57 | + c_api_extracted_library_dir = __c_api_extracted_root_dir / 'bin' / 'x64' |
| 58 | + c_api_library_file_name = f'{__c_api_library_name}.dll' |
| 59 | + c_api_library_file_name2 = f'{__c_api_library_name}.lib' |
| 60 | + else: |
| 61 | + c_api_extracted_library_dir = __c_api_extracted_root_dir / 'bin' / 'x86' |
| 62 | + c_api_library_file_name = f'{__c_api_library_name}.dll' |
| 63 | + c_api_library_file_name2 = f'{__c_api_library_name}.lib' |
| 64 | + elif platform.system() == 'Darwin': |
| 65 | + if is_x64: |
| 66 | + __c_api_extracted_root_dir = __c_api_extracted_root_dir / f'DXFeedAll-{self.__version}-x64-no-tls' |
| 67 | + c_api_extracted_library_dir = __c_api_extracted_root_dir / 'bin' / 'x64' |
| 68 | + c_api_library_file_name = f'lib{__c_api_library_name}.dylib' |
| 69 | + else: |
| 70 | + raise Exception('Unsupported platform') |
| 71 | + else: |
| 72 | + if is_x64: |
| 73 | + __c_api_extracted_root_dir = __c_api_extracted_root_dir / f'DXFeedAll-{self.__version}-x64-no-tls' |
| 74 | + c_api_extracted_library_dir = __c_api_extracted_root_dir / 'bin' / 'x64' |
| 75 | + c_api_library_file_name = f'lib{__c_api_library_name}.so' |
| 76 | + else: |
| 77 | + raise Exception('Unsupported platform') |
| 78 | + |
| 79 | + if not os.path.exists(self.__path_to_copy_includes): |
| 80 | + shutil.copytree(__c_api_extracted_root_dir / 'include', self.__path_to_copy_includes) |
| 81 | + if not os.path.exists(self.__path_to_copy_libs / c_api_library_file_name): |
| 82 | + if not os.path.exists(self.__path_to_copy_libs): |
| 83 | + os.makedirs(self.__path_to_copy_libs) |
| 84 | + shutil.copyfile(c_api_extracted_library_dir / c_api_library_file_name, |
| 85 | + self.__path_to_copy_libs / c_api_library_file_name) |
| 86 | + if platform.system() == 'Windows': |
| 87 | + # noinspection PyUnboundLocalVariable |
| 88 | + if not os.path.exists(self.__path_to_copy_libs / c_api_library_file_name2): |
| 89 | + shutil.copyfile(c_api_extracted_library_dir / c_api_library_file_name2, |
| 90 | + self.__path_to_copy_libs / c_api_library_file_name2) |
| 91 | + |
| 92 | + return __c_api_library_name |
12 | 93 |
|
13 | | -DEFAULT_C_API_VERSION = '8.3.0' |
14 | | -c_api_version = os.getenv('DXFEED_C_API_VERSION', DEFAULT_C_API_VERSION) |
15 | 94 |
|
16 | 95 | try: |
17 | 96 | from Cython.Build import cythonize |
|
24 | 103 |
|
25 | 104 | root_path = Path(__file__).resolve().parent |
26 | 105 |
|
27 | | -is_x64 = 8 * struct.calcsize("P") == 64 |
28 | | - |
29 | | -if platform.system() == 'Windows': |
30 | | - current_os = 'windows' |
31 | | -elif platform.system() == 'Darwin': |
32 | | - current_os = 'macosx' |
33 | | -else: |
34 | | - current_os = 'centos' # Since centos uses an earlier version of libc |
| 106 | +pyproject = toml.load(root_path / 'pyproject.toml') |
| 107 | +c_api_version = pyproject['build']['native-dependencies']['dxfeed_c_api'] |
| 108 | +if c_api_version == 'env': |
| 109 | + c_api_version = os.getenv('DXFEED_C_API_VERSION') |
35 | 110 |
|
36 | 111 | c_api_root_dir = root_path / 'dxfeed' / 'dxfeed-c-api' |
| 112 | +path_to_extract = root_path / 'dxfeed' / 'tmp' |
37 | 113 | c_api_include_dir = c_api_root_dir / 'include' |
38 | 114 | c_api_bin_dir = root_path / 'dxfeed' / 'core' |
39 | | -path_to_extract = root_path / 'dxfeed' / 'tmp' |
40 | | -c_api_extracted_root_dir = path_to_extract / f'dxfeed-c-api-{c_api_version}-no-tls' |
41 | | - |
42 | | -if (not os.path.exists(path_to_extract)) or (not os.path.exists(c_api_extracted_root_dir)): |
43 | | - url = f'https://github.com/dxFeed/dxfeed-c-api/releases/download/{c_api_version}/dxfeed-c-api-{c_api_version}-' \ |
44 | | - f'{current_os}-no-tls.zip ' |
45 | | - print(f'Downloading the "{url}"') |
46 | | - resp = urlopen(url) |
47 | | - zipfile = ZipFile(BytesIO(resp.read())) |
48 | | - print(f'Extracting to "{path_to_extract}"') |
49 | | - zipfile.extractall(path_to_extract) |
50 | | - |
51 | | -if __debug__: |
52 | | - # noinspection PyUnreachableCode |
53 | | - c_api_debug_suffix = 'd' |
54 | | -else: |
55 | | - c_api_debug_suffix = '' |
56 | | - |
57 | | -if is_x64: |
58 | | - c_api_x64_suffix = '_64' |
59 | | -else: |
60 | | - c_api_x64_suffix = '' |
61 | 115 |
|
62 | | -c_api_library_name = f'DXFeed{c_api_debug_suffix}{c_api_x64_suffix}' |
63 | | - |
64 | | -if platform.system() == 'Windows': |
65 | | - if is_x64: |
66 | | - c_api_extracted_library_dir = c_api_extracted_root_dir / 'bin' / 'x64' |
67 | | - c_api_library_file_name = f'{c_api_library_name}.dll' |
68 | | - c_api_library_file_name2 = f'{c_api_library_name}.lib' |
69 | | - else: |
70 | | - c_api_extracted_library_dir = c_api_extracted_root_dir / 'bin' / 'x86' |
71 | | - c_api_library_file_name = f'{c_api_library_name}.dll' |
72 | | - c_api_library_file_name2 = f'{c_api_library_name}.lib' |
73 | | -elif platform.system() == 'Darwin': |
74 | | - if is_x64: |
75 | | - c_api_extracted_root_dir = c_api_extracted_root_dir / f'DXFeedAll-{c_api_version}-x64-no-tls' |
76 | | - c_api_extracted_library_dir = c_api_extracted_root_dir / 'bin' / 'x64' |
77 | | - c_api_library_file_name = f'lib{c_api_library_name}.dylib' |
78 | | - else: |
79 | | - raise Exception('Unsupported platform') |
80 | | -else: |
81 | | - if is_x64: |
82 | | - c_api_extracted_root_dir = c_api_extracted_root_dir / f'DXFeedAll-{c_api_version}-x64-no-tls' |
83 | | - c_api_extracted_library_dir = c_api_extracted_root_dir / 'bin' / 'x64' |
84 | | - c_api_library_name = 'DXFeed_64' |
85 | | - c_api_library_file_name = f'lib{c_api_library_name}.so' |
86 | | - else: |
87 | | - raise Exception('Unsupported platform') |
88 | | - |
89 | | -if not os.path.exists(c_api_include_dir): |
90 | | - shutil.copytree(c_api_extracted_root_dir / 'include', c_api_include_dir) |
91 | | -if not os.path.exists(c_api_bin_dir / c_api_library_file_name): |
92 | | - # os.makedirs(c_api_bin_dir) |
93 | | - shutil.copyfile(c_api_extracted_library_dir / c_api_library_file_name, c_api_bin_dir / c_api_library_file_name) |
94 | | -if platform.system() == 'Windows': |
95 | | - # noinspection PyUnboundLocalVariable |
96 | | - if not os.path.exists(c_api_bin_dir / c_api_library_file_name2): |
97 | | - shutil.copyfile(c_api_extracted_library_dir / c_api_library_file_name2, c_api_bin_dir / c_api_library_file_name2) |
| 116 | +downloader = Downloader(c_api_version, path_to_extract, c_api_include_dir, c_api_bin_dir) |
| 117 | +c_api_library_name = downloader.download() |
98 | 118 |
|
99 | 119 | if platform.system() == 'Windows': |
100 | 120 | runtime_library_dirs = None |
@@ -152,7 +172,3 @@ def build_extensions(): |
152 | 172 | build_ext_cmd.ensure_finalized() |
153 | 173 | build_ext_cmd.inplace = 1 |
154 | 174 | build_ext_cmd.run() |
155 | | - |
156 | | - print(f'Removing the "{path_to_extract}"') |
157 | | - if os.path.exists(path_to_extract): |
158 | | - shutil.rmtree(path_to_extract) |
|
0 commit comments