1
+ import sys
2
+ from typing import Type
3
+
1
4
import os
2
5
import shutil
6
+ import tarfile
3
7
import platform
4
8
from pathlib import Path
5
9
from setuptools import setup , Extension , Command
10
+ from setuptools .command import build_ext
11
+ from distutils .core import Command as DistCommand
6
12
from warnings import warn
7
13
8
14
FORCE_BINARIES = "NOVA_FORCE" in os .environ and os .environ ["NOVA_FORCE" ].lower () == "binaries"
@@ -35,20 +41,20 @@ def create_if_none(path: Path):
35
41
36
42
37
43
class UpdateBinariesCommand (Command ):
38
- rebuild_binaries : bool
44
+ build_binaries : bool
39
45
40
- user_options = [("rebuild -binaries=" , None , "Whether to build nova-physics from scratch." )]
46
+ user_options = [("dont-build -binaries=" , None , "Whether to build nova-physics from scratch." )]
41
47
description = "Update binaries for distribution"
42
48
43
49
def finalize_options (self ) -> None :
44
- if isinstance (self .rebuild_binaries , str ):
45
- if self .rebuild_binaries .lower () == "false" :
46
- self .rebuild_binaries = False
50
+ if isinstance (self .build_binaries , str ):
51
+ if self .build_binaries .lower () == "false" :
52
+ self .build_binaries = False
47
53
else :
48
- self .rebuild_binaries = True
54
+ self .build_binaries = True
49
55
50
56
def run (self ) -> None :
51
- if self .rebuild_binaries :
57
+ if self .build_binaries :
52
58
build_nova_physics ()
53
59
54
60
create_if_none (PREBUILT_OS_DIR )
@@ -65,9 +71,16 @@ def run(self) -> None:
65
71
shutil .copytree (to_copy , dir_name )
66
72
else :
67
73
shutil .copy (to_copy , dir_name )
74
+ elif path .is_file () and path .suffix == ".gz" :
75
+ with tarfile .open (name = path , mode = "r:gz" ) as tar :
76
+ subdir_and_files = [
77
+ tarinfo for tarinfo in tar .getmembers ()
78
+ if tarinfo .name .startswith ("./include" )
79
+ ]
80
+ tar .extractall (path = PREBUILT_DIR , members = subdir_and_files )
68
81
69
82
def initialize_options (self ) -> None :
70
- self .rebuild_binaries = True
83
+ self .build_binaries = BUILD_BINARIES
71
84
72
85
73
86
def innit_checks ():
@@ -111,6 +124,13 @@ def build_nova_physics():
111
124
)
112
125
113
126
127
+ NOVA_TO_LINK = Path ("dummy/path" )
128
+
129
+ BUILD_BINARIES = "--dont-build-binaries" not in sys .argv
130
+ if not BUILD_BINARIES :
131
+ sys .argv .remove ("--dont-build-binaries" )
132
+
133
+
114
134
def get_nova_to_link ():
115
135
if use_binaries ():
116
136
for path in LOCAL_BINARIES .iterdir ():
@@ -154,10 +174,21 @@ def get_nova_sources():
154
174
return sources
155
175
156
176
177
+ def pre_build (self ):
178
+ global NOVA_TO_LINK
179
+ innit_checks ()
180
+ NOVA_TO_LINK = get_nova_to_link ().relative_to (PACKAGE_DIR )
181
+ self ._old_run ()
182
+
183
+
184
+ def generate_cmd_class (orig : Type [DistCommand ]):
185
+ return type (f"Overriden{ orig .__name__ .capitalize ()} " , (orig ,), {"run" : pre_build , "_old_run" : orig .run })
186
+
187
+
157
188
def main ():
158
189
innit_checks ()
159
190
160
- nova_to_link = str (get_nova_to_link (). relative_to ( PACKAGE_DIR ) )
191
+ nova_to_link = str (NOVA_TO_LINK )
161
192
stubs_to_override = {REAL_PACKAGE / "__init__.pyi" : NOVA_PYTHON_STUB , REAL_PACKAGE / "_nova.pyi" : NOVA_PYTHON_STUB }
162
193
163
194
for write_to , read_from in stubs_to_override .items ():
@@ -190,7 +221,10 @@ def main():
190
221
"nova" : ["*.pyi" ]
191
222
},
192
223
packages = ["nova" ],
193
- cmdclass = {"update_binaries" : UpdateBinariesCommand }
224
+ cmdclass = {
225
+ "update_binaries" : UpdateBinariesCommand ,
226
+ "build_ext" : generate_cmd_class (build_ext .build_ext )
227
+ }
194
228
)
195
229
196
230
0 commit comments