-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
117 lines (96 loc) · 4.08 KB
/
setup.py
File metadata and controls
117 lines (96 loc) · 4.08 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
import os
import platform
import shutil
import tarfile
import zipfile
from pathlib import Path
from urllib.request import urlretrieve
from setuptools import setup
from setuptools.command.build import build
CPYTHON_NEAR_VERSION = os.environ.get("CPYTHON_NEAR_VERSION", "v3.13.5-near")
BINARYEN_VERSION = os.environ.get("BINARYEN_VERSION", "123")
BINARYEN_PLATFORM_MAP = {
"linux": ("x86_64-linux", "tar.gz"),
"macos_x86_64": ("x86_64-macos", "tar.gz"),
"macos_arm64": ("arm64-macos", "tar.gz"),
"windows": ("x86_64-windows", "tar.gz"),
}
class CustomBuild(build):
def run(self):
bin_dir = Path("src/cpython_near_wasm_opt/bin")
lib_dir = Path("src/cpython_near_wasm_opt/lib")
for dir in (bin_dir, lib_dir):
if dir.exists():
shutil.rmtree(dir)
dir.mkdir(parents=True, exist_ok=True)
self.download_binaryen(bin_dir, lib_dir)
self.download_cpython(lib_dir)
super().run()
def download_binaryen(self, bin_dir, lib_dir):
build_platform = os.environ.get("BUILD_PLATFORM")
if build_platform:
system = build_platform.lower()
else:
system = platform.system().lower()
if system == "darwin":
system = f"macos_{platform.machine().lower()}"
elif system == "windows":
system = "windows"
else:
system = "linux"
build_dir = Path("build")
if build_dir.exists():
shutil.rmtree(build_dir)
if system not in BINARYEN_PLATFORM_MAP:
raise RuntimeError(f"Unsupported platform: {system}")
platform_name, ext = BINARYEN_PLATFORM_MAP[system]
url = f"https://github.com/WebAssembly/binaryen/releases/download/version_{BINARYEN_VERSION}/binaryen-version_{BINARYEN_VERSION}-{platform_name}.{ext}"
archive_path = f"binaryen.{ext}"
print(f"Downloading {url}...")
urlretrieve(url, archive_path)
binary_name_suffixes = (
"wasm-dis",
"wasm-as",
"wasm-opt",
"wasm-dis.exe",
"wasm-as.exe",
"wasm-opt.exe",
)
if ext == "tar.gz":
with tarfile.open(archive_path, "r:gz") as tar:
for member in tar.getmembers():
if member.name.endswith(binary_name_suffixes):
member.name = os.path.basename(member.name)
tar.extract(member, bin_dir)
if member.name.endswith("libbinaryen.dylib"):
member.name = os.path.basename(member.name)
tar.extract(member, lib_dir)
elif ext == "zip":
with zipfile.ZipFile(archive_path, "r") as zip_file:
for name in zip_file.namelist():
if name.endswith(binary_name_suffixes):
with (
zip_file.open(name) as src,
open(bin_dir / os.path.basename(name), "wb") as dst,
):
shutil.copyfileobj(src, dst)
if system not in ["windows"]:
for binary in bin_dir.glob("wasm-*"):
binary.chmod(0o755)
os.remove(archive_path)
def download_cpython(self, lib_dir):
url = f"https://github.com/past-hypothesis/cpython-near/releases/download/{CPYTHON_NEAR_VERSION}/python-wasm-near-{CPYTHON_NEAR_VERSION}.zip"
archive_path = f"python-wasm-near-{CPYTHON_NEAR_VERSION}.zip"
print(f"Downloading {url}...")
urlretrieve(url, archive_path)
with zipfile.ZipFile(archive_path, "r") as zip_file:
for name in zip_file.namelist():
with (
zip_file.open(name) as src,
open(lib_dir / os.path.basename(name), "wb") as dst,
):
shutil.copyfileobj(src, dst)
os.remove(archive_path)
setup(
cmdclass={"build": CustomBuild},
)