forked from pierrepaleo/sift_pyocl
-
Notifications
You must be signed in to change notification settings - Fork 4
/
setup.py
319 lines (269 loc) · 10.1 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Project: Sift implementation in Python + OpenCL
# https://github.com/kif/sift_pyocl
#
"""
Installer script for SIFT algorithm in PyOpenCL
"""
from __future__ import division, with_statement, print_function
__authors__ = ["Jérôme Kieffer"]
__contact__ = "[email protected]"
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
__date__ = "11/04/2016"
__status__ = "stable"
__license__ = """
Permission is hereby granted, free of charge, to any person
obtaining a copy of ethis 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.
"""
import os
import sys
import glob
import shutil
import platform
try:
# setuptools allows the creation of wheels
from setuptools import setup, Command
from setuptools.command.sdist import sdist
from setuptools.command.build_ext import build_ext
from setuptools.command.install_data import install_data
from setuptools.command.install import install
from setuptools.command.build_py import build_py as _build_py
except ImportError:
from distutils.core import setup, Command
from distutils.command.sdist import sdist
from distutils.command.build_ext import build_ext
from distutils.command.install_data import install_data
from distutils.command.install import install
from distutils.command.build_py import build_py as _build_py
from numpy.distutils.core import Extension as _Extension
if sys.version_info[0] == 2:
import ConfigParser
else:
import configparser as ConfigParser
from numpy.distutils.misc_util import get_numpy_include_dirs
from distutils.sysconfig import get_python_lib
# ###############################################################################
# Check for Cython
# ###############################################################################
try:
from Cython.Distutils import build_ext
CYTHON = True
except ImportError:
CYTHON = False
if CYTHON:
try:
import Cython.Compiler.Version
except ImportError:
CYTHON = False
else:
if Cython.Compiler.Version.version < "0.17":
CYTHON = False
if CYTHON:
cython_c_ext = ".pyx"
else:
cython_c_ext = ".c"
from distutils.command.build_ext import build_ext
cmdclass = {}
def rewriteManifest(with_testimages=False):
"""
Rewrite the "Manifest" file ... if needed
@param with_testimages: include
"""
base = os.path.dirname(os.path.abspath(__file__))
manifest_file = os.path.join(base, "MANIFEST.in")
if not os.path.isfile(manifest_file):
print("MANIFEST file is missing !!!")
return
manifest = [i.strip() for i in open(manifest_file)]
changed = False
if with_testimages:
testimages = ["test/testimages/" + i for i in os.listdir(os.path.join(base, "test", "testimages"))]
for image in testimages:
if image not in manifest:
manifest.append("include " + image)
changed = True
else:
for line in manifest[:]:
if line.startswith("include test/testimages"):
changed = True
manifest.remove(line)
if changed:
with open(manifest_file, "w") as f:
f.write(os.linesep.join(manifest))
# remove MANIFEST: will be re generated !
os.unlink(manifest_file[:-3])
if ("sdist" in sys.argv):
if ("--with-testimages" in sys.argv):
sys.argv.remove("--with-testimages")
rewriteManifest(with_testimages=True)
else:
rewriteManifest(with_testimages=False)
pkg_name = "sift_pyocl" # relative to site-packages ...
data_files = [(pkg_name, glob.glob("openCL/*.cl"))]
class smart_install_data(install_data):
def run(self):
install_cmd = self.get_finalized_command('install')
self.install_dir = getattr(install_cmd, 'install_lib')
print("DATA to be installed in %s" % self.install_dir)
return install_data.run(self)
cmdclass['install_data'] = smart_install_data
if sys.platform == "win32":
# This is for mingw32/gomp?
# data_files[0][1].append(os.path.join("dll", "pthreadGC2.dll"))
root = os.path.dirname(os.path.abspath(__file__))
tocopy_files = []
script_files = []
for i in os.listdir(os.path.join(root, "scripts")):
if os.path.isfile(os.path.join(root, "scripts", i)):
if i.endswith(".py"):
script_files.append(os.path.join("scripts", i))
else:
tocopy_files.append(os.path.join("scripts", i))
for i in tocopy_files:
filein = os.path.join(root, i)
if (filein + ".py") not in script_files:
shutil.copyfile(filein, filein + ".py")
script_files.append(filein + ".py")
else:
script_files = glob.glob("scripts/*")
version = [eval(l.split("=")[1]) for l in open(os.path.join(os.path.dirname(
os.path.abspath(__file__)), "sift-src", "__init__.py"))
if l.strip().startswith("version")][0]
# We subclass the build_ext class in order to handle compiler flags
# for openmp and opencl etc in a cross platform way
translator = {
# Compiler
# name, compileflag, linkflag
'msvc' : {
'openmp' : ('/openmp', ' '),
'debug' : ('/Zi', ' '),
'OpenCL' : 'OpenCL',
},
'mingw32':{
'openmp' : ('-fopenmp', '-fopenmp'),
'debug' : ('-g', '-g'),
'stdc++' : 'stdc++',
'OpenCL' : 'OpenCL'
},
'default':{
'openmp' : ('-fopenmp', '-fopenmp'),
'debug' : ('-g', '-g'),
'stdc++' : 'stdc++',
'OpenCL' : 'OpenCL'
}
}
class build_ext_sift(build_ext):
def build_extensions(self):
if self.compiler.compiler_type in translator:
trans = translator[self.compiler.compiler_type]
else:
trans = translator['default']
for e in self.extensions:
e.extra_compile_args = [trans[a][0] if a in trans else a
for a in e.extra_compile_args]
e.extra_link_args = [trans[a][1] if a in trans else a
for a in e.extra_link_args]
e.libraries = filter(None, [trans[a] if a in trans else None
for a in e.libraries])
# If you are confused look here:
# print e, e.libraries
# print e.extra_compile_args
# print e.extra_link_args
build_ext.build_extensions(self)
cmdclass['build_ext'] = build_ext_sift
class PyTest(Command):
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
import sys, subprocess
os.chdir("test")
errno = subprocess.call([sys.executable, 'test_all.py'])
if errno != 0:
print("Tests did not pass !!!")
# raise SystemExit(errno)
else:
print("All Tests passed")
os.chdir("..")
cmdclass['test'] = PyTest
#######################
# build_doc commandes #
#######################
try:
import sphinx
import sphinx.util.console
sphinx.util.console.color_terminal = lambda: False
from sphinx.setup_command import BuildDoc
except ImportError:
sphinx = None
if sphinx:
class build_doc(BuildDoc):
def run(self):
# make sure the python path is pointing to the newly built
# code so that the documentation is built on this and not a
# previously installed version
build = self.get_finalized_command('build')
sys.path.insert(0, os.path.abspath(build.build_lib))
# Build the Users Guide in HTML and TeX format
for builder in ('html', 'latex'):
self.builder = builder
self.builder_target_dir = os.path.join(self.build_dir, builder)
self.mkpath(self.builder_target_dir)
builder_index = 'index_{0}.txt'.format(builder)
BuildDoc.run(self)
sys.path.pop(0)
cmdclass['build_doc'] = build_doc
classifiers = """
Development Status :: 5 - Production/Stable
Intended Audience :: Developers
Programming Language :: Python
Topic :: Software Development :: Libraries :: Python Modules
Operating System :: Microsoft :: Windows
Operating System :: Unix
Operating System :: MacOS :: MacOS X
Operating System :: POSIX
"""
setup(name='sift_pyocl',
version=version,
author="Pierre Paleo, Jérôme Kieffer",
author_email="[email protected]",
description='Python/OpenCL implementation of Sift algorithm image alignment',
url="https://github.com/kif/sift_pyocl",
download_url="https://github.com/kif/sift_pyocl/archive/master.zip",
scripts=script_files,
packages=[pkg_name],
package_dir={pkg_name: "sift-src"},
test_suite="test",
cmdclass=cmdclass,
data_files=data_files,
classifiers=[ i for i in classifiers.split("\n") if i],
license="MIT"
)
try:
import pyopencl
except ImportError:
print("""sift can use pyopencl to run on parallel accelerators like GPU; this is not optional !!!.
This python module can be found on:
http://pypi.python.org/pypi/pyopencl
""")