-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
214 lines (180 loc) · 6.09 KB
/
Copy pathsetup.py
File metadata and controls
214 lines (180 loc) · 6.09 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python3
import json
import os
import shutil
import subprocess
from pathlib import Path
from setuptools import find_packages, setup
from setuptools.command.build_py import build_py as _build_py
try:
from wheel.bdist_wheel import bdist_wheel as _bdist_wheel
except ImportError: # pragma: no cover - build backend installs wheel for real builds.
_bdist_wheel = None
ROOT = Path(__file__).resolve().parent
PACKAGE_JSON = ROOT / "package.json"
README = ROOT / "README.md"
PYTHON_SRC = ROOT / "python" / "src"
DIST_DIR = ROOT / "dist"
DEFAULT_GOCACHE_DIR = ROOT / ".cache" / "go-build"
PACKAGE_NAME = "rqdata-cli"
IMPORT_NAME = "rqdata_cli"
PLATFORM_TARGETS = {
"linux-x64": {
"goos": "linux",
"goarch": "amd64",
"binary_name": "rqdata",
"dist_name": "rqdata-linux-amd64",
"wheel_plat_name": "manylinux2014_x86_64",
},
"darwin-x64": {
"goos": "darwin",
"goarch": "amd64",
"binary_name": "rqdata",
"dist_name": "rqdata-macos-amd64",
"wheel_plat_name": "macosx_10_15_x86_64",
},
"darwin-arm64": {
"goos": "darwin",
"goarch": "arm64",
"binary_name": "rqdata",
"dist_name": "rqdata-macos-arm64",
"wheel_plat_name": "macosx_11_0_arm64",
},
"win32-x64": {
"goos": "windows",
"goarch": "amd64",
"binary_name": "rqdata.exe",
"dist_name": "rqdata-windows-amd64.exe",
"wheel_plat_name": "win_amd64",
},
}
CURRENT_TARGETS = {
("linux", "x86_64"): "linux-x64",
("linux", "amd64"): "linux-x64",
("darwin", "x86_64"): "darwin-x64",
("darwin", "amd64"): "darwin-x64",
("darwin", "arm64"): "darwin-arm64",
("win32", "x86_64"): "win32-x64",
("win32", "amd64"): "win32-x64",
}
def load_package_json():
return json.loads(PACKAGE_JSON.read_text(encoding="utf-8"))
def detect_current_target():
import platform
import sys
machine = platform.machine().lower()
key = (sys.platform, machine)
target = CURRENT_TARGETS.get(key)
if target:
return target
raise RuntimeError(
f"unsupported platform for wheel build: {sys.platform}-{machine}. "
"Set RQDATA_PY_TARGET explicitly."
)
def resolve_target():
key = os.environ.get("RQDATA_PY_TARGET") or detect_current_target()
target = PLATFORM_TARGETS.get(key)
if not target:
supported = ", ".join(sorted(PLATFORM_TARGETS))
raise RuntimeError(
f"unsupported RQDATA_PY_TARGET={key!r}. Supported values: {supported}"
)
return key, target
def ensure_binary(version, target_key, target):
binary_path = DIST_DIR / target["dist_name"]
if binary_path.exists():
return binary_path
DIST_DIR.mkdir(parents=True, exist_ok=True)
gocache_dir = Path(os.environ.get("GOCACHE_DIR", DEFAULT_GOCACHE_DIR))
gocache_dir.mkdir(parents=True, exist_ok=True)
ldflags = f"-s -w -X github.com/ricequant/rqdata-cli/cmd.Version={version}"
env = os.environ.copy()
env.update(
{
"CGO_ENABLED": "0",
"GOOS": target["goos"],
"GOARCH": target["goarch"],
"GOCACHE": str(gocache_dir),
}
)
print(f"Building Go binary for {target_key} -> {binary_path.name}")
subprocess.run(
["go", "build", "-trimpath", "-ldflags", ldflags, "-o", str(binary_path), "main.go"],
cwd=ROOT,
env=env,
check=True,
)
if target["binary_name"] != "rqdata.exe":
binary_path.chmod(0o755)
return binary_path
class build_py(_build_py):
def run(self):
super().run()
package_info = load_package_json()
version = package_info["version"]
target_key, target = resolve_target()
binary_source = ensure_binary(version, target_key, target)
destination_dir = Path(self.build_lib) / IMPORT_NAME / "bin"
destination_dir.mkdir(parents=True, exist_ok=True)
destination = destination_dir / target["binary_name"]
shutil.copy2(binary_source, destination)
if target["binary_name"] != "rqdata.exe":
destination.chmod(0o755)
if _bdist_wheel is not None:
class bdist_wheel(_bdist_wheel):
def finalize_options(self):
super().finalize_options()
_, target = resolve_target()
self.root_is_pure = False
self.plat_name = target["wheel_plat_name"]
self.plat_name_supplied = True
def get_tag(self):
_, target = resolve_target()
return ("py3", "none", target["wheel_plat_name"])
else: # pragma: no cover
bdist_wheel = None
package_info = load_package_json()
cmdclass = {
"build_py": build_py,
}
if bdist_wheel is not None:
cmdclass["bdist_wheel"] = bdist_wheel
setup(
name=PACKAGE_NAME,
version=package_info["version"],
description=package_info["description"],
long_description=README.read_text(encoding="utf-8"),
long_description_content_type="text/markdown",
license=package_info["license"],
url=package_info["homepage"],
project_urls={
"Source": package_info["repository"]["url"],
"Issues": package_info["bugs"]["url"],
},
python_requires=">=3.8",
packages=find_packages(where=str(PYTHON_SRC)),
package_dir={"": "python/src"},
include_package_data=True,
package_data={IMPORT_NAME: ["bin/*"]},
entry_points={
"console_scripts": [
"rqdata=rqdata_cli.__main__:main",
]
},
cmdclass=cmdclass,
keywords=package_info["keywords"],
classifiers=[
"Development Status :: 4 - Beta",
"Environment :: Console",
"Intended Audience :: Developers",
"Intended Audience :: Financial and Insurance Industry",
"Operating System :: MacOS",
"Operating System :: Microsoft :: Windows",
"Operating System :: POSIX :: Linux",
"Programming Language :: Go",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"Topic :: Office/Business :: Financial",
"Topic :: Software Development :: Libraries :: Python Modules",
],
)