-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.py
executable file
·343 lines (303 loc) · 12.3 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import os, os.path, sys, shutil, re, itertools, warnings
import argparse, shlex, subprocess
from collections import namedtuple
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext as _build_ext
pack_name = 'rpy2'
pack_version = __import__('rpy').__version__
default_lib_directory = 'bin' if sys.platform=='win32' else 'lib'
package_prefix='.'
class build_ext(_build_ext):
"""
-DRPY_STRNDUP : definition of strndup()
-DRPY_VERBOSE
-DRPY_DEBUG_PRESERV
-DRPY_DEBUG_PROMISE : evaluation of promises
-DRPY_DEBUG_OBJECTINIT : initialization of PySexpObject
-DRPY_DEBUG_CONSOLE : console I/O
-DRPY_DEBUG_COBJECT : SexpObject passed as a CObject
-DRPY_DEBUG_GRDEV
"""
user_options = _build_ext.user_options + \
[
('ignore-check-rversion', None, 'ignore checks for supported R versions')]
boolean_options = _build_ext.boolean_options + \
['ignore-check-rversion', ] #+ \
#['r-autoconfig', ]
def initialize_options(self):
try:
super(build_ext, self).initialize_options()
except TypeError:
# distutils parent class an old style Python class
_build_ext.initialize_options(self)
self.r_autoconfig = None
self.r_home = None
self.r_home_lib = None
self.ignore_check_rversion = False
def finalize_options(self):
self.set_undefined_options('build')
try:
r_home = subprocess.check_output(("R", "RHOME"),
universal_newlines=True)
except:
raise SystemExit("Error: Tried to guess R's HOME but no R command in the PATH.")
r_home = r_home.split(os.linesep)
#Twist if 'R RHOME' spits out a warning
if r_home[0].startswith("WARNING"):
r_home = r_home[1].rstrip()
else:
r_home = r_home[0].rstrip()
rexec = RExec(r_home)
if rexec.version[0] == 'development' or \
cmp_version(rexec.version[:2], [2, 8]) == -1:
if self.ignore_check_rversion:
warnings.warn("R did not seem to have the minimum required version number")
else:
raise SystemExit("Error: R >= 2.8 required (and R told '%s')." %'.'.join(rexec.version))
try:
super(build_ext, self).finalize_options()
except TypeError:
# distutils parent class an old style Python class
_build_ext.finalize_options(self)
def run(self):
try:
super(build_ext, self).run()
except TypeError:
# distutils parent class an old style Python class
_build_ext.run(self)
def cmp_version(x, y):
if (x[0] < y[0]):
return -1
if (x[0] > y[0]):
return 1
if (x[0] == y[0]):
if len(x) == 1 or len(y) == 1:
return 0
return cmp_version(x[1:], y[1:])
class RExec(object):
""" Compilation-related configuration parameters used by R. """
BINARY_BASE = 'R'
WIN_BINARY_EXT = '.exe'
BINARY_FNAME = None
def __init__(self, r_home):
if 'win' in sys.platform.lower():
self.BINARY_FNAME = self.BINARY_BASE + self.WIN_BINARY_EXT
else:
self.BINARY_FNAME = self.BINARY_BASE
if sys.platform == "win32" and "64 bit" in sys.version:
r_exec = os.path.join(r_home, 'bin', 'x64', self.BINARY_FNAME)
else:
r_exec = os.path.join(r_home, 'bin', self.BINARY_FNAME)
self._r_exec = r_exec
self._version = None
@property
def version(self):
if self._version is not None:
return self._version
p = subprocess.Popen(
(self._r_exec, '--version'),
stderr=subprocess.PIPE,
stdout=subprocess.PIPE
)
p_out, p_err = p.communicate()
# some R versions return the version info in stderr instead of stdout
output = p_out or p_err
output = iter(output.split(os.linesep))
rversion = next(output)
#Twist if 'R RHOME' spits out a warning
if rversion.startswith("WARNING"):
rversion = next(output)
print(rversion)
m = re.match('^R ([^ ]+) ([^ ]+) .+$', rversion)
if m is None:
# return dummy version 0.0
rversion = [0, 0]
else:
rversion = m.groups()[1]
if m.groups()[0] == 'version':
rversion = [int(x) for x in rversion.split('.')]
else:
rversion = ['development', '']
self._version = rversion
return self._version
def cmd_config(self, about, allow_empty=False):
cmd = (self._r_exec, 'CMD', 'config', about)
print(subprocess.list2cmdline(cmd))
output = subprocess.check_output(cmd,
universal_newlines = True)
output = output.split(os.linesep)
#Twist if 'R RHOME' spits out a warning
if output[0].startswith("WARNING"):
output = output[1:]
output = rconfig.strip()
return output
def getRinterface_ext():
#r_libs = [os.path.join(RHOME, 'lib'), os.path.join(RHOME, 'modules')]
r_libs = []
extra_link_args = []
extra_compile_args = []
include_dirs = []
libraries = []
library_dirs = []
#FIXME: crude way (will break in many cases)
#check how to get how to have a configure step
define_macros = []
if sys.platform == 'win32':
define_macros.append(('Win32', 1))
if "64 bit" in sys.version:
define_macros.append(('Win64', 1))
extra_link_args.append('-m64')
extra_compile_args.append('-m64')
# MS_WIN64 only defined by pyconfig.h for MSVC.
# See http://bugs.python.org/issue4709
define_macros.append(('MS_WIN64', 1))
else:
define_macros.append(('R_INTERFACE_PTRS', 1))
define_macros.append(('HAVE_POSIX_SIGJMP', 1))
define_macros.append(('RIF_HAS_RSIGHAND', 1))
define_macros.append(('CSTACK_DEFNS', 1))
define_macros.append(('HAS_READLINE', 1))
if sys.byteorder == 'big':
define_macros.append(('RPY_BIGENDIAN', 1))
else:
pass
try:
r_home = subprocess.check_output(("R", "RHOME"),
universal_newlines=False)
except WindowsError:
# try again, looking for "R.bat" in the PATH
try:
r_home = subprocess.check_output(("R.bat", "RHOME"),
universal_newlines=False)
except:
raise SystemExit("Error: Tried to guess R's HOME but no R command in the PATH.")
except:
raise SystemExit("Error: Tried to guess R's HOME but no R command in the PATH.")
r_home = r_home.split(os.linesep)
#Twist if 'R RHOME' spits out a warning
if r_home[0].startswith("WARNING"):
r_home = r_home[1].rstrip()
else:
r_home = r_home[0].rstrip()
rexec = RExec(r_home)
if rexec.version[0] == 'development' or \
cmp_version(rexec.version[:2], [2, 8]) == -1:
if self.ignore_check_rversion:
warnings.warn("R did not seem to have the minimum required version number")
else:
raise SystemExit("Error: R >= 2.8 required (and R told '%s')." %'.'.join(rexec.version))
ldf = shlex.split(' '.join(rexec.cmd_config('--ldflags')))
cppf = shlex.split(' '.join(rexec.cmd_config('--cppflags')))
#lapacklibs = rexec.cmd_config('LAPACK_LIBS', True)
#blaslibs = rexec.cmd_config('BLAS_LIBS', True)
parser = argparse.ArgumentParser()
parser.add_argument('-I', action='append')
parser.add_argument('-L', action='append')
parser.add_argument('-l', action='append')
# compile
args, unknown = parser.parse_known_args(cppf)
include_dirs.extend(args.I)
extra_compile_args.extend(unknown)
# link
args, unknown = parser.parse_known_args(ldf)
# OS X's frameworks need special attention
if args.L is None:
# presumably OS X and framework:
libraries.extend([x for x in args.l if x != 'R'])
else:
library_dirs.extend(args.L)
libraries.extend(args.l)
extra_link_args.extend(unknown)
rinterface_ext = Extension(
name = pack_name + '.rinterface._rinterface',
sources = [os.path.join(package_prefix,
'rpy', 'rinterface', '_rinterface.c')
],
depends = [os.path.join(package_prefix,
'rpy', 'rinterface', 'embeddedr.h'),
os.path.join(package_prefix,
'rpy', 'rinterface', 'r_utils.h'),
os.path.join(package_prefix,
'rpy', 'rinterface', 'buffer.h'),
os.path.join(package_prefix,
'rpy', 'rinterface', 'sequence.h'),
os.path.join(package_prefix,
'rpy', 'rinterface', 'sexp.h'),
os.path.join(package_prefix,
'rpy', 'rinterface', '_rinterface.h'),
os.path.join(package_prefix,
'rpy', 'rinterface', 'rpy_device.h')
],
include_dirs = [os.path.join(package_prefix,
'rpy', 'rinterface'),] + include_dirs,
libraries = libraries,
library_dirs = r_libs,
define_macros = define_macros,
runtime_library_dirs = r_libs,
extra_compile_args=extra_compile_args,
extra_link_args = extra_link_args
)
rpy_device_ext = Extension(
pack_name + '.rinterface._rpy_device',
[
os.path.join(package_prefix,
'rpy', 'rinterface', '_rpy_device.c'),
],
include_dirs = include_dirs +
[os.path.join('rpy', 'rinterface'), ],
libraries = libraries,
library_dirs = r_libs,
define_macros = define_macros,
runtime_library_dirs = r_libs,
extra_compile_args=extra_compile_args,
extra_link_args = extra_link_args
)
return [rinterface_ext, rpy_device_ext]
if __name__ == '__main__':
rinterface_exts = []
ri_ext = getRinterface_ext()
rinterface_exts.extend(ri_ext)
pack_dir = {pack_name: os.path.join(package_prefix, 'rpy')}
#import setuptools.command.install
#for scheme in setuptools.command.install.INSTALL_SCHEMES.values():
# scheme['data'] = scheme['purelib']
requires=[]
if sys.version_info[0] < 3:
requires.append('singledispatch')
setup(
cmdclass = {'build_ext': build_ext},
name = pack_name,
version = pack_version,
description = "Python interface to the R language (embedded R)",
url = "http://rpy.sourceforge.net",
license = "GPLv2+",
author = "Laurent Gautier",
author_email = "lgautier@gmail.com",
requires = requires,
ext_modules = rinterface_exts,
package_dir = pack_dir,
packages = [pack_name,
pack_name + '.rlike',
pack_name + '.rlike.tests',
pack_name + '.rinterface',
pack_name + '.rinterface.tests',
pack_name + '.robjects',
pack_name + '.robjects.tests',
pack_name + '.robjects.lib',
pack_name + '.robjects.lib.tests',
pack_name + '.interactive',
pack_name + '.interactive.tests',
pack_name + '.ipython',
pack_name + '.ipython.tests'
],
classifiers = ['Programming Language :: Python',
'Programming Language :: Python :: 3',
'License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+)',
'Intended Audience :: Developers',
'Intended Audience :: Science/Research',
'Development Status :: 5 - Production/Stable'
],
package_data = {
'rpy2': ['images/*.png', ],
'rpy2': ['doc/source/rpy2_logo.png', ]}
)