1+ import sys
2+ from typing import Type
3+
14import os
25import shutil
6+ import tarfile
37import platform
48from pathlib import Path
59from setuptools import setup , Extension , Command
10+ from setuptools .command import build_ext
11+ from distutils .core import Command as DistCommand
612from warnings import warn
713
814FORCE_BINARIES = "NOVA_FORCE" in os .environ and os .environ ["NOVA_FORCE" ].lower () == "binaries"
@@ -35,20 +41,20 @@ def create_if_none(path: Path):
3541
3642
3743class UpdateBinariesCommand (Command ):
38- rebuild_binaries : bool
44+ build_binaries : bool
3945
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." )]
4147 description = "Update binaries for distribution"
4248
4349 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
4753 else :
48- self .rebuild_binaries = True
54+ self .build_binaries = True
4955
5056 def run (self ) -> None :
51- if self .rebuild_binaries :
57+ if self .build_binaries :
5258 build_nova_physics ()
5359
5460 create_if_none (PREBUILT_OS_DIR )
@@ -65,9 +71,16 @@ def run(self) -> None:
6571 shutil .copytree (to_copy , dir_name )
6672 else :
6773 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 )
6881
6982 def initialize_options (self ) -> None :
70- self .rebuild_binaries = True
83+ self .build_binaries = BUILD_BINARIES
7184
7285
7386def innit_checks ():
@@ -111,6 +124,13 @@ def build_nova_physics():
111124 )
112125
113126
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+
114134def get_nova_to_link ():
115135 if use_binaries ():
116136 for path in LOCAL_BINARIES .iterdir ():
@@ -154,10 +174,21 @@ def get_nova_sources():
154174 return sources
155175
156176
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+
157188def main ():
158189 innit_checks ()
159190
160- nova_to_link = str (get_nova_to_link (). relative_to ( PACKAGE_DIR ) )
191+ nova_to_link = str (NOVA_TO_LINK )
161192 stubs_to_override = {REAL_PACKAGE / "__init__.pyi" : NOVA_PYTHON_STUB , REAL_PACKAGE / "_nova.pyi" : NOVA_PYTHON_STUB }
162193
163194 for write_to , read_from in stubs_to_override .items ():
@@ -190,7 +221,10 @@ def main():
190221 "nova" : ["*.pyi" ]
191222 },
192223 packages = ["nova" ],
193- cmdclass = {"update_binaries" : UpdateBinariesCommand }
224+ cmdclass = {
225+ "update_binaries" : UpdateBinariesCommand ,
226+ "build_ext" : generate_cmd_class (build_ext .build_ext )
227+ }
194228 )
195229
196230
0 commit comments