-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
executable file
·132 lines (113 loc) · 5.08 KB
/
setup.py
File metadata and controls
executable file
·132 lines (113 loc) · 5.08 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
"""
Copyright (C) 2009-2017 Jussi Leinonen, Finnish Meteorological Institute,
California Institute of Technology
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
from setuptools import setup # , Extension
import subprocess
import os
from setuptools.command.build_ext import build_ext as _build_ext
import glob
import shutil
# Custom build extension class to invoke f2py manually
class PyTMatrixBuildExt(_build_ext):
def run(self):
# Call f2py to compile Fortran code into a Python extension
self.build_fortran_extension()
# Proceed with the usual build_ext behavior
super().run()
def build_fortran_extension(self):
# Path to Fortran sources
sources = [
os.path.join(
"pytmatrix", "fortran_tm", "pytmatrix.pyf"
), # Interface file for f2py
os.path.join("pytmatrix", "fortran_tm", "ampld.lp.f"),
os.path.join("pytmatrix", "fortran_tm", "lpd.f"),
]
# The output module name should match the Python import path
output_module = "fortran_tm.pytmatrix"
# Get the current working directory
cwd = os.getcwd()
include_dir = os.path.join(cwd, "pytmatrix", "fortran_tm")
# Command to run f2py and compile the Fortran code into a shared object (.so)
cmd = ["f2py", "-c", "-m", output_module, "-I", include_dir] + sources
try:
print("Running f2py command:", " ".join(cmd))
# Use subprocess.run to capture stdout and stderr
result = subprocess.run(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
)
# Check for non-zero return code (indicating failure)
if result.returncode != 0:
print(f"f2py failed with error code {result.returncode}")
print(f"Standard output:\n{result.stdout}")
print(f"Standard error:\n{result.stderr}")
raise subprocess.CalledProcessError(result.returncode, cmd)
# After successful compilation, find the generated .so file
compiled_ext = glob.glob("*.so") + glob.glob("*.pyd")
if compiled_ext:
ext_file = compiled_ext[0]
target_dir = os.path.join("pytmatrix", "fortran_tm")
# Ensure the target directory exists
if not os.path.exists(target_dir):
os.makedirs(target_dir)
# Move the .so file to the correct directory
shutil.move(ext_file, os.path.join(target_dir, ext_file))
print(f"Moved {ext_file} to {target_dir}")
except subprocess.CalledProcessError as e:
print(f"f2py failed with error code {e.returncode}")
raise
# Run the f2py command to compile the extension
print("Running f2py to compile Fortran sources:")
subprocess.check_call(cmd)
# Basic metadata about your package
setup(
name="pytmatrix",
version="0.3.3",
author="Jussi Leinonen",
author_email="jsleinonen@gmail.com",
description="T-matrix scattering computations",
license="MIT",
long_description="A Python code for computing the scattering properties of homogeneous nonspherical scatterers with the T-Matrix method. Requires NumPy and SciPy.",
classifiers=[
"Development Status :: 4 - Beta",
"Intended Audience :: Science/Research",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Programming Language :: Fortran",
"Programming Language :: Python",
"Topic :: Scientific/Engineering :: Physics",
],
packages=[
"pytmatrix",
"pytmatrix.test",
"pytmatrix.quadrature",
"pytmatrix.fortran_tm",
],
package_data={
"pytmatrix": ["ice_refr.dat"],
"pytmatrix.fortran_tm": ["ampld.par.f"],
},
# Install dependencies
install_requires=["numpy", "scipy"],
ext_modules=[],
# Custom build step
cmdclass={
"build_ext": PyTMatrixBuildExt, # Use our custom build_ext that runs f2py
},
zip_safe=False, # Avoid zip safe for binary extensions
)