forked from oanegros/MicroscopyNodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
155 lines (116 loc) · 3.92 KB
/
build.py
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
import glob
import os
import subprocess
import sys
from dataclasses import dataclass
from typing import List, Union
# import bpy
import tomlkit
toml_path = "microscopynodes/blender_manifest.toml"
whl_path = "./microscopynodes/wheels"
blender_path ="/Applications/Blender3.app/Contents/MacOS/Blender"
@dataclass
class Platform:
pypi_suffix: str
metadata: str
# tags for blender metadata
# platforms = ["windows-x64", "macos-arm64", "linux-x64", "windows-arm64", "macos-x64"]
windows_x64 = Platform(pypi_suffix="win_amd64", metadata="windows-x64")
linux_x64 = Platform(pypi_suffix="manylinux2014_x86_64", metadata="linux-x64")
macos_arm = Platform(pypi_suffix="macosx_12_0_arm64", metadata="macos-arm64")
macos_intel = Platform(pypi_suffix="macosx_10_16_x86_64", metadata="macos-x64")
required_packages = [
"tifffile==2023.4.12",
"scikit-image==0.22.0",
]
build_platforms = [
windows_x64,
linux_x64,
macos_arm,
macos_intel,
]
def run_python(args: str):
python = os.path.realpath(sys.executable)
subprocess.run([python] + args.split(" "))
def remove_whls():
for whl_file in glob.glob(os.path.join(whl_path, "*.whl")):
os.remove(whl_file)
def download_whls(
platforms: Union[Platform, List[Platform]],
required_packages: List[str] = required_packages,
python_version="3.11",
clean: bool = True,
):
if isinstance(platforms, Platform):
platforms = [platforms]
if clean:
remove_whls()
for platform in platforms:
run_python(
f"-m pip download {' '.join(required_packages)} --dest ./microscopynodes/wheels --only-binary=:all: --python-version={python_version} --platform={platform.pypi_suffix}"
)
def update_toml_whls(platforms):
# Define the path for wheel files
wheels_dir = "microscopynodes/wheels"
wheel_files = glob.glob(f"{wheels_dir}/*.whl")
wheel_files.sort()
# Packages to remove
packages_to_remove = {
"numpy"
}
# Filter out unwanted wheel files
to_remove = []
to_keep = []
for whl in wheel_files:
if any(pkg in whl for pkg in packages_to_remove):
to_remove.append(whl)
else:
to_keep.append(whl)
# Remove the unwanted wheel files from the filesystem
for whl in to_remove:
os.remove(whl)
# Load the TOML file
with open(toml_path, "r") as file:
manifest = tomlkit.parse(file.read())
# Update the wheels list with the remaining wheel files
manifest["wheels"] = [f"./wheels/{os.path.basename(whl)}" for whl in to_keep]
# Simplify platform handling
if not isinstance(platforms, list):
platforms = [platforms]
manifest["platforms"] = [p.metadata for p in platforms]
# Write the updated TOML file
with open(toml_path, "w") as file:
file.write(
tomlkit.dumps(manifest)
.replace('["', '[\n\t"')
.replace("\\\\", "/")
.replace('", "', '",\n\t"')
.replace('"]', '",\n]')
)
def clean_files(suffix: str = ".blend1") -> None:
pattern_to_remove = f"microscopynodes/**/*{suffix}"
for blend1_file in glob.glob(pattern_to_remove, recursive=True):
os.remove(blend1_file)
def build_extension(split: bool = True) -> None:
for suffix in [".blend1", ".MNSession"]:
clean_files(suffix=suffix)
if split:
subprocess.run(
f"{blender_path} --command extension build"
" --split-platforms --source-dir microscopynodes --output-dir .".split(" ")
)
else:
subprocess.run(
f"{blender_path} --command extension build "
"--source-dir microscopynodes --output-dir .".split(" ")
)
def build(platform) -> None:
download_whls(platform)
update_toml_whls(platform)
build_extension()
def main():
# for platform in build_platforms:
# build(platform)
build(build_platforms)
if __name__ == "__main__":
main()