-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev_build.py
More file actions
171 lines (137 loc) · 4.66 KB
/
dev_build.py
File metadata and controls
171 lines (137 loc) · 4.66 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
#!/usr/bin/env python3
"""
Build script for CFD Python
Usage:
python build.py # Build and install in development mode
python build.py build # Build only (no install)
python build.py install # Build and install
python build.py test # Run tests
python build.py clean # Clean build artifacts
python build.py all # Clean, build, install, and test
"""
import argparse
import shutil
import subprocess
import sys
from pathlib import Path
# Project paths
PROJECT_ROOT = Path(__file__).parent.resolve()
CFD_ROOT = PROJECT_ROOT.parent / "cfd"
BUILD_DIR = PROJECT_ROOT / "build"
def run(cmd, cwd=None, check=True):
"""Run a command and print it"""
print(f">>> {cmd}")
result = subprocess.run(cmd, shell=True, cwd=cwd or PROJECT_ROOT)
if check and result.returncode != 0:
sys.exit(result.returncode)
return result
def build_cfd_library():
"""Build the C library (required before building Python extension)"""
print("\n=== Building CFD C Library ===")
if not CFD_ROOT.exists():
print(f"ERROR: CFD library not found at {CFD_ROOT}")
sys.exit(1)
# Check for build script in cfd directory
build_script = CFD_ROOT / "build.sh"
if build_script.exists():
run("./build.sh build", cwd=CFD_ROOT)
else:
# Use cmake directly
run("cmake -B build -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF", cwd=CFD_ROOT)
run("cmake --build build --config Release", cwd=CFD_ROOT)
def build():
"""Build the Python extension"""
print("\n=== Building Python Extension ===")
# Ensure C library is built
cfd_lib_dir = CFD_ROOT / "build" / "lib" / "Release"
if not cfd_lib_dir.exists():
build_cfd_library()
# Build using pip
run(f"{sys.executable} -m pip install --no-build-isolation -e .")
def install():
"""Install the package"""
print("\n=== Installing Package ===")
run(f"{sys.executable} -m pip install .")
def develop():
"""Install in development/editable mode"""
print("\n=== Installing in Development Mode ===")
run(f"{sys.executable} -m pip install -e .")
def test():
"""Run tests"""
print("\n=== Running Tests ===")
run(f"{sys.executable} -m pytest tests/ -v")
def clean():
"""Clean build artifacts"""
print("\n=== Cleaning Build Artifacts ===")
dirs_to_clean = [
BUILD_DIR,
PROJECT_ROOT / "dist",
PROJECT_ROOT / "wheelhouse",
PROJECT_ROOT / "cfd_python.egg-info",
PROJECT_ROOT / ".pytest_cache",
]
for d in dirs_to_clean:
if d.exists():
print(f"Removing {d}")
shutil.rmtree(d)
# Remove __pycache__ directories
for pycache in PROJECT_ROOT.rglob("__pycache__"):
print(f"Removing {pycache}")
shutil.rmtree(pycache)
# Remove .pyd/.so files (skip virtual environments)
# Common venv directory names to exclude
venv_patterns = {".venv", "venv", ".env", "env", "virtualenv", ".virtualenv"}
for ext in ["*.pyd", "*.so"]:
for f in PROJECT_ROOT.rglob(ext):
# Check if any path component matches a venv pattern
if not any(part in venv_patterns for part in f.parts):
print(f"Removing {f}")
f.unlink()
def verify():
"""Verify the installation works"""
print("\n=== Verifying Installation ===")
verify_cmd = (
f'{sys.executable} -c "import cfd_python; '
f"print(f'Version: {{cfd_python.__version__}}'); "
f"print(f'Solvers: {{cfd_python.list_solvers()}}')\""
)
result = run(verify_cmd, check=False)
return result.returncode == 0
def main():
parser = argparse.ArgumentParser(description="Build script for CFD Python")
parser.add_argument(
"command",
nargs="?",
default="develop",
choices=["build", "install", "develop", "test", "clean", "all", "verify", "cfd"],
help="Command to run (default: develop)",
)
args = parser.parse_args()
if args.command == "build":
build()
elif args.command == "install":
build_cfd_library()
install()
elif args.command == "develop":
build_cfd_library()
develop()
elif args.command == "test":
test()
elif args.command == "clean":
clean()
elif args.command == "verify":
verify()
elif args.command == "cfd":
build_cfd_library()
elif args.command == "all":
clean()
build_cfd_library()
develop()
if verify():
test()
else:
print("ERROR: Verification failed")
sys.exit(1)
print("\nDone!")
if __name__ == "__main__":
main()