forked from CosmoStat/cosmostat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
222 lines (186 loc) · 6.91 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
220
221
#! /usr/bin/env python
##########################################################################
# XXX - Copyright (C) XXX, 2017
# Distributed under the terms of the CeCILL-B license, as published by
# the CEA-CNRS-INRIA. Refer to the LICENSE file or to
# http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
# for details.
# COSMOSTAT setup
##########################################################################
# System import
import os
import re
import sys
import platform
import subprocess
from pprint import pprint
from distutils.version import LooseVersion
from setuptools.command.build_ext import build_ext
from setuptools import setup, find_packages, Extension
from setuptools.command.test import test as TestCommand
from setuptools.command.install import install
from importlib import import_module
from setuptools import setup, find_packages
setup(
name="pycs",
author="CosmoStat Laboratory",
author_email="",
version="0.0.1rc1",
packages=find_packages(),
# install_requires = [
# 'lenspack @ git+https://github.com/CosmoStat/lenspack.git@master#egg=lenspack',
# ]
)
# Package information
release_info = {}
infopath = os.path.abspath(
os.path.join(os.path.dirname(__file__), "pycs", "info.py"))
with open(infopath) as open_file:
exec(open_file.read(), release_info)
pkgdata = {
"pycs": [
os.path.join("test", "*.py"),
os.path.join("test", "*.json")]
}
scripts = [
os.path.join("pycs")
]
# Workaround
if "--release" in sys.argv:
sys.argv.remove("--release")
scripts = [
os.path.join("pycs"),
]
class CMakeExtension(Extension):
""" Use absolute path in setuptools extension.
"""
def __init__(self, name, sourcedir=""):
Extension.__init__(self, name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)
def pipinstall(package_list):
""" Pip install PyPi packages.
"""
if not isinstance(package_list, list):
raise TypeError('preinstall inputs must be of type list.')
for package in package_list:
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
class CMakeBuild(build_ext):
""" Define a cmake build extension.
"""
def _set_pybind_path(self):
""" Set path to Pybind11 include directory.
"""
self.pybind_path = getattr(import_module('pybind11'), 'get_include')()
def run(self):
""" Redifine the run method.
"""
# Set preinstall requirements
preinstall_list = release_info["PREINSTALL_REQUIRES"]
# Preinstall packages
pipinstall(preinstall_list)
# Set Pybind11 path
self._set_pybind_path()
# Check cmake is installed and is sufficiently new.
try:
out = subprocess.check_output(["cmake", "--version"])
except OSError:
raise RuntimeError(
"CMake must be installed to build the following extensions: " +
", ".join(e.name for e in self.extensions))
cmake_version = LooseVersion(re.search(r"version\s*([\d.]+)",
out.decode()).group(1))
if cmake_version < "3.0.0":
raise RuntimeError("CMake >= 3.0.0 is required.")
# Build extensions
for ext in self.extensions:
self.build_extension(ext)
def build_extension(self, ext):
""" Build extension with cmake.
"""
# Define cmake arguments
extdir = os.path.abspath(
os.path.dirname(self.get_ext_fullpath(ext.name)))
cmake_args = ["-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=" + extdir,
"-DPYTHON_EXECUTABLE=" + sys.executable,
"-DPYBIND11_INCLUDE_DIR=" + self.pybind_path]
cfg = "Debug" if self.debug else "Release"
build_args = ["--config", cfg]
if platform.system() == "Windows":
cmake_args += ["-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{0}={1}".format(
cfg.upper(), extdir)]
if sys.maxsize > 2 ** 32:
cmake_args += ["-A", "x64"]
build_args += ["--", "/m"]
else:
cmake_args += ["-DCMAKE_BUILD_TYPE=" + cfg]
build_args += ["--", "-j8"]
# Call cmake in specific environment
env = os.environ.copy()
env["CXXFLAGS"] = '{0} -DVERSION_INFO=\\"{1}\\"'.format(
env.get("CXXFLAGS", ""), self.distribution.get_version())
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
print("Building 'pycs' in {0}...".format(self.build_temp))
print("Cmake args:")
pprint(cmake_args)
print("Cmake build args:")
pprint(build_args)
subprocess.check_call(["cmake", ext.sourcedir] + cmake_args,
cwd=self.build_temp, env=env)
subprocess.check_call(["cmake", "--build", "."] + build_args,
cwd=self.build_temp)
print()
class HybridTestCommand(TestCommand):
""" Define custom mix Python/C++ test runner.
We will execute both Python unittest tests and C++ Catch tests.
"""
def distutils_dir_name(self, dname):
""" Returns the name of a distutils build directory.
"""
dir_name = "{dirname}.{platform}-{version[0]}.{version[1]}"
return dir_name.format(dirname=dname,
platform=sysconfig.get_platform(),
version=sys.version_info)
def run(self):
""" Run hybrid tests.
"""
# Run Python tests
super(HybridTestCommand, self).run()
print("\nPython tests complete, now running C++ tests...\n")
# Run catch tests
test_dir = os.path.join("build", self.distutils_dir_name("temp"),
"sparse2d", "src", "sparse2d", "tests")
print("\nExpect C++ test script in {0}.\n".format(test_dir))
subprocess.call(["./*_test"], cwd=test_dir, shell=True)
class PluginBuild(install):
""" Install Plugins
Install plugins from PyPi following pycs build.
"""
def run(self):
pipinstall(release_info["PLUGINS"])
install.run(self)
# Write setup
setup(
name=release_info["NAME"],
description=release_info["DESCRIPTION"],
long_description=release_info["LONG_DESCRIPTION"],
license=release_info["LICENSE"],
classifiers=release_info["CLASSIFIERS"],
author=release_info["AUTHOR"],
author_email=release_info["AUTHOR_EMAIL"],
version=release_info["VERSION"],
url=release_info["URL"],
packages=find_packages(exclude="doc"),
platforms=release_info["PLATFORMS"],
extras_require=release_info["EXTRA_REQUIRES"],
install_requires=release_info["REQUIRES"],
package_data=pkgdata,
scripts=scripts,
ext_modules=[CMakeExtension(
"pymrs", sourcedir=os.path.join("src", "cxx"))],
cmdclass={
"build_ext": CMakeBuild,
"test": HybridTestCommand,
"install": PluginBuild
}
)