forked from joshloyal/L1-Trend-Filtering
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
182 lines (135 loc) · 4.74 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
from __future__ import print_function
import os
import sys
import contextlib
import subprocess
import glob
from setuptools import setup, find_packages, Extension
HERE = os.path.dirname(os.path.abspath(__file__))
# import ``__version__` from code base
exec(open(os.path.join(HERE, 'l1tf', 'version.py')).read())
with open('requirements.txt') as f:
INSTALL_REQUIRES = [l.strip() for l in f.readlines() if l]
with open('test_requirements.txt') as f:
TEST_REQUIRES = [l.strip() for l in f.readlines() if l]
try:
import numpy
except ImportError:
print('numpy is required during installation')
sys.exit(1)
try:
import scipy
except ImportError:
print('scipy is required during installation')
sys.exit(1)
@contextlib.contextmanager
def chdir(new_dir):
old_dir = os.getcwd()
try:
sys.path.insert(0, new_dir)
yield
finally:
del sys.path[0]
os.chdir(old_dir)
def find_cython(dir, files=None):
if files is None:
files = []
for file in os.listdir(dir):
path = os.path.join(dir, file)
if os.path.isfile(path) and path.endswith(".pyx"):
files.append(path.replace(os.path.sep, ".")[:-4])
elif os.path.isdir(path):
find_cython(path, files)
return files
def clean(path):
for name in find_cython(path):
name = name.replace('.', os.path.sep)
for ext in ['*.c', '*.so', '*.o', '*.html']:
file_path = glob.glob(os.path.join(path, name + ext))
if file_path and os.path.exists(file_path[0]):
os.unlink(file_path[0])
def get_include():
return os.path.join(HERE, 'src')
def get_sources():
files = []
src_path = get_include()
for name in os.listdir(src_path):
path = os.path.join(src_path, name)
if os.path.isfile(path) and path.endswith(".c"):
files.append(path)
return files
def generate_cython(cython_cov=False):
print("Cythonizing sources")
for source in find_cython(HERE):
source = source.replace('.', os.path.sep) + '.pyx'
cythonize_source(source, cython_cov)
def cythonize_source(source, cython_cov=False):
print("Processing %s" % source)
flags = ['--fast-fail']
if cython_cov:
flags.extend(['--directive', 'linetrace=True'])
try:
p = subprocess.call(['cython'] + flags + [source])
if p != 0:
raise Exception('Cython failed')
except OSError:
raise OSError('Cython needs to be installed')
def make_extension(ext_name, macros=[]):
ext_path = ext_name.replace('.', os.path.sep) + '.c'
mod_name = '.'.join(ext_name.split('.')[-2:])
return Extension(
mod_name,
sources=[ext_path] + get_sources(),
include_dirs=[get_include(), numpy.get_include(), "."],
extra_compile_args=["-O3", "-Wall", "-fPIC"],
define_macros=macros)
def generate_extensions(macros=[]):
ext_modules = []
for mod_name in find_cython(HERE):
ext_modules.append(make_extension(mod_name, macros=macros))
return ext_modules
DISTNAME = 'L1 Trend Filtering'
DESCRIPTION = 'This is a Python package wrapper around the C solver for the l1 trend filtering algorithm written by Kwangmoo Koh, Seung-Jean Kim and Stephen Boyd.'
with open('README.rst') as f:
LONG_DESCRIPTION = f.read()
MAINTAINER = 'Joshua D. Loyal'
MAINTAINER_EMAIL = '[email protected]'
URL = 'https://joshloyal.github.io/l1tf'
DOWNLOAD_URL = 'https://pypi.org/project/l1tf/#files'
LICENSE = 'GPLv2'
VERSION = __version__
CLASSIFIERS = []
def setup_package():
if len(sys.argv) > 1 and sys.argv[1] == 'clean':
return clean(HERE)
cython_cov = 'CYTHON_COV' in os.environ
macros = []
if cython_cov:
print("Adding coverage information to cythonized files.")
macros = [('CYTHON_TRACE_NOGIL', 1)]
with chdir(HERE):
generate_cython(cython_cov)
ext_modules = generate_extensions(macros=macros)
setup(
name=DISTNAME,
maintainer=MAINTAINER,
maintainer_email=MAINTAINER_EMAIL,
description=DESCRIPTION,
license=LICENSE,
url=URL,
version=VERSION,
download_url=DOWNLOAD_URL,
long_description=LONG_DESCRIPTION,
zip_safe=False,
classifiers=CLASSIFIERS,
package_data={'': ['l1tf' + os.path.sep + '*.pyx', 'l1tf' + os.path.sep + '.pxd']},
include_package_data=True,
packages=find_packages(),
install_requires=INSTALL_REQUIRES,
extras_require={'test': TEST_REQUIRES},
setup_requires=['pytest-runner'],
tests_require=TEST_REQUIRES,
ext_modules=ext_modules
)
if __name__ == '__main__':
setup_package()