-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathsetup_developer.py
More file actions
301 lines (255 loc) · 10.1 KB
/
setup_developer.py
File metadata and controls
301 lines (255 loc) · 10.1 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import os
import shutil
import warnings
import platform
import subprocess as sb
from os.path import isfile, join
from os import listdir
import numpy
from setuptools import setup, find_packages
from setuptools.extension import Extension
from setuptools.command.build_ext import build_ext
from Cython.Build import cythonize
from Cython.Compiler.Errors import CompileError
try:
from isp import ROOT_DIR
except Exception as e:
raise RuntimeError(f"Could not import isp. Ensure ISP is importable. Error: {e}")
system_computer = platform.system().lower()
machine_computer = platform.machine().lower()
if system_computer in ("linux", "linux2"):
system_path = os.path.join(ROOT_DIR, "linux_bin")
elif system_computer == "darwin":
if machine_computer == "arm64":
system_path = os.path.join(ROOT_DIR, "mac_m_bin") # Apple Silicon
else:
system_path = os.path.join(ROOT_DIR, "mac_bin") # Intel mac
else:
# Unsupported platforms won't stop the build; just print info.
print(f"Unsupported system: {system_computer} ({machine_computer})")
system_path = os.path.join(ROOT_DIR, "bin_unknown")
cy_path = os.path.join(ROOT_DIR, "cython_code")
def exc_cmd(cmd, **kwargs):
"""
Execute a subprocess Popen and catch except.
:param cmd: The command to be execute.
:param kwargs: All subprocess.Popen(..).
:return: The stdout.
:except excepts: SubprocessError, CalledProcessError
:keyword stdin: Default=subprocess.PIPE
:keyword stdout: Default=subprocess.PIPE
:keyword stderr: Default=subprocess.PIPE
:keyword encoding: Default=utf-8
"""
stdout = kwargs.pop("stdout", sb.PIPE)
stdin = kwargs.pop("stdin", sb.PIPE)
stderr = kwargs.pop("stderr", sb.PIPE)
encoding = kwargs.pop("encoding", "utf-8")
with sb.Popen(cmd, stdin=stdin, stdout=stdout, stderr=stderr, encoding=encoding, **kwargs) as p:
try:
std_out, std_err = p.communicate(timeout=15)
except sb.TimeoutExpired:
p.kill()
std_out, std_err = p.communicate() # try again if timeout fails.
if p.returncode != 0: # Bad error.
raise sb.CalledProcessError(p.returncode, std_err.strip())
elif len(std_err) != 0: # Some possible errors trowed by the running subprocess, but not critical.
raise sb.SubprocessError(std_err.strip())
return std_out.strip()
def extract_nll():
try:
folder_nll = os.path.join(ROOT_DIR,"NLL7")
if os.path.isdir(folder_nll):
shutil.rmtree(folder_nll)
except:
pass
extract_at = os.path.join(ROOT_DIR, "NLL7")
try:
if not os.path.isdir(extract_at):
os.mkdir(extract_at)
else:
shutil.rmtree(extract_at)
os.mkdir(extract_at)
shutil.unpack_archive("NLL2023_src.tgz", extract_at)
os.mkdir(os.path.join(extract_at, "bin"))
except IOError as error:
print("Warnning", error)
return extract_at
def extract_focmec():
try:
folder_focmec = os.path.join(ROOT_DIR,"focmec")
if os.path.isdir(folder_focmec):
shutil.rmtree(folder_focmec)
except:
pass
check_focmec = os.path.join(ROOT_DIR, "focmec")
try:
if not os.path.isdir(check_focmec):
shutil.unpack_archive("focmec2023_src.tgz", ROOT_DIR)
else:
shutil.rmtree(check_focmec)
shutil.unpack_archive("focmec2023_src.tgz", ROOT_DIR)
except IOError as error:
print(error)
return check_focmec
def extract_mti():
try:
folder_mti = os.path.join(ROOT_DIR, "mti","green_source")
if os.path.isdir(folder_mti):
shutil.rmtree(folder_mti)
except:
pass
try:
folder_work_mti = os.path.join(ROOT_DIR, "mti","green")
if os.path.isdir(older_work_mti):
shutil.rmtree(older_work_mti)
except:
pass
extract_at = os.path.join(ROOT_DIR, "mti")
try:
if not os.path.isdir(extract_at):
os.mkdir(extract_at)
shutil.unpack_archive("isola2023_src.tgz", extract_at)
else:
shutil.unpack_archive("isola2023_src.tgz", extract_at)
if not os.path.isdir(folder_work_mti):
os.mkdir(folder_work_mti)
except IOError as error:
print(error)
return extract_at
class CustomBuildExtCommand(build_ext):
def run(self):
print("Extracting files")
nll_dir = extract_nll()
focmec_dir = extract_focmec()
mti_dir = extract_mti()
print("Finished extraction")
print("Start compiling")
self.make_nll(nll_dir)
self.make_focmec(focmec_dir)
self.make_mti(mti_dir)
print("Finished Compilation")
# run build
build_ext.run(self)
def make_nll(self, nll_dir):
src_path = os.path.join(nll_dir, "src")
bin_path = os.path.join(nll_dir, "bin")
command = "export MYBIN={bin_path}; cd {src_path}; sh compile_nll.sh".format(bin_path=bin_path, src_path=src_path)
try:
exc_cmd(command, shell=True)
print("NLL7 successfully installed")
except sb.CalledProcessError as e: # this is a bad error/warning.
print("Warnning on trying to run nll make file / NLL7 successfully installed")
print(e)
except sb.SubprocessError: # some warnings nothing so bad.
print("NLL7 successfully installed")
def make_focmec(self, focmec_dir):
src_path = os.path.join(focmec_dir, "src")
command = "sh build_package_sh"
try:
exc_cmd(command, cwd=src_path, shell=True)
print("FOCMEC successfully installed")
except sb.CalledProcessError as e: # this is a bad error.
print("Error on trying to run focmec make file.")
print(e)
except sb.SubprocessError: # some warnings nothing so bad.
print("FOCMEC successfully installed")
def make_mti(self, mti_dir):
green_path = os.path.join(mti_dir,"green_source")
work_dir = os.path.join(mti_dir,"green")
command = "sh compile_mti_ifort.sh"
try:
exc_cmd(command, cwd=green_path, shell=True)
print("MTI successfully installed")
self.copy_mti_binaries(green_path, work_dir)
except sb.CalledProcessError as e: # this is a bad error.
print("Error on trying to run MTI make file.")
print(e)
except sb.SubprocessError: # some warnings nothing so bad.
print("MTI successfully installed")
def copy_mti_binaries(self, source_code, destination):
allfiles = [f for f in listdir(source_code) if isfile(join(source_code, f))]
# iterate on all files to move them to destination folder
for f in allfiles:
src_path = os.path.join(source_code, f)
dst_path = os.path.join(destination, f)
shutil.copy(src_path, dst_path)
# ----------------------------------------------------------------------
# Build Cython extensions with per-module try/except
# ----------------------------------------------------------------------
extra_compile_args = ["/O2"] if system_computer == "windows" else ["-O3"]
extra_link_args = []
extra_libraries = [] if system_computer == "windows" else ["m"] # libm on POSIX
def try_add_extension(modname, src):
"""
Try to cythonize a single extension. On failure, warn and continue.
Returns a list (possibly empty) of cythonized Extension objects.
"""
try:
ext = Extension(
name=modname,
sources=[src],
include_dirs=[numpy.get_include(), cy_path],
language="c",
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args,
libraries=extra_libraries,
)
return cythonize(
[ext],
language_level=3,
compiler_directives={
"boundscheck": False,
"wraparound": False,
"cdivision": True,
"nonecheck": False,
},
)
except CompileError as e:
warnings.warn(f"Could not compile {modname}: {e}. Skipping.")
except Exception as e:
warnings.warn(f"Unexpected error compiling {modname}: {e}. Skipping.")
return []
# Collect target .pyx modules (explicit list; add/remove as needed)
# cy_modules = [
# ("isp.cython_code.hampel", os.path.join(cy_path, "hampel.pyx")),
# # CF modules:
# ("isp.cython_code.rec_filter", os.path.join(cy_path, "rec_filter.pyx")),
# ("isp.cython_code.lib_rec_hos", os.path.join(cy_path, "lib_rec_hos.pyx")),
# ("isp.cython_code.lib_rec_rms", os.path.join(cy_path, "lib_rec_rms.pyx")),
# ("isp.cython_code.lib_rosenberger",os.path.join(cy_path, "lib_rosenberger.pyx")),
# ("isp.cython_code.lib_rec_cc", os.path.join(cy_path, "lib_rec_cc.pyx")),
# # Optional/problematic ones (won't break the build if they fail):
# ("isp.cython_code.ccwt_cy", os.path.join(cy_path, "ccwt_cy.pyx")),
# ("isp.cython_code.whiten", os.path.join(cy_path, "whiten.pyx")),
# ]
cy_modules = [
("isp.cython_code.hampel", os.path.join(cy_path, "hampel.pyx")),
("isp.cython_code.ccwt_cy", os.path.join(cy_path, "ccwt_cy.pyx")),
("isp.cython_code.whiten", os.path.join(cy_path, "whiten.pyx"))]
ext_list = []
for modname, src in cy_modules:
if os.path.isfile(src):
ext_list.extend(try_add_extension(modname, src))
else:
warnings.warn(f"Source not found for {modname}: {src}. Skipping.")
# ----------------------------------------------------------------------
# Setup
# ----------------------------------------------------------------------
setup(
cmdclass={'build_ext': CustomBuildExtCommand},
name='isp_package',
version='2.0',
description='ISP setup script',
packages=find_packages(include=['isp', 'isp.*']),
include_dirs=[numpy.get_include()],
ext_modules=ext_list,
)
#setup(
# cmdclass={
# 'build_ext': CustomBuildExtCommand,
# },
# ext_modules=cythonize(os.path.join(cy_path, 'ccwt_cy.pyx')),
# include_dirs=[numpy.get_include()],
# ext_package='isp.c_lib'
#)