-
Notifications
You must be signed in to change notification settings - Fork 50
/
setup.py
219 lines (185 loc) · 7.27 KB
/
setup.py
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
import json, marshal, os, shutil
from setuptools import setup, Extension
import sys
from distutils.core import Command
from files.version import __version__
from pathlib import Path
###################################################################################################
#
# Setup for NREL-PySAM Package
#
###################################################################################################
latest_version = __version__
DEBUG = False
# defaults and include directories
defaults_dir = os.environ['SAMNTDIR'] + "/api/api_autogen/library/defaults/"
includepath = os.environ['SAMNTDIR'] + "/api/include"
srcpath = os.environ['SAMNTDIR'] + "/api/src"
this_directory = os.environ['PYSAMDIR']
libpath = this_directory + "/files"
# prepare package description
with open(os.path.join(this_directory, 'RELEASE.md'), encoding='utf-8') as f:
long_description = f.read()
# prepare package
libfiles = ['__init__.py', 'version.py']
extra_compile_args = ["-Wno-implicit-function-declaration", "-Wno-unused-function", "-Wno-strict-prototypes"]
extra_link_args = []
defines = []
libs = ['SAM_api']
if DEBUG:
libs += ['sscd']
else:
libs += ['ssc']
if sys.platform == 'darwin':
from distutils import sysconfig
vars = sysconfig.get_config_vars()
vars['LDSHARED'] = vars['LDSHARED'].replace('-bundle', '-dynamiclib')
libfiles += ['libSAM_api.so']
if DEBUG:
libfiles += ['libsscd.so']
else:
libfiles += ['libssc.so']
extra_link_args = ["-headerpad_max_install_names", "-Wl,-rpath,@loader_path/"]
extra_compile_args.append("-Wno-ignored-attributes")
if sys.platform == 'linux':
libfiles += ['libSAM_api.so']
if DEBUG:
libfiles += ['libsscd.so']
else:
libfiles += ['libssc.so']
extra_link_args = ["-Wl,-rpath,$ORIGIN/"]
extra_compile_args.append('-Wno-attributes')
if sys.platform == 'win32':
libfiles += ['SAM_api.dll', 'SAM_api.lib']
if DEBUG:
libfiles += ['sscd.dll', 'sscd.lib']
else:
libfiles += ['ssc.dll', 'ssc.lib']
defines = [('__WINDOWS__', '1')]
extra_compile_args = []
if DEBUG:
extra_compile_args = ["/DEBUG", "/Od"]
###################################################################################################
#
# Copy Required Source and Data Files
#
###################################################################################################
# serialize all defaults into dict
def _decode(o):
if isinstance(o, str):
try:
return float(o)
except ValueError:
return o
elif isinstance(o, dict):
dic = {}
for k, v in o.items():
if k != "hybrid_dispatch_schedule" and k != "biopwr_plant_tou_grid":
dic[k] = _decode(v)
else:
dic[k] = v
return dic
elif isinstance(o, list):
return [_decode(v) for v in o]
else:
return o
defaults_df_dir = 'files/defaults'
if os.path.exists(defaults_df_dir):
shutil.rmtree(defaults_df_dir)
os.mkdir(defaults_df_dir)
# generate defaults and copy them into installation
for filename in os.listdir(defaults_dir):
with open(defaults_dir + '/' + filename) as f:
name = os.path.splitext(filename)
if name[1] != '.json':
continue
data = json.load(f)
dic = data[list(data.keys())[0]]
with open('files/defaults/' + name[0].lower() + '.df', "wb") as out:
marshal.dump(dic, out)
for filename in os.listdir(defaults_df_dir):
libfiles.append('defaults/' + os.path.splitext(filename)[0] + '.df')
# copy over stub pyi files into "files" folder for export
stub_files = []
shutil.copyfile(os.path.join(this_directory, "stubs", 'AdjustmentFactors.pyi'),
os.path.join(this_directory, 'stubs', 'stubs', 'AdjustmentFactors.pyi'))
for filename in os.listdir(os.path.join(this_directory, "stubs", "stubs")):
if ".pyi" not in filename:
continue
shutil.copy(os.path.join(this_directory, "stubs", "stubs", filename), os.path.join(this_directory, "files"))
stub_files.append(os.path.join(filename))
libfiles += stub_files
hybrid_stubs = []
for filename in os.listdir(os.path.join(this_directory, "files", "Hybrids")):
if ".pyi" not in filename:
continue
hybrid_stubs.append(os.path.join("Hybrids", filename))
libfiles += hybrid_stubs
# make list of all extension modules
extension_modules = [Extension('PySAM.AdjustmentFactors',
['src/AdjustmentFactors.c'],
define_macros=defines,
include_dirs=[srcpath, includepath, this_directory + "/src"],
library_dirs=[libpath],
libraries=libs,
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args
)]
for filename in os.listdir(this_directory + "/modules"):
extension_modules.append(Extension('PySAM.' + os.path.splitext(filename)[0],
['modules/' + filename],
define_macros=defines,
include_dirs=[srcpath, includepath, this_directory + "/src"],
library_dirs=[libpath],
libraries=libs,
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args
))
# function to rename macosx distribution for Python 3.7 to be minimum version of 10.12 instead of 10.14
class PostProcess(Command):
description = "rename macosx distribution for Python 3.7 to be minimum version of 10.12 instead of 10.14"
user_options = []
def initialize_options(self):
self.cwd = None
def finalize_options(self):
self.cwd = os.getcwd()
def run(self):
assert os.getcwd() == self.cwd, 'Must be in package root: %s' % self.cwd
name = "NREL_PySAM-" + latest_version + "-" + "cp37-cp37m-macosx_10_14_x86_64.whl"
newname = "NREL_PySAM-" + latest_version + "-" + "cp37-cp37m-macosx_10_12_x86_64.whl"
os.system('mv ./dist/' + name + ' ./dist/' + newname)
###################################################################################################
#
# setup script
#
###################################################################################################
def read_lines(filename):
with open(filename) as f_in:
return f_in.readlines()
setup(
name='NREL-PySAM',
version=latest_version,
url='https://nrel-pysam.readthedocs.io',
description="National Renewable Energy Laboratory's System Advisor Model Python Wrapper",
long_description=long_description,
long_description_content_type='text/markdown',
license='BSD 3-Clause',
author="dguittet",
author_email="[email protected]",
include_package_data=True,
packages=['PySAM', 'PySAM.Hybrids'],
package_dir={'PySAM': 'files', 'PySAM.Hybrids': 'files/Hybrids'},
package_data={
'': libfiles},
setup_requires=["pytest-runner"],
tests_require=["pytest"],
install_requires=read_lines(Path(__file__).parent / "requirements.txt"),
cmdclass={
'post': PostProcess
},
ext_modules=extension_modules
)
# Clean up
shutil.rmtree(defaults_df_dir)
for f in stub_files:
os.remove(os.path.join(this_directory, "files", f))