-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathsetup.py
More file actions
194 lines (176 loc) · 6.33 KB
/
setup.py
File metadata and controls
194 lines (176 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import distutils.command.install as dist_install
import glob
import os
import pathlib
import shutil
import sys
import sysconfig
from distutils import dist
from setuptools import Extension, find_packages, setup
from setuptools.command.build_py import build_py
API_VER = os.environ.get("API_VER", "6.7.7")
REVISION = ""
BUILD_VER = os.environ.get("BUILD_VER") or (API_VER + "." + REVISION if REVISION else API_VER)
# Get the long description from relevant files
with open("README.md", encoding="utf-8") as f:
readme = f.read()
if sys.platform.startswith("darwin"):
if API_VER < "6.6.9":
print(
"Error: Platform", sys.platform, "API Version <", API_VER, "not supported"
)
sys.exit(-1)
API_DIR = os.path.join("api", API_VER, "darwin")
if API_VER >= "6.7.7":
# Handle macOS frameworks
INC_DIRS = [
os.path.join(API_DIR, "thostmduserapi_se.framework/Versions/A/Headers"),
os.path.join(API_DIR, "thosttraderapi_se.framework/Versions/A/Headers"),
]
LIB_DIRS = [API_DIR]
LIB_NAMES = []
# Get direct paths to the framework libraries
MD_LIB = os.path.join(
API_DIR, "thostmduserapi_se.framework/Versions/A/thostmduserapi_se"
)
TRADER_LIB = os.path.join(
API_DIR, "thosttraderapi_se.framework/Versions/A/thosttraderapi_se"
)
API_LIBS = [MD_LIB, TRADER_LIB]
LINK_ARGS = [
"-Wl,-rpath,@loader_path",
MD_LIB,
TRADER_LIB,
]
COMPILE_ARGS = []
# Define framework files for package_data
FRAMEWORK_FILES = ["*.framework", "*.framework/**/*"]
else:
# Handle older versions with static libraries
API_LIBS = glob.glob(API_DIR + "/*.a")
INC_DIRS = [API_DIR]
LIB_DIRS = [API_DIR]
LIB_NAMES = []
LINK_ARGS = ["-Wl,-rpath,$ORIGIN"]
LINK_ARGS.extend(API_LIBS)
COMPILE_ARGS = []
elif sys.platform.startswith("linux"):
API_DIR = os.path.join("api", API_VER, "linux")
API_LIBS = glob.glob(API_DIR + "/*.so")
LIB_NAMES = [pathlib.Path(path).stem[3:] for path in API_LIBS]
INC_DIRS = [API_DIR]
LIB_DIRS = [API_DIR]
LINK_ARGS = ["-Wl,-rpath,$ORIGIN"]
COMPILE_ARGS = []
elif sys.platform.startswith("win"):
if API_VER < "6.6.9":
print(
"Error: Platform", sys.platform, "API Version <", API_VER, "not supported"
)
sys.exit(-1)
API_DIR = os.path.join("api", API_VER, "windows")
API_LIBS = glob.glob(API_DIR + "/*.dll")
LIB_NAMES = [pathlib.Path(path).stem for path in API_LIBS] + ["iconv"]
INC_DIRS = [
API_DIR,
os.path.join(sysconfig.get_config_var("base"), "Library", "include"),
]
LIB_DIRS = [
API_DIR,
os.path.join(sysconfig.get_config_var("base"), "Library", "lib"),
]
LINK_ARGS = []
COMPILE_ARGS = ["/utf-8", "/wd4101"]
else:
print("Error: Platform", sys.platform, "not supported")
sys.exit(-1)
def get_install_data_dir():
d = dist.Distribution()
install_cmd = dist_install.install(d)
install_cmd.finalize_options()
return install_cmd.install_data
package_data = []
if not sys.platform.startswith("darwin"):
package_data = [os.path.basename(lib) for lib in API_LIBS]
else:
if API_VER >= "6.7.7":
package_data = FRAMEWORK_FILES
else:
package_data = []
class BuildPy(build_py):
def run(self):
self.run_command("build_ext")
result = super().run()
# Get the build directory
build_lib = self.get_finalized_command("build").build_lib
build_ctp_dir = os.path.join(build_lib, "ctp")
# Move SWIG-generated ctp.py from current directory to the package directory
if os.path.exists("ctp.py"):
shutil.move("ctp.py", os.path.join(build_ctp_dir, "ctp.py"))
# Copy API libraries
if package_data:
for lib in API_LIBS:
lib_name = os.path.basename(lib)
dst = os.path.join(build_ctp_dir, lib_name)
if not sys.platform.startswith("darwin"):
shutil.copy2(lib, dst)
else:
# Copy the framework to the package directory
framework_name = f"{lib_name}.framework"
framework_path = os.path.join(API_DIR, framework_name)
dst_framework = os.path.join(build_ctp_dir, framework_name)
if os.path.exists(dst_framework):
shutil.rmtree(dst_framework)
def ignore_dsstore(dir, files):
return [f for f in files if f == ".DS_Store"]
shutil.copytree(
framework_path,
dst_framework,
symlinks=True,
ignore=ignore_dsstore,
)
return result
CTP_EXT = Extension(
"ctp._ctp",
["ctp.i"],
# ['ctp_wrap.cpp'],
include_dirs=INC_DIRS,
library_dirs=LIB_DIRS,
extra_link_args=LINK_ARGS,
extra_compile_args=COMPILE_ARGS,
libraries=LIB_NAMES,
language="c++",
swig_opts=["-py3", "-c++", "-threads"] + ["-I" + inc for inc in INC_DIRS],
)
try:
setup(
name="ctp-python",
version=BUILD_VER,
author="Keli Hu",
author_email="dev@keli.hu",
description="""CTP for python""",
long_description=readme,
long_description_content_type="text/markdown",
url="https://github.com/keli/ctp-python",
ext_modules=[CTP_EXT],
packages=["ctp"], # Define ctp as a package
package_data={"ctp": package_data},
classifiers=[
"License :: OSI Approved :: BSD License",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: Python :: Implementation :: CPython",
],
cmdclass={
"build_py": BuildPy,
},
)
finally:
pass